2222
2323var arrify = require ( 'arrify' ) ;
2424
25+ /*! Developer Documentation
26+ *
27+ * @param {module:datastore|module:transaction } scope - The parent scope the
28+ * query was created from.
29+ */
2530/**
2631 * Build a Query object.
2732 *
28- * **Queries should be built with {module:datastore#createQuery} and run via
29- * {module:datastore#runQuery }.**
33+ * **Queries are built with {module:datastore#createQuery} and
34+ * {module:transaction#createQuery }.**
3035 *
3136 * @resource [Datastore Queries]{@link http://goo.gl/Cag0r6}
3237 *
@@ -45,12 +50,14 @@ var arrify = require('arrify');
4550 * var datastore = gcloud.datastore();
4651 * var query = datastore.createQuery('AnimalNamespace', 'Lion');
4752 */
48- function Query ( namespace , kinds ) {
53+ function Query ( scope , namespace , kinds ) {
4954 if ( ! kinds ) {
5055 kinds = namespace ;
5156 namespace = null ;
5257 }
5358
59+ this . scope = scope ;
60+
5461 this . namespace = namespace || null ;
5562 this . kinds = kinds ;
5663
@@ -73,15 +80,17 @@ function Query(namespace, kinds) {
7380 * @return {module:datastore/query }
7481 *
7582 * @example
76- * // Retrieve a list of people related to person "1234",
77- * // disabling auto pagination
83+ * //-
84+ * // Retrieve a list of people related to "Dave", with auto-pagination
85+ * // disabled.
86+ * //-
7887 * var query = datastore.createQuery('Person')
79- * .hasAncestor(datastore.key(['Person', 1234 ]))
88+ * .hasAncestor(datastore.key(['Person', 'Dave' ]))
8089 * .autoPaginate(false);
8190 *
8291 * function callback(err, entities, nextQuery, apiResponse) {
8392 * if (nextQuery) {
84- * // More results might exist, so we'll manually fetch them
93+ * // More results might exist, so we'll manually fetch them.
8594 * datastore.runQuery(nextQuery, callback);
8695 * }
8796 * }
@@ -103,7 +112,7 @@ Query.prototype.autoPaginate = function(autoPaginateVal) {
103112 * @resource [Datastore Filters]{@link https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Filters}
104113 *
105114 * @param {string } property - The field name.
106- * @param {string= } operator - Operator (=, <, >, <=, >=). Default: `=`.
115+ * @param {string= } operator - Operator (=, <, >, <=, >=). Default: `=`
107116 * @param {* } value - Value to compare property to.
108117 * @return {module:datastore/query }
109118 *
@@ -295,4 +304,76 @@ Query.prototype.offset = function(n) {
295304 return this ;
296305} ;
297306
307+ /**
308+ * Run the query.
309+ *
310+ * @param {object= } options - Optional configuration.
311+ * @param {string } options.consistency - Specify either `strong` or `eventual`.
312+ * If not specified, default values are chosen by Datastore for the
313+ * operation. Learn more about strong and eventual consistency
314+ * [here](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore).
315+ * @param {function= } callback - The callback function. If omitted, a readable
316+ * stream instance is returned.
317+ * @param {?error } callback.err - An error returned while making this request
318+ * @param {object[] } callback.entities - A list of entities.
319+ * @param {?object } callback.nextQuery - If present, query with this object to
320+ * check for more results.
321+ * @param {object } callback.apiResponse - The full API response.
322+ *
323+ * @example
324+ * query.run(function(err, entities) {});
325+ *
326+ * //-
327+ * // To control how many API requests are made and page through the results
328+ * // manually, call `autoPaginate(false)` on your query.
329+ * //-
330+ * query.autoPaginate(false);
331+ *
332+ * function callback(err, entities, nextQuery, apiResponse) {
333+ * if (nextQuery) {
334+ * // More results might exist.
335+ * nextQuery.run(callback);
336+ * }
337+ * }
338+ *
339+ * query.run(callback);
340+ *
341+ * //-
342+ * // If you omit the callback, `run` will automatically call subsequent queries
343+ * // until no results remain. Entity objects will be pushed as they are found.
344+ * //-
345+ * query.run()
346+ * .on('error', console.error)
347+ * .on('data', function (entity) {})
348+ * .on('end', function() {
349+ * // All entities retrieved.
350+ * });
351+ *
352+ * //-
353+ * // If you anticipate many results, you can end a stream early to prevent
354+ * // unnecessary processing and API requests.
355+ * //-
356+ * query.run()
357+ * .on('data', function (entity) {
358+ * this.end();
359+ * });
360+ *
361+ * //-
362+ * // A keys-only query returns just the keys of the result entities instead of
363+ * // the entities themselves, at lower latency and cost.
364+ * //-
365+ * query.select('__key__');
366+ *
367+ * query.run(function(err, entities) {
368+ * // entities[].key = Key object
369+ * // entities[].data = Empty object
370+ * });
371+ */
372+ Query . prototype . run = function ( ) {
373+ var query = this ;
374+ var args = [ query ] . concat ( [ ] . slice . call ( arguments ) ) ;
375+
376+ return this . scope . runQuery . apply ( this . scope , args ) ;
377+ } ;
378+
298379module . exports = Query ;
0 commit comments