forked from dynamoose/dynamoose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Query.js
727 lines (619 loc) · 19.4 KB
/
Query.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
'use strict';
var dynamoose = require('../');
dynamoose.AWS.config.update({
accessKeyId: 'AKID',
secretAccessKey: 'SECRET',
region: 'us-east-1'
});
dynamoose.local();
var Schema = dynamoose.Schema;
var should = require('should');
describe('Query', function (){
this.timeout(10000);
before(function (done) {
dynamoose.setDefaults({ prefix: '', suffix: '' });
var dogSchema = new Schema({
ownerId: {
type: Number,
validate: function(v) { return v > 0; },
hashKey: true,
index: [{
global: true,
rangeKey: 'color',
name: 'ColorRangeIndex',
project: true, // ProjectionType: ALL
},{
global: true,
rangeKey: 'breed',
name: 'BreedRangeIndex',
project: true, // ProjectionType: ALL
}]
},
breed: {
type: String,
required: true,
index: {
global: true,
rangeKey: 'ownerId',
name: 'BreedIndex',
project: true, // ProjectionType: ALL
throughput: 5 // read and write are both 5
}
},
origin: {
type: String,
index: true // name: originLocalIndex, ProjectionType: ALL
},
name: {
type: String,
rangeKey: true,
index: true // name: nameLocalIndex, ProjectionType: ALL
},
color: {
type: String,
default: 'Brown',
index: [{ // name: colorLocalIndex
project: ['name'] // ProjectionType: INCLUDE
},{ // name: colorGlobalIndex, no ragne key
global: true,
project: ['name'] // ProjectionType: INCLUDE
}]
},
siblings: {
type: 'list',
list: [ {
type: String
} ]
},
age: {
type: Number
}
});
var Dog = dynamoose.model('Dog', dogSchema);
function addDogs (dogs) {
if(dogs.length <= 0) {
return done();
}
var dog = new Dog(dogs.pop());
dog.save(function (err) {
if (err) {
return done(err);
}
addDogs(dogs);
});
}
addDogs([
{ownerId:1, name: 'Foxy Lady', breed: 'Jack Russell Terrier', color: 'White, Brown and Black', siblings: ['Quincy', 'Princes'], age: 2},
{ownerId:2, name: 'Quincy', breed: 'Jack Russell Terrier', color: 'White and Brown', siblings: ['Foxy Lady', 'Princes'], age: 3},
{ownerId:2, name: 'Princes', breed: 'Jack Russell Terrier', color: 'White and Brown', siblings: ['Foxy Lady', 'Quincy'], age: 6},
{ownerId:3, name: 'Toto', breed: 'Terrier', color: 'Brown', age: 1},
{ownerId:4, name: 'Oddie', breed: 'beagle', color: 'Tan', age: 2},
{ownerId:5, name: 'Pluto', breed: 'unknown', color: 'Mustard', age: 4},
{ownerId:6, name: 'Brian Griffin', breed: 'unknown', color: 'White', age: 5},
{ownerId:7, name: 'Scooby Doo', breed: 'Great Dane', age: 2},
{ownerId:8, name: 'Blue', breed: 'unknown', color: 'Blue', age: 1},
{ownerId:9, name: 'Lady', breed: ' Cocker Spaniel', age: 6},
{ownerId:10, name: 'Copper', breed: 'Hound', age: 8},
{ownerId:11, name: 'Old Yeller', breed: 'unknown', color: 'Tan', age: 1},
{ownerId:12, name: 'Hooch', breed: 'Dogue de Bordeaux', color: 'Brown', age: 3},
{ownerId:13, name: 'Rin Tin Tin', breed: 'German Shepherd', age: 5},
{ownerId:14, name: 'Benji', breed: 'unknown', age: 1},
{ownerId:15, name: 'Wishbone', breed: 'Jack Russell Terrier', color: 'White', age: 2},
{ownerId:16, name: 'Marley', breed: 'Labrador Retriever', color: 'Yellow', age: 9},
{ownerId:17, name: 'Beethoven', breed: 'St. Bernard', age: 3},
{ownerId:18, name: 'Lassie', breed: 'Collie', color: 'tan and white', age: 4},
{ownerId:19, name: 'Snoopy', breed: 'beagle', color: 'black and white', age: 6},
{ownerId:20, name: 'Max', breed: 'Westie', age: 7, origin: 'Scotland'},
{ownerId:20, name: 'Gigi', breed: 'Spaniel', color: 'Chocolate', age: 1, origin: 'Great Britain'},
{ownerId:20, name: 'Mimo', breed: 'Boxer', color: 'Chocolate', age: 2, origin: 'Germany'},
{ownerId:20, name: 'Bepo', breed: 'French Bulldog', color: 'Grey', age: 4, origin: 'France'},
]);
});
after(function (done) {
var Dog = dynamoose.model('Dog');
Dog.$__.table.delete(function (err) {
if(err) {
done(err);
}
delete dynamoose.models.Dog;
done();
});
});
it('Basic Query', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(2).exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(2);
done();
});
});
it('Basic Query One', function (done) {
var Dog = dynamoose.model('Dog');
Dog.queryOne('ownerId').eq(1).exec(function (err, oneDog) {
should.not.exist(err);
should.exist(oneDog);
oneDog.ownerId.should.eql(1);
done();
});
});
it('Basic Query on Secondary Global Index', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(4);
dogs[0].ownerId.should.eql(1);
done();
});
});
it('Query on Secondary Global Index with range - no results', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').where('ownerId').eq(4).exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(0);
done();
});
});
it('Query on Secondary Global Index with range', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown').where('ownerId').lt(8).exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(2);
done();
});
});
it('Query on Secondary Global Index with same hashKey', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(20).where('color').beginsWith('Choc').exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(2);
dogs[0].name.should.eql('Gigi');
dogs[1].name.should.eql('Mimo');
done();
});
});
it('Query on Secondary Global Index with same hashKey and 2nd in index list', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').using('BreedRangeIndex').eq(20).where('breed').beginsWith('Sp').exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(1);
dogs[0].name.should.eql('Gigi');
done();
});
});
it('Query with Secondary Local Index as range', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(20).where('origin').beginsWith('G').exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(2);
done();
});
});
it('where() must follow eq()', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').where("test")
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: where() must follow eq()');
done();
});
});
it('filter() must follow comparison', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').filter("test")
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: filter() must follow comparison');
done();
});
});
it('eq must follow query()', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').lt(5)
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: eq must follow query()');
done();
});
});
it('Should throw first error', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').lt(5)
.where().filter().compVal().beginsWith().in().between()
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: eq must follow query()');
done();
});
});
it('Basic Query on SGI descending', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').descending().exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(4);
dogs[0].ownerId.should.eql(15);
done();
});
});
it('Basic Query on SGI ascending', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').ascending().exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(4);
dogs[0].ownerId.should.eql(1);
done();
});
});
it('Basic Query on SGI limit 1', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').limit(1).exec(function (err, dogs) {
should.not.exist(err);
should.exist(dogs.lastKey);
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(1);
done();
});
});
it('Basic Query on SGI startAt key', function (done) {
var Dog = dynamoose.model('Dog');
var startKey = { breed: { S: 'Jack Russell Terrier' },
ownerId: { N: '1' },
name: { S: 'Foxy Lady' } };
Dog.query('breed').eq('Jack Russell Terrier').startAt(startKey).limit(1).exec(function (err, dogs) {
should.not.exist(err);
should.exist(dogs.lastKey);
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(2);
done();
});
});
it('Basic Query on SGI with attributes', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier').attributes(['name']).exec(function (err, dogs) {
should.not.exist(err);
should.not.exist(dogs.lastKey);
dogs.length.should.eql(4);
dogs[0].should.not.have.property('ownerId');
dogs[0].should.have.property('name', 'Foxy Lady');
done();
});
});
it('Basic Query with consistent read', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(2).consistent().exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(2);
done();
});
});
it('Basic Query on SGI with filter contains', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.where('ownerId').eq(1)
.filter('color').contains('Black').exec()
.then(function (dogs) {
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(1);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter contains on list', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.where('ownerId').eq(2)
.filter('siblings').contains('Quincy').exec()
.then(function (dogs) {
// console.log('The dogs', dogs);
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(2);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter null', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.filter('color').not().null().exec()
.then(function (dogs) {
dogs.length.should.eql(5);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not null', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.filter('color').null().exec()
.then(function (dogs) {
dogs.length.should.eql(0);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter le', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').le(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(4);
dogs[dogs.length - 1].ownerId.should.eql(11);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not le', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').not().le(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(14);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter ge', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').ge(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(2);
dogs[0].ownerId.should.eql(11);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not ge', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').not().ge(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(3);
dogs[0].ownerId.should.eql(5);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter gt', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').gt(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(14);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not gt', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').not().gt(11)
.exec()
.then(function (dogs) {
dogs.length.should.eql(4);
dogs[0].ownerId.should.eql(5);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not eq and not lt', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.where('ownerId').not().lt(10)
.and()
.filter('color').not().eq('Brown')
.exec()
.then(function (dogs) {
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(11);
done();
})
.catch(done);
});
it('Basic Query on SGI with filter not contains or beginsWith', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('color').not().contains('Brown')
.or()
.filter('name').beginsWith('Q').exec()
.then(function (dogs) {
dogs.length.should.eql(2);
done();
})
.catch(done);
});
it('beginsWith() cannot follow not()', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('color').not().contains('Brown')
.or()
.filter('name').not().beginsWith('Q')
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: beginsWith() cannot follow not()');
done();
});
});
it('Basic Query on SGI with filter between', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('age').between(5,7)
.exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(1);
dogs[0].ownerId.should.eql(2);
done();
});
});
it('between() cannot follow not()', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('age').not().between(5,7)
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: between() cannot follow not()');
done();
});
});
it('Basic Query on SGI with filter in', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('color').in(["White and Brown", "White"])
.exec(function (err, dogs) {
should.not.exist(err);
dogs.length.should.eql(3);
dogs[0].ownerId.should.eql(2);
done();
});
});
it('in() cannot follow not()', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('Jack Russell Terrier')
.filter('color').not().in(["White and Brown", "White"])
.exec(function (err) {
should.exist(err.message);
err.message.should.eql('Invalid Query state: in() cannot follow not()');
done();
});
});
it('Query.count', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(20).count().all().exec()
.then(function (count) {
count.should.eql(4);
done();
})
.catch(done);
});
it('Query.counts', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('breed').eq('unknown')
.and()
.filter('color').not().eq('Brown')
.counts().all().exec()
.then(function (counts) {
counts.scannedCount.should.eql(5);
counts.count.should.eql(4);
done();
})
.catch(done);
});
it('Query.all', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(20).limit(2).all().exec()
.then(function (dogs) {
dogs.length.should.eql(4);
done();
})
.catch(done);
});
it('Query.all(1, 3)', function (done) {
var Dog = dynamoose.model('Dog');
Dog.query('ownerId').eq(20).limit(1).all(1000, 3).exec()
.then(function (dogs) {
dogs.length.should.eql(3);
dogs.timesQueried.should.eql(3);
done();
})
.catch(done);
});
it('Should allow multiple indexes and query correctly', function (done) {
var schema = new dynamoose.Schema({
id: {
type: String,
hashKey: true,
required: true
},
orgId: {
type: String,
index: [{
global : true,
name : 'OrganizationCreateAtIndex',
rangeKey : 'createdAt',
throughput: 1
}, {
global : true,
name : 'OrganizationExpectedArriveAtIndex',
rangeKey : 'expectedArriveAt',
throughput: 1
}],
required: true,
},
expectedArriveAt: Date
},{
throughput: 1,
timestamps: true
});
var Log = dynamoose.model('Log-1', schema);
var log1 = new Log({id: "test1", orgId: "org1", expectedArriveAt: Date.now()});
log1.save(function() {
Log.query('orgId').eq("org1")
.where('expectedArriveAt').lt( new Date() )
.exec()
.then(function(res){
res.length.should.eql(1);
Log.query('orgId').eq("org1")
.where('createdAt').lt( new Date() )
.exec()
.then(function(res){
res.length.should.eql(1);
done();
})
.catch(function(e){
done(e);
});
})
.catch(function(e){
done(e);
});
});
});
it('Should allow multiple local indexes and query correctly', async function () {
var schema = new dynamoose.Schema({
id: {
type: String,
hashKey: true,
},
orgId: {
type: String,
rangeKey: true,
},
updatedAt: {
type: Date,
index: {
global: false,
name : 'OrganizationUpdatedAtIndex'
}
},
expectedArriveAt: {
type: Date,
index: {
global: false,
name : 'OrganizationExpectedArriveAtIndex'
}
}
},{
throughput: 1,
timestamps: true
});
var Log = dynamoose.model('Log-2', schema);
var log1 = new Log({id: "test1", orgId: "org1", expectedArriveAt: Date.now()});
var log2 = new Log({id: "test1", orgId: "org2", expectedArriveAt: Date.now()});
await log1.save();
await log2.save();
var res = await Log.query('id').eq("test1")
.where('expectedArriveAt').lt( new Date() )
.exec()
res.length.should.eql(2);
var res2 = await Log.query('id').eq("test1")
.where('updatedAt').le( log1.createdAt.getTime() )
.exec();
res2.length.should.eql(1);
});
});