Skip to content

Commit 59152a9

Browse files
callmehiphopstephenplusplus
authored andcommitted
datastore: add promise support (#1704)
1 parent 86ddf2c commit 59152a9

File tree

9 files changed

+708
-377
lines changed

9 files changed

+708
-377
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ datastore.save({
4949
}
5050
});
5151
});
52+
53+
// Promises are also supported by omitting callbacks.
54+
datastore.save({
55+
key: blogPostKey,
56+
data: blogPostData
57+
}).then(function() {
58+
// The blog post is not published!
59+
});
60+
61+
// It's also possible to integrate with third-party Promise libraries.
62+
var datastore = require('@google-cloud/datastore')({
63+
promise: require('bluebird')
64+
});
5265
```
5366

5467

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"datastore"
5151
],
5252
"dependencies": {
53-
"@google-cloud/common": "^0.6.0",
53+
"@google-cloud/common": "^0.7.0",
5454
"arrify": "^1.0.0",
5555
"concat-stream": "^1.5.0",
5656
"create-error-class": "^3.0.2",
@@ -67,6 +67,7 @@
6767
"deep-strict-equal": "^0.2.0",
6868
"mocha": "^3.0.1",
6969
"proxyquire": "^1.7.10",
70+
"sinon": "^1.17.6",
7071
"through2": "^2.0.0"
7172
},
7273
"scripts": {

src/query.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,40 @@ Query.prototype.offset = function(n) {
296296
* query.run(function(err, entities, info) {});
297297
*
298298
* //-
299-
* // If you omit the callback, you will get the matching entities in a readable
300-
* // object stream.
299+
* // A keys-only query returns just the keys of the result entities instead of
300+
* // the entities themselves, at lower latency and cost.
301+
* //-
302+
* query.select('__key__');
303+
*
304+
* query.run(function(err, entities) {
305+
* var keys = entities.map(function(entity) {
306+
* return entity[datastore.KEY];
307+
* });
308+
* });
309+
*
310+
* //-
311+
* // If the callback is omitted, we'll return a Promise.
301312
* //-
302-
* query.run()
313+
* query.run().then(function(data) {
314+
* var entities = data[0];
315+
* });
316+
*/
317+
Query.prototype.run = function() {
318+
var query = this;
319+
var args = [query].concat([].slice.call(arguments));
320+
321+
return this.scope.runQuery.apply(this.scope, args);
322+
};
323+
324+
/**
325+
* Run the query as a readable object stream.
326+
*
327+
* @param {object=} options - Optional configuration. See
328+
* {module:datastore/query#run} for a complete list of options.
329+
* @return {stream}
330+
*
331+
* @example
332+
* query.runStream()
303333
* .on('error', console.error)
304334
* .on('data', function (entity) {})
305335
* .on('info', function(info) {})
@@ -311,28 +341,16 @@ Query.prototype.offset = function(n) {
311341
* // If you anticipate many results, you can end a stream early to prevent
312342
* // unnecessary processing and API requests.
313343
* //-
314-
* query.run()
315-
* .on('data', function(entity) {
344+
* query.runStream()
345+
* .on('data', function (entity) {
316346
* this.end();
317347
* });
318-
*
319-
* //-
320-
* // A keys-only query returns just the keys of the result entities instead of
321-
* // the entities themselves, at lower latency and cost.
322-
* //-
323-
* query.select('__key__');
324-
*
325-
* query.run(function(err, entities) {
326-
* var keys = entities.map(function(entity) {
327-
* return entity[datastore.KEY];
328-
* });
329-
* });
330348
*/
331-
Query.prototype.run = function() {
349+
Query.prototype.runStream = function() {
332350
var query = this;
333351
var args = [query].concat([].slice.call(arguments));
334352

335-
return this.scope.runQuery.apply(this.scope, args);
353+
return this.scope.runQueryStream.apply(this.scope, args);
336354
};
337355

338356
module.exports = Query;

0 commit comments

Comments
 (0)