1 | /*!
|
2 | * Copyright 2014 Google LLC.
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 | /// <reference types="node" />
|
17 | /// <reference types="node" />
|
18 | /// <reference types="node" />
|
19 | import { Key } from 'readline';
|
20 | import { Datastore } from '.';
|
21 | import { Entity } from './entity';
|
22 | import { Transaction } from './transaction';
|
23 | import { CallOptions } from 'google-gax';
|
24 | import { RunQueryStreamOptions } from '../src/request';
|
25 | export declare type Operator = '=' | '<' | '>' | '<=' | '>=' | 'HAS_ANCESTOR' | '!=' | 'IN' | 'NOT_IN';
|
26 | export interface OrderOptions {
|
27 | descending?: boolean;
|
28 | }
|
29 | export interface Order {
|
30 | name: string;
|
31 | sign: '-' | '+';
|
32 | }
|
33 | export interface Filter {
|
34 | name: string;
|
35 | val: any;
|
36 | op: Operator;
|
37 | }
|
38 | /**
|
39 | * Build a Query object.
|
40 | *
|
41 | * **Queries are built with {module:datastore#createQuery} and
|
42 | * {@link Transaction#createQuery}.**
|
43 | *
|
44 | * @see {@link http://goo.gl/Cag0r6| Datastore Queries}
|
45 | *
|
46 | * @class
|
47 | * @param {Datastore|Transaction} scope The parent scope the query was created
|
48 | * from.
|
49 | * @param {string} [namespace] Namespace to query entities from.
|
50 | * @param {string[]} kinds Kind to query.
|
51 | *
|
52 | * @example
|
53 | * ```
|
54 | * const {Datastore} = require('@google-cloud/datastore');
|
55 | * const datastore = new Datastore();
|
56 | * const query = datastore.createQuery('AnimalNamespace', 'Lion');
|
57 | * ```
|
58 | */
|
59 | declare class Query {
|
60 | scope?: Datastore | Transaction;
|
61 | namespace?: string | null;
|
62 | kinds: string[];
|
63 | filters: Filter[];
|
64 | orders: Order[];
|
65 | groupByVal: Array<{}>;
|
66 | selectVal: Array<{}>;
|
67 | startVal: string | Buffer | null;
|
68 | endVal: string | Buffer | null;
|
69 | limitVal: number;
|
70 | offsetVal: number;
|
71 | constructor(scope?: Datastore | Transaction, kinds?: string[] | null);
|
72 | constructor(scope?: Datastore | Transaction, namespace?: string | null, kinds?: string[]);
|
73 | /**
|
74 | * Datastore allows querying on properties. Supported comparison operators
|
75 | * are `=`, `<`, `>`, `<=`, `>=`, `!=`, `HAS_ANCESTOR`, `IN` and `NOT_IN`.
|
76 | *
|
77 | * *To filter by ancestors, see {module:datastore/query#hasAncestor}.*
|
78 | *
|
79 | * @see {//cloud.google.com/datastore/docs/concepts/queries#datastore-property-filter-nodejs| Datastore Filters}
https: |
80 | *
|
81 | * string} property The field name.
{ |
82 | * string} [operator="="] Operator (=, <, >, <=, >=).
{ |
83 | * {*} value Value to compare property to.
|
84 | * {Query}
|
85 | *
|
86 | *
|
87 | * ```
|
88 | * const {Datastore} = require('@google-cloud/datastore');
|
89 | * const datastore = new Datastore();
|
90 | * const query = datastore.createQuery('Company');
|
91 | *
|
92 | * //-
|
93 | * // List all companies that are located in California.
|
94 | * //-
|
95 | * const caliQuery = query.filter('state', 'CA');
|
96 | *
|
97 | * //-
|
98 | * // List all companies named Google that have less than 400 employees.
|
99 | * //-
|
100 | * const companyQuery = query
|
101 | * .filter('name', 'Google')
|
102 | * .filter('size', '<', 400);
|
103 | *
|
104 | * //-
|
105 | * // To filter by key, use `__key__` for the property name. Filter on keys
|
106 | * // stored as properties is not currently supported.
|
107 | * //-
|
108 | * const key = datastore.key(['Company', 'Google']);
|
109 | * const keyQuery = query.filter('__key__', key);
|
110 | * ```
|
111 | */
|
112 | filter(property: string, value: {}): Query;
|
113 | filter(property: string, operator: Operator, value: {}): Query;
|
114 | /**
|
115 | * Filter a query by ancestors.
|
116 | *
|
117 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ancestor-query-nodejs| Datastore Ancestor Filters}
|
118 | *
|
119 | * @param {Key} key Key object to filter by.
|
120 | * @returns {Query}
|
121 | *
|
122 | * @example
|
123 | * ```
|
124 | * const {Datastore} = require('@google-cloud/datastore');
|
125 | * const datastore = new Datastore();
|
126 | * const query = datastore.createQuery('MyKind');
|
127 | * const ancestoryQuery = query.hasAncestor(datastore.key(['Parent', 123]));
|
128 | * ```
|
129 | */
|
130 | hasAncestor(key: Key): this;
|
131 | /**
|
132 | * Sort the results by a property name in ascending or descending order. By
|
133 | * default, an ascending sort order will be used.
|
134 | *
|
135 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ascending-sort-nodejs| Datastore Sort Orders}
|
136 | *
|
137 | * @param {string} property The property to order by.
|
138 | * @param {object} [options] Options object.
|
139 | * @param {boolean} [options.descending=false] Sort the results by a property
|
140 | * name in descending order.
|
141 | * @returns {Query}
|
142 | *
|
143 | * @example
|
144 | * ```
|
145 | * const {Datastore} = require('@google-cloud/datastore');
|
146 | * const datastore = new Datastore();
|
147 | * const companyQuery = datastore.createQuery('Company');
|
148 | *
|
149 | * // Sort by size ascendingly.
|
150 | * const companiesAscending = companyQuery.order('size');
|
151 | *
|
152 | * // Sort by size descendingly.
|
153 | * const companiesDescending = companyQuery.order('size', {
|
154 | * descending: true
|
155 | * });
|
156 | * ```
|
157 | */
|
158 | order(property: string, options?: OrderOptions): this;
|
159 | /**
|
160 | * Group query results by a list of properties.
|
161 | *
|
162 | * @param {array} properties Properties to group by.
|
163 | * @returns {Query}
|
164 | *
|
165 | * @example
|
166 | * ```
|
167 | * const {Datastore} = require('@google-cloud/datastore');
|
168 | * const datastore = new Datastore();
|
169 | * const companyQuery = datastore.createQuery('Company');
|
170 | * const groupedQuery = companyQuery.groupBy(['name', 'size']);
|
171 | * ```
|
172 | */
|
173 | groupBy(fieldNames: string | string[]): this;
|
174 | /**
|
175 | * Retrieve only select properties from the matched entities.
|
176 | *
|
177 | * Queries that select a subset of properties are called Projection Queries.
|
178 | *
|
179 | * @see {@link https://cloud.google.com/datastore/docs/concepts/projectionqueries| Projection Queries}
|
180 | *
|
181 | * @param {string|string[]} fieldNames Properties to return from the matched
|
182 | * entities.
|
183 | * @returns {Query}
|
184 | *
|
185 | * @example
|
186 | * ```
|
187 | * const {Datastore} = require('@google-cloud/datastore');
|
188 | * const datastore = new Datastore();
|
189 | * const companyQuery = datastore.createQuery('Company');
|
190 | *
|
191 | * // Only retrieve the name property.
|
192 | * const selectQuery = companyQuery.select('name');
|
193 | *
|
194 | * // Only retrieve the name and size properties.
|
195 | * const selectQuery = companyQuery.select(['name', 'size']);
|
196 | * ```
|
197 | */
|
198 | select(fieldNames: string | string[]): this;
|
199 | /**
|
200 | * Set a starting cursor to a query.
|
201 | *
|
202 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets| Query Cursors}
|
203 | *
|
204 | * @param {string} cursorToken The starting cursor token.
|
205 | * @returns {Query}
|
206 | *
|
207 | * @example
|
208 | * ```
|
209 | * const {Datastore} = require('@google-cloud/datastore');
|
210 | * const datastore = new Datastore();
|
211 | * const companyQuery = datastore.createQuery('Company');
|
212 | *
|
213 | * const cursorToken = 'X';
|
214 | *
|
215 | * // Retrieve results starting from cursorToken.
|
216 | * const startQuery = companyQuery.start(cursorToken);
|
217 | * ```
|
218 | */
|
219 | start(start: string | Buffer): this;
|
220 | /**
|
221 | * Set an ending cursor to a query.
|
222 | *
|
223 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Query_cursors| Query Cursors}
|
224 | *
|
225 | * @param {string} cursorToken The ending cursor token.
|
226 | * @returns {Query}
|
227 | *
|
228 | * @example
|
229 | * ```
|
230 | * const {Datastore} = require('@google-cloud/datastore');
|
231 | * const datastore = new Datastore();
|
232 | * const companyQuery = datastore.createQuery('Company');
|
233 | *
|
234 | * const cursorToken = 'X';
|
235 | *
|
236 | * // Retrieve results limited to the extent of cursorToken.
|
237 | * const endQuery = companyQuery.end(cursorToken);
|
238 | * ```
|
239 | */
|
240 | end(end: string | Buffer): this;
|
241 | /**
|
242 | * Set a limit on a query.
|
243 | *
|
244 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Limits}
|
245 | *
|
246 | * @param {number} n The number of results to limit the query to.
|
247 | * @returns {Query}
|
248 | *
|
249 | * @example
|
250 | * ```
|
251 | * const {Datastore} = require('@google-cloud/datastore');
|
252 | * const datastore = new Datastore();
|
253 | * const companyQuery = datastore.createQuery('Company');
|
254 | *
|
255 | * // Limit the results to 10 entities.
|
256 | * const limitQuery = companyQuery.limit(10);
|
257 | * ```
|
258 | */
|
259 | limit(n: number): this;
|
260 | /**
|
261 | * Set an offset on a query.
|
262 | *
|
263 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Offsets}
|
264 | *
|
265 | * @param {number} n The offset to start from after the start cursor.
|
266 | * @returns {Query}
|
267 | *
|
268 | * @example
|
269 | * ```
|
270 | * const {Datastore} = require('@google-cloud/datastore');
|
271 | * const datastore = new Datastore();
|
272 | * const companyQuery = datastore.createQuery('Company');
|
273 | *
|
274 | * // Start from the 101st result.
|
275 | * const offsetQuery = companyQuery.offset(100);
|
276 | * ```
|
277 | */
|
278 | offset(n: number): this;
|
279 | /**
|
280 | * Run the query.
|
281 | *
|
282 | * @param {object} [options] Optional configuration.
|
283 | * @param {string} [options.consistency] Specify either `strong` or `eventual`.
|
284 | * If not specified, default values are chosen by Datastore for the
|
285 | * operation. Learn more about strong and eventual consistency
|
286 | * [here](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore).
|
287 | * @param {object} [options.gaxOptions] Request configuration options, outlined
|
288 | * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
|
289 | * @param {boolean | IntegerTypeCastOptions} [options.wrapNumbers=false]
|
290 | * Wrap values of integerValue type in {@link Datastore#Int} objects.
|
291 | * If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
|
292 | * If an `object`, this will return a value returned by
|
293 | * `wrapNumbers.integerTypeCastFunction`.
|
294 | * Please see {@link IntegerTypeCastOptions} for options descriptions.
|
295 | * @param {function} [callback] The callback function. If omitted, a readable
|
296 | * stream instance is returned.
|
297 | * @param {?error} callback.err An error returned while making this request
|
298 | * @param {object[]} callback.entities A list of entities.
|
299 | * @param {object} callback.info An object useful for pagination.
|
300 | * @param {?string} callback.info.endCursor Use this in a follow-up query to
|
301 | * begin from where these results ended.
|
302 | * @param {string} callback.info.moreResults Datastore responds with one of:
|
303 | *
|
304 | * - {@link Datastore#MORE_RESULTS_AFTER_LIMIT}: There *may* be more
|
305 | * results after the specified limit.
|
306 | * - {@link Datastore#MORE_RESULTS_AFTER_CURSOR}: There *may* be more
|
307 | * results after the specified end cursor.
|
308 | * - {@link Datastore#NO_MORE_RESULTS}: There are no more results.
|
309 | *
|
310 | * @example
|
311 | * ```
|
312 | * const {Datastore} = require('@google-cloud/datastore');
|
313 | * const datastore = new Datastore();
|
314 | * const query = datastore.createQuery('Company');
|
315 | *
|
316 | * query.run((err, entities, info) => {
|
317 | * // entities = An array of records.
|
318 | *
|
319 | * // Access the Key object for an entity.
|
320 | * const firstEntityKey = entities[0][datastore.KEY];
|
321 | * });
|
322 | *
|
323 | * //-
|
324 | * // A keys-only query returns just the keys of the result entities instead
|
325 | * of
|
326 | * // the entities themselves, at lower latency and cost.
|
327 | * //-
|
328 | * query.select('__key__');
|
329 | *
|
330 | * query.run((err, entities) => {
|
331 | * const keys = entities.map((entity) => {
|
332 | * return entity[datastore.KEY];
|
333 | * });
|
334 | * });
|
335 | *
|
336 | * //-
|
337 | * // If the callback is omitted, we'll return a Promise.
|
338 | * //-
|
339 | * query.run().then((data) => {
|
340 | * const entities = data[0];
|
341 | * });
|
342 | * ```
|
343 | */
|
344 | run(options?: RunQueryOptions): Promise<RunQueryResponse>;
|
345 | run(options: RunQueryOptions, callback: RunQueryCallback): void;
|
346 | run(callback: RunQueryCallback): void;
|
347 | /**
|
348 | * Run the query as a readable object stream.
|
349 | *
|
350 | * @method Query#runStream
|
351 | * @param {object} [options] Optional configuration. See
|
352 | * {@link Query#run} for a complete list of options.
|
353 | * @returns {stream}
|
354 | *
|
355 | * @example
|
356 | * ```
|
357 | * const {Datastore} = require('@google-cloud/datastore');
|
358 | * const datastore = new Datastore();
|
359 | * const query = datastore.createQuery('Company');
|
360 | *
|
361 | * query.runStream()
|
362 | * .on('error', console.error)
|
363 | * .on('data', function (entity) {
|
364 | * // Access the Key object for this entity.
|
365 | * const key = entity[datastore.KEY];
|
366 | * })
|
367 | * .on('info', (info) => {})
|
368 | * .on('end', () => {
|
369 | * // All entities retrieved.
|
370 | * });
|
371 | *
|
372 | * //-
|
373 | * // If you anticipate many results, you can end a stream early to prevent
|
374 | * // unnecessary processing and API requests.
|
375 | * //-
|
376 | * query.runStream()
|
377 | * .on('data', function (entity) {
|
378 | * this.end();
|
379 | * });
|
380 | * ```
|
381 | */
|
382 | runStream(options?: RunQueryStreamOptions): import("stream").Transform;
|
383 | }
|
384 | export interface QueryProto {
|
385 | startCursor?: string | Buffer;
|
386 | distinctOn: {};
|
387 | kind: {};
|
388 | order: {};
|
389 | projection: {};
|
390 | endCursor?: string | Buffer;
|
391 | limit?: {};
|
392 | offset?: number;
|
393 | filter?: {};
|
394 | }
|
395 | /**
|
396 | * Reference to the {@link Query} class.
|
397 | * @name module:@google-cloud/datastore.Query
|
398 | * @see Query
|
399 | */
|
400 | export { Query };
|
401 | export interface IntegerTypeCastOptions {
|
402 | integerTypeCastFunction: Function;
|
403 | properties?: string | string[];
|
404 | }
|
405 | export interface RunQueryOptions {
|
406 | consistency?: 'strong' | 'eventual';
|
407 | gaxOptions?: CallOptions;
|
408 | wrapNumbers?: boolean | IntegerTypeCastOptions;
|
409 | }
|
410 | export interface RunQueryCallback {
|
411 | (err: Error | null, entities?: Entity[], info?: RunQueryInfo): void;
|
412 | }
|
413 | export declare type RunQueryResponse = [Entity[], RunQueryInfo];
|
414 | export interface RunQueryInfo {
|
415 | endCursor?: string;
|
416 | moreResults?: 'MORE_RESULTS_TYPE_UNSPECIFIED' | 'NOT_FINISHED' | 'MORE_RESULTS_AFTER_LIMIT' | 'MORE_RESULTS_AFTER_CURSOR' | 'NO_MORE_RESULTS';
|
417 | }
|