@e22m4u/js-repository-mongodb-adapter 0.0.22 → 0.0.23
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/mongodb-adapter.js +44 -9
- package/src/mongodb-adapter.spec.js +142 -0
- package/src/utils/is-iso-date.js +1 -1
package/package.json
CHANGED
package/src/mongodb-adapter.js
CHANGED
|
@@ -160,6 +160,20 @@ export class MongodbAdapter extends Adapter {
|
|
|
160
160
|
return value;
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
+
/**
|
|
164
|
+
* Coerce date.
|
|
165
|
+
*
|
|
166
|
+
* @param value
|
|
167
|
+
* @returns {Date|*}
|
|
168
|
+
* @private
|
|
169
|
+
*/
|
|
170
|
+
_coerceDate(value) {
|
|
171
|
+
if (value == null) return value;
|
|
172
|
+
if (value instanceof Date) return value;
|
|
173
|
+
if (isIsoDate(value)) return new Date(value);
|
|
174
|
+
return value;
|
|
175
|
+
}
|
|
176
|
+
|
|
163
177
|
/**
|
|
164
178
|
* To database.
|
|
165
179
|
*
|
|
@@ -401,6 +415,7 @@ export class MongodbAdapter extends Adapter {
|
|
|
401
415
|
// string
|
|
402
416
|
if (typeof cond === 'string') {
|
|
403
417
|
query[key] = this._coerceId(cond);
|
|
418
|
+
query[key] = this._coerceDate(query[key]);
|
|
404
419
|
return;
|
|
405
420
|
}
|
|
406
421
|
// ObjectId
|
|
@@ -413,27 +428,35 @@ export class MongodbAdapter extends Adapter {
|
|
|
413
428
|
const opConds = [];
|
|
414
429
|
// eq
|
|
415
430
|
if ('eq' in cond) {
|
|
416
|
-
|
|
431
|
+
let eq = this._coerceId(cond.eq);
|
|
432
|
+
eq = this._coerceDate(eq);
|
|
433
|
+
opConds.push({$eq: eq});
|
|
417
434
|
}
|
|
418
435
|
// neq
|
|
419
436
|
if ('neq' in cond) {
|
|
420
|
-
|
|
437
|
+
let neq = this._coerceId(cond.neq);
|
|
438
|
+
neq = this._coerceDate(neq);
|
|
439
|
+
opConds.push({$ne: neq});
|
|
421
440
|
}
|
|
422
441
|
// gt
|
|
423
442
|
if ('gt' in cond) {
|
|
424
|
-
|
|
443
|
+
const gt = this._coerceDate(cond.gt);
|
|
444
|
+
opConds.push({$gt: gt});
|
|
425
445
|
}
|
|
426
446
|
// lt
|
|
427
447
|
if ('lt' in cond) {
|
|
428
|
-
|
|
448
|
+
const lt = this._coerceDate(cond.lt);
|
|
449
|
+
opConds.push({$lt: lt});
|
|
429
450
|
}
|
|
430
451
|
// gte
|
|
431
452
|
if ('gte' in cond) {
|
|
432
|
-
|
|
453
|
+
const gte = this._coerceDate(cond.gte);
|
|
454
|
+
opConds.push({$gte: gte});
|
|
433
455
|
}
|
|
434
456
|
// lte
|
|
435
457
|
if ('lte' in cond) {
|
|
436
|
-
|
|
458
|
+
const lte = this._coerceDate(cond.lte);
|
|
459
|
+
opConds.push({$lte: lte});
|
|
437
460
|
}
|
|
438
461
|
// inq
|
|
439
462
|
if ('inq' in cond) {
|
|
@@ -443,7 +466,12 @@ export class MongodbAdapter extends Adapter {
|
|
|
443
466
|
'an Array of possible values',
|
|
444
467
|
cond.inq,
|
|
445
468
|
);
|
|
446
|
-
|
|
469
|
+
const inq = cond.inq.map(v => {
|
|
470
|
+
v = this._coerceId(v);
|
|
471
|
+
v = this._coerceDate(v);
|
|
472
|
+
return v;
|
|
473
|
+
});
|
|
474
|
+
opConds.push({$in: inq});
|
|
447
475
|
}
|
|
448
476
|
// nin
|
|
449
477
|
if ('nin' in cond) {
|
|
@@ -453,7 +481,12 @@ export class MongodbAdapter extends Adapter {
|
|
|
453
481
|
'an Array of possible values',
|
|
454
482
|
cond,
|
|
455
483
|
);
|
|
456
|
-
|
|
484
|
+
const nin = cond.nin.map(v => {
|
|
485
|
+
v = this._coerceId(v);
|
|
486
|
+
v = this._coerceDate(v);
|
|
487
|
+
return v;
|
|
488
|
+
});
|
|
489
|
+
opConds.push({$nin: nin});
|
|
457
490
|
}
|
|
458
491
|
// between
|
|
459
492
|
if ('between' in cond) {
|
|
@@ -463,7 +496,9 @@ export class MongodbAdapter extends Adapter {
|
|
|
463
496
|
'an Array of 2 elements',
|
|
464
497
|
cond.between,
|
|
465
498
|
);
|
|
466
|
-
|
|
499
|
+
const gte = this._coerceDate(cond.between[0]);
|
|
500
|
+
const lte = this._coerceDate(cond.between[1]);
|
|
501
|
+
opConds.push({$gte: gte, $lte: lte});
|
|
467
502
|
}
|
|
468
503
|
// exists
|
|
469
504
|
if ('exists' in cond) {
|
|
@@ -812,6 +812,148 @@ describe('MongodbAdapter', function () {
|
|
|
812
812
|
expect(res[0]).to.be.eql(oid);
|
|
813
813
|
});
|
|
814
814
|
|
|
815
|
+
it('converts property value to an instance of Date', async function () {
|
|
816
|
+
const date = new Date();
|
|
817
|
+
const isoDate = date.toISOString();
|
|
818
|
+
const input = {foo: isoDate};
|
|
819
|
+
const schema = createSchema();
|
|
820
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
821
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
822
|
+
const res = A._buildQuery('model', input);
|
|
823
|
+
expect(res.foo).to.be.instanceof(Date);
|
|
824
|
+
expect(res.foo).to.be.eql(date);
|
|
825
|
+
});
|
|
826
|
+
|
|
827
|
+
it('the "eq" operator converts Date string to an instance', async function () {
|
|
828
|
+
const date = new Date();
|
|
829
|
+
const isoDate = date.toISOString();
|
|
830
|
+
const input = {foo: {eq: isoDate}};
|
|
831
|
+
const schema = createSchema();
|
|
832
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
833
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
834
|
+
const {
|
|
835
|
+
foo: {$eq: res},
|
|
836
|
+
} = A._buildQuery('model', input);
|
|
837
|
+
expect(res).to.be.instanceOf(Date);
|
|
838
|
+
expect(res).to.be.eql(date);
|
|
839
|
+
});
|
|
840
|
+
|
|
841
|
+
it('the "neq" operator converts Date string to an instance', async function () {
|
|
842
|
+
const date = new Date();
|
|
843
|
+
const isoDate = date.toISOString();
|
|
844
|
+
const input = {foo: {neq: isoDate}};
|
|
845
|
+
const schema = createSchema();
|
|
846
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
847
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
848
|
+
const {
|
|
849
|
+
foo: {$ne: res},
|
|
850
|
+
} = A._buildQuery('model', input);
|
|
851
|
+
expect(res).to.be.instanceOf(Date);
|
|
852
|
+
expect(res).to.be.eql(date);
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
it('the "gt" operator converts Date string to an instance', async function () {
|
|
856
|
+
const date = new Date();
|
|
857
|
+
const isoDate = date.toISOString();
|
|
858
|
+
const input = {foo: {gt: isoDate}};
|
|
859
|
+
const schema = createSchema();
|
|
860
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
861
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
862
|
+
const {
|
|
863
|
+
foo: {$gt: res},
|
|
864
|
+
} = A._buildQuery('model', input);
|
|
865
|
+
expect(res).to.be.instanceOf(Date);
|
|
866
|
+
expect(res).to.be.eql(date);
|
|
867
|
+
});
|
|
868
|
+
|
|
869
|
+
it('the "lt" operator converts Date string to an instance', async function () {
|
|
870
|
+
const date = new Date();
|
|
871
|
+
const isoDate = date.toISOString();
|
|
872
|
+
const input = {foo: {lt: isoDate}};
|
|
873
|
+
const schema = createSchema();
|
|
874
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
875
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
876
|
+
const {
|
|
877
|
+
foo: {$lt: res},
|
|
878
|
+
} = A._buildQuery('model', input);
|
|
879
|
+
expect(res).to.be.instanceOf(Date);
|
|
880
|
+
expect(res).to.be.eql(date);
|
|
881
|
+
});
|
|
882
|
+
|
|
883
|
+
it('the "gte" operator converts Date string to an instance', async function () {
|
|
884
|
+
const date = new Date();
|
|
885
|
+
const isoDate = date.toISOString();
|
|
886
|
+
const input = {foo: {gte: isoDate}};
|
|
887
|
+
const schema = createSchema();
|
|
888
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
889
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
890
|
+
const {
|
|
891
|
+
foo: {$gte: res},
|
|
892
|
+
} = A._buildQuery('model', input);
|
|
893
|
+
expect(res).to.be.instanceOf(Date);
|
|
894
|
+
expect(res).to.be.eql(date);
|
|
895
|
+
});
|
|
896
|
+
|
|
897
|
+
it('the "lte" operator converts Date string to an instance', async function () {
|
|
898
|
+
const date = new Date();
|
|
899
|
+
const isoDate = date.toISOString();
|
|
900
|
+
const input = {foo: {lte: isoDate}};
|
|
901
|
+
const schema = createSchema();
|
|
902
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
903
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
904
|
+
const {
|
|
905
|
+
foo: {$lte: res},
|
|
906
|
+
} = A._buildQuery('model', input);
|
|
907
|
+
expect(res).to.be.instanceOf(Date);
|
|
908
|
+
expect(res).to.be.eql(date);
|
|
909
|
+
});
|
|
910
|
+
|
|
911
|
+
it('the "inq" operator converts Date string to an instance', async function () {
|
|
912
|
+
const date = new Date();
|
|
913
|
+
const isoDate = date.toISOString();
|
|
914
|
+
const input = {foo: {inq: [isoDate]}};
|
|
915
|
+
const schema = createSchema();
|
|
916
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
917
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
918
|
+
const {
|
|
919
|
+
foo: {$in: res},
|
|
920
|
+
} = A._buildQuery('model', input);
|
|
921
|
+
expect(res[0]).to.be.instanceOf(Date);
|
|
922
|
+
expect(res[0]).to.be.eql(date);
|
|
923
|
+
});
|
|
924
|
+
|
|
925
|
+
it('the "nin" operator converts Date string to an instance', async function () {
|
|
926
|
+
const date = new Date();
|
|
927
|
+
const isoDate = date.toISOString();
|
|
928
|
+
const input = {foo: {nin: [isoDate]}};
|
|
929
|
+
const schema = createSchema();
|
|
930
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
931
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
932
|
+
const {
|
|
933
|
+
foo: {$nin: res},
|
|
934
|
+
} = A._buildQuery('model', input);
|
|
935
|
+
expect(res[0]).to.be.instanceOf(Date);
|
|
936
|
+
expect(res[0]).to.be.eql(date);
|
|
937
|
+
});
|
|
938
|
+
|
|
939
|
+
it('the "between" operator converts Date string to an instance', async function () {
|
|
940
|
+
const date1 = new Date();
|
|
941
|
+
const date2 = new Date();
|
|
942
|
+
const isoDate1 = date1.toISOString();
|
|
943
|
+
const isoDate2 = date2.toISOString();
|
|
944
|
+
const input = {foo: {between: [isoDate1, isoDate2]}};
|
|
945
|
+
const schema = createSchema();
|
|
946
|
+
schema.defineModel({name: 'model', datasource: 'mongodb'});
|
|
947
|
+
const A = await schema.getService(AdapterRegistry).getAdapter('mongodb');
|
|
948
|
+
const {
|
|
949
|
+
foo: {$gte: res1, $lte: res2},
|
|
950
|
+
} = A._buildQuery('model', input);
|
|
951
|
+
expect(res1).to.be.instanceOf(Date);
|
|
952
|
+
expect(res1).to.be.eql(date1);
|
|
953
|
+
expect(res2).to.be.instanceOf(Date);
|
|
954
|
+
expect(res2).to.be.eql(date2);
|
|
955
|
+
});
|
|
956
|
+
|
|
815
957
|
it('combines the given operators by the "and" clause', async function () {
|
|
816
958
|
const input = {
|
|
817
959
|
foo: {
|
package/src/utils/is-iso-date.js
CHANGED