@opra/mongodb 1.0.0-alpha.16 → 1.0.0-alpha.18
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-utils/prepare-filter.js +1 -1
- package/cjs/adapter-utils/prepare-key-values.js +1 -1
- package/cjs/adapter-utils/prepare-patch.js +1 -1
- package/cjs/adapter-utils/prepare-projection.js +2 -3
- package/cjs/adapter-utils/prepare-sort.js +1 -1
- package/cjs/mongo-collection-service.js +111 -94
- package/cjs/mongo-entity-service.js +86 -69
- package/cjs/mongo-nested-service.js +236 -120
- package/cjs/mongo-service.js +31 -22
- package/cjs/mongo-singleton-service.js +61 -41
- package/esm/mongo-collection-service.js +111 -94
- package/esm/mongo-entity-service.js +86 -69
- package/esm/mongo-nested-service.js +236 -120
- package/esm/mongo-service.js +31 -22
- package/esm/mongo-singleton-service.js +61 -41
- package/package.json +5 -4
- package/types/mongo-collection-service.d.ts +32 -62
- package/types/mongo-entity-service.d.ts +73 -47
- package/types/mongo-nested-service.d.ts +55 -20
- package/types/mongo-service.d.ts +28 -26
- package/types/mongo-singleton-service.d.ts +19 -29
|
@@ -4,6 +4,7 @@ exports.MongoNestedService = void 0;
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const common_1 = require("@opra/common");
|
|
6
6
|
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
7
|
+
const valgen_1 = require("valgen");
|
|
7
8
|
const mongo_adapter_js_1 = require("./mongo-adapter.js");
|
|
8
9
|
const mongo_service_js_1 = require("./mongo-service.js");
|
|
9
10
|
/**
|
|
@@ -25,7 +26,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
25
26
|
this.fieldName = fieldName;
|
|
26
27
|
this.nestedKey = options?.nestedKey || '_id';
|
|
27
28
|
this.defaultLimit = options?.defaultLimit || 10;
|
|
28
|
-
this
|
|
29
|
+
this.nestedFilter = options?.nestedFilter;
|
|
29
30
|
}
|
|
30
31
|
/**
|
|
31
32
|
* Retrieves the data type of the array field
|
|
@@ -64,24 +65,21 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
64
65
|
* @throws {ResourceNotAvailableError} - If the parent document is not found.
|
|
65
66
|
*/
|
|
66
67
|
async create(documentId, input, options) {
|
|
67
|
-
const
|
|
68
|
-
if (id != null)
|
|
69
|
-
input._id = id;
|
|
70
|
-
const info = {
|
|
68
|
+
const command = {
|
|
71
69
|
crud: 'create',
|
|
72
70
|
method: 'create',
|
|
73
71
|
byId: false,
|
|
74
72
|
documentId,
|
|
75
|
-
nestedId: id,
|
|
76
73
|
input,
|
|
77
74
|
options,
|
|
78
75
|
};
|
|
79
|
-
return this.
|
|
76
|
+
return this._executeCommand(command, () => this._create(command));
|
|
80
77
|
}
|
|
81
|
-
async _create(
|
|
78
|
+
async _create(command) {
|
|
82
79
|
const inputCodec = this.getInputCodec('create');
|
|
83
|
-
const
|
|
84
|
-
doc
|
|
80
|
+
const { documentId, options } = command;
|
|
81
|
+
const doc = inputCodec(command.input);
|
|
82
|
+
doc._id = doc._id || this._generateId(command);
|
|
85
83
|
const docFilter = mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']);
|
|
86
84
|
const r = await this._dbUpdateOne(docFilter, {
|
|
87
85
|
$push: { [this.fieldName]: doc },
|
|
@@ -89,12 +87,20 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
89
87
|
if (r.matchedCount) {
|
|
90
88
|
if (!options)
|
|
91
89
|
return doc;
|
|
92
|
-
const
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
90
|
+
const findCommand = {
|
|
91
|
+
crud: 'read',
|
|
92
|
+
method: 'findById',
|
|
93
|
+
byId: true,
|
|
94
|
+
documentId,
|
|
95
|
+
nestedId: doc[this.nestedKey],
|
|
96
|
+
options: {
|
|
97
|
+
...options,
|
|
98
|
+
sort: undefined,
|
|
99
|
+
filter: undefined,
|
|
100
|
+
skip: undefined,
|
|
101
|
+
},
|
|
102
|
+
};
|
|
103
|
+
const out = await this._findById(findCommand);
|
|
98
104
|
if (out)
|
|
99
105
|
return out;
|
|
100
106
|
}
|
|
@@ -108,20 +114,22 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
108
114
|
* @returns {Promise<number>} - A promise that resolves to the count of documents.
|
|
109
115
|
*/
|
|
110
116
|
async count(documentId, options) {
|
|
111
|
-
const
|
|
117
|
+
const command = {
|
|
112
118
|
crud: 'read',
|
|
113
119
|
method: 'count',
|
|
114
120
|
byId: false,
|
|
115
121
|
documentId,
|
|
116
122
|
options,
|
|
117
123
|
};
|
|
118
|
-
return this.
|
|
119
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
120
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
121
|
-
|
|
122
|
-
|
|
124
|
+
return this._executeCommand(command, async () => {
|
|
125
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
126
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
127
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
128
|
+
return this._count(command);
|
|
129
|
+
});
|
|
123
130
|
}
|
|
124
|
-
async _count(
|
|
131
|
+
async _count(command) {
|
|
132
|
+
const { documentId, options } = command;
|
|
125
133
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
126
134
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
127
135
|
options?.documentFilter,
|
|
@@ -154,7 +162,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
154
162
|
* @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
|
|
155
163
|
*/
|
|
156
164
|
async delete(documentId, nestedId, options) {
|
|
157
|
-
const
|
|
165
|
+
const command = {
|
|
158
166
|
crud: 'delete',
|
|
159
167
|
method: 'delete',
|
|
160
168
|
byId: true,
|
|
@@ -162,13 +170,17 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
162
170
|
nestedId,
|
|
163
171
|
options,
|
|
164
172
|
};
|
|
165
|
-
return this.
|
|
166
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
167
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
168
|
-
|
|
169
|
-
|
|
173
|
+
return this._executeCommand(command, async () => {
|
|
174
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
175
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
176
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
177
|
+
return this._delete(command);
|
|
178
|
+
});
|
|
170
179
|
}
|
|
171
|
-
async _delete(
|
|
180
|
+
async _delete(command) {
|
|
181
|
+
const { documentId, nestedId, options } = command;
|
|
182
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
183
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'nestedId' });
|
|
172
184
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
173
185
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
174
186
|
options?.documentFilter,
|
|
@@ -187,20 +199,22 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
187
199
|
* @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
|
|
188
200
|
*/
|
|
189
201
|
async deleteMany(documentId, options) {
|
|
190
|
-
const
|
|
202
|
+
const command = {
|
|
191
203
|
crud: 'delete',
|
|
192
204
|
method: 'deleteMany',
|
|
193
205
|
byId: false,
|
|
194
206
|
documentId,
|
|
195
207
|
options,
|
|
196
208
|
};
|
|
197
|
-
return this.
|
|
198
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
199
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
200
|
-
|
|
201
|
-
|
|
209
|
+
return this._executeCommand(command, async () => {
|
|
210
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
211
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
212
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
213
|
+
return this._deleteMany(command);
|
|
214
|
+
});
|
|
202
215
|
}
|
|
203
|
-
async _deleteMany(
|
|
216
|
+
async _deleteMany(command) {
|
|
217
|
+
const { documentId, options } = command;
|
|
204
218
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
205
219
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
206
220
|
options?.documentFilter,
|
|
@@ -224,7 +238,24 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
224
238
|
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the record exists or not.
|
|
225
239
|
*/
|
|
226
240
|
async exists(documentId, nestedId, options) {
|
|
227
|
-
|
|
241
|
+
const command = {
|
|
242
|
+
crud: 'read',
|
|
243
|
+
method: 'exists',
|
|
244
|
+
byId: true,
|
|
245
|
+
documentId,
|
|
246
|
+
nestedId,
|
|
247
|
+
options,
|
|
248
|
+
};
|
|
249
|
+
return this._executeCommand(command, async () => {
|
|
250
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
251
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
252
|
+
await this._getNestedFilter(command),
|
|
253
|
+
documentFilter,
|
|
254
|
+
command.options?.filter,
|
|
255
|
+
]);
|
|
256
|
+
command.options = { ...command.options, filter };
|
|
257
|
+
return !!(await this._findById(command));
|
|
258
|
+
});
|
|
228
259
|
}
|
|
229
260
|
/**
|
|
230
261
|
* Checks if an object with the given arguments exists.
|
|
@@ -234,7 +265,20 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
234
265
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
235
266
|
*/
|
|
236
267
|
async existsOne(documentId, options) {
|
|
237
|
-
|
|
268
|
+
const command = {
|
|
269
|
+
crud: 'read',
|
|
270
|
+
method: 'exists',
|
|
271
|
+
byId: false,
|
|
272
|
+
documentId,
|
|
273
|
+
options,
|
|
274
|
+
};
|
|
275
|
+
return this._executeCommand(command, async () => {
|
|
276
|
+
const documentFilter = await this._getDocumentFilter(command);
|
|
277
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
|
|
278
|
+
const findCommand = command;
|
|
279
|
+
findCommand.options = { ...command.options, filter, documentFilter, projection: ['_id'] };
|
|
280
|
+
return !!(await this._findOne(findCommand));
|
|
281
|
+
});
|
|
238
282
|
}
|
|
239
283
|
/**
|
|
240
284
|
* Finds an element in array field by its parent ID and ID.
|
|
@@ -245,7 +289,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
245
289
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
246
290
|
*/
|
|
247
291
|
async findById(documentId, nestedId, options) {
|
|
248
|
-
const
|
|
292
|
+
const command = {
|
|
249
293
|
crud: 'read',
|
|
250
294
|
method: 'findById',
|
|
251
295
|
byId: true,
|
|
@@ -253,24 +297,32 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
253
297
|
nestedId,
|
|
254
298
|
options,
|
|
255
299
|
};
|
|
256
|
-
return this.
|
|
257
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
258
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
259
|
-
|
|
260
|
-
|
|
300
|
+
return this._executeCommand(command, async () => {
|
|
301
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
302
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
303
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
304
|
+
return this._findById(command);
|
|
305
|
+
});
|
|
261
306
|
}
|
|
262
|
-
async _findById(
|
|
307
|
+
async _findById(command) {
|
|
308
|
+
const { documentId, nestedId, options } = command;
|
|
309
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
310
|
+
(0, valgen_1.isNotNullish)(nestedId, { label: 'nestedId' });
|
|
263
311
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
264
312
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(nestedId, [this.nestedKey]),
|
|
265
313
|
options?.filter,
|
|
266
314
|
]);
|
|
267
|
-
const
|
|
268
|
-
...
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
315
|
+
const findManyCommand = {
|
|
316
|
+
...command,
|
|
317
|
+
options: {
|
|
318
|
+
...options,
|
|
319
|
+
filter,
|
|
320
|
+
limit: 1,
|
|
321
|
+
skip: undefined,
|
|
322
|
+
sort: undefined,
|
|
323
|
+
},
|
|
324
|
+
};
|
|
325
|
+
const rows = await this._findMany(findManyCommand);
|
|
274
326
|
return rows?.[0];
|
|
275
327
|
}
|
|
276
328
|
/**
|
|
@@ -281,24 +333,31 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
281
333
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
|
|
282
334
|
*/
|
|
283
335
|
async findOne(documentId, options) {
|
|
284
|
-
const
|
|
336
|
+
const command = {
|
|
285
337
|
crud: 'read',
|
|
286
338
|
method: 'findOne',
|
|
287
339
|
byId: false,
|
|
288
340
|
documentId,
|
|
289
341
|
options,
|
|
290
342
|
};
|
|
291
|
-
return this.
|
|
292
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
293
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
async _findOne(documentId, options) {
|
|
298
|
-
const rows = await this._findMany(documentId, {
|
|
299
|
-
...options,
|
|
300
|
-
limit: 1,
|
|
343
|
+
return this._executeCommand(command, async () => {
|
|
344
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
345
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
346
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
347
|
+
return this._findOne(command);
|
|
301
348
|
});
|
|
349
|
+
}
|
|
350
|
+
async _findOne(command) {
|
|
351
|
+
const { documentId, options } = command;
|
|
352
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
353
|
+
const findManyCommand = {
|
|
354
|
+
...command,
|
|
355
|
+
options: {
|
|
356
|
+
...options,
|
|
357
|
+
limit: 1,
|
|
358
|
+
},
|
|
359
|
+
};
|
|
360
|
+
const rows = await this._findMany(findManyCommand);
|
|
302
361
|
return rows?.[0];
|
|
303
362
|
}
|
|
304
363
|
/**
|
|
@@ -309,28 +368,31 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
309
368
|
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
310
369
|
*/
|
|
311
370
|
async findMany(documentId, options) {
|
|
312
|
-
const
|
|
371
|
+
const command = {
|
|
313
372
|
crud: 'read',
|
|
314
373
|
method: 'findMany',
|
|
315
374
|
byId: false,
|
|
316
375
|
documentId,
|
|
317
376
|
options,
|
|
318
377
|
};
|
|
319
|
-
return this.
|
|
320
|
-
const documentFilter = await this._getDocumentFilter(
|
|
321
|
-
const nestedFilter = await this._getNestedFilter(
|
|
322
|
-
|
|
323
|
-
...options,
|
|
324
|
-
documentFilter,
|
|
378
|
+
return this._executeCommand(command, async () => {
|
|
379
|
+
const documentFilter = await this._getDocumentFilter(command);
|
|
380
|
+
const nestedFilter = await this._getNestedFilter(command);
|
|
381
|
+
command.options = {
|
|
382
|
+
...command.options,
|
|
325
383
|
nestedFilter,
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
384
|
+
documentFilter,
|
|
385
|
+
limit: command.options?.limit || this.defaultLimit,
|
|
386
|
+
};
|
|
387
|
+
return this._findMany(command);
|
|
388
|
+
});
|
|
329
389
|
}
|
|
330
|
-
async _findMany(
|
|
390
|
+
async _findMany(command) {
|
|
391
|
+
const { documentId, options } = command;
|
|
392
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
331
393
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
332
394
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
333
|
-
options
|
|
395
|
+
options?.documentFilter,
|
|
334
396
|
]);
|
|
335
397
|
const mongoOptions = {
|
|
336
398
|
...(0, lodash_omit_1.default)(options, ['documentFilter', 'nestedFilter', 'projection', 'sort', 'skip', 'limit', 'filter', 'count']),
|
|
@@ -341,7 +403,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
341
403
|
{ $unwind: { path: '$' + this.fieldName } },
|
|
342
404
|
{ $replaceRoot: { newRoot: '$' + this.fieldName } },
|
|
343
405
|
];
|
|
344
|
-
if (options?.filter || options
|
|
406
|
+
if (options?.filter || options?.nestedFilter) {
|
|
345
407
|
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter, options.nestedFilter]);
|
|
346
408
|
stages.push({ $match: optionsFilter });
|
|
347
409
|
}
|
|
@@ -376,28 +438,31 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
376
438
|
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
377
439
|
*/
|
|
378
440
|
async findManyWithCount(documentId, options) {
|
|
379
|
-
const
|
|
441
|
+
const command = {
|
|
380
442
|
crud: 'read',
|
|
381
443
|
method: 'findMany',
|
|
382
444
|
byId: false,
|
|
383
445
|
documentId,
|
|
384
446
|
options,
|
|
385
447
|
};
|
|
386
|
-
return this.
|
|
387
|
-
const documentFilter = await this._getDocumentFilter(
|
|
388
|
-
const nestedFilter = await this._getNestedFilter(
|
|
389
|
-
|
|
390
|
-
...options,
|
|
391
|
-
documentFilter,
|
|
448
|
+
return this._executeCommand(command, async () => {
|
|
449
|
+
const documentFilter = await this._getDocumentFilter(command);
|
|
450
|
+
const nestedFilter = await this._getNestedFilter(command);
|
|
451
|
+
command.options = {
|
|
452
|
+
...command.options,
|
|
392
453
|
nestedFilter,
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
454
|
+
documentFilter,
|
|
455
|
+
limit: command.options?.limit || this.defaultLimit,
|
|
456
|
+
};
|
|
457
|
+
return this._findManyWithCount(command);
|
|
458
|
+
});
|
|
396
459
|
}
|
|
397
|
-
async _findManyWithCount(
|
|
460
|
+
async _findManyWithCount(command) {
|
|
461
|
+
const { documentId, options } = command;
|
|
462
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
398
463
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
399
464
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, ['_id']),
|
|
400
|
-
options
|
|
465
|
+
options?.documentFilter,
|
|
401
466
|
]);
|
|
402
467
|
const mongoOptions = {
|
|
403
468
|
...(0, lodash_omit_1.default)(options, ['pick', 'include', 'omit', 'sort', 'skip', 'limit', 'filter', 'count']),
|
|
@@ -415,8 +480,8 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
415
480
|
},
|
|
416
481
|
},
|
|
417
482
|
];
|
|
418
|
-
if (options?.filter || options
|
|
419
|
-
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter, options
|
|
483
|
+
if (options?.filter || options?.nestedFilter) {
|
|
484
|
+
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter, options?.nestedFilter]);
|
|
420
485
|
dataStages.push({ $match: optionsFilter });
|
|
421
486
|
}
|
|
422
487
|
if (options?.skip)
|
|
@@ -469,21 +534,44 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
469
534
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
470
535
|
* @param {AnyId} nestedId - The ID of the item to update within the document.
|
|
471
536
|
* @param {PatchDTO<T>} input - The new data to update the item with.
|
|
472
|
-
* @param {MongoNestedService.
|
|
537
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Additional update options.
|
|
473
538
|
* @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
|
|
474
539
|
* @throws {Error} If an error occurs while updating the item.
|
|
475
540
|
*/
|
|
476
541
|
async update(documentId, nestedId, input, options) {
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
542
|
+
const command = {
|
|
543
|
+
crud: 'update',
|
|
544
|
+
method: 'update',
|
|
545
|
+
byId: true,
|
|
546
|
+
documentId,
|
|
547
|
+
nestedId,
|
|
548
|
+
input,
|
|
549
|
+
options,
|
|
550
|
+
};
|
|
551
|
+
return this._executeCommand(command, async () => {
|
|
552
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
553
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
554
|
+
command.options = {
|
|
555
|
+
...command.options,
|
|
556
|
+
filter,
|
|
557
|
+
documentFilter,
|
|
558
|
+
};
|
|
559
|
+
const r = await this._updateOnly(command);
|
|
560
|
+
if (r) {
|
|
561
|
+
const findCommand = {
|
|
562
|
+
crud: 'read',
|
|
563
|
+
method: 'findById',
|
|
564
|
+
byId: true,
|
|
565
|
+
documentId,
|
|
566
|
+
nestedId,
|
|
567
|
+
options: { ...options, sort: undefined },
|
|
568
|
+
};
|
|
569
|
+
const out = this._findById(findCommand);
|
|
570
|
+
if (out)
|
|
571
|
+
return out;
|
|
572
|
+
}
|
|
573
|
+
throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + nestedId);
|
|
483
574
|
});
|
|
484
|
-
if (out)
|
|
485
|
-
return out;
|
|
486
|
-
throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.nestedKey, documentId + '/' + nestedId);
|
|
487
575
|
}
|
|
488
576
|
/**
|
|
489
577
|
* Update an array element with new data. Returns 1 if document updated 0 otherwise.
|
|
@@ -491,29 +579,45 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
491
579
|
* @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
|
|
492
580
|
* @param {MongoAdapter.AnyId} nestedId - The ID of the document to update.
|
|
493
581
|
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
494
|
-
* @param {MongoNestedService.
|
|
582
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Optional update options.
|
|
495
583
|
* @returns {Promise<number>} - A promise that resolves to the number of elements updated.
|
|
496
584
|
*/
|
|
497
585
|
async updateOnly(documentId, nestedId, input, options) {
|
|
498
|
-
const
|
|
586
|
+
const command = {
|
|
499
587
|
crud: 'update',
|
|
500
588
|
method: 'update',
|
|
501
589
|
byId: true,
|
|
502
590
|
documentId,
|
|
503
591
|
nestedId,
|
|
592
|
+
input,
|
|
504
593
|
options,
|
|
505
594
|
};
|
|
506
|
-
return this.
|
|
507
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
508
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
509
|
-
|
|
510
|
-
|
|
595
|
+
return this._executeCommand(command, async () => {
|
|
596
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
597
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
598
|
+
command.options = {
|
|
599
|
+
...command.options,
|
|
600
|
+
filter,
|
|
601
|
+
documentFilter,
|
|
602
|
+
};
|
|
603
|
+
return await this._updateOnly(command);
|
|
604
|
+
});
|
|
511
605
|
}
|
|
512
|
-
async _updateOnly(
|
|
606
|
+
async _updateOnly(command) {
|
|
607
|
+
const { documentId, nestedId, options } = command;
|
|
608
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
609
|
+
(0, valgen_1.isNotNullish)(nestedId, { label: 'nestedId' });
|
|
513
610
|
let filter = mongo_adapter_js_1.MongoAdapter.prepareKeyValues(nestedId, [this.nestedKey]);
|
|
514
611
|
if (options?.filter)
|
|
515
612
|
filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([filter, options?.filter]);
|
|
516
|
-
|
|
613
|
+
const updateManyCommand = {
|
|
614
|
+
...command,
|
|
615
|
+
options: {
|
|
616
|
+
...command.options,
|
|
617
|
+
filter,
|
|
618
|
+
},
|
|
619
|
+
};
|
|
620
|
+
return await this._updateMany(updateManyCommand);
|
|
517
621
|
}
|
|
518
622
|
/**
|
|
519
623
|
* Updates multiple array elements in document
|
|
@@ -524,7 +628,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
524
628
|
* @returns {Promise<number>} - A promise that resolves to the number of documents updated.
|
|
525
629
|
*/
|
|
526
630
|
async updateMany(documentId, input, options) {
|
|
527
|
-
const
|
|
631
|
+
const command = {
|
|
528
632
|
crud: 'update',
|
|
529
633
|
method: 'updateMany',
|
|
530
634
|
documentId,
|
|
@@ -532,13 +636,17 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
532
636
|
input,
|
|
533
637
|
options,
|
|
534
638
|
};
|
|
535
|
-
return this.
|
|
536
|
-
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
537
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(
|
|
538
|
-
|
|
539
|
-
|
|
639
|
+
return this._executeCommand(command, async () => {
|
|
640
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command)]);
|
|
641
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getNestedFilter(command), command.options?.filter]);
|
|
642
|
+
command.options = { ...command.options, filter, documentFilter };
|
|
643
|
+
return this._updateMany(command);
|
|
644
|
+
});
|
|
540
645
|
}
|
|
541
|
-
async _updateMany(
|
|
646
|
+
async _updateMany(command) {
|
|
647
|
+
const { documentId, input } = command;
|
|
648
|
+
(0, valgen_1.isNotNullish)(documentId, { label: 'documentId' });
|
|
649
|
+
let options = command.options;
|
|
542
650
|
const inputCodec = this.getInputCodec('update');
|
|
543
651
|
const doc = inputCodec(input);
|
|
544
652
|
if (!Object.keys(doc).length)
|
|
@@ -557,8 +665,16 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
557
665
|
fieldPrefix: this.fieldName + (options?.filter ? '.$[elem].' : '.$[].'),
|
|
558
666
|
});
|
|
559
667
|
const r = await this._dbUpdateOne(matchFilter, update, options);
|
|
560
|
-
if (options?.count)
|
|
561
|
-
|
|
668
|
+
if (options?.count) {
|
|
669
|
+
const countCommand = {
|
|
670
|
+
crud: 'read',
|
|
671
|
+
method: 'count',
|
|
672
|
+
byId: false,
|
|
673
|
+
documentId,
|
|
674
|
+
options,
|
|
675
|
+
};
|
|
676
|
+
return await this._count(countCommand);
|
|
677
|
+
}
|
|
562
678
|
return r.modifiedCount || 0;
|
|
563
679
|
}
|
|
564
680
|
/**
|
|
@@ -570,7 +686,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
|
|
|
570
686
|
* that resolves to the common filter, or undefined if not available.
|
|
571
687
|
*/
|
|
572
688
|
_getNestedFilter(args) {
|
|
573
|
-
return typeof this
|
|
689
|
+
return typeof this.nestedFilter === 'function' ? this.nestedFilter(args, this) : this.nestedFilter;
|
|
574
690
|
}
|
|
575
691
|
}
|
|
576
692
|
exports.MongoNestedService = MongoNestedService;
|