Chain the operators to build the query. query.filter(...).order(...).start(...)
Call query.run() to execute the query. query.run([options]).then( ... )
Create the query
constquery=MyModel.query(/* {string}. -- optional. A namespace to execute the query */ <namespace>, /* {object} -- optional. A transaction to execute the query from */ <transaction>);
query.run({/* {boolean}. -- optional. Default: false If set to true will return all the properties of the entity, regardless of the *read* parameter defined in the Schema */ readAll:true|false,/* {string} -- optional. "JSON"|"ENTITY". Default: "JSON" Format of the response, either plain object or entity instances */ format:'JSON',/* {boolean}. -- optional. Default: false Adds a "__key" property to the entity data with the complete Key from the Datastore. */ showKey:true|false,/* {boolean}. -- optional. Default: the "global" cache configuration "true" = read from the cache and prime the cache with the query response */ cache:true|false,/* {number|object} -- optional. Default: the cache.ttl.queries value Custom TTL value for the cache. For multi-store it can be an object of ttl values */ ttl: <number> | <Object>, /* Specify either "strong" or "eventual". If not specified, default values are chosen by Datastore for the operation.*/ consistency: 'strong' | 'eventual');
@Returns: the response is an object with 2 properties:
entities
nextPageCursor // only present if there are More Results to fetch
Example:
constUser=require('./user.model');// 1. Build the queryconstquery=User.query().filter('firstname','=','John').filter('age','>=',4).order('lastname', { descending:true }).limit(10);// 2. Execute the query.// with Promisequery.run().then((response) => {constentities=response.entities;constnextPageCursor=response.nextPageCursor; // not present if no more results});// or with a callbackquery.run(function(err, response) {if (err) {// deal with err }constentities=response.entities;constnextPageCursor=response.nextPageCursor;});// You can then use the "nextPageCursor" when calling the same query and pass it as start valueconstquery=User.query().filter('name','=','John').filter('age','>=',4).order('lastname', { descending:true }).start(nextPageCursor);// Query on namespaceconstnamespace='com.dev.server';constquery=User.query(namespace).filter('name','=','John');// Query in a transactionconsttransaction=gstore.transaction();transaction.run().then(() => {// Create the query inside the transactionconstquery=User.query(null, transaction).filter('name','=','John');query.run().then(() => {// other operations inside the transaction...transaction.commit().then( ... ) }); });// run with optionsquery.run({ readAll:true, format:'ENTITY' }).then( ... )// cache ttl query.run({ ttl:3600 }).then( ... )// cache ttl multi-storequery.run({ ttl: { memory:600, redis:3600 }).then( ... )