GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — nodejs-datastore/src/query.ts at main · googleapis/nodejs-datastore

This repository was archived by the owner on Feb 20, 2026. It is now read-only.

Expand file tree

/

query.ts

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
649 lines (616 loc) · 18.1 KB
/

query.ts

Copy path

File metadata and controls

649 lines (616 loc) · 18.1 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*!
* Copyright 2014 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import arrify = require('arrify');
import {Key} from 'readline';
import {Datastore} from '.';
import {Entity} from './entity';
import {EntityFilter, isFilter, AllowedFilterValueType} from './filter';
import {Transaction} from './transaction';
import {CallOptions} from 'google-gax';
import {RunQueryStreamOptions} from '../src/request';
import * as gaxInstance from 'google-gax';
import {google} from '../protos/protos';
export type Operator =
| '='
| '<'
| '>'
| '<='
| '>='
| 'HAS_ANCESTOR'
| '!='
| 'IN'
| 'NOT_IN';
export interface OrderOptions {
descending?: boolean;
}
export interface Order {
name: string;
sign: '-' | '+';
}
export interface Filter {
name: string;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
val: any;
op: Operator;
}
/**
* A Query object is used to build and execute queries for entities stored in Datastore.
*
* Create a Query object with {@link Datastore#createQuery} or
* {@link Transaction#createQuery}.
*
* @see {@link http://goo.gl/Cag0r6| Datastore Queries}
*
* @class
* @param {Datastore|Transaction} scope The parent scope the query was created
* from.
* @param {string} [namespace] Namespace to query entities from.
* @param {string[]} kinds Kind to query.
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const query = datastore.createQuery('AnimalNamespace', 'Lion');
* ```
*/
class Query {
scope?: Datastore | Transaction;
namespace?: string | null;
kinds: string[];
filters: Filter[];
entityFilters: EntityFilter[];
orders: Order[];
groupByVal: Array<{}>;
selectVal: Array<{}>;
startVal: string | Buffer | null;
endVal: string | Buffer | null;
limitVal: number;
offsetVal: number;
constructor(scope?: Datastore | Transaction, kinds?: string[] | null);
constructor(
scope?: Datastore | Transaction,
namespace?: string | null,
kinds?: string[],
);
constructor(
scope?: Datastore | Transaction,
namespaceOrKinds?: string | string[] | null,
kinds?: string[],
) {
let namespace = namespaceOrKinds as string | null;
if (!kinds) {
kinds = namespaceOrKinds as string[];
namespace = null;
}
/**
* @name Query#scope
* @type {Datastore|Transaction}
*/
this.scope = scope;
/**
* @name Query#namespace
* @type {?string}
*/
this.namespace = namespace || null;
/**
* @name Query#kinds
* @type {string}
*/
this.kinds = kinds;
/**
* @name Query#filters
* @type {array}
*/
this.filters = [];
/**
* @name Query#entityFilters
* @type {array}
*/
this.entityFilters = [];
/**
* @name Query#orders
* @type {array}
*/
this.orders = [];
/**
* @name Query#groupByVal
* @type {array}
*/
this.groupByVal = [];
/**
* @name Query#selectVal
* @type {array}
*/
this.selectVal = [];
// pagination
/**
* @name Query#startVal
* @type {?number}
*/
this.startVal = null;
/**
* @name Query#endVal
* @type {?number}
*/
this.endVal = null;
/**
* @name Query#limitVal
* @type {number}
*/
this.limitVal = -1;
/**
* @name Query#offsetVal
* @type {number}
*/
this.offsetVal = -1;
}
/**
* Datastore allows querying on properties. Supported comparison operators
* are `=`, `<`, `>`, `<=`, `>=`, `!=`, `HAS_ANCESTOR`, `IN` and `NOT_IN`.
*
* *To filter by ancestors, see {module:datastore/query#hasAncestor}.*
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-property-filter-nodejs| Datastore Filters}
*
* @param {string | EntityFilter} propertyOrFilter The field name.
* @param {string} [operator="="] Operator (=, <, >, <=, >=).
* @param {*} value Value to compare property to.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const query = datastore.createQuery('Company');
*
* //-
* // List all companies that are located in California.
* //-
* const caliQuery = query.filter('state', 'CA');
*
* //-
* // List all companies named Google that have less than 400 employees.
* //-
* const companyQuery = query
* .filter('name', 'Google')
* .filter('size', '<', 400);
*
* //-
* // To filter by key, use `__key__` for the property name. Filter on keys
* // stored as properties is not currently supported.
* //-
* const key = datastore.key(['Company', 'Google']);
* const keyQuery = query.filter('__key__', key);
* ```
*/
filter(filter: EntityFilter): Query;
filter<T extends string>(
property: T,
value: AllowedFilterValueType<T>,
): Query;
filter<T extends string>(
property: T,
operator: Operator,
value: AllowedFilterValueType<T>,
): Query;
filter<T extends string>(
propertyOrFilter: T | EntityFilter,
operatorOrValue?: Operator | AllowedFilterValueType<T>,
value?: AllowedFilterValueType<T>,
): Query {
if (arguments.length > 1) {
gaxInstance.warn(
'filter',
'Providing Filter objects like Composite Filter or Property Filter is recommended when using .filter',
);
}
switch (arguments.length) {
case 1: {
if (isFilter(propertyOrFilter)) {
this.entityFilters.push(propertyOrFilter);
}
break;
}
case 2: {
this.filters.push({
name: (propertyOrFilter as String).trim(),
op: '=',
val: operatorOrValue as AllowedFilterValueType<T>,
});
break;
}
case 3: {
this.filters.push({
name: (propertyOrFilter as String).trim(),
op: (operatorOrValue as Operator).trim() as Operator,
val: value,
});
}
}
return this;
}
/**
* Filter a query by ancestors.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ancestor-query-nodejs| Datastore Ancestor Filters}
*
* @param {Key} key Key object to filter by.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const query = datastore.createQuery('MyKind');
* const ancestoryQuery = query.hasAncestor(datastore.key(['Parent', 123]));
* ```
*/
hasAncestor(key: Key) {
this.filters.push({name: '__key__', op: 'HAS_ANCESTOR', val: key});
return this;
}
/**
* Sort the results by a property name in ascending or descending order. By
* default, an ascending sort order will be used.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-ascending-sort-nodejs| Datastore Sort Orders}
*
* @param {string} property The property to order by.
* @param {object} [options] Options object.
* @param {boolean} [options.descending=false] Sort the results by a property
* name in descending order.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* // Sort by size ascendingly.
* const companiesAscending = companyQuery.order('size');
*
* // Sort by size descendingly.
* const companiesDescending = companyQuery.order('size', {
* descending: true
* });
* ```
*/
order(property: string, options?: OrderOptions) {
const sign = options && options.descending ? '-' : '+';
this.orders.push({name: property, sign});
return this;
}
/**
* Group query results by a list of properties.
*
* @param {array} properties Properties to group by.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
* const groupedQuery = companyQuery.groupBy(['name', 'size']);
* ```
*/
groupBy(fieldNames: string | string[]) {
this.groupByVal = arrify(fieldNames);
return this;
}
/**
* Retrieve only select properties from the matched entities.
*
* Queries that select a subset of properties are called Projection Queries.
*
* @see {@link https://cloud.google.com/datastore/docs/samples/datastore-projection-query| Projection Queries}
*
* @param {string|string[]} fieldNames Properties to return from the matched
* entities.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* // Only retrieve the name property.
* const selectQuery = companyQuery.select('name');
*
* // Only retrieve the name and size properties.
* const selectQuery = companyQuery.select(['name', 'size']);
* ```
*/
select(fieldNames: string | string[]) {
this.selectVal = arrify(fieldNames);
return this;
}
/**
* Set a starting cursor to a query.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#cursors_limits_and_offsets| Query Cursors}
*
* @param {string} cursorToken The starting cursor token.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* const cursorToken = 'X';
*
* // Retrieve results starting from cursorToken.
* const startQuery = companyQuery.start(cursorToken);
* ```
*/
start(start: string | Buffer) {
this.startVal = start;
return this;
}
/**
* Set an ending cursor to a query.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Query_cursors| Query Cursors}
*
* @param {string} cursorToken The ending cursor token.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* const cursorToken = 'X';
*
* // Retrieve results limited to the extent of cursorToken.
* const endQuery = companyQuery.end(cursorToken);
* ```
*/
end(end: string | Buffer) {
this.endVal = end;
return this;
}
/**
* Set a limit on a query.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Limits}
*
* @param {number} n The number of results to limit the query to.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* // Limit the results to 10 entities.
* const limitQuery = companyQuery.limit(10);
* ```
*/
limit(n: number) {
this.limitVal = n;
return this;
}
/**
* Set an offset on a query.
*
* @see {@link https://cloud.google.com/datastore/docs/concepts/queries#datastore-limit-nodejs| Query Offsets}
*
* @param {number} n The offset to start from after the start cursor.
* @returns {Query}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const companyQuery = datastore.createQuery('Company');
*
* // Start from the 101st result.
* const offsetQuery = companyQuery.offset(100);
* ```
*/
offset(n: number) {
this.offsetVal = n;
return this;
}
/**
* Run the query.
*
* @param {object} [options] Optional configuration.
* @param {string} [options.consistency] Specify either `strong` or `eventual`.
* If not specified, default values are chosen by Datastore for the
* operation. Learn more about strong and eventual consistency
* [here](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore).
* @param {object} [options.gaxOptions] Request configuration options, outlined
* here: https://googleapis.github.io/gax-nodejs/global.html#CallOptions.
* @param {boolean | IntegerTypeCastOptions} [options.wrapNumbers=false]
* Wrap values of integerValue type in {@link Datastore#Int} objects.
* If a `boolean`, this will wrap values in {@link Datastore#Int} objects.
* If an `object`, this will return a value returned by
* `wrapNumbers.integerTypeCastFunction`.
* Please see {@link IntegerTypeCastOptions} for options descriptions.
* @param {function} [callback] The callback function. If omitted, a readable
* stream instance is returned.
* @param {?error} callback.err An error returned while making this request
* @param {object[]} callback.entities A list of entities.
* @param {object} callback.info An object useful for pagination.
* @param {?string} callback.info.endCursor Use this in a follow-up query to
* begin from where these results ended.
* @param {string} callback.info.moreResults Datastore responds with one of:
*
* - {@link Datastore#MORE_RESULTS_AFTER_LIMIT}: There *may* be more
* results after the specified limit.
* - {@link Datastore#MORE_RESULTS_AFTER_CURSOR}: There *may* be more
* results after the specified end cursor.
* - {@link Datastore#NO_MORE_RESULTS}: There are no more results.
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const query = datastore.createQuery('Company');
*
* query.run((err, entities, info) => {
* // entities = An array of records.
*
* // Access the Key object for an entity.
* const firstEntityKey = entities[0][datastore.KEY];
* });
*
* //-
* // A keys-only query returns just the keys of the result entities instead
* of
* // the entities themselves, at lower latency and cost.
* //-
* query.select('__key__');
*
* query.run((err, entities) => {
* const keys = entities.map((entity) => {
* return entity[datastore.KEY];
* });
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* query.run().then((data) => {
* const entities = data[0];
* });
* ```
*/
run(options?: RunQueryOptions): Promise<RunQueryResponse>;
run(options: RunQueryOptions, callback: RunQueryCallback): void;
run(callback: RunQueryCallback): void;
run(
optionsOrCallback?: RunQueryOptions | RunQueryCallback,
cb?: RunQueryCallback,
): void | Promise<RunQueryResponse> {
const options =
typeof optionsOrCallback === 'object' ? optionsOrCallback : {};
const callback =
typeof optionsOrCallback === 'function' ? optionsOrCallback : cb!;
const runQuery = this.scope!.runQuery.bind(this.scope);
return runQuery(this, options, callback);
}
/**
* Run the query as a readable object stream.
*
* @method Query#runStream
* @param {object} [options] Optional configuration. See
* {@link Query#run} for a complete list of options.
* @returns {stream}
*
* @example
* ```
* const {Datastore} = require('@google-cloud/datastore');
* const datastore = new Datastore();
* const query = datastore.createQuery('Company');
*
* query.runStream()
* .on('error', console.error)
* .on('data', function (entity) {
* // Access the Key object for this entity.
* const key = entity[datastore.KEY];
* })
* .on('info', (info) => {})
* .on('end', () => {
* // All entities retrieved.
* });
*
* //-
* // If you anticipate many results, you can end a stream early to prevent
* // unnecessary processing and API requests.
* //-
* query.runStream()
* .on('data', function (entity) {
* this.end();
* });
* ```
*/
runStream(options?: RunQueryStreamOptions) {
return this.scope!.runQueryStream(this, options);
}
}
export interface QueryProto {
startCursor?: string | Buffer;
distinctOn: {};
kind: {};
order: {};
projection: {};
endCursor?: string | Buffer;
limit?: {};
offset?: number;
filter?: {};
}
/**
* Reference to the {@link Query} class.
* @name module:@google-cloud/datastore.Query
* @see Query
*/
export {Query};
export interface IntegerTypeCastOptions {
integerTypeCastFunction: Function;
properties?: string | string[];
}
export interface ExplainOptions {
analyze?: boolean;
}
export interface RunQueryOptions {
consistency?: 'strong' | 'eventual';
readTime?: number;
gaxOptions?: CallOptions;
explainOptions?: ExplainOptions;
wrapNumbers?: boolean | IntegerTypeCastOptions;
}
export interface RunQueryCallback {
(err: Error | null, entities?: Entity[], info?: RunQueryInfo): void;
}
export type RunQueryResponse = [Entity[], RunQueryInfo];
export type RunAggregateQueryResponse = any;
export interface RunQueryInfo {
endCursor?: string;
moreResults?:
| 'MORE_RESULTS_TYPE_UNSPECIFIED'
| 'NOT_FINISHED'
| 'MORE_RESULTS_AFTER_LIMIT'
| 'MORE_RESULTS_AFTER_CURSOR'
| 'NO_MORE_RESULTS';
explainMetrics?: ExplainMetrics;
}
export interface ExplainMetrics {
planSummary?: PlanSummary;
executionStats?: ExecutionStats;
}
export interface ExecutionStats {
resultsReturned?: number;
executionDuration?: google.protobuf.IDuration;
readOperations?: number;
debugStats?: {
[key: string]: any;
};
}
export interface PlanSummary {
indexesUsed: {
[key: string]: any;
}[];
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.