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
330 lines (308 loc) · 8.79 KB
/
Copy pathdataset.js
File metadata and controls
330 lines (308 loc) · 8.79 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
/**
* 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 */
var conn = require('../common/connection.js');
/** @type module:datastore/entity */
var entity = require('./entity.js');
/** @type module:datastore/pb */
var pb = require('./pb.js');
/** @type module:datastore/query */
var Query = require('./query.js');
/** @type module:datastore/transaction */
var Transaction = require('./transaction.js');
/** @type module:common/util */
var util = require('../common/util.js');
/**
* Scopes for Google Datastore access.
* @const {array} SCOPES
*/
var SCOPES = [
'https://www.googleapis.com/auth/datastore',
'https://www.googleapis.com/auth/userinfo.email'
];
/**
* Intract 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.
* @param {string} options.namespace - Namespace to isolate transactions to.
*
* @example
* ```js
* var dataset = new Dataset({
* projectId: 'my-project',
* keyFileName: '/path/to/keyfile.json'
* });
* ```
*/
function Dataset(options) {
options = options || {};
this.connection = new conn.Connection({
keyFilename: options.keyFilename,
scopes: SCOPES
});
this.id = 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
* ```js
* var key;
*
* // Create a key from the dataset's namespace.
* key = dataset.key('Company', 123);
*
* // Create a key from a provided namespace and path.
* key = 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.
*
* @borrows {module:datastore/query} as createQuery
*
* @param {string=} namespace - Optional namespace.
* @param {string|array} kinds - Kinds to query.
*
* @example
* ```js
* var query;
*
* // If your dataset was scoped to a namespace at initialization, your query
* // will likewise be scoped to that namespace.
* query = dataset.createQuery(['Lion', 'Chimp']);
*
* // However, you may override the namespace per query.
* query = dataset.createQuery('AnimalNamespace', ['Lion', 'Chimp']);
*
* // You may also remove the namespace altogether.
* query = dataset.createQuery(null, ['Lion', 'Chimp']);
* ```
* @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));
};
/**
* @borrows {module:datastore/transaction#get} as get
*
* @example
* ```js
* dataset.get([
* datastore.key('Company', 123),
* datastore.key('Product', 'Computer')
* ], function(err, entities) {});
* ```
*/
Dataset.prototype.get = function(key, callback) {
this.transaction.get(key, callback);
};
/**
* @borrows {module:datastore/transaction#save} as save
*
* @example
* ```js
* // Save a single entity.
* dataset.save({
* key: datastore.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: datastore.key('Company', 123),
* data: {
* HQ: 'Dallas, TX'
* }
* },
* {
* key: datastore.key('Product', 'Computer'),
* data: {
* vendor: 'Dell'
* }
* }
* ], function(err, keys) {});
* ```
*/
Dataset.prototype.save = function(key, obj, callback) {
this.transaction.save(key, obj, callback);
};
/**
* @borrows {module:datastore/transaction#delete} as delete
*
* @example
* ```js
* // Delete a single entity.
* dataset.delete(datastore.key('Company', 123), function(err) {});
*
* // Delete multiple entities at once.
* dataset.delete([
* datastore.key('Company', 123),
* datastore.key('Product', 'Computer')
* ], function(err) {});
* ```
*/
Dataset.prototype.delete = function(key, callback) {
this.transaction.delete(key, callback);
};
/**
* @borrows {module:datastore/transaction#runQuery} as runQuery
*
* @example
* ```js
* // 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
* ```js
* dataset.transaction(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(datastore.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) {
return callback(err);
}
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
* ```js
* // The following call will create 100 new IDs from the Company kind, which
* // exists under the default namespace.
* var incompleteKey = datastore.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 = datastore.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) {
return callback(err);
}
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.
*
* @private
* @return {module:datastore/transaction}
*/
Dataset.prototype.createTransaction_ = function() {
return new Transaction(this.connection, this.id);
};
/**
* Exports Dataset.
* @type {Dataset}
*/
module.exports = Dataset;