@opra/mongodb 1.4.2 → 1.4.4
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/cjs/adapter/mongo-adapter.js +41 -12
- package/cjs/adapter/mongo-patch-generator.js +14 -4
- package/cjs/adapter/prepare-filter.js +11 -3
- package/cjs/adapter/prepare-projection.js +8 -3
- package/cjs/services/mongo-collection-service.js +61 -15
- package/cjs/services/mongo-entity-service.js +36 -12
- package/cjs/services/mongo-nested-service.js +137 -37
- package/cjs/services/mongo-service.js +25 -10
- package/cjs/services/mongo-singleton-service.js +20 -5
- package/esm/adapter/mongo-adapter.js +41 -12
- package/esm/adapter/mongo-patch-generator.js +14 -4
- package/esm/adapter/prepare-filter.js +11 -3
- package/esm/adapter/prepare-projection.js +9 -4
- package/esm/services/mongo-collection-service.js +61 -15
- package/esm/services/mongo-entity-service.js +36 -12
- package/esm/services/mongo-nested-service.js +138 -38
- package/esm/services/mongo-service.js +26 -11
- package/esm/services/mongo-singleton-service.js +20 -5
- package/package.json +4 -4
|
@@ -109,7 +109,8 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
109
109
|
options,
|
|
110
110
|
};
|
|
111
111
|
input[this.nestedKey] =
|
|
112
|
-
input[this.nestedKey] == null ||
|
|
112
|
+
input[this.nestedKey] == null ||
|
|
113
|
+
input[this.nestedKey] === ''
|
|
113
114
|
? this._generateId(command)
|
|
114
115
|
: input[this.nestedKey];
|
|
115
116
|
return this._executeCommand(command, () => this._create(command));
|
|
@@ -153,8 +154,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
153
154
|
options,
|
|
154
155
|
};
|
|
155
156
|
return this._executeCommand(command, async () => {
|
|
156
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
157
|
-
|
|
157
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
158
|
+
await this._getDocumentFilter(command),
|
|
159
|
+
]);
|
|
160
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
161
|
+
await this._getNestedFilter(command),
|
|
162
|
+
command.options?.filter,
|
|
163
|
+
]);
|
|
158
164
|
command.options = { ...command.options, filter, documentFilter };
|
|
159
165
|
return this._count(command);
|
|
160
166
|
});
|
|
@@ -207,8 +213,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
207
213
|
options,
|
|
208
214
|
};
|
|
209
215
|
return this._executeCommand(command, async () => {
|
|
210
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
211
|
-
|
|
216
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
217
|
+
await this._getDocumentFilter(command),
|
|
218
|
+
]);
|
|
219
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
220
|
+
await this._getNestedFilter(command),
|
|
221
|
+
command.options?.filter,
|
|
222
|
+
]);
|
|
212
223
|
command.options = { ...command.options, filter, documentFilter };
|
|
213
224
|
return this._delete(command);
|
|
214
225
|
});
|
|
@@ -221,7 +232,10 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
221
232
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
222
233
|
options?.documentFilter,
|
|
223
234
|
]);
|
|
224
|
-
const pullFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
235
|
+
const pullFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
236
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(nestedId, [this.nestedKey]),
|
|
237
|
+
options?.filter,
|
|
238
|
+
]) || {};
|
|
225
239
|
const update = {
|
|
226
240
|
$pull: { [this.fieldName]: pullFilter },
|
|
227
241
|
};
|
|
@@ -250,8 +264,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
250
264
|
options,
|
|
251
265
|
};
|
|
252
266
|
return this._executeCommand(command, async () => {
|
|
253
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
254
|
-
|
|
267
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
268
|
+
await this._getDocumentFilter(command),
|
|
269
|
+
]);
|
|
270
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
271
|
+
await this._getNestedFilter(command),
|
|
272
|
+
command.options?.filter,
|
|
273
|
+
]);
|
|
255
274
|
command.options = { ...command.options, filter, documentFilter };
|
|
256
275
|
return this._deleteMany(command);
|
|
257
276
|
});
|
|
@@ -302,7 +321,9 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
302
321
|
options,
|
|
303
322
|
};
|
|
304
323
|
return this._executeCommand(command, async () => {
|
|
305
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
324
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
325
|
+
await this._getDocumentFilter(command),
|
|
326
|
+
]);
|
|
306
327
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
307
328
|
await this._getNestedFilter(command),
|
|
308
329
|
documentFilter,
|
|
@@ -329,9 +350,17 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
329
350
|
};
|
|
330
351
|
return this._executeCommand(command, async () => {
|
|
331
352
|
const documentFilter = await this._getDocumentFilter(command);
|
|
332
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
353
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
354
|
+
documentFilter,
|
|
355
|
+
command.options?.filter,
|
|
356
|
+
]);
|
|
333
357
|
const findCommand = command;
|
|
334
|
-
findCommand.options = {
|
|
358
|
+
findCommand.options = {
|
|
359
|
+
...command.options,
|
|
360
|
+
filter,
|
|
361
|
+
documentFilter,
|
|
362
|
+
projection: ['_id'],
|
|
363
|
+
};
|
|
335
364
|
return !!(await this._findOne(findCommand));
|
|
336
365
|
});
|
|
337
366
|
}
|
|
@@ -345,8 +374,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
345
374
|
options,
|
|
346
375
|
};
|
|
347
376
|
return this._executeCommand(command, async () => {
|
|
348
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
349
|
-
|
|
377
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
378
|
+
await this._getDocumentFilter(command),
|
|
379
|
+
]);
|
|
380
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
381
|
+
await this._getNestedFilter(command),
|
|
382
|
+
command.options?.filter,
|
|
383
|
+
]);
|
|
350
384
|
command.options = { ...command.options, filter, documentFilter };
|
|
351
385
|
return this._findById(command);
|
|
352
386
|
});
|
|
@@ -381,8 +415,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
381
415
|
options,
|
|
382
416
|
};
|
|
383
417
|
return this._executeCommand(command, async () => {
|
|
384
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
385
|
-
|
|
418
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
419
|
+
await this._getDocumentFilter(command),
|
|
420
|
+
]);
|
|
421
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
422
|
+
await this._getNestedFilter(command),
|
|
423
|
+
command.options?.filter,
|
|
424
|
+
]);
|
|
386
425
|
command.options = { ...command.options, filter, documentFilter };
|
|
387
426
|
return this._findOne(command);
|
|
388
427
|
});
|
|
@@ -438,7 +477,10 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
438
477
|
stages.push(...options.preStages);
|
|
439
478
|
/** Filter */
|
|
440
479
|
if (options?.filter || options?.nestedFilter) {
|
|
441
|
-
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
480
|
+
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
481
|
+
options?.filter,
|
|
482
|
+
options.nestedFilter,
|
|
483
|
+
]);
|
|
442
484
|
stages.push({ $match: optionsFilter });
|
|
443
485
|
}
|
|
444
486
|
/** Sort */
|
|
@@ -462,7 +504,15 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
462
504
|
const db = this.getDatabase();
|
|
463
505
|
const collection = await this.getCollection(db);
|
|
464
506
|
const cursor = collection.aggregate(stages, {
|
|
465
|
-
...(0, objects_1.omit)(options, [
|
|
507
|
+
...(0, objects_1.omit)(options, [
|
|
508
|
+
'documentFilter',
|
|
509
|
+
'nestedFilter',
|
|
510
|
+
'projection',
|
|
511
|
+
'sort',
|
|
512
|
+
'skip',
|
|
513
|
+
'limit',
|
|
514
|
+
'filter',
|
|
515
|
+
]),
|
|
466
516
|
session: options?.session ?? this.getSession(),
|
|
467
517
|
});
|
|
468
518
|
try {
|
|
@@ -519,7 +569,10 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
519
569
|
dataStages.push(...options.preStages);
|
|
520
570
|
/** Filter */
|
|
521
571
|
if (options?.filter || options?.nestedFilter) {
|
|
522
|
-
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
572
|
+
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
573
|
+
options?.filter,
|
|
574
|
+
options?.nestedFilter,
|
|
575
|
+
]);
|
|
523
576
|
dataStages.push({ $match: optionsFilter });
|
|
524
577
|
}
|
|
525
578
|
/** Sort */
|
|
@@ -543,7 +596,15 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
543
596
|
const db = this.getDatabase();
|
|
544
597
|
const collection = await this.getCollection(db);
|
|
545
598
|
const cursor = collection.aggregate(stages, {
|
|
546
|
-
...(0, objects_1.omit)(options, [
|
|
599
|
+
...(0, objects_1.omit)(options, [
|
|
600
|
+
'documentFilter',
|
|
601
|
+
'nestedFilter',
|
|
602
|
+
'projection',
|
|
603
|
+
'sort',
|
|
604
|
+
'skip',
|
|
605
|
+
'limit',
|
|
606
|
+
'filter',
|
|
607
|
+
]),
|
|
547
608
|
session: options?.session ?? this.getSession(),
|
|
548
609
|
});
|
|
549
610
|
try {
|
|
@@ -577,8 +638,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
577
638
|
options,
|
|
578
639
|
};
|
|
579
640
|
return this._executeCommand(command, async () => {
|
|
580
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
581
|
-
|
|
641
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
642
|
+
await this._getDocumentFilter(command),
|
|
643
|
+
]);
|
|
644
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
645
|
+
await this._getNestedFilter(command),
|
|
646
|
+
command.options?.filter,
|
|
647
|
+
]);
|
|
582
648
|
command.options = {
|
|
583
649
|
...command.options,
|
|
584
650
|
filter,
|
|
@@ -621,8 +687,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
621
687
|
options,
|
|
622
688
|
};
|
|
623
689
|
return this._executeCommand(command, async () => {
|
|
624
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
625
|
-
|
|
690
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
691
|
+
await this._getDocumentFilter(command),
|
|
692
|
+
]);
|
|
693
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
694
|
+
await this._getNestedFilter(command),
|
|
695
|
+
command.options?.filter,
|
|
696
|
+
]);
|
|
626
697
|
command.options = {
|
|
627
698
|
...command.options,
|
|
628
699
|
filter,
|
|
@@ -665,8 +736,13 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
665
736
|
options,
|
|
666
737
|
};
|
|
667
738
|
return this._executeCommand(command, async () => {
|
|
668
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
669
|
-
|
|
739
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
740
|
+
await this._getDocumentFilter(command),
|
|
741
|
+
]);
|
|
742
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
743
|
+
await this._getNestedFilter(command),
|
|
744
|
+
command.options?.filter,
|
|
745
|
+
]);
|
|
670
746
|
command.options = { ...command.options, filter, documentFilter };
|
|
671
747
|
return this._updateMany(command);
|
|
672
748
|
});
|
|
@@ -685,7 +761,9 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
685
761
|
{ [this.fieldName]: { $exists: true } },
|
|
686
762
|
]);
|
|
687
763
|
if (options?.filter) {
|
|
688
|
-
const elemMatch = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter], {
|
|
764
|
+
const elemMatch = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter], {
|
|
765
|
+
fieldPrefix: 'elem.',
|
|
766
|
+
});
|
|
689
767
|
options.arrayFilters = [elemMatch];
|
|
690
768
|
}
|
|
691
769
|
const patchGenerator = new mongo_patch_generator_js_1.MongoPatchGenerator();
|
|
@@ -723,7 +801,9 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
723
801
|
* that resolves to the common filter, or undefined if not available.
|
|
724
802
|
*/
|
|
725
803
|
_getNestedFilter(args) {
|
|
726
|
-
return typeof this.nestedFilter === 'function'
|
|
804
|
+
return typeof this.nestedFilter === 'function'
|
|
805
|
+
? this.nestedFilter(args, this)
|
|
806
|
+
: this.nestedFilter;
|
|
727
807
|
}
|
|
728
808
|
async _executeCommand(command, commandFn) {
|
|
729
809
|
try {
|
|
@@ -769,44 +849,64 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
769
849
|
throw e;
|
|
770
850
|
}
|
|
771
851
|
}
|
|
852
|
+
async _beforeCreate(
|
|
772
853
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
773
|
-
|
|
854
|
+
command) {
|
|
774
855
|
// Do nothing
|
|
775
856
|
}
|
|
857
|
+
async _beforeUpdate(
|
|
776
858
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
777
|
-
|
|
859
|
+
command) {
|
|
778
860
|
// Do nothing
|
|
779
861
|
}
|
|
862
|
+
async _beforeUpdateMany(
|
|
780
863
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
781
|
-
|
|
864
|
+
command) {
|
|
782
865
|
// Do nothing
|
|
783
866
|
}
|
|
867
|
+
async _beforeDelete(
|
|
784
868
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
785
|
-
|
|
869
|
+
command) {
|
|
786
870
|
// Do nothing
|
|
787
871
|
}
|
|
872
|
+
async _beforeDeleteMany(
|
|
788
873
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
789
|
-
|
|
874
|
+
command) {
|
|
790
875
|
// Do nothing
|
|
791
876
|
}
|
|
877
|
+
async _afterCreate(
|
|
792
878
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
793
|
-
|
|
879
|
+
command,
|
|
880
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
881
|
+
result) {
|
|
794
882
|
// Do nothing
|
|
795
883
|
}
|
|
884
|
+
async _afterUpdate(
|
|
885
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
886
|
+
command,
|
|
796
887
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
797
|
-
|
|
888
|
+
result) {
|
|
798
889
|
// Do nothing
|
|
799
890
|
}
|
|
891
|
+
async _afterUpdateMany(
|
|
800
892
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
801
|
-
|
|
893
|
+
command,
|
|
894
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
895
|
+
affected) {
|
|
802
896
|
// Do nothing
|
|
803
897
|
}
|
|
898
|
+
async _afterDelete(
|
|
899
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
900
|
+
command,
|
|
804
901
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
805
|
-
|
|
902
|
+
affected) {
|
|
806
903
|
// Do nothing
|
|
807
904
|
}
|
|
905
|
+
async _afterDeleteMany(
|
|
906
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
907
|
+
command,
|
|
808
908
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
809
|
-
|
|
909
|
+
affected) {
|
|
810
910
|
// Do nothing
|
|
811
911
|
}
|
|
812
912
|
}
|
|
@@ -45,10 +45,12 @@ class MongoService extends core_1.ServiceBase {
|
|
|
45
45
|
for(context, overwriteProperties, overwriteContext) {
|
|
46
46
|
if (overwriteProperties?.documentFilter && this.documentFilter) {
|
|
47
47
|
overwriteProperties.documentFilter = [
|
|
48
|
-
...(Array.isArray(this.documentFilter)
|
|
48
|
+
...(Array.isArray(this.documentFilter)
|
|
49
|
+
? this.documentFilter
|
|
50
|
+
: [this.documentFilter]),
|
|
49
51
|
...(Array.isArray(overwriteProperties?.documentFilter)
|
|
50
|
-
? overwriteProperties
|
|
51
|
-
: [overwriteProperties
|
|
52
|
+
? overwriteProperties.documentFilter
|
|
53
|
+
: [overwriteProperties.documentFilter]),
|
|
52
54
|
];
|
|
53
55
|
}
|
|
54
56
|
return super.for(context, overwriteProperties, overwriteContext);
|
|
@@ -61,7 +63,9 @@ class MongoService extends core_1.ServiceBase {
|
|
|
61
63
|
* @throws {Error} If the collection name is not defined.
|
|
62
64
|
*/
|
|
63
65
|
getCollectionName() {
|
|
64
|
-
const out = typeof this.collectionName === 'function'
|
|
66
|
+
const out = typeof this.collectionName === 'function'
|
|
67
|
+
? this.collectionName(this)
|
|
68
|
+
: this.collectionName;
|
|
65
69
|
if (out)
|
|
66
70
|
return out;
|
|
67
71
|
throw new Error('collectionName is not defined');
|
|
@@ -74,7 +78,9 @@ class MongoService extends core_1.ServiceBase {
|
|
|
74
78
|
* @throws {Error} If the resource name is not defined.
|
|
75
79
|
*/
|
|
76
80
|
getResourceName() {
|
|
77
|
-
const out = typeof this.resourceName === 'function'
|
|
81
|
+
const out = typeof this.resourceName === 'function'
|
|
82
|
+
? this.resourceName(this)
|
|
83
|
+
: this.resourceName || this.getCollectionName();
|
|
78
84
|
if (out)
|
|
79
85
|
return out;
|
|
80
86
|
throw new Error('resourceName is not defined');
|
|
@@ -184,7 +190,9 @@ class MongoService extends core_1.ServiceBase {
|
|
|
184
190
|
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
185
191
|
*/
|
|
186
192
|
_generateId(command) {
|
|
187
|
-
return typeof this.idGenerator === 'function'
|
|
193
|
+
return typeof this.idGenerator === 'function'
|
|
194
|
+
? this.idGenerator(command, this)
|
|
195
|
+
: new mongodb_1.ObjectId();
|
|
188
196
|
}
|
|
189
197
|
/**
|
|
190
198
|
* Retrieves the common filter used for querying documents.
|
|
@@ -195,8 +203,10 @@ class MongoService extends core_1.ServiceBase {
|
|
|
195
203
|
* that resolves to the common filter, or undefined if not available.
|
|
196
204
|
*/
|
|
197
205
|
_getDocumentFilter(command) {
|
|
198
|
-
const commonFilter = Array.isArray(this.documentFilter)
|
|
199
|
-
|
|
206
|
+
const commonFilter = Array.isArray(this.documentFilter)
|
|
207
|
+
? this.documentFilter
|
|
208
|
+
: [this.documentFilter];
|
|
209
|
+
const mapped = commonFilter.map(f => typeof f === 'function' ? f(command, this) : f);
|
|
200
210
|
return mapped.length > 1 ? mongo_adapter_js_1.MongoAdapter.prepareFilter(mapped) : mapped[0];
|
|
201
211
|
}
|
|
202
212
|
async _executeCommand(command, commandFn) {
|
|
@@ -204,7 +214,8 @@ class MongoService extends core_1.ServiceBase {
|
|
|
204
214
|
const next = async () => {
|
|
205
215
|
proto = proto ? Object.getPrototypeOf(proto) : this;
|
|
206
216
|
while (proto) {
|
|
207
|
-
if (proto.interceptor &&
|
|
217
|
+
if (proto.interceptor &&
|
|
218
|
+
Object.prototype.hasOwnProperty.call(proto, 'interceptor')) {
|
|
208
219
|
return await proto.interceptor.call(this, next, command, this);
|
|
209
220
|
}
|
|
210
221
|
proto = Object.getPrototypeOf(proto);
|
|
@@ -248,7 +259,11 @@ class MongoService extends core_1.ServiceBase {
|
|
|
248
259
|
let validator = this._outputCodecs[operation];
|
|
249
260
|
if (validator)
|
|
250
261
|
return validator;
|
|
251
|
-
const options = {
|
|
262
|
+
const options = {
|
|
263
|
+
projection: '*',
|
|
264
|
+
partial: 'deep',
|
|
265
|
+
ignoreHiddenFields: true,
|
|
266
|
+
};
|
|
252
267
|
const dataType = this.dataType;
|
|
253
268
|
validator = dataType.generateCodec('decode', options);
|
|
254
269
|
this._outputCodecs[operation] = validator;
|
|
@@ -94,7 +94,10 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
94
94
|
options,
|
|
95
95
|
};
|
|
96
96
|
return this._executeCommand(command, async () => {
|
|
97
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
97
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
98
|
+
await this._getDocumentFilter(command),
|
|
99
|
+
command.options?.filter,
|
|
100
|
+
]);
|
|
98
101
|
command.options = { ...command.options, filter };
|
|
99
102
|
return this._delete(command);
|
|
100
103
|
});
|
|
@@ -115,7 +118,10 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
115
118
|
};
|
|
116
119
|
return this._executeCommand(command, async () => {
|
|
117
120
|
const documentFilter = await this._getDocumentFilter(command);
|
|
118
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
121
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
122
|
+
documentFilter,
|
|
123
|
+
command.options?.filter,
|
|
124
|
+
]);
|
|
119
125
|
const findCommand = command;
|
|
120
126
|
findCommand.options = { ...command.options, filter, projection: ['_id'] };
|
|
121
127
|
return !!(await this._findById(findCommand));
|
|
@@ -131,7 +137,10 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
131
137
|
};
|
|
132
138
|
return this._executeCommand(command, async () => {
|
|
133
139
|
const documentFilter = await this._getDocumentFilter(command);
|
|
134
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
140
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
141
|
+
documentFilter,
|
|
142
|
+
command.options?.filter,
|
|
143
|
+
]);
|
|
135
144
|
command.options = { ...command.options, filter };
|
|
136
145
|
return this._findById(command);
|
|
137
146
|
});
|
|
@@ -154,7 +163,10 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
154
163
|
options,
|
|
155
164
|
};
|
|
156
165
|
return this._executeCommand(command, async () => {
|
|
157
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
166
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
167
|
+
await this._getDocumentFilter(command),
|
|
168
|
+
command.options?.filter,
|
|
169
|
+
]);
|
|
158
170
|
command.options = { ...command.options, filter };
|
|
159
171
|
const matchCount = await this._updateOnly(command);
|
|
160
172
|
if (matchCount) {
|
|
@@ -189,7 +201,10 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
189
201
|
options,
|
|
190
202
|
};
|
|
191
203
|
return this._executeCommand(command, async () => {
|
|
192
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
204
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
205
|
+
await this._getDocumentFilter(command),
|
|
206
|
+
command.options?.filter,
|
|
207
|
+
]);
|
|
193
208
|
command.options = { ...command.options, filter };
|
|
194
209
|
return this._updateOnly(command);
|
|
195
210
|
});
|
|
@@ -14,7 +14,8 @@ export var MongoAdapter;
|
|
|
14
14
|
}
|
|
15
15
|
const ctx = context;
|
|
16
16
|
const { operation } = ctx;
|
|
17
|
-
if (operation?.composition?.startsWith('Entity.') &&
|
|
17
|
+
if (operation?.composition?.startsWith('Entity.') &&
|
|
18
|
+
operation.compositionOptions?.type) {
|
|
18
19
|
const controller = operation.owner;
|
|
19
20
|
switch (operation.composition) {
|
|
20
21
|
case 'Entity.Create': {
|
|
@@ -22,15 +23,24 @@ export var MongoAdapter;
|
|
|
22
23
|
const options = {
|
|
23
24
|
projection: ctx.queryParams.projection,
|
|
24
25
|
};
|
|
25
|
-
return {
|
|
26
|
+
return {
|
|
27
|
+
method: 'create',
|
|
28
|
+
data,
|
|
29
|
+
options,
|
|
30
|
+
};
|
|
26
31
|
}
|
|
27
32
|
case 'Entity.Delete': {
|
|
28
|
-
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
33
|
+
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
34
|
+
controller.parameters.find(p => p.keyParam);
|
|
29
35
|
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
30
36
|
const options = {
|
|
31
37
|
filter: ctx.queryParams.filter,
|
|
32
38
|
};
|
|
33
|
-
return {
|
|
39
|
+
return {
|
|
40
|
+
method: 'delete',
|
|
41
|
+
key,
|
|
42
|
+
options,
|
|
43
|
+
};
|
|
34
44
|
}
|
|
35
45
|
case 'Entity.DeleteMany': {
|
|
36
46
|
const options = {
|
|
@@ -41,16 +51,19 @@ export var MongoAdapter;
|
|
|
41
51
|
case 'Entity.FindMany': {
|
|
42
52
|
const options = {
|
|
43
53
|
filter: ctx.queryParams.filter,
|
|
44
|
-
projection: ctx.queryParams.projection ||
|
|
54
|
+
projection: ctx.queryParams.projection ||
|
|
55
|
+
operation.compositionOptions.defaultProjection,
|
|
45
56
|
count: ctx.queryParams.count,
|
|
46
|
-
limit: ctx.queryParams.limit ||
|
|
57
|
+
limit: ctx.queryParams.limit ||
|
|
58
|
+
operation.compositionOptions.defaultLimit,
|
|
47
59
|
skip: ctx.queryParams.skip,
|
|
48
60
|
sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
49
61
|
};
|
|
50
62
|
return { method: 'findMany', options };
|
|
51
63
|
}
|
|
52
64
|
case 'Entity.Get': {
|
|
53
|
-
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
65
|
+
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
66
|
+
controller.parameters.find(p => p.keyParam);
|
|
54
67
|
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
55
68
|
const options = {
|
|
56
69
|
projection: ctx.queryParams.projection,
|
|
@@ -60,30 +73,46 @@ export var MongoAdapter;
|
|
|
60
73
|
}
|
|
61
74
|
case 'Entity.Replace': {
|
|
62
75
|
const data = await ctx.getBody();
|
|
63
|
-
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
76
|
+
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
77
|
+
controller.parameters.find(p => p.keyParam);
|
|
64
78
|
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
65
79
|
const options = {
|
|
66
80
|
projection: ctx.queryParams.projection,
|
|
67
81
|
filter: ctx.queryParams.filter,
|
|
68
82
|
};
|
|
69
|
-
return {
|
|
83
|
+
return {
|
|
84
|
+
method: 'replace',
|
|
85
|
+
key,
|
|
86
|
+
data,
|
|
87
|
+
options,
|
|
88
|
+
};
|
|
70
89
|
}
|
|
71
90
|
case 'Entity.Update': {
|
|
72
91
|
const data = await ctx.getBody();
|
|
73
|
-
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
92
|
+
const keyParam = operation.parameters.find(p => p.keyParam) ||
|
|
93
|
+
controller.parameters.find(p => p.keyParam);
|
|
74
94
|
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
75
95
|
const options = {
|
|
76
96
|
projection: ctx.queryParams.projection,
|
|
77
97
|
filter: ctx.queryParams.filter,
|
|
78
98
|
};
|
|
79
|
-
return {
|
|
99
|
+
return {
|
|
100
|
+
method: 'update',
|
|
101
|
+
key,
|
|
102
|
+
data,
|
|
103
|
+
options,
|
|
104
|
+
};
|
|
80
105
|
}
|
|
81
106
|
case 'Entity.UpdateMany': {
|
|
82
107
|
const data = await ctx.getBody();
|
|
83
108
|
const options = {
|
|
84
109
|
filter: ctx.queryParams.filter,
|
|
85
110
|
};
|
|
86
|
-
return {
|
|
111
|
+
return {
|
|
112
|
+
method: 'updateMany',
|
|
113
|
+
data,
|
|
114
|
+
options,
|
|
115
|
+
};
|
|
87
116
|
}
|
|
88
117
|
default:
|
|
89
118
|
break;
|
|
@@ -85,7 +85,9 @@ export class MongoPatchGenerator {
|
|
|
85
85
|
delete v[keyField];
|
|
86
86
|
/** Add array filter */
|
|
87
87
|
ctx.arrayFilters = ctx.arrayFilters || [];
|
|
88
|
-
ctx.arrayFilters.push({
|
|
88
|
+
ctx.arrayFilters.push({
|
|
89
|
+
[`${arrayFilterName}.${keyField}`]: keyValue,
|
|
90
|
+
});
|
|
89
91
|
/** Process each object in array */
|
|
90
92
|
this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v);
|
|
91
93
|
}
|
|
@@ -135,7 +137,9 @@ export class MongoPatchGenerator {
|
|
|
135
137
|
}
|
|
136
138
|
continue;
|
|
137
139
|
}
|
|
138
|
-
ctx.$push[pathDot + key] = Array.isArray(value)
|
|
140
|
+
ctx.$push[pathDot + key] = Array.isArray(value)
|
|
141
|
+
? { $each: value }
|
|
142
|
+
: value;
|
|
139
143
|
}
|
|
140
144
|
}
|
|
141
145
|
_processPull(ctx, dataType, path, input) {
|
|
@@ -155,10 +159,16 @@ export class MongoPatchGenerator {
|
|
|
155
159
|
keyField = field.keyField || field.type.keyField;
|
|
156
160
|
if (!keyField)
|
|
157
161
|
continue;
|
|
158
|
-
ctx.$pull[pathDot + key] = {
|
|
162
|
+
ctx.$pull[pathDot + key] = {
|
|
163
|
+
$elemMatch: {
|
|
164
|
+
[keyField]: Array.isArray(value) ? { $in: value } : value,
|
|
165
|
+
},
|
|
166
|
+
};
|
|
159
167
|
}
|
|
160
168
|
else {
|
|
161
|
-
ctx.$pull[pathDot + key] = Array.isArray(value)
|
|
169
|
+
ctx.$pull[pathDot + key] = Array.isArray(value)
|
|
170
|
+
? { $in: value }
|
|
171
|
+
: value;
|
|
162
172
|
}
|
|
163
173
|
}
|
|
164
174
|
}
|
|
@@ -61,7 +61,11 @@ export default function prepareFilter(filters, options) {
|
|
|
61
61
|
}
|
|
62
62
|
i++;
|
|
63
63
|
}
|
|
64
|
-
return i
|
|
64
|
+
return i
|
|
65
|
+
? options?.fieldPrefix
|
|
66
|
+
? addPrefix(out, options.fieldPrefix)
|
|
67
|
+
: out
|
|
68
|
+
: undefined;
|
|
65
69
|
}
|
|
66
70
|
function addPrefix(source, prefix) {
|
|
67
71
|
if (typeof source !== 'object')
|
|
@@ -96,7 +100,9 @@ function prepareFilterAst(ast, negative) {
|
|
|
96
100
|
return prepareFilterAst(ast.expression, !negative);
|
|
97
101
|
}
|
|
98
102
|
if (ast instanceof OpraFilter.LogicalExpression) {
|
|
99
|
-
const items = ast.items
|
|
103
|
+
const items = ast.items
|
|
104
|
+
.map(x => prepareFilterAst(x, negative))
|
|
105
|
+
.filter(x => x != null);
|
|
100
106
|
if (ast.op === 'or')
|
|
101
107
|
return { $or: items };
|
|
102
108
|
return { $and: items };
|
|
@@ -119,7 +125,9 @@ function prepareFilterAst(ast, negative) {
|
|
|
119
125
|
if (op === '=')
|
|
120
126
|
return { $or: [{ [left]: null }, { [left]: { $exists: false } }] };
|
|
121
127
|
if (op === '!=')
|
|
122
|
-
return {
|
|
128
|
+
return {
|
|
129
|
+
$and: [{ $ne: { [left]: null } }, { [left]: { $exists: true } }],
|
|
130
|
+
};
|
|
123
131
|
}
|
|
124
132
|
const mngOp = opMap[ast.op];
|
|
125
133
|
if (mngOp) {
|