-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathdataset.js
More file actions
277 lines (253 loc) · 7.61 KB
/
dataset.js
File metadata and controls
277 lines (253 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*!
* Copyright 2014 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*!
* @module datastore/dataset
*/
'use strict';
var nodeutil = require('util');
/**
* @type module:common/connection
* @private
*/
var conn = require('../common/connection.js');
/**
* @type module:datastore/entity
* @private
*/
var entity = require('./entity.js');
/**
* @type module:datastore/pb
* @private
*/
var pb = require('./pb.js');
/**
* @type module:datastore/query
* @private
*/
var Query = require('./query.js');
/**
* @type module:datastore/transaction
* @private
*/
var Transaction = require('./transaction.js');
/**
* @type module:common/util
* @private
*/
var util = require('../common/util.js');
/**
* @type module:datastore/request
* @private
*/
var DatastoreRequest = require('./request.js');
/**
* Scopes for Google Datastore access.
* @const {array} SCOPES
* @private
*/
var SCOPES = [
'https://www.googleapis.com/auth/datastore',
'https://www.googleapis.com/auth/userinfo.email'
];
/**
* Interact with a dataset from the
* [Google Cloud Datastore]{@link https://developers.google.com/datastore/}.
*
* @constructor
* @alias module:datastore/dataset
*
* @param {object=} options options object
* @param {string=} options.projectId - Dataset ID. This is your project ID from
* the Google Developers Console.
* @param {string=} options.keyFilename - Full path to the JSON key downloaded
* from the Google Developers Console. Alternatively, you may provide a
* `credentials` object.
* @param {object=} options.credentials - Credentials object, used in place of
* a `keyFilename`.
* @param {string} options.namespace - Namespace to isolate transactions to.
*
* @example
* var dataset = datastore.dataset({
* projectId: 'my-project',
* keyFilename: '/path/to/keyfile.json'
* });
*/
function Dataset(options) {
if (!(this instanceof Dataset)) {
return new Dataset(options);
}
options = options || {};
this.connection = new conn.Connection({
credentials: options.credentials,
keyFilename: options.keyFilename,
scopes: SCOPES
});
this.projectId = options.projectId;
this.namespace = options.namespace;
}
nodeutil.inherits(Dataset, DatastoreRequest);
/**
* Helper to create a Key object, scoped to the dataset's namespace by default.
*
* You may also specify a configuration object to define a namespace and path.
*
* @param {...*=} options - Key path. To specify or override a namespace,
* you must use an object here to explicitly state it.
* @param {object=} options - Configuration object.
* @param {...*=} options.path - Key path.
* @param {string=} options.namespace - Optional namespace.
*
* @example
* var key;
*
* // Create an incomplete key from the dataset namespace, kind='Company'
* key = dataset.key('Company');
*
* // A complete key from the dataset namespace, kind='Company', id=123
* key = dataset.key(['Company', 123]);
*
* // A complete key from the dataset namespace, kind='Company', name='Google'
* // Note: `id` is used for numeric identifiers and `name` is used otherwise
* key = dataset.key(['Company', 'Google']);
*
* // A complete key from a provided namespace and path.
* key = dataset.key({
* namespace: 'My-NS',
* path: ['Company', 123]
* });
*
* @return {Key} A newly created Key from the options given.
*/
Dataset.prototype.key = function(options) {
options = util.is(options, 'object') ? options : {
namespace: this.namespace,
path: util.arrayize(options)
};
return new entity.Key(options);
};
/**
* Create a query from the current dataset to query the specified kinds, scoped
* to the namespace provided at the initialization of the dataset.
*
* *Dataset query reference: {@link http://goo.gl/Cag0r6}*
*
* @borrows {module:datastore/query} as createQuery
* @see {module:datastore/query}
*
* @param {string=} namespace - Optional namespace.
* @param {string|array} kinds - Kinds to query.
* @return {module:datastore/query}
*/
Dataset.prototype.createQuery = function(namespace, kinds) {
if (arguments.length === 1) {
kinds = util.arrayize(namespace);
namespace = this.namespace;
}
return new Query(namespace, util.arrayize(kinds));
};
/**
* Run a function in the context of a new transaction. Transactions allow you to
* perform multiple operations, committing your changes atomically.
*
* @borrows {module:datastore/transaction#begin} as runInTransaction
*
* @param {function} fn - The function to run in the context of a transaction.
* @param {function} callback - The callback function.
*
* @example
* dataset.runInTransaction(function(transaction, done) {
* // From the `transaction` object, execute dataset methods as usual.
* // Call `done` when you're ready to commit all of the changes.
* transaction.get(dataset.key(['Company', 123]), function(err, entity) {
* if (err) {
* transaction.rollback(done);
* return;
* }
*
* done();
* });
* }, function(err) {});
*/
Dataset.prototype.runInTransaction = function(fn, callback) {
var newTransaction = this.createTransaction_();
newTransaction.begin(function(err) {
if (err) {
callback(err);
return;
}
fn(newTransaction, newTransaction.finalize.bind(newTransaction, callback));
});
};
/**
* Generate IDs without creating entities.
*
* @param {Key} incompleteKey - The key object to complete.
* @param {number} n - How many IDs to generate.
* @param {function} callback - The callback function.
*
* @example
* // The following call will create 100 new IDs from the Company kind, which
* // exists under the default namespace.
* var incompleteKey = dataset.key('Company');
* dataset.allocateIds(incompleteKey, 100, function(err, keys) {});
*
* // You may prefer to create IDs from a non-default namespace by providing an
* // incomplete key with a namespace. Similar to the previous example, the call
* // below will create 100 new IDs, but from the Company kind that exists under
* // the "ns-test" namespace.
* var incompleteKey = dataset.key({
* namespace: 'ns-test',
* path: ['Company']
* });
* dataset.allocateIds(incompleteKey, 100, function(err, keys) {});
*/
Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
if (entity.isKeyComplete(incompleteKey)) {
throw new Error('An incomplete key should be provided.');
}
var incompleteKeys = [];
for (var i = 0; i < n; i++) {
incompleteKeys.push(entity.keyToKeyProto(incompleteKey));
}
this.createRequest(
'allocateIds',
new pb.AllocateIdsRequest({ key: incompleteKeys }),
pb.AllocateIdsResponse, function(err, resp) {
if (err) {
callback(err);
return;
}
var keys = [];
(resp.key || []).forEach(function(k) {
keys.push(entity.keyFromKeyProto(k));
});
callback(null, keys);
});
};
/**
* Create a new Transaction object using the existing connection and dataset.
*
* @return {module:datastore/transaction}
* @private
*/
Dataset.prototype.createTransaction_ = function() {
return new Transaction(this.connection, this.projectId);
};
/**
* Exports Dataset
* @type {Dataset}
*/
module.exports = Dataset;