forked from googleapis/google-cloud-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.js
More file actions
355 lines (332 loc) · 9.97 KB
/
dataset.js
File metadata and controls
355 lines (332 loc) · 9.97 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*!
* 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';
/**
* @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');
/**
* 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
* @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;
this.transaction = this.createTransaction_();
}
/**
* 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.
*
* @example
* // Create a key from the dataset's namespace.
* var company123 = dataset.key('Company', 123);
*
* // Create a key from a provided namespace and path.
* var nsCompany123 = dataset.key({
* namespace: 'My-NS',
* path: ['Company', 123]
* });
*/
Dataset.prototype.key = function(keyConfig) {
if (!util.is(keyConfig, 'object')) {
keyConfig = {
namespace: this.namespace,
path: util.toArray(arguments)
};
}
return new entity.Key(keyConfig);
};
/**
* 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));
};
/**
* Retrieve the entities identified with the specified key(s) in the current
* transaction. Get operations require a valid key to retrieve the
* key-identified entity from Datastore.
*
* @borrows {module:datastore/transaction#get} as get
*
* @param {Key|Key[]} key - Datastore key object(s).
* @param {function} callback - The callback function.
*
* @example
* dataset.get([
* dataset.key('Company', 123),
* dataset.key('Product', 'Computer')
* ], function(err, entities) {});
*/
Dataset.prototype.get = function(key, callback) {
this.transaction.get(key, callback);
};
/**
* Insert or update the specified object(s) in the current transaction. If a
* key is incomplete, its associated object is inserted and its generated
* identifier is returned to the callback.
*
* @borrows {module:datastore/transaction#save} as save
*
* @param {object|object[]} entities - Datastore key object(s).
* @param {Key} entities.key - Datastore key object.
* @param {object} entities.data - Data to save with the provided key.
* @param {function} callback - The callback function.
*
* @example
* // Save a single entity.
* dataset.save({
* key: dataset.key('Company', null),
* data: {
* rating: '10'
* }
* }, function(err, key) {
* // Because we gave an incomplete key as an argument, `key` will be
* // populated with the complete, generated key.
* });
*
* // Save multiple entities at once.
* dataset.save([
* {
* key: dataset.key('Company', 123),
* data: {
* HQ: 'Dallas, TX'
* }
* },
* {
* key: dataset.key('Product', 'Computer'),
* data: {
* vendor: 'Dell'
* }
* }
* ], function(err, keys) {});
*/
Dataset.prototype.save = function(key, obj, callback) {
this.transaction.save(key, obj, callback);
};
/**
* Delete all entities identified with the specified key(s) in the current
* transaction.
*
* @param {Key|Key[]} key - Datastore key object(s).
* @param {function} callback - The callback function.
*
* @borrows {module:datastore/transaction#delete} as delete
*
* @example
* // Delete a single entity.
* dataset.delete(dataset.key('Company', 123), function(err) {});
*
* // Delete multiple entities at once.
* dataset.delete([
* dataset.key('Company', 123),
* dataset.key('Product', 'Computer')
* ], function(err) {});
*/
Dataset.prototype.delete = function(key, callback) {
this.transaction.delete(key, callback);
};
/**
* Datastore allows you to query entities by kind, filter them by property
* filters, and sort them by a property name. Projection and pagination are also
* supported. If more results are available, a query to retrieve the next page
* is provided to the callback function.
*
* @borrows {module:datastore/transaction#runQuery} as runQuery
*
* @param {module:datastore/query} query - Query object.
* @param {function} callback - The callback function.
*
* @example
* // Retrieve 5 companies.
* dataset.runQuery(queryObject, function(err, entities, nextQuery) {
* // `nextQuery` is not null if there are more results.
* if (nextQuery) {
* dataset.runQuery(nextQuery, function(err, entities, nextQuery) {});
* }
* });
*/
Dataset.prototype.runQuery = function(q, callback) {
this.transaction.runQuery(q, callback);
};
/**
* 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', null);
* 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('ns-test', 'Company', null);
* 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.transaction.makeReq(
'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);
};
module.exports = Dataset;