-
Notifications
You must be signed in to change notification settings - Fork 668
Expand file tree
/
Copy pathdataset.js
More file actions
268 lines (240 loc) · 7.47 KB
/
dataset.js
File metadata and controls
268 lines (240 loc) · 7.47 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
/*!
* 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:datastore/entity}
* @private
*/
var entity = require('./entity.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](https://developers.google.com/datastore/).
*
* @constructor
* @alias module:datastore/dataset
* @mixes module:datastore/request
*
* @param {object=} options - [Configuration object](#/docs/?method=gcloud).
* @param {string=} options.apiEndpoint - Override the default API endpoint used
* to reach Datastore. This is useful for connecting to your local Datastore
* server (usually "http://localhost:8080").
* @param {string} options.namespace - Namespace to isolate transactions to.
*
* @example
* var datastore = gcloud.datastore;
*
* var dataset = datastore.dataset({
* projectId: 'my-project',
* keyFilename: '/path/to/keyfile.json'
* });
*
* //-
* // Connect to your local Datastore server.
* //-
* var dataset = datastore.dataset({
* projectId: 'my-project',
* apiEndpoint: 'http://localhost:8080'
* });
*
* //-
* // The `process.env.DATASTORE_HOST` environment variable is also recognized.
* // If set, you may omit the `apiEndpoint` option.
* //-
*/
function Dataset(options) {
if (!(this instanceof Dataset)) {
return new Dataset(options);
}
options = options || {};
if (!options.projectId) {
throw util.missingProjectIdError;
}
this.makeAuthorizedRequest_ = util.makeAuthorizedRequestFactory({
customEndpoint: typeof options.apiEndpoint !== 'undefined',
credentials: options.credentials,
keyFile: options.keyFilename,
scopes: SCOPES,
email: options.email
});
this.apiEndpoint = Dataset.determineApiEndpoint_(options);
this.namespace = options.namespace;
this.projectId = options.projectId;
}
nodeutil.inherits(Dataset, DatastoreRequest);
/**
* Determine the appropriate endpoint to use for API requests. If not explicitly
* defined, check for the "DATASTORE_HOST" environment variable, used to connect
* to a local Datastore server.
*
* @private
*
* @param {object} options - Configuration object.
* @param {string=} options.apiEndpoint - Custom API endpoint.
*/
Dataset.determineApiEndpoint_ = function(options) {
var apiEndpoint = 'https://www.googleapis.com';
var trailingSlashes = new RegExp('/*$');
if (options.apiEndpoint) {
apiEndpoint = options.apiEndpoint;
} else if (process.env.DATASTORE_HOST) {
apiEndpoint = process.env.DATASTORE_HOST;
}
if (apiEndpoint.indexOf('http') !== 0) {
apiEndpoint = 'http://' + apiEndpoint;
}
return apiEndpoint.replace(trailingSlashes, '');
};
/**
* 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.
* @return {Key} A newly created Key from the options given.
*
* @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]
* });
*/
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 kind, scoped
* to the namespace provided at the initialization of the dataset.
*
* *[Reference](http://goo.gl/Cag0r6).*
*
* @borrows {module:datastore/query} as createQuery
* @see {module:datastore/query}
*
* @param {string=} namespace - Optional namespace.
* @param {string} kind - Kind to query.
* @return {module:datastore/query}
*/
Dataset.prototype.createQuery = function(namespace, kind) {
if (arguments.length === 1) {
kind = util.arrayize(namespace);
namespace = this.namespace;
}
return new Query(namespace, util.arrayize(kind));
};
/**
* Run a function in the context of a new transaction. Transactions allow you to
* perform multiple operations, committing your changes atomically. When you are
* finished making your changes within the transaction, run the done() function
* provided in the callback function to commit your changes. See an example
* below for more information.
*
* @borrows {module:datastore/transaction#begin} as runInTransaction
*
* @param {function} fn - The function to run in the context of a transaction.
* @param {module:datastore/transaction} fn.transaction - The Transaction.
* @param {function} fn.done - Function used to commit changes.
* @param {function} callback - The callback function.
* @param {?error} callback.err - An error returned while making this request
*
*
* @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.commit_.bind(newTransaction, callback));
});
};
/**
* Create a new Transaction object using the existing connection and dataset.
*
* @return {module:datastore/transaction}
* @private
*/
Dataset.prototype.createTransaction_ = function() {
return new Transaction(this, this.projectId);
};
module.exports = Dataset;