@opra/mongodb 0.32.6 → 0.33.0
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/mongo-array-service.js +212 -159
- package/cjs/mongo-collection-service.js +194 -107
- package/cjs/mongo-service.js +48 -34
- package/cjs/mongo-singleton-service.js +85 -58
- package/esm/mongo-array-service.js +213 -160
- package/esm/mongo-collection-service.js +195 -108
- package/esm/mongo-service.js +48 -34
- package/esm/mongo-singleton-service.js +86 -59
- package/package.json +4 -4
- package/types/mongo-array-service.d.ts +74 -20
- package/types/mongo-collection-service.d.ts +60 -9
- package/types/mongo-service.d.ts +22 -25
- package/types/mongo-singleton-service.d.ts +31 -7
- package/types/types.d.ts +2 -1
|
@@ -38,11 +38,11 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
38
38
|
* @param {AnyId} id - The ID of the resource.
|
|
39
39
|
* @param {MongoArrayService.ExistsOptions} [options] - Optional parameters for checking resource existence.
|
|
40
40
|
* @return {Promise<void>} - A promise that resolves with no value upon success.
|
|
41
|
-
* @throws {
|
|
41
|
+
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
42
42
|
*/
|
|
43
43
|
async assert(documentId, id, options) {
|
|
44
44
|
if (!(await this.exists(documentId, id, options)))
|
|
45
|
-
throw new common_1.
|
|
45
|
+
throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.arrayKey, documentId + '/' + id);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Adds a single item into the array field.
|
|
@@ -51,18 +51,20 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
51
51
|
* @param {T} input - The item to be added to the array field.
|
|
52
52
|
* @param {MongoArrayService.CreateOptions} [options] - Optional options for the create operation.
|
|
53
53
|
* @return {Promise<PartialDTO<T>>} - A promise that resolves with the partial output of the created item.
|
|
54
|
-
* @throws {
|
|
54
|
+
* @throws {ResourceNotAvailableError} - If the parent document is not found.
|
|
55
55
|
*/
|
|
56
56
|
async create(documentId, input, options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
const info = {
|
|
58
|
+
crud: 'create',
|
|
59
|
+
method: 'create',
|
|
60
|
+
documentId,
|
|
61
|
+
itemId: input._id,
|
|
62
|
+
input,
|
|
63
|
+
options
|
|
64
|
+
};
|
|
65
|
+
return this._intercept(() => this._create(documentId, input, options), info);
|
|
66
|
+
}
|
|
67
|
+
async _create(documentId, input, options) {
|
|
66
68
|
const encode = this.getEncoder('create');
|
|
67
69
|
const doc = encode(input, { coerce: true });
|
|
68
70
|
doc._id = doc._id || this._generateId();
|
|
@@ -70,18 +72,19 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
70
72
|
const r = await this.__updateOne(docFilter, {
|
|
71
73
|
$push: { [this.fieldName]: doc }
|
|
72
74
|
}, options);
|
|
73
|
-
if (r.
|
|
75
|
+
if (r.matchedCount) {
|
|
74
76
|
if (!options)
|
|
75
77
|
return doc;
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
78
|
+
const id = doc[this.arrayKey];
|
|
79
|
+
const out = await this._findById(documentId, id, {
|
|
80
|
+
...options,
|
|
81
|
+
filter: undefined,
|
|
82
|
+
skip: undefined
|
|
83
|
+
});
|
|
84
|
+
if (out)
|
|
85
|
+
return out;
|
|
83
86
|
}
|
|
84
|
-
throw new common_1.
|
|
87
|
+
throw new common_1.ResourceNotAvailableError(this.getResourceName(), documentId);
|
|
85
88
|
}
|
|
86
89
|
/**
|
|
87
90
|
* Counts the number of documents in the collection that match the specified parentId and options.
|
|
@@ -92,26 +95,36 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
92
95
|
* @returns {Promise<number>} - A promise that resolves to the count of documents.
|
|
93
96
|
*/
|
|
94
97
|
async count(documentId, options) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
98
|
+
const info = {
|
|
99
|
+
crud: 'read',
|
|
100
|
+
method: 'count',
|
|
101
|
+
documentId,
|
|
102
|
+
options
|
|
103
|
+
};
|
|
104
|
+
return this._intercept(async () => {
|
|
105
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
106
|
+
await this._getDocumentFilter(info)
|
|
107
|
+
]);
|
|
108
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
109
|
+
await this._getArrayFilter(info),
|
|
110
|
+
options?.filter
|
|
111
|
+
]);
|
|
112
|
+
return this._count(documentId, { ...options, filter, documentFilter });
|
|
113
|
+
}, info);
|
|
114
|
+
}
|
|
115
|
+
async _count(documentId, options) {
|
|
102
116
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
103
117
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, [this.collectionKey]),
|
|
104
|
-
|
|
118
|
+
options?.documentFilter
|
|
105
119
|
]);
|
|
106
120
|
const stages = [
|
|
107
121
|
{ $match: matchFilter },
|
|
108
122
|
{ $unwind: { path: "$" + this.fieldName } },
|
|
109
123
|
{ $replaceRoot: { newRoot: "$" + this.fieldName } }
|
|
110
124
|
];
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
stages.push({ $match: optionsFilter });
|
|
125
|
+
if (options?.filter) {
|
|
126
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
127
|
+
stages.push({ $match: filter });
|
|
115
128
|
}
|
|
116
129
|
stages.push({ $count: '*' });
|
|
117
130
|
const r = await this.__aggregate(stages, options);
|
|
@@ -132,22 +145,32 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
132
145
|
* @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
|
|
133
146
|
*/
|
|
134
147
|
async delete(documentId, id, options) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
148
|
+
const info = {
|
|
149
|
+
crud: 'delete',
|
|
150
|
+
method: 'delete',
|
|
151
|
+
documentId,
|
|
152
|
+
itemId: id,
|
|
153
|
+
options
|
|
154
|
+
};
|
|
155
|
+
return this._intercept(async () => {
|
|
156
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
157
|
+
await this._getDocumentFilter(info)
|
|
158
|
+
]);
|
|
159
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
160
|
+
await this._getArrayFilter(info),
|
|
161
|
+
options?.filter
|
|
162
|
+
]);
|
|
163
|
+
return this._delete(documentId, id, { ...options, filter, documentFilter });
|
|
164
|
+
}, info);
|
|
165
|
+
}
|
|
166
|
+
async _delete(documentId, id, options) {
|
|
143
167
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
144
168
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, [this.collectionKey]),
|
|
145
|
-
|
|
169
|
+
options?.documentFilter
|
|
146
170
|
]);
|
|
147
171
|
const pullFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
148
172
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, [this.arrayKey]),
|
|
149
|
-
options?.filter
|
|
150
|
-
await this._getArrayFilter()
|
|
173
|
+
options?.filter
|
|
151
174
|
]) || {};
|
|
152
175
|
const r = await this.__updateOne(matchFilter, {
|
|
153
176
|
$pull: { [this.fieldName]: pullFilter }
|
|
@@ -162,23 +185,31 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
162
185
|
* @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
|
|
163
186
|
*/
|
|
164
187
|
async deleteMany(documentId, options) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
188
|
+
const info = {
|
|
189
|
+
crud: 'delete',
|
|
190
|
+
method: 'deleteMany',
|
|
191
|
+
documentId,
|
|
192
|
+
options
|
|
193
|
+
};
|
|
194
|
+
return this._intercept(async () => {
|
|
195
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
196
|
+
await this._getDocumentFilter(info)
|
|
197
|
+
]);
|
|
198
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
199
|
+
await this._getArrayFilter(info),
|
|
200
|
+
options?.filter
|
|
201
|
+
]);
|
|
202
|
+
return this._deleteMany(documentId, { ...options, filter, documentFilter });
|
|
203
|
+
}, info);
|
|
204
|
+
}
|
|
205
|
+
async _deleteMany(documentId, options) {
|
|
172
206
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
173
207
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, [this.collectionKey]),
|
|
174
|
-
|
|
208
|
+
options?.documentFilter
|
|
175
209
|
]);
|
|
176
210
|
// Count matching items, we will use this as result
|
|
177
211
|
const matchCount = await this.count(documentId, options);
|
|
178
|
-
const pullFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter(
|
|
179
|
-
options?.filter,
|
|
180
|
-
await this._getArrayFilter()
|
|
181
|
-
]) || {};
|
|
212
|
+
const pullFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter) || {};
|
|
182
213
|
const r = await this.__updateOne(matchFilter, {
|
|
183
214
|
$pull: { [this.fieldName]: pullFilter }
|
|
184
215
|
}, options);
|
|
@@ -195,15 +226,7 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
195
226
|
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the record exists or not.
|
|
196
227
|
*/
|
|
197
228
|
async exists(documentId, id, options) {
|
|
198
|
-
|
|
199
|
-
return this.$interceptor(() => this.exists(documentId, id, { ...options, __interceptor__: true }), {
|
|
200
|
-
crud: 'read',
|
|
201
|
-
method: 'exists',
|
|
202
|
-
documentId,
|
|
203
|
-
itemId: id,
|
|
204
|
-
options
|
|
205
|
-
}, this);
|
|
206
|
-
return !!(await this.findById(documentId, id, { ...options, pick: ['_id'] }));
|
|
229
|
+
return !!(await this.findById(documentId, id, { ...options, pick: ['_id'], omit: undefined, include: undefined }));
|
|
207
230
|
}
|
|
208
231
|
/**
|
|
209
232
|
* Finds an element in array field by its parent ID and ID.
|
|
@@ -214,19 +237,37 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
214
237
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
215
238
|
*/
|
|
216
239
|
async findById(documentId, id, options) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
240
|
+
const info = {
|
|
241
|
+
crud: 'read',
|
|
242
|
+
method: 'findById',
|
|
243
|
+
documentId,
|
|
244
|
+
itemId: id,
|
|
245
|
+
options
|
|
246
|
+
};
|
|
247
|
+
return this._intercept(async () => {
|
|
248
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
249
|
+
await this._getDocumentFilter(info)
|
|
250
|
+
]);
|
|
251
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
252
|
+
await this._getArrayFilter(info),
|
|
253
|
+
options?.filter
|
|
254
|
+
]);
|
|
255
|
+
return this._findById(documentId, id, { ...options, filter, documentFilter });
|
|
256
|
+
}, info);
|
|
257
|
+
}
|
|
258
|
+
async _findById(documentId, id, options) {
|
|
225
259
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
226
260
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, [this.arrayKey]),
|
|
227
261
|
options?.filter
|
|
228
262
|
]);
|
|
229
|
-
|
|
263
|
+
const rows = await this._findMany(documentId, {
|
|
264
|
+
...options,
|
|
265
|
+
filter,
|
|
266
|
+
limit: 1,
|
|
267
|
+
skip: undefined,
|
|
268
|
+
sort: undefined
|
|
269
|
+
});
|
|
270
|
+
return rows?.[0];
|
|
230
271
|
}
|
|
231
272
|
/**
|
|
232
273
|
* Finds the first array element that matches the given parentId.
|
|
@@ -236,14 +277,25 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
236
277
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
|
|
237
278
|
*/
|
|
238
279
|
async findOne(documentId, options) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
280
|
+
const info = {
|
|
281
|
+
crud: 'read',
|
|
282
|
+
method: 'findOne',
|
|
283
|
+
documentId,
|
|
284
|
+
options
|
|
285
|
+
};
|
|
286
|
+
return this._intercept(async () => {
|
|
287
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
288
|
+
await this._getDocumentFilter(info)
|
|
289
|
+
]);
|
|
290
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
291
|
+
await this._getArrayFilter(info),
|
|
292
|
+
options?.filter
|
|
293
|
+
]);
|
|
294
|
+
return this._findOne(documentId, { ...options, filter, documentFilter });
|
|
295
|
+
}, info);
|
|
296
|
+
}
|
|
297
|
+
async _findOne(documentId, options) {
|
|
298
|
+
const rows = await this._findMany(documentId, {
|
|
247
299
|
...options,
|
|
248
300
|
limit: 1
|
|
249
301
|
});
|
|
@@ -257,16 +309,22 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
257
309
|
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
258
310
|
*/
|
|
259
311
|
async findMany(documentId, options) {
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
312
|
+
const args = {
|
|
313
|
+
crud: 'read',
|
|
314
|
+
method: 'findMany',
|
|
315
|
+
documentId,
|
|
316
|
+
options
|
|
317
|
+
};
|
|
318
|
+
return this._intercept(async () => {
|
|
319
|
+
const documentFilter = await this._getDocumentFilter(args);
|
|
320
|
+
const arrayFilter = await this._getArrayFilter(args);
|
|
321
|
+
return this._findMany(documentId, { ...options, documentFilter, arrayFilter });
|
|
322
|
+
}, args);
|
|
323
|
+
}
|
|
324
|
+
async _findMany(documentId, options) {
|
|
267
325
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
268
326
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, [this.collectionKey]),
|
|
269
|
-
|
|
327
|
+
options.documentFilter
|
|
270
328
|
]);
|
|
271
329
|
const mongoOptions = {
|
|
272
330
|
...(0, lodash_omit_1.default)(options, ['pick', 'include', 'omit', 'sort', 'skip', 'limit', 'filter', 'count'])
|
|
@@ -287,11 +345,10 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
287
345
|
}
|
|
288
346
|
});
|
|
289
347
|
}
|
|
290
|
-
|
|
291
|
-
if (options?.filter || contextArrayFilter) {
|
|
348
|
+
if (options?.filter || options.arrayFilter) {
|
|
292
349
|
const optionsFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
293
350
|
options?.filter,
|
|
294
|
-
|
|
351
|
+
options.arrayFilter
|
|
295
352
|
]);
|
|
296
353
|
dataStages.push({ $match: optionsFilter });
|
|
297
354
|
}
|
|
@@ -332,12 +389,12 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
332
389
|
* @param {AnyId} id - The ID of the item.
|
|
333
390
|
* @param {MongoArrayService.FindOneOptions<T>} [options] - The options for finding the item.
|
|
334
391
|
* @returns {Promise<PartialDTO<T>>} - The item found.
|
|
335
|
-
* @throws {
|
|
392
|
+
* @throws {ResourceNotAvailableError} - If the item is not found.
|
|
336
393
|
*/
|
|
337
394
|
async get(documentId, id, options) {
|
|
338
395
|
const out = await this.findById(documentId, id, options);
|
|
339
396
|
if (!out)
|
|
340
|
-
throw new common_1.
|
|
397
|
+
throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.arrayKey, documentId + '/' + id);
|
|
341
398
|
return out;
|
|
342
399
|
}
|
|
343
400
|
/**
|
|
@@ -351,24 +408,13 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
351
408
|
* @throws {Error} If an error occurs while updating the item.
|
|
352
409
|
*/
|
|
353
410
|
async update(documentId, id, input, options) {
|
|
354
|
-
if (this.$interceptor && !options?.__interceptor__)
|
|
355
|
-
return this.$interceptor(() => this.update(documentId, id, input, { ...options, __interceptor__: true }), {
|
|
356
|
-
crud: 'update',
|
|
357
|
-
method: 'update',
|
|
358
|
-
documentId,
|
|
359
|
-
itemId: id,
|
|
360
|
-
options
|
|
361
|
-
}, this);
|
|
362
411
|
const r = await this.updateOnly(documentId, id, input, options);
|
|
363
412
|
if (!r)
|
|
364
413
|
return;
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
Error.captureStackTrace(e);
|
|
370
|
-
throw e;
|
|
371
|
-
}
|
|
414
|
+
const out = await this._findById(documentId, id, options);
|
|
415
|
+
if (out)
|
|
416
|
+
return out;
|
|
417
|
+
throw new common_1.ResourceNotAvailableError(this.getResourceName() + '.' + this.arrayKey, documentId + '/' + id);
|
|
372
418
|
}
|
|
373
419
|
/**
|
|
374
420
|
* Update an array element with new data. Returns 1 if document updated 0 otherwise.
|
|
@@ -380,19 +426,29 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
380
426
|
* @returns {Promise<number>} - A promise that resolves to the number of elements updated.
|
|
381
427
|
*/
|
|
382
428
|
async updateOnly(documentId, id, input, options) {
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
429
|
+
const info = {
|
|
430
|
+
crud: 'update',
|
|
431
|
+
method: 'update',
|
|
432
|
+
documentId,
|
|
433
|
+
itemId: id,
|
|
434
|
+
options
|
|
435
|
+
};
|
|
436
|
+
return this._intercept(async () => {
|
|
437
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
438
|
+
await this._getDocumentFilter(info)
|
|
439
|
+
]);
|
|
440
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
441
|
+
await this._getArrayFilter(info),
|
|
442
|
+
options?.filter
|
|
443
|
+
]);
|
|
444
|
+
return this._updateOnly(documentId, id, input, { ...options, filter, documentFilter });
|
|
445
|
+
}, info);
|
|
446
|
+
}
|
|
447
|
+
async _updateOnly(documentId, id, input, options) {
|
|
392
448
|
let filter = mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, [this.arrayKey]);
|
|
393
449
|
if (options?.filter)
|
|
394
450
|
filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([filter, options?.filter]);
|
|
395
|
-
return await this.
|
|
451
|
+
return await this._updateMany(documentId, input, { ...options, filter });
|
|
396
452
|
}
|
|
397
453
|
/**
|
|
398
454
|
* Updates multiple array elements in document
|
|
@@ -403,56 +459,48 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
403
459
|
* @returns {Promise<number>} - A promise that resolves to the number of documents updated.
|
|
404
460
|
*/
|
|
405
461
|
async updateMany(documentId, input, options) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
462
|
+
const info = {
|
|
463
|
+
crud: 'update',
|
|
464
|
+
method: 'updateMany',
|
|
465
|
+
documentId,
|
|
466
|
+
input,
|
|
467
|
+
options
|
|
468
|
+
};
|
|
469
|
+
return this._intercept(async () => {
|
|
470
|
+
const documentFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
471
|
+
await this._getDocumentFilter(info)
|
|
472
|
+
]);
|
|
473
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
474
|
+
await this._getArrayFilter(info),
|
|
475
|
+
options?.filter
|
|
476
|
+
]);
|
|
477
|
+
return this._updateMany(documentId, input, { ...options, filter, documentFilter });
|
|
478
|
+
}, info);
|
|
479
|
+
}
|
|
480
|
+
async _updateMany(documentId, input, options) {
|
|
414
481
|
const encode = this.getEncoder('update');
|
|
415
482
|
const doc = encode(input, { coerce: true });
|
|
416
483
|
if (!Object.keys(doc).length)
|
|
417
484
|
return 0;
|
|
418
485
|
const matchFilter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
419
486
|
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(documentId, [this.collectionKey]),
|
|
420
|
-
|
|
487
|
+
options?.documentFilter,
|
|
421
488
|
{ [this.fieldName]: { $exists: true } }
|
|
422
489
|
]);
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
const elemMatch = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter, contextArrayFilter], { fieldPrefix: 'elem.' });
|
|
490
|
+
if (options?.filter) {
|
|
491
|
+
const elemMatch = mongo_adapter_js_1.MongoAdapter.prepareFilter([options?.filter], { fieldPrefix: 'elem.' });
|
|
426
492
|
options = options || {};
|
|
427
493
|
options.arrayFilters = [elemMatch];
|
|
428
494
|
}
|
|
429
495
|
const update = mongo_adapter_js_1.MongoAdapter.preparePatch(doc, {
|
|
430
|
-
fieldPrefix: this.fieldName + (
|
|
496
|
+
fieldPrefix: this.fieldName + (options?.filter ? '.$[elem].' : '.$[].')
|
|
431
497
|
});
|
|
432
498
|
const r = await this.__updateOne(matchFilter, update, options);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
* Updates multiple elements and returns the count of elements that were updated.
|
|
437
|
-
*
|
|
438
|
-
* @param {AnyId} documentId - The ID of the document to update.
|
|
439
|
-
* @param {PatchDTO<T>} input - The partial document to update with.
|
|
440
|
-
* @param {MongoArrayService.UpdateManyOptions<T>} [options] - The options for updating multiple documents.
|
|
441
|
-
* @return {Promise<number>} A promise that resolves to the number of elements updated.
|
|
442
|
-
*/
|
|
443
|
-
async updateManyReturnCount(documentId, input, options) {
|
|
444
|
-
if (this.$interceptor && !options?.__interceptor__)
|
|
445
|
-
return this.$interceptor(() => this.updateManyReturnCount(documentId, input, { ...options, __interceptor__: true }), {
|
|
446
|
-
crud: 'update',
|
|
447
|
-
method: 'updateManyReturnCount',
|
|
448
|
-
documentId,
|
|
449
|
-
input,
|
|
450
|
-
options
|
|
451
|
-
}, this);
|
|
452
|
-
const r = await this.updateMany(documentId, input, options);
|
|
453
|
-
return r
|
|
499
|
+
if (!options?.count)
|
|
500
|
+
return r.modifiedCount;
|
|
501
|
+
return r.modifiedCount
|
|
454
502
|
// Count matching items that fits filter criteria
|
|
455
|
-
? await this.
|
|
503
|
+
? await this._count(documentId, options)
|
|
456
504
|
: 0;
|
|
457
505
|
}
|
|
458
506
|
/**
|
|
@@ -486,9 +534,9 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
486
534
|
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
487
535
|
* that resolves to the common filter, or undefined if not available.
|
|
488
536
|
*/
|
|
489
|
-
_getDocumentFilter() {
|
|
537
|
+
_getDocumentFilter(args) {
|
|
490
538
|
return typeof this.$documentFilter === 'function' ?
|
|
491
|
-
this.$documentFilter(this) : this.$documentFilter;
|
|
539
|
+
this.$documentFilter(args, this) : this.$documentFilter;
|
|
492
540
|
}
|
|
493
541
|
/**
|
|
494
542
|
* Retrieves the common filter used for querying array elements.
|
|
@@ -498,9 +546,14 @@ class MongoArrayService extends mongo_service_js_1.MongoService {
|
|
|
498
546
|
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
499
547
|
* that resolves to the common filter, or undefined if not available.
|
|
500
548
|
*/
|
|
501
|
-
_getArrayFilter() {
|
|
549
|
+
_getArrayFilter(args) {
|
|
502
550
|
return typeof this.$arrayFilter === 'function' ?
|
|
503
|
-
this.$arrayFilter(this) : this.$arrayFilter;
|
|
551
|
+
this.$arrayFilter(args, this) : this.$arrayFilter;
|
|
552
|
+
}
|
|
553
|
+
async _intercept(callback, args) {
|
|
554
|
+
if (this.$interceptor)
|
|
555
|
+
return this.$interceptor(callback, args, this);
|
|
556
|
+
return callback();
|
|
504
557
|
}
|
|
505
558
|
}
|
|
506
559
|
exports.MongoArrayService = MongoArrayService;
|