-
Notifications
You must be signed in to change notification settings - Fork 106
/
concepts.js
1215 lines (1015 loc) · 30.6 KB
/
concepts.js
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2017 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.
'use strict';
const sinon = require('sinon');
const assert = require('assert');
// By default, the client will authenticate using the service account file
// specified by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use
// the project specified by the GCLOUD_PROJECT environment variable. See
// https://googlecloudplatform.github.io/gcloud-node/#/docs/google-cloud/latest/guides/authentication
const {Datastore, PropertyFilter, and} = require('@google-cloud/datastore');
function makeStub() {
return sinon.stub().returns(Promise.resolve([]));
}
// This mock is used in the documentation snippets.
let datastore = {
delete: makeStub(),
get: makeStub(),
insert: makeStub(),
key: makeStub(),
update: makeStub(),
upsert: makeStub(),
runQuery: sinon.stub().returns(Promise.resolve([[]])),
save: makeStub(),
};
const namespace = `${Date.now()}`;
class TestHelper {
constructor(projectId) {
const options = {
projectId: projectId,
namespace,
};
this.datastore = new Datastore(options);
}
}
class Entity extends TestHelper {
constructor(projectId) {
super(projectId);
// To create the keys, we have to use this instance of Datastore.
datastore.key = this.datastore.key;
datastore.namespace = this.datastore.namespace;
this.incompleteKey = this.getIncompleteKey();
this.namedKey = this.getNamedKey();
this.keyWithParent = this.getKeyWithParent();
this.keyWithMultiLevelParent = this.getKeyWithMultiLevelParent();
}
getIncompleteKey() {
// [START datastore_incomplete_key]
const taskKey = datastore.key('Task');
// [END datastore_incomplete_key]
return taskKey;
}
getNamedKey() {
// [START datastore_named_key]
const taskKey = datastore.key(['Task', 'sampleTask']);
// [END datastore_named_key]
return taskKey;
}
getKeyWithParent() {
// [START datastore_key_with_parent]
const taskKey = datastore.key([
'TaskList',
'default',
'Task',
'sampleTask',
]);
// [END datastore_key_with_parent]
return taskKey;
}
getKeyWithMultiLevelParent() {
// [START datastore_key_with_multilevel_parent]
const taskKey = datastore.key([
'User',
'alice',
'TaskList',
'default',
'Task',
'sampleTask',
]);
// [END datastore_key_with_multilevel_parent]
return taskKey;
}
getTask() {
// [START datastore_basic_entity]
const task = {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
};
// [END datastore_basic_entity]
return task;
}
testIncompleteKey() {
return this.datastore.save({
key: this.incompleteKey,
data: {},
});
}
testNamedKey() {
return this.datastore.save({
key: this.namedKey,
data: {},
});
}
testKeyWithParent() {
return this.datastore.save({
key: this.keyWithParent,
data: {},
});
}
testKeyWithMultiLevelParent() {
return this.datastore.save({
key: this.keyWithMultiLevelParent,
data: {},
});
}
testEntityWithParent() {
// [START datastore_entity_with_parent]
const taskKey = datastore.key([
'TaskList',
'default',
'Task',
'sampleTask',
]);
const task = {
key: taskKey,
data: {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
},
};
// [END datastore_entity_with_parent]
return this.datastore.save(task);
}
testProperties() {
// [START datastore_properties]
const task = [
{
name: 'category',
value: 'Personal',
},
{
name: 'created',
value: new Date(),
},
{
name: 'done',
value: false,
},
{
name: 'priority',
value: 4,
},
{
name: 'percent_complete',
value: 10.0,
},
{
name: 'description',
value: 'Learn Cloud Datastore',
excludeFromIndexes: true,
},
];
// [END datastore_properties]
return this.datastore.save({
key: this.incompleteKey,
data: task,
});
}
testArrayValue() {
// [START datastore_array_value]
const task = {
tags: ['fun', 'programming'],
collaborators: ['alice', 'bob'],
};
// [END datastore_array_value]
return this.datastore.save({
key: this.incompleteKey,
data: task,
});
}
testBasicEntity() {
return this.datastore.save({
key: this.getIncompleteKey(),
data: this.getTask(),
});
}
async testUpsert() {
// [START datastore_upsert]
const taskKey = datastore.key('Task');
const task = {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
};
const entity = {
key: taskKey,
data: task,
};
await datastore.upsert(entity);
// Task inserted successfully.
// [END datastore_upsert]
return this.datastore.upsert({
key: this.datastore.key(['Task', 1]),
data: task,
});
}
testInsert() {
// [START datastore_insert]
const taskKey = datastore.key('Task');
const task = {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
};
const entity = {
key: taskKey,
data: task,
};
datastore.insert(entity).then(() => {
// Task inserted successfully.
});
// [END datastore_insert]
return this.datastore.save({
method: 'insert',
key: taskKey,
data: task,
});
}
async testLookup() {
// [START datastore_lookup]
const taskKey = datastore.key('Task');
const [entity] = await datastore.get(taskKey);
// entity = {
// category: 'Personal',
// done: false,
// priority: 4,
// description: 'Learn Cloud Datastore',
// [Symbol(KEY)]:
// Key {
// namespace: undefined,
// id: '...',
// kind: 'Task',
// path: [Getter]
// }
// }
// };
console.log(entity);
// [END datastore_lookup]
await this.datastore.save({
method: 'insert',
key: taskKey,
data: {},
});
return this.datastore.get(taskKey);
}
async testUpdate() {
// [START datastore_update]
const taskKey = datastore.key('Task');
const task = {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
};
const entity = {
key: taskKey,
data: task,
};
await datastore.update(entity);
// Task updated successfully.
// [END datastore_update]
await this.datastore.save({
method: 'insert',
key: taskKey,
data: {},
});
return this.datastore.update({key: taskKey, data: task});
}
async testDelete() {
// [START datastore_delete]
const taskKey = datastore.key('Task');
await datastore.delete(taskKey);
// Task deleted successfully.
// [END datastore_delete]
await this.datastore.save({
method: 'insert',
key: taskKey,
data: {},
});
return this.datastore.delete(taskKey);
}
async testBatchUpsert() {
// [START datastore_batch_upsert]
const taskKey1 = this.datastore.key(['Task', 1]);
const taskKey2 = this.datastore.key(['Task', 2]);
const task1 = {
category: 'Personal',
done: false,
priority: 4,
description: 'Learn Cloud Datastore',
};
const task2 = {
category: 'Work',
done: false,
priority: 8,
description: 'Integrate Cloud Datastore',
};
const entities = [
{
key: taskKey1,
data: task1,
},
{
key: taskKey2,
data: task2,
},
];
await datastore.upsert(entities);
// Tasks inserted successfully.
// [END datastore_batch_upsert]
return this.datastore.upsert([
{
key: taskKey1,
data: task1,
},
{
key: taskKey2,
data: task2,
},
]);
}
async testBatchLookup() {
// [START datastore_batch_lookup]
const taskKey1 = this.datastore.key(['Task', 1]);
const taskKey2 = this.datastore.key(['Task', 2]);
const keys = [taskKey1, taskKey2];
const [tasks] = await datastore.get(keys);
// Tasks retrieved successfully.
console.log(tasks);
// [END datastore_batch_lookup]
return this.datastore.get([taskKey1, taskKey2]);
}
async testBatchDelete() {
// [START datastore_batch_delete]
const taskKey1 = this.datastore.key(['Task', 1]);
const taskKey2 = this.datastore.key(['Task', 2]);
const keys = [taskKey1, taskKey2];
await datastore.delete(keys);
// Tasks deleted successfully.
// [END datastore_batch_delete]
return this.datastore.delete([taskKey1, taskKey2]);
}
}
class Index extends TestHelper {
testUnindexedPropertyQuery() {
const datastore = this.datastore;
// [START datastore_unindexed_property_query]
const query = datastore
.createQuery('Task')
.filter(new PropertyFilter('description', '=', 'A task description.'));
// [END datastore_unindexed_property_query]
return this.datastore.runQuery(query);
}
async testExplodingProperties() {
const original = datastore.key;
datastore.key = this.datastore.key;
// [START datastore_exploding_properties]
const task = {
method: 'insert',
key: datastore.key('Task'),
data: {
tags: ['fun', 'programming', 'learn'],
collaborators: ['alice', 'bob', 'charlie'],
created: new Date(),
},
};
// [END datastore_exploding_properties]
datastore.key = original;
await this.datastore.save(task);
assert.ok(task.key);
assert.ok(task.key.id);
}
}
class Metadata extends TestHelper {
async testNamespaceRunQuery() {
const datastore = this.datastore;
const startNamespace = 'Animals';
const endNamespace = 'Zoos';
await datastore.save({
key: datastore.key({
namespace: 'Animals',
path: ['Ant', 1],
}),
data: {},
});
// [START datastore_namespace_run_query]
async function runNamespaceQuery(startNamespace, endNamespace) {
const startKey = datastore.key(['__namespace__', startNamespace]);
const endKey = datastore.key(['__namespace__', endNamespace]);
const query = datastore
.createQuery('__namespace__')
.select('__key__')
.filter(
and([
new PropertyFilter('__key__', '>=', startKey),
new PropertyFilter('__key__', '<', endKey),
])
);
const [entities] = await datastore.runQuery(query);
const namespaces = entities.map(entity => entity[datastore.KEY].name);
console.log('Namespaces:');
namespaces.forEach(namespace => console.log(namespace));
return namespaces;
}
// [END datastore_namespace_run_query]
const namespaces = await runNamespaceQuery(startNamespace, endNamespace);
assert.strictEqual(namespaces.includes('Animals'), true);
}
async testKindRunQuery() {
const datastore = this.datastore;
// [START datastore_kind_run_query]
async function runKindQuery() {
const query = datastore.createQuery('__kind__').select('__key__');
const [entities] = await datastore.runQuery(query);
const kinds = entities.map(entity => entity[datastore.KEY].name);
console.log('Kinds:');
kinds.forEach(kind => console.log(kind));
return kinds;
}
// [END datastore_kind_run_query]
const kinds = await runKindQuery();
assert.strictEqual(kinds.includes('Account'), true);
}
async testPropertyRunQuery() {
const datastore = this.datastore;
// [START datastore_property_run_query]
async function runPropertyQuery() {
const query = datastore.createQuery('__property__').select('__key__');
const [entities] = await datastore.runQuery(query);
// @TODO convert below object to map
const propertiesByKind = {};
entities.forEach(entity => {
const key = entity[datastore.KEY];
const kind = key.path[1];
const property = key.path[3];
propertiesByKind[kind] = propertiesByKind[kind] || [];
propertiesByKind[kind].push(property);
});
console.log('Properties by Kind:');
for (const key in propertiesByKind) {
console.log(key, propertiesByKind[key]);
}
return propertiesByKind;
}
// [END datastore_property_run_query]
const propertiesByKind = await runPropertyQuery();
assert.deepStrictEqual(propertiesByKind.Account, ['balance']);
}
async testPropertyByKindRunQuery() {
const datastore = this.datastore;
// [START datastore_property_by_kind_run_query]
async function runPropertyByKindQuery() {
const ancestorKey = datastore.key(['__kind__', 'Account']);
const query = datastore
.createQuery('__property__')
.hasAncestor(ancestorKey);
const [entities] = await datastore.runQuery(query);
const representationsByProperty = {};
entities.forEach(entity => {
const key = entity[datastore.KEY];
const propertyName = key.name;
const propertyType = entity.property_representation;
representationsByProperty[propertyName] = propertyType;
});
console.log('Task property representations:');
for (const key in representationsByProperty) {
console.log(key, representationsByProperty[key]);
}
return representationsByProperty;
}
// [END datastore_property_by_kind_run_query]
const propertiesByKind = await runPropertyByKindQuery();
assert.deepStrictEqual(propertiesByKind, {
balance: ['INT64'],
});
}
}
class Query extends TestHelper {
constructor(projectId) {
super(projectId);
this.basicQuery = this.getBasicQuery();
this.projectionQuery = this.getProjectionQuery();
this.ancestorQuery = this.getAncestorQuery();
}
getBasicQuery() {
const datastore = this.datastore;
// [START datastore_basic_query]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('done', '=', false),
new PropertyFilter('priority', '>=', 4),
])
)
.order('priority', {
descending: true,
});
// [END datastore_basic_query]
return query;
}
getProjectionQuery() {
const datastore = this.datastore;
// [START datastore_projection_query]
const query = datastore
.createQuery('Task')
.select(['priority', 'percent_complete']);
// [END datastore_projection_query]
return query;
}
getAncestorQuery() {
const datastore = this.datastore;
// [START datastore_ancestor_query]
const ancestorKey = datastore.key(['TaskList', 'default']);
const query = datastore.createQuery('Task').hasAncestor(ancestorKey);
// [END datastore_ancestor_query]
return query;
}
async testRunQuery() {
const query = this.basicQuery;
// [START datastore_run_query]
const [tasks] = await datastore.runQuery(query);
console.log('Tasks:');
tasks.forEach(task => console.log(task));
// [END datastore_run_query]
return this.datastore.runQuery(query);
}
testPropertyFilter() {
const datastore = this.datastore;
// [START datastore_property_filter]
const query = datastore
.createQuery('Task')
.filter(new PropertyFilter('done', '=', false));
// [END datastore_property_filter]
return this.datastore.runQuery(query);
}
testCompositeFilter() {
const datastore = this.datastore;
// [START datastore_composite_filter]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('done', '=', false),
new PropertyFilter('priority', '=', 4),
])
);
// [END datastore_composite_filter]
return this.datastore.runQuery(query);
}
testKeyFilter() {
const datastore = this.datastore;
// [START datastore_key_filter]
const query = datastore
.createQuery('Task')
.filter(
new PropertyFilter('__key__', '>', datastore.key(['Task', 'someTask']))
);
// [END datastore_key_filter]
return this.datastore.runQuery(query);
}
testAscendingSort() {
const datastore = this.datastore;
// [START datastore_ascending_sort]
const query = datastore.createQuery('Task').order('created');
// [END datastore_ascending_sort]
return this.datastore.runQuery(query);
}
testDescendingSort() {
const datastore = this.datastore;
// [START datastore_descending_sort]
const query = datastore.createQuery('Task').order('created', {
descending: true,
});
// [END datastore_descending_sort]
return this.datastore.runQuery(query);
}
testMultiSort() {
const datastore = this.datastore;
// [START datastore_multi_sort]
const query = datastore
.createQuery('Task')
.order('priority', {
descending: true,
})
.order('created');
// [END datastore_multi_sort]
return this.datastore.runQuery(query);
}
testKindlessQuery() {
const datastore = this.datastore;
const lastSeenKey = this.datastore.key(['Task', Date.now()]);
// [START datastore_kindless_query]
const query = datastore
.createQuery()
.filter(new PropertyFilter('__key__', '>', lastSeenKey))
.limit(1);
// [END datastore_kindless_query]
return this.datastore.runQuery(query);
}
async testRunQueryProjection() {
const datastore = this.datastore;
const query = this.projectionQuery;
// [START datastore_run_query_projection]
async function runProjectionQuery() {
const priorities = [];
const percentCompletes = [];
const [tasks] = await datastore.runQuery(query);
tasks.forEach(task => {
priorities.push(task.priority);
percentCompletes.push(task.percent_complete);
});
return {
priorities: priorities,
percentCompletes: percentCompletes,
};
}
// [END datastore_run_query_projection]
return await runProjectionQuery();
}
testKeysOnlyQuery() {
const datastore = this.datastore;
// [START datastore_keys_only_query]
const query = datastore.createQuery().select('__key__').limit(1);
// [END datastore_keys_only_query]
return this.datastore.runQuery(query);
}
testDistinctOnQuery() {
const datastore = this.datastore;
// [START datastore_distinct_on_query]
const query = datastore
.createQuery('Task')
.groupBy('category')
.order('category')
.order('priority');
// [END datastore_distinct_on_query]
return this.datastore.runQuery(query);
}
testArrayValueInequalityRange() {
const datastore = this.datastore;
// [START datastore_array_value_inequality_range]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('tag', '>', 'learn'),
new PropertyFilter('tag', '<', 'math'),
])
);
// [END datastore_array_value_inequality_range]
return this.datastore.runQuery(query);
}
testArrayValueEquality() {
const datastore = this.datastore;
// [START datastore_array_value_equality]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('tag', '=', 'fun'),
new PropertyFilter('tag', '=', 'programming'),
])
);
// [END datastore_array_value_equality]
return this.datastore.runQuery(query);
}
testInequalityRange() {
const datastore = this.datastore;
// [START datastore_inequality_range]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('created', '>', new Date('1990-01-01T00:00:00z')),
new PropertyFilter('created', '<', new Date('2000-12-31T23:59:59z')),
])
);
// [END datastore_inequality_range]
return this.datastore.runQuery(query);
}
testInequalityInvalid() {
const datastore = this.datastore;
// [START datastore_inequality_invalid]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('priority', '>', 3),
new PropertyFilter('created', '>', new Date('1990-01-01T00:00:00z')),
])
);
// [END datastore_inequality_invalid]
return this.datastore.runQuery(query);
}
testEqualAndInequalityRange() {
const datastore = this.datastore;
// [START datastore_equal_and_inequality_range]
const query = datastore
.createQuery('Task')
.filter(
and([
new PropertyFilter('priority', '=', 4),
new PropertyFilter('done', '=', false),
new PropertyFilter('created', '>', new Date('1990-01-01T00:00:00z')),
new PropertyFilter('created', '<', new Date('2000-12-31T23:59:59z')),
])
);
// [END datastore_equal_and_inequality_range]
return this.datastore.runQuery(query);
}
testInequalitySort() {
const datastore = this.datastore;
// [START datastore_inequality_sort]
const query = datastore
.createQuery('Task')
.filter(new PropertyFilter('priority', '>', 3))
.order('priority')
.order('created');
// [END datastore_inequality_sort]
return this.datastore.runQuery(query);
}
testInequalitySortInvalidNotSame() {
const datastore = this.datastore;
// [START datastore_inequality_sort_invalid_not_same]
const query = datastore
.createQuery('Task')
.filter(new PropertyFilter('priority', '>', 3))
.order('created');
// [END datastore_inequality_sort_invalid_not_same]
return this.datastore.runQuery(query);
}
testInequalitySortInvalidNotFirst() {
const datastore = this.datastore;
// [START datastore_inequality_sort_invalid_not_first]
const query = datastore
.createQuery('Task')
.filter(new PropertyFilter('priority', '>', 3))
.order('created')
.order('priority');
// [END datastore_inequality_sort_invalid_not_first]
return this.datastore.runQuery(query);
}
testLimit() {
const datastore = this.datastore;
// [START datastore_limit]
const query = datastore.createQuery('Task').limit(5);
// [END datastore_limit]
return this.datastore.runQuery(query);
}
async testCursorPaging() {
const datastore = this.datastore;
const pageSize = 1;
// [START datastore_cursor_paging]
// By default, google-cloud-node will automatically paginate through all of
// the results that match a query. However, this sample implements manual
// pagination using limits and cursor tokens.
async function runPageQuery(pageCursor) {
let query = datastore.createQuery('Task').limit(pageSize);
if (pageCursor) {
query = query.start(pageCursor);
}
const results = await datastore.runQuery(query);
const entities = results[0];
const info = results[1];
if (info.moreResults !== Datastore.NO_MORE_RESULTS) {
// If there are more results to retrieve, the end cursor is
// automatically set on `info`. To get this value directly, access
// the `endCursor` property.
const results = await runPageQuery(info.endCursor);
// Concatenate entities
results[0] = entities.concat(results[0]);
return results;
}
return [entities, info];
}
// [END datastore_cursor_paging]
const [entities, info] = await runPageQuery();
assert.strictEqual(Array.isArray(entities), true);
if (!info || !info.endCursor) {
throw new Error('An `info` with an `endCursor` is not present.');
}
}
async testEventualConsistentQuery() {
const datastoreMock = datastore;
datastore = this.datastore;
// [START datastore_eventual_consistent_query]
const ancestorKey = datastore.key(['TaskList', 'default']);
const query = datastore.createQuery('Task').hasAncestor(ancestorKey);
query.run({consistency: 'eventual'});
// [END datastore_eventual_consistent_query]
const [entities] = await query.run({consistency: 'eventual'});
datastore = datastoreMock;
return entities;
}
}
// [START datastore_transactional_update]
async function transferFunds(fromKey, toKey, amount) {