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 | import { Key } from 'readline';
|
17 | import { Datastore } from '.';
|
18 | import { Entity } from './entity';
|
19 | import { EntityFilter, AllowedFilterValueType } from './filter';
|
20 | import { Transaction } from './transaction';
|
21 | import { CallOptions } from 'google-gax';
|
22 | import { RunQueryStreamOptions } from '../src/request';
|
23 | import { google } from '../protos/protos';
|
24 | export type Operator = '=' | '<' | '>' | '<=' | '>=' | 'HAS_ANCESTOR' | '!=' | 'IN' | 'NOT_IN';
|
25 | export interface OrderOptions {
|
26 | descending?: boolean;
|
27 | }
|
28 | export interface Order {
|
29 | name: string;
|
30 | sign: '-' | '+';
|
31 | }
|
32 | export interface Filter {
|
33 | name: string;
|
34 | val: any;
|
35 | op: Operator;
|
36 | }
|
37 | /**
|
38 | * A Query object is used to build and execute queries for entities stored in Datastore.
|
39 | *
|
40 | * Create a Query object with {@link Datastore#createQuery} or
|
41 | * {@link Transaction#createQuery}.
|
42 | *
|
43 | * @see {@link http://goo.gl/Cag0r6| Datastore Queries}
|
44 | *
|
45 | * @class
|
46 | * @param {Datastore|Transaction} scope The parent scope the query was created
|
47 | * from.
|
48 | * @param {string} [namespace] Namespace to query entities from.
|
49 | * @param {string[]} kinds Kind to query.
|
50 | *
|
51 | * @example
|
52 | * ```
|
53 | * const {Datastore} = require('@google-cloud/datastore');
|
54 | * const datastore = new Datastore();
|
55 | * const query = datastore.createQuery('AnimalNamespace', 'Lion');
|
56 | * ```
|
57 | */
|
58 | declare class Query {
|
59 | scope?: Datastore | Transaction;
|
60 | namespace?: string | null;
|
61 | kinds: string[];
|
62 | filters: Filter[];
|
63 | entityFilters: EntityFilter[];
|
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 | EntityFilter} propertyOrFilter 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(filter: EntityFilter): Query;
|
113 | filter<T extends string>(property: T, value: AllowedFilterValueType<T>): Query;
|
114 | filter<T extends string>(property: T, operator: Operator, value: AllowedFilterValueType<T>): Query;
|
115 | /**
|
116 | * Filter a query by ancestors.
|
117 | *
|
118 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ancestor-query-nodejs| Datastore Ancestor Filters}
|
119 | *
|
120 | * @param {Key} key Key object to filter by.
|
121 | * @returns {Query}
|
122 | *
|
123 | * @example
|
124 | * ```
|
125 | * const {Datastore} = require('@google-cloud/datastore');
|
126 | * const datastore = new Datastore();
|
127 | * const query = datastore.createQuery('MyKind');
|
128 | * const ancestoryQuery = query.hasAncestor(datastore.key(['Parent', 123]));
|
129 | * ```
|
130 | */
|
131 | hasAncestor(key: Key): this;
|
132 | /**
|
133 | * Sort the results by a property name in ascending or descending order. By
|
134 | * default, an ascending sort order will be used.
|
135 | *
|
136 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ascending-sort-nodejs| Datastore Sort Orders}
|
137 | *
|
138 | * @param {string} property The property to order by.
|
139 | * @param {object} [options] Options object.
|
140 | * @param {boolean} [options.descending=false] Sort the results by a property
|
141 | * name in descending order.
|
142 | * @returns {Query}
|
143 | *
|
144 | * @example
|
145 | * ```
|
146 | * const {Datastore} = require('@google-cloud/datastore');
|
147 | * const datastore = new Datastore();
|
148 | * const companyQuery = datastore.createQuery('Company');
|
149 | *
|
150 | * // Sort by size ascendingly.
|
151 | * const companiesAscending = companyQuery.order('size');
|
152 | *
|
153 | * // Sort by size descendingly.
|
154 | * const companiesDescending = companyQuery.order('size', {
|
155 | * descending: true
|
156 | * });
|
157 | * ```
|
158 | */
|
159 | order(property: string, options?: OrderOptions): this;
|
160 | /**
|
161 | * Group query results by a list of properties.
|
162 | *
|
163 | * @param {array} properties Properties to group by.
|
164 | * @returns {Query}
|
165 | *
|
166 | * @example
|
167 | * ```
|
168 | * const {Datastore} = require('@google-cloud/datastore');
|
169 | * const datastore = new Datastore();
|
170 | * const companyQuery = datastore.createQuery('Company');
|
171 | * const groupedQuery = companyQuery.groupBy(['name', 'size']);
|
172 | * ```
|
173 | */
|
174 | groupBy(fieldNames: string | string[]): this;
|
175 | /**
|
176 | * Retrieve only select properties from the matched entities.
|
177 | *
|
178 | * Queries that select a subset of properties are called Projection Queries.
|
179 | *
|
180 | * @see {@link https://cloud.google.com/datastore/docs/samples/datastore-projection-query| Projection Queries}
|
181 | *
|
182 | * @param {string|string[]} fieldNames Properties to return from the matched
|
183 | * entities.
|
184 | * @returns {Query}
|
185 | *
|
186 | * @example
|
187 | * ```
|
188 | * const {Datastore} = require('@google-cloud/datastore');
|
189 | * const datastore = new Datastore();
|
190 | * const companyQuery = datastore.createQuery('Company');
|
191 | *
|
192 | * // Only retrieve the name property.
|
193 | * const selectQuery = companyQuery.select('name');
|
194 | *
|
195 | * // Only retrieve the name and size properties.
|
196 | * const selectQuery = companyQuery.select(['name', 'size']);
|
197 | * ```
|
198 | */
|
199 | select(fieldNames: string | string[]): this;
|
200 | /**
|
201 | * Set a starting cursor to a query.
|
202 | *
|
203 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets| Query Cursors}
|
204 | *
|
205 | * @param {string} cursorToken The starting cursor token.
|
206 | * @returns {Query}
|
207 | *
|
208 | * @example
|
209 | * ```
|
210 | * const {Datastore} = require('@google-cloud/datastore');
|
211 | * const datastore = new Datastore();
|
212 | * const companyQuery = datastore.createQuery('Company');
|
213 | *
|
214 | * const cursorToken = 'X';
|
215 | *
|
216 | * // Retrieve results starting from cursorToken.
|
217 | * const startQuery = companyQuery.start(cursorToken);
|
218 | * ```
|
219 | */
|
220 | start(start: string | Buffer): this;
|
221 | /**
|
222 | * Set an ending cursor to a query.
|
223 | *
|
224 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Query_cursors| Query Cursors}
|
225 | *
|
226 | * @param {string} cursorToken The ending cursor token.
|
227 | * @returns {Query}
|
228 | *
|
229 | * @example
|
230 | * ```
|
231 | * const {Datastore} = require('@google-cloud/datastore');
|
232 | * const datastore = new Datastore();
|
233 | * const companyQuery = datastore.createQuery('Company');
|
234 | *
|
235 | * const cursorToken = 'X';
|
236 | *
|
237 | * // Retrieve results limited to the extent of cursorToken.
|
238 | * const endQuery = companyQuery.end(cursorToken);
|
239 | * ```
|
240 | */
|
241 | end(end: string | Buffer): this;
|
242 | /**
|
243 | * Set a limit on a query.
|
244 | *
|
245 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Limits}
|
246 | *
|
247 | * @param {number} n The number of results to limit the query to.
|
248 | * @returns {Query}
|
249 | *
|
250 | * @example
|
251 | * ```
|
252 | * const {Datastore} = require('@google-cloud/datastore');
|
253 | * const datastore = new Datastore();
|
254 | * const companyQuery = datastore.createQuery('Company');
|
255 | *
|
256 | * // Limit the results to 10 entities.
|
257 | * const limitQuery = companyQuery.limit(10);
|
258 | * ```
|
259 | */
|
260 | limit(n: number): this;
|
261 | /**
|
262 | * Set an offset on a query.
|
263 | *
|
264 | * @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Offsets}
|
265 | *
|
266 | * @param {number} n The offset to start from after the start cursor.
|
267 | * @returns {Query}
|
268 | *
|
269 | * @example
|
270 | * ```
|
271 | * const {Datastore} = require('@google-cloud/datastore');
|
272 | * const datastore = new Datastore();
|
273 | * const companyQuery = datastore.createQuery('Company');
|
274 | *
|
275 | * // Start from the 101st result.
|
276 | * const offsetQuery = companyQuery.offset(100);
|
277 | * ```
|
278 | */
|
279 | offset(n: number): this;
|
280 | /**
|
281 | * Run the query.
|
282 | *
|
283 | * @param {object} [options] Optional configuration.
|
284 | * @param {string} [options.consistency] Specify either `strong` or `eventual`.
|
285 | * If not specified, default values are chosen by Datastore for the
|
286 | * operation. Learn more about strong and eventual consistency
|
287 | * [here](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore).
|
288 | * @param {object} [options.gaxOptions] Request configuration options, outlined
|
289 | * here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
|
290 | * @param {boolean | IntegerTypeCastOptions} [options.wrapNumbers=false]
|
291 | * Wrap values of integerValue type in {@link Datastore#Int} objects.
|
292 | * If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
|
293 | * If an `object`, this will return a value returned by
|
294 | * `wrapNumbers.integerTypeCastFunction`.
|
295 | * Please see {@link IntegerTypeCastOptions} for options descriptions.
|
296 | * @param {function} [callback] The callback function. If omitted, a readable
|
297 | * stream instance is returned.
|
298 | * @param {?error} callback.err An error returned while making this request
|
299 | * @param {object[]} callback.entities A list of entities.
|
300 | * @param {object} callback.info An object useful for pagination.
|
301 | * @param {?string} callback.info.endCursor Use this in a follow-up query to
|
302 | * begin from where these results ended.
|
303 | * @param {string} callback.info.moreResults Datastore responds with one of:
|
304 | *
|
305 | * - {@link Datastore#MORE_RESULTS_AFTER_LIMIT}: There *may* be more
|
306 | * results after the specified limit.
|
307 | * - {@link Datastore#MORE_RESULTS_AFTER_CURSOR}: There *may* be more
|
308 | * results after the specified end cursor.
|
309 | * - {@link Datastore#NO_MORE_RESULTS}: There are no more results.
|
310 | *
|
311 | * @example
|
312 | * ```
|
313 | * const {Datastore} = require('@google-cloud/datastore');
|
314 | * const datastore = new Datastore();
|
315 | * const query = datastore.createQuery('Company');
|
316 | *
|
317 | * query.run((err, entities, info) => {
|
318 | * // entities = An array of records.
|
319 | *
|
320 | * // Access the Key object for an entity.
|
321 | * const firstEntityKey = entities[0][datastore.KEY];
|
322 | * });
|
323 | *
|
324 | * //-
|
325 | * // A keys-only query returns just the keys of the result entities instead
|
326 | * of
|
327 | * // the entities themselves, at lower latency and cost.
|
328 | * //-
|
329 | * query.select('__key__');
|
330 | *
|
331 | * query.run((err, entities) => {
|
332 | * const keys = entities.map((entity) => {
|
333 | * return entity[datastore.KEY];
|
334 | * });
|
335 | * });
|
336 | *
|
337 | * //-
|
338 | * // If the callback is omitted, we'll return a Promise.
|
339 | * //-
|
340 | * query.run().then((data) => {
|
341 | * const entities = data[0];
|
342 | * });
|
343 | * ```
|
344 | */
|
345 | run(options?: RunQueryOptions): Promise<RunQueryResponse>;
|
346 | run(options: RunQueryOptions, callback: RunQueryCallback): void;
|
347 | run(callback: RunQueryCallback): void;
|
348 | /**
|
349 | * Run the query as a readable object stream.
|
350 | *
|
351 | * @method Query#runStream
|
352 | * @param {object} [options] Optional configuration. See
|
353 | * {@link Query#run} for a complete list of options.
|
354 | * @returns {stream}
|
355 | *
|
356 | * @example
|
357 | * ```
|
358 | * const {Datastore} = require('@google-cloud/datastore');
|
359 | * const datastore = new Datastore();
|
360 | * const query = datastore.createQuery('Company');
|
361 | *
|
362 | * query.runStream()
|
363 | * .on('error', console.error)
|
364 | * .on('data', function (entity) {
|
365 | * // Access the Key object for this entity.
|
366 | * const key = entity[datastore.KEY];
|
367 | * })
|
368 | * .on('info', (info) => {})
|
369 | * .on('end', () => {
|
370 | * // All entities retrieved.
|
371 | * });
|
372 | *
|
373 | * //-
|
374 | * // If you anticipate many results, you can end a stream early to prevent
|
375 | * // unnecessary processing and API requests.
|
376 | * //-
|
377 | * query.runStream()
|
378 | * .on('data', function (entity) {
|
379 | * this.end();
|
380 | * });
|
381 | * ```
|
382 | */
|
383 | runStream(options?: RunQueryStreamOptions): import("stream").Transform;
|
384 | }
|
385 | export interface QueryProto {
|
386 | startCursor?: string | Buffer;
|
387 | distinctOn: {};
|
388 | kind: {};
|
389 | order: {};
|
390 | projection: {};
|
391 | endCursor?: string | Buffer;
|
392 | limit?: {};
|
393 | offset?: number;
|
394 | filter?: {};
|
395 | }
|
396 | /**
|
397 | * Reference to the {@link Query} class.
|
398 | * @name module:@google-cloud/datastore.Query
|
399 | * @see Query
|
400 | */
|
401 | export { Query };
|
402 | export interface IntegerTypeCastOptions {
|
403 | integerTypeCastFunction: Function;
|
404 | properties?: string | string[];
|
405 | }
|
406 | export interface ExplainOptions {
|
407 | analyze?: boolean;
|
408 | }
|
409 | export interface RunQueryOptions {
|
410 | consistency?: 'strong' | 'eventual';
|
411 | readTime?: number;
|
412 | gaxOptions?: CallOptions;
|
413 | explainOptions?: ExplainOptions;
|
414 | wrapNumbers?: boolean | IntegerTypeCastOptions;
|
415 | }
|
416 | export interface RunQueryCallback {
|
417 | (err: Error | null, entities?: Entity[], info?: RunQueryInfo): void;
|
418 | }
|
419 | export type RunQueryResponse = [Entity[], RunQueryInfo];
|
420 | export type RunAggregateQueryResponse = any;
|
421 | export interface RunQueryInfo {
|
422 | endCursor?: string;
|
423 | moreResults?: 'MORE_RESULTS_TYPE_UNSPECIFIED' | 'NOT_FINISHED' | 'MORE_RESULTS_AFTER_LIMIT' | 'MORE_RESULTS_AFTER_CURSOR' | 'NO_MORE_RESULTS';
|
424 | explainMetrics?: ExplainMetrics;
|
425 | }
|
426 | export interface ExplainMetrics {
|
427 | planSummary?: PlanSummary;
|
428 | executionStats?: ExecutionStats;
|
429 | }
|
430 | export interface ExecutionStats {
|
431 | resultsReturned?: number;
|
432 | executionDuration?: google.protobuf.IDuration;
|
433 | readOperations?: number;
|
434 | debugStats?: {
|
435 | [key: string]: any;
|
436 | };
|
437 | }
|
438 | export interface PlanSummary {
|
439 | indexesUsed: {
|
440 | [key: string]: any;
|
441 | }[];
|
442 | }
|