@opra/mongodb 0.33.3 → 0.33.5
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 +9 -0
- package/cjs/mongo-collection-service.js +84 -22
- package/cjs/mongo-service.js +71 -66
- package/esm/mongo-array-service.js +9 -0
- package/esm/mongo-collection-service.js +84 -22
- package/esm/mongo-service.js +71 -66
- package/package.json +3 -3
- package/types/mongo-array-service.d.ts +17 -10
- package/types/mongo-collection-service.d.ts +41 -25
- package/types/mongo-service.d.ts +53 -70
|
@@ -47,6 +47,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
47
47
|
const info = {
|
|
48
48
|
crud: 'create',
|
|
49
49
|
method: 'create',
|
|
50
|
+
byId: false,
|
|
50
51
|
documentId: input._id,
|
|
51
52
|
input,
|
|
52
53
|
options
|
|
@@ -82,6 +83,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
82
83
|
const info = {
|
|
83
84
|
crud: 'read',
|
|
84
85
|
method: 'count',
|
|
86
|
+
byId: true,
|
|
85
87
|
options
|
|
86
88
|
};
|
|
87
89
|
return this._intercept(async () => {
|
|
@@ -107,6 +109,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
107
109
|
const info = {
|
|
108
110
|
crud: 'delete',
|
|
109
111
|
method: 'delete',
|
|
112
|
+
byId: false,
|
|
110
113
|
documentId: id,
|
|
111
114
|
options
|
|
112
115
|
};
|
|
@@ -135,7 +138,8 @@ export class MongoCollectionService extends MongoService {
|
|
|
135
138
|
async deleteMany(options) {
|
|
136
139
|
const info = {
|
|
137
140
|
crud: 'delete',
|
|
138
|
-
method: '
|
|
141
|
+
method: 'deleteMany',
|
|
142
|
+
byId: false,
|
|
139
143
|
options
|
|
140
144
|
};
|
|
141
145
|
return this._intercept(async () => {
|
|
@@ -151,6 +155,25 @@ export class MongoCollectionService extends MongoService {
|
|
|
151
155
|
const r = await this.__deleteMany(filter, omit(options, 'filter'));
|
|
152
156
|
return r.deletedCount;
|
|
153
157
|
}
|
|
158
|
+
async distinct(field, options) {
|
|
159
|
+
const info = {
|
|
160
|
+
crud: 'read',
|
|
161
|
+
method: 'distinct',
|
|
162
|
+
byId: true,
|
|
163
|
+
options
|
|
164
|
+
};
|
|
165
|
+
return this._intercept(async () => {
|
|
166
|
+
const filter = MongoAdapter.prepareFilter([
|
|
167
|
+
await this._getDocumentFilter(info),
|
|
168
|
+
options?.filter,
|
|
169
|
+
]);
|
|
170
|
+
return this._distinct(field, { ...options, filter });
|
|
171
|
+
}, info);
|
|
172
|
+
}
|
|
173
|
+
async _distinct(field, options) {
|
|
174
|
+
const filter = MongoAdapter.prepareFilter(options?.filter);
|
|
175
|
+
return await this.__distinct(field, filter, omit(options, 'filter'));
|
|
176
|
+
}
|
|
154
177
|
/**
|
|
155
178
|
* Checks if an object with the given id exists.
|
|
156
179
|
*
|
|
@@ -181,6 +204,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
181
204
|
const info = {
|
|
182
205
|
crud: 'read',
|
|
183
206
|
method: 'findById',
|
|
207
|
+
byId: false,
|
|
184
208
|
documentId: id,
|
|
185
209
|
options
|
|
186
210
|
};
|
|
@@ -219,6 +243,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
219
243
|
const info = {
|
|
220
244
|
crud: 'read',
|
|
221
245
|
method: 'findOne',
|
|
246
|
+
byId: false,
|
|
222
247
|
options
|
|
223
248
|
};
|
|
224
249
|
return this._intercept(async () => {
|
|
@@ -260,6 +285,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
260
285
|
const info = {
|
|
261
286
|
crud: 'read',
|
|
262
287
|
method: 'findMany',
|
|
288
|
+
byId: false,
|
|
263
289
|
options
|
|
264
290
|
};
|
|
265
291
|
return this._intercept(async () => {
|
|
@@ -349,6 +375,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
349
375
|
crud: 'update',
|
|
350
376
|
method: 'update',
|
|
351
377
|
documentId: id,
|
|
378
|
+
byId: true,
|
|
352
379
|
input,
|
|
353
380
|
options,
|
|
354
381
|
};
|
|
@@ -361,21 +388,33 @@ export class MongoCollectionService extends MongoService {
|
|
|
361
388
|
}, info);
|
|
362
389
|
}
|
|
363
390
|
async _update(id, input, options) {
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
const
|
|
391
|
+
const isUpdateFilter = Array.isArray(input) ||
|
|
392
|
+
!!Object.keys(input).find(x => x.startsWith('$'));
|
|
393
|
+
const isDocument = !Array.isArray(input) &&
|
|
394
|
+
!!Object.keys(input).find(x => !x.startsWith('$'));
|
|
395
|
+
if (isUpdateFilter && isDocument)
|
|
396
|
+
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
397
|
+
let update;
|
|
398
|
+
if (isDocument) {
|
|
399
|
+
const encode = this.getEncoder('update');
|
|
400
|
+
const doc = encode(input, { coerce: true });
|
|
401
|
+
update = MongoAdapter.preparePatch(doc);
|
|
402
|
+
update.$set = update.$set || {};
|
|
403
|
+
}
|
|
404
|
+
else
|
|
405
|
+
update = input;
|
|
406
|
+
const filter = MongoAdapter.prepareFilter([
|
|
407
|
+
MongoAdapter.prepareKeyValues(id, [this.collectionKey]),
|
|
408
|
+
options?.filter
|
|
409
|
+
]);
|
|
367
410
|
const mongoOptions = {
|
|
368
411
|
...options,
|
|
369
412
|
includeResultMetadata: false,
|
|
370
413
|
upsert: undefined,
|
|
371
414
|
projection: MongoAdapter.prepareProjection(this.getDataType(), options),
|
|
372
415
|
};
|
|
373
|
-
const filter = MongoAdapter.prepareFilter([
|
|
374
|
-
MongoAdapter.prepareKeyValues(id, [this.collectionKey]),
|
|
375
|
-
options?.filter
|
|
376
|
-
]);
|
|
377
416
|
const decode = this.getDecoder();
|
|
378
|
-
const out = await this.__findOneAndUpdate(filter,
|
|
417
|
+
const out = await this.__findOneAndUpdate(filter, update, mongoOptions);
|
|
379
418
|
return out ? decode(out, { coerce: true }) : undefined;
|
|
380
419
|
}
|
|
381
420
|
/**
|
|
@@ -391,6 +430,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
391
430
|
crud: 'update',
|
|
392
431
|
method: 'update',
|
|
393
432
|
documentId: id,
|
|
433
|
+
byId: true,
|
|
394
434
|
input,
|
|
395
435
|
options,
|
|
396
436
|
};
|
|
@@ -403,22 +443,33 @@ export class MongoCollectionService extends MongoService {
|
|
|
403
443
|
}, info);
|
|
404
444
|
}
|
|
405
445
|
async _updateOnly(id, input, options) {
|
|
446
|
+
const isUpdateFilter = Array.isArray(input) ||
|
|
447
|
+
!!Object.keys(input).find(x => x.startsWith('$'));
|
|
448
|
+
const isDocument = !Array.isArray(input) &&
|
|
449
|
+
!!Object.keys(input).find(x => !x.startsWith('$'));
|
|
450
|
+
if (isUpdateFilter && isDocument)
|
|
451
|
+
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
452
|
+
let update;
|
|
453
|
+
if (isDocument) {
|
|
454
|
+
const encode = this.getEncoder('update');
|
|
455
|
+
const doc = encode(input, { coerce: true });
|
|
456
|
+
update = MongoAdapter.preparePatch(doc);
|
|
457
|
+
if (!Object.keys(doc).length)
|
|
458
|
+
return 0;
|
|
459
|
+
}
|
|
460
|
+
else
|
|
461
|
+
update = input;
|
|
406
462
|
const filter = MongoAdapter.prepareFilter([
|
|
407
463
|
MongoAdapter.prepareKeyValues(id, [this.collectionKey]),
|
|
408
464
|
options?.filter
|
|
409
465
|
]);
|
|
410
|
-
const encode = this.getEncoder('update');
|
|
411
|
-
const doc = encode(input, { coerce: true });
|
|
412
|
-
if (!Object.keys(doc).length)
|
|
413
|
-
return 0;
|
|
414
|
-
const patch = MongoAdapter.preparePatch(doc);
|
|
415
466
|
const mongoOptions = {
|
|
416
467
|
...options,
|
|
417
468
|
includeResultMetadata: false,
|
|
418
469
|
upsert: undefined,
|
|
419
470
|
projection: MongoAdapter.prepareProjection(this.getDataType(), options),
|
|
420
471
|
};
|
|
421
|
-
const out = await this.__updateOne(filter,
|
|
472
|
+
const out = await this.__updateOne(filter, update, mongoOptions);
|
|
422
473
|
return out.matchedCount;
|
|
423
474
|
}
|
|
424
475
|
/**
|
|
@@ -432,6 +483,7 @@ export class MongoCollectionService extends MongoService {
|
|
|
432
483
|
const info = {
|
|
433
484
|
crud: 'update',
|
|
434
485
|
method: 'updateMany',
|
|
486
|
+
byId: false,
|
|
435
487
|
input,
|
|
436
488
|
options,
|
|
437
489
|
};
|
|
@@ -444,18 +496,28 @@ export class MongoCollectionService extends MongoService {
|
|
|
444
496
|
}, info);
|
|
445
497
|
}
|
|
446
498
|
async _updateMany(input, options) {
|
|
447
|
-
const
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
499
|
+
const isUpdateFilter = Array.isArray(input) ||
|
|
500
|
+
!!Object.keys(input).find(x => x.startsWith('$'));
|
|
501
|
+
const isDocument = !Array.isArray(input) &&
|
|
502
|
+
!!Object.keys(input).find(x => !x.startsWith('$'));
|
|
503
|
+
if (isUpdateFilter && isDocument)
|
|
504
|
+
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
505
|
+
let update;
|
|
506
|
+
if (isDocument) {
|
|
507
|
+
const encode = this.getEncoder('update');
|
|
508
|
+
const doc = encode(input, { coerce: true });
|
|
509
|
+
update = MongoAdapter.preparePatch(doc);
|
|
510
|
+
if (!Object.keys(doc).length)
|
|
511
|
+
return 0;
|
|
512
|
+
}
|
|
513
|
+
else
|
|
514
|
+
update = input;
|
|
453
515
|
const mongoOptions = {
|
|
454
516
|
...omit(options, 'filter'),
|
|
455
517
|
upsert: undefined
|
|
456
518
|
};
|
|
457
519
|
const filter = MongoAdapter.prepareFilter(options?.filter);
|
|
458
|
-
const r = await this.__updateMany(filter,
|
|
520
|
+
const r = await this.__updateMany(filter, update, mongoOptions);
|
|
459
521
|
return r.matchedCount;
|
|
460
522
|
}
|
|
461
523
|
/**
|
package/esm/mongo-service.js
CHANGED
|
@@ -9,8 +9,8 @@ export class MongoService extends ApiService {
|
|
|
9
9
|
/**
|
|
10
10
|
* Constructs a new instance
|
|
11
11
|
*
|
|
12
|
-
* @param
|
|
13
|
-
* @param
|
|
12
|
+
* @param dataType - The data type of the array elements.
|
|
13
|
+
* @param [options] - The options for the array service.
|
|
14
14
|
* @constructor
|
|
15
15
|
*/
|
|
16
16
|
constructor(dataType, options) {
|
|
@@ -34,7 +34,6 @@ export class MongoService extends ApiService {
|
|
|
34
34
|
/**
|
|
35
35
|
* Retrieves the data type of the document
|
|
36
36
|
*
|
|
37
|
-
* @returns {ComplexType} The complex data type of the field.
|
|
38
37
|
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
39
38
|
*/
|
|
40
39
|
getDataType() {
|
|
@@ -43,8 +42,7 @@ export class MongoService extends ApiService {
|
|
|
43
42
|
/**
|
|
44
43
|
* Retrieves the encoder for the specified operation.
|
|
45
44
|
*
|
|
46
|
-
* @param
|
|
47
|
-
* @returns {IsObject.Validator<T>} - The encoder for the specified operation.
|
|
45
|
+
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
48
46
|
*/
|
|
49
47
|
getEncoder(operation) {
|
|
50
48
|
let encoder = this._encoders[operation];
|
|
@@ -56,8 +54,6 @@ export class MongoService extends ApiService {
|
|
|
56
54
|
}
|
|
57
55
|
/**
|
|
58
56
|
* Retrieves the decoder.
|
|
59
|
-
*
|
|
60
|
-
* @returns {IsObject.Validator<T>} - The encoder for the specified operation.
|
|
61
57
|
*/
|
|
62
58
|
getDecoder() {
|
|
63
59
|
let decoder = this._decoder;
|
|
@@ -70,9 +66,8 @@ export class MongoService extends ApiService {
|
|
|
70
66
|
/**
|
|
71
67
|
* Executes the provided function within a transaction.
|
|
72
68
|
*
|
|
73
|
-
* @param
|
|
74
|
-
* @param
|
|
75
|
-
* @returns {Promise<any>} - A promise that resolves with the result of the function execution within the transaction.
|
|
69
|
+
* @param callback - The function to be executed within the transaction.
|
|
70
|
+
* @param [options] - Optional options for the transaction.
|
|
76
71
|
*/
|
|
77
72
|
async withTransaction(callback, options) {
|
|
78
73
|
let session = this.getSession();
|
|
@@ -101,12 +96,12 @@ export class MongoService extends ApiService {
|
|
|
101
96
|
}
|
|
102
97
|
finally {
|
|
103
98
|
// Restore old session property
|
|
104
|
-
if (hasOldSession)
|
|
99
|
+
if (hasOldSession)
|
|
105
100
|
this.session = oldSessionGetter;
|
|
106
|
-
await session.endSession();
|
|
107
|
-
}
|
|
108
101
|
else
|
|
109
102
|
delete this.session;
|
|
103
|
+
if (!oldInTransaction)
|
|
104
|
+
await session.endSession();
|
|
110
105
|
}
|
|
111
106
|
}
|
|
112
107
|
/**
|
|
@@ -114,9 +109,8 @@ export class MongoService extends ApiService {
|
|
|
114
109
|
* one will be added to each of the documents missing it by the driver, mutating the document. This behavior
|
|
115
110
|
* can be overridden by setting the **forceServerObjectId** flag.
|
|
116
111
|
*
|
|
117
|
-
* @param
|
|
118
|
-
* @param
|
|
119
|
-
* @returns {Promise<mongodb.InsertOneWriteOpResult<mongodb.OptionalId<T>>>} - A promise that resolves with the result of the insert operation.
|
|
112
|
+
* @param doc - The document to insert
|
|
113
|
+
* @param options - Optional settings for the command
|
|
120
114
|
* @protected
|
|
121
115
|
*/
|
|
122
116
|
async __insertOne(doc, options) {
|
|
@@ -137,9 +131,8 @@ export class MongoService extends ApiService {
|
|
|
137
131
|
/**
|
|
138
132
|
* Gets the number of documents matching the filter.
|
|
139
133
|
*
|
|
140
|
-
* @param
|
|
141
|
-
* @param
|
|
142
|
-
* @returns {Promise<number>} - The number of documents matching the filter.
|
|
134
|
+
* @param filter - The filter used to match documents.
|
|
135
|
+
* @param options - The options for counting documents.
|
|
143
136
|
* @protected
|
|
144
137
|
*/
|
|
145
138
|
async __countDocuments(filter, options) {
|
|
@@ -161,9 +154,9 @@ export class MongoService extends ApiService {
|
|
|
161
154
|
/**
|
|
162
155
|
* Delete a document from a collection
|
|
163
156
|
*
|
|
164
|
-
* @param
|
|
165
|
-
* @param
|
|
166
|
-
* @
|
|
157
|
+
* @param filter - The filter used to select the document to remove
|
|
158
|
+
* @param options - Optional settings for the command
|
|
159
|
+
* @protected
|
|
167
160
|
*/
|
|
168
161
|
async __deleteOne(filter, options) {
|
|
169
162
|
const db = this.getDatabase();
|
|
@@ -181,12 +174,10 @@ export class MongoService extends ApiService {
|
|
|
181
174
|
}
|
|
182
175
|
}
|
|
183
176
|
/**
|
|
184
|
-
*
|
|
177
|
+
* Delete multiple documents from a collection
|
|
185
178
|
*
|
|
186
|
-
* @param
|
|
187
|
-
*
|
|
188
|
-
* @param {mongodb.DeleteOptions} [options] - The options for the delete operation.
|
|
189
|
-
* @returns {Promise<mongodb.DeleteResult>} A promise that resolves with the delete result object.
|
|
179
|
+
* @param filter - The filter used to select the documents to remove
|
|
180
|
+
* @param options - Optional settings for the command
|
|
190
181
|
* @protected
|
|
191
182
|
*/
|
|
192
183
|
async __deleteMany(filter, options) {
|
|
@@ -205,12 +196,33 @@ export class MongoService extends ApiService {
|
|
|
205
196
|
}
|
|
206
197
|
}
|
|
207
198
|
/**
|
|
208
|
-
*
|
|
209
|
-
*
|
|
199
|
+
* Gets the number of documents matching the filter.
|
|
200
|
+
*
|
|
201
|
+
* @param field - Field of the document to find distinct values for
|
|
202
|
+
* @param filter - The filter for filtering the set of documents to which we apply the distinct filter.
|
|
203
|
+
* @param options - Optional settings for the command
|
|
204
|
+
* @protected
|
|
205
|
+
*/
|
|
206
|
+
async __distinct(field, filter, options) {
|
|
207
|
+
const db = this.getDatabase();
|
|
208
|
+
const collection = await this.getCollection(db);
|
|
209
|
+
options = {
|
|
210
|
+
...options,
|
|
211
|
+
session: options?.session || this.getSession()
|
|
212
|
+
};
|
|
213
|
+
try {
|
|
214
|
+
return await collection.distinct(field, filter || {}, options);
|
|
215
|
+
}
|
|
216
|
+
catch (e) {
|
|
217
|
+
await this.$onError?.(e, this);
|
|
218
|
+
throw e;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2
|
|
210
223
|
*
|
|
211
|
-
* @param
|
|
212
|
-
* @param
|
|
213
|
-
* @returns {Promise<mongodb.ChangeStream<T>>} - A promise that resolves to a Change Stream that represents the result of the aggregation.
|
|
224
|
+
* @param pipeline - An array of aggregation pipelines to execute
|
|
225
|
+
* @param options - Optional settings for the command
|
|
214
226
|
* @protected
|
|
215
227
|
*/
|
|
216
228
|
async __aggregate(pipeline, options) {
|
|
@@ -231,10 +243,9 @@ export class MongoService extends ApiService {
|
|
|
231
243
|
/**
|
|
232
244
|
* Fetches the first document that matches the filter
|
|
233
245
|
*
|
|
234
|
-
* @param
|
|
235
|
-
* @param
|
|
246
|
+
* @param filter - Query for find Operation
|
|
247
|
+
* @param options - Optional settings for the command
|
|
236
248
|
* @protected
|
|
237
|
-
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the first matching document, or undefined if no match is found
|
|
238
249
|
*/
|
|
239
250
|
async __findOne(filter, options) {
|
|
240
251
|
const db = this.getDatabase();
|
|
@@ -252,11 +263,11 @@ export class MongoService extends ApiService {
|
|
|
252
263
|
}
|
|
253
264
|
}
|
|
254
265
|
/**
|
|
255
|
-
* Creates a cursor for a filter that can be used to iterate over results from MongoDB
|
|
266
|
+
* Creates a cursor for a filter that can be used to iterate over results from MongoDB
|
|
256
267
|
*
|
|
257
|
-
* @param
|
|
258
|
-
*
|
|
259
|
-
* @
|
|
268
|
+
* @param filter - The filter predicate. If unspecified,
|
|
269
|
+
* then all documents in the collection will match the predicate
|
|
270
|
+
* @param options - Optional settings for the command
|
|
260
271
|
* @protected
|
|
261
272
|
*/
|
|
262
273
|
async __find(filter, options) {
|
|
@@ -275,13 +286,12 @@ export class MongoService extends ApiService {
|
|
|
275
286
|
}
|
|
276
287
|
}
|
|
277
288
|
/**
|
|
278
|
-
* Update a single document in a collection
|
|
289
|
+
* Update a single document in a collection
|
|
279
290
|
*
|
|
280
|
-
* @param
|
|
281
|
-
* @param
|
|
282
|
-
* @param
|
|
291
|
+
* @param filter - The filter used to select the document to update
|
|
292
|
+
* @param update - The update operations to be applied to the document
|
|
293
|
+
* @param options - Optional settings for the command
|
|
283
294
|
* @protected
|
|
284
|
-
* @returns {Promise<mongodb.UpdateResult>} - A promise that resolves to the result of the update operation.
|
|
285
295
|
*/
|
|
286
296
|
async __updateOne(filter, update, options) {
|
|
287
297
|
const db = this.getDatabase();
|
|
@@ -301,13 +311,12 @@ export class MongoService extends ApiService {
|
|
|
301
311
|
/**
|
|
302
312
|
* Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.
|
|
303
313
|
*
|
|
304
|
-
* @param
|
|
305
|
-
* @param
|
|
306
|
-
* @param
|
|
307
|
-
* @returns {Promise<T | null>} - A promise that resolves to the updated document or null if no document matched the filter.
|
|
314
|
+
* @param filter - The filter used to select the document to update
|
|
315
|
+
* @param update - Update operations to be performed on the document
|
|
316
|
+
* @param options - Optional settings for the command
|
|
308
317
|
* @protected
|
|
309
318
|
*/
|
|
310
|
-
async __findOneAndUpdate(filter,
|
|
319
|
+
async __findOneAndUpdate(filter, update, options) {
|
|
311
320
|
const db = this.getDatabase();
|
|
312
321
|
const collection = await this.getCollection(db);
|
|
313
322
|
const opts = {
|
|
@@ -317,7 +326,7 @@ export class MongoService extends ApiService {
|
|
|
317
326
|
session: options?.session || this.getSession(),
|
|
318
327
|
};
|
|
319
328
|
try {
|
|
320
|
-
return await collection.findOneAndUpdate(filter || {},
|
|
329
|
+
return await collection.findOneAndUpdate(filter || {}, update, opts);
|
|
321
330
|
}
|
|
322
331
|
catch (e) {
|
|
323
332
|
await this.$onError?.(e, this);
|
|
@@ -325,15 +334,14 @@ export class MongoService extends ApiService {
|
|
|
325
334
|
}
|
|
326
335
|
}
|
|
327
336
|
/**
|
|
328
|
-
* Update multiple documents in a collection
|
|
337
|
+
* Update multiple documents in a collection
|
|
329
338
|
*
|
|
330
|
-
* @param
|
|
331
|
-
* @param
|
|
332
|
-
* @param
|
|
333
|
-
* @return {Promise<mongodb.UpdateResult>} - A Promise that resolves to the result of the update operation.
|
|
339
|
+
* @param filter - The filter used to select the documents to update
|
|
340
|
+
* @param update - The update operations to be applied to the documents
|
|
341
|
+
* @param options - Optional settings for the command
|
|
334
342
|
* @protected
|
|
335
343
|
*/
|
|
336
|
-
async __updateMany(filter,
|
|
344
|
+
async __updateMany(filter, update, options) {
|
|
337
345
|
const db = this.getDatabase();
|
|
338
346
|
const collection = await this.getCollection(db);
|
|
339
347
|
options = {
|
|
@@ -342,7 +350,7 @@ export class MongoService extends ApiService {
|
|
|
342
350
|
upsert: false
|
|
343
351
|
};
|
|
344
352
|
try {
|
|
345
|
-
return await collection.updateMany(filter || {},
|
|
353
|
+
return await collection.updateMany(filter || {}, update, options);
|
|
346
354
|
}
|
|
347
355
|
catch (e) {
|
|
348
356
|
await this.$onError?.(e, this);
|
|
@@ -354,7 +362,6 @@ export class MongoService extends ApiService {
|
|
|
354
362
|
*
|
|
355
363
|
* @protected
|
|
356
364
|
*
|
|
357
|
-
* @returns {Promise<mongodb.Db>} The database connection.
|
|
358
365
|
* @throws {Error} If the context or database is not set.
|
|
359
366
|
*/
|
|
360
367
|
getDatabase() {
|
|
@@ -370,7 +377,6 @@ export class MongoService extends ApiService {
|
|
|
370
377
|
*
|
|
371
378
|
* @protected
|
|
372
379
|
*
|
|
373
|
-
* @returns {Promise<mongodb.ClientSession>} The database connection.
|
|
374
380
|
* @throws {Error} If the context or database is not set.
|
|
375
381
|
*/
|
|
376
382
|
getSession() {
|
|
@@ -381,9 +387,8 @@ export class MongoService extends ApiService {
|
|
|
381
387
|
/**
|
|
382
388
|
* Retrieves a MongoDB collection from the given database.
|
|
383
389
|
*
|
|
384
|
-
* @param
|
|
390
|
+
* @param db - The MongoDB database.
|
|
385
391
|
* @protected
|
|
386
|
-
* @returns {Promise<mongodb.Collection<T>>} A promise that resolves to the MongoDB collection.
|
|
387
392
|
*/
|
|
388
393
|
async getCollection(db) {
|
|
389
394
|
return db.collection(this.getCollectionName());
|
|
@@ -392,7 +397,7 @@ export class MongoService extends ApiService {
|
|
|
392
397
|
* Retrieves the collection name.
|
|
393
398
|
*
|
|
394
399
|
* @protected
|
|
395
|
-
* @returns
|
|
400
|
+
* @returns The collection name.
|
|
396
401
|
* @throws {Error} If the collection name is not defined.
|
|
397
402
|
*/
|
|
398
403
|
getCollectionName() {
|
|
@@ -407,7 +412,7 @@ export class MongoService extends ApiService {
|
|
|
407
412
|
* Retrieves the resource name.
|
|
408
413
|
*
|
|
409
414
|
* @protected
|
|
410
|
-
* @returns {string} The
|
|
415
|
+
* @returns {string} The resource name.
|
|
411
416
|
* @throws {Error} If the collection name is not defined.
|
|
412
417
|
*/
|
|
413
418
|
getResourceName() {
|
|
@@ -421,9 +426,9 @@ export class MongoService extends ApiService {
|
|
|
421
426
|
/**
|
|
422
427
|
* Generates an encoder for the specified operation.
|
|
423
428
|
*
|
|
424
|
-
* @param
|
|
429
|
+
* @param operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
|
|
425
430
|
* @protected
|
|
426
|
-
* @returns
|
|
431
|
+
* @returns - The generated encoder for the specified operation.
|
|
427
432
|
*/
|
|
428
433
|
_generateEncoder(operation) {
|
|
429
434
|
const dataType = this.getDataType();
|
|
@@ -438,7 +443,7 @@ export class MongoService extends ApiService {
|
|
|
438
443
|
* Generates an encoder for the specified operation.
|
|
439
444
|
*
|
|
440
445
|
* @protected
|
|
441
|
-
* @returns
|
|
446
|
+
* @returns - The generated encoder for the specified operation.
|
|
442
447
|
*/
|
|
443
448
|
_generateDecoder() {
|
|
444
449
|
const dataType = this.getDataType();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "0.33.
|
|
3
|
+
"version": "0.33.5",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -30,8 +30,8 @@
|
|
|
30
30
|
"ts-gems": "^3.1.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
|
-
"@opra/common": "^0.33.
|
|
34
|
-
"@opra/core": "^0.33.
|
|
33
|
+
"@opra/common": "^0.33.5",
|
|
34
|
+
"@opra/core": "^0.33.5",
|
|
35
35
|
"mongodb": ">=6.x.x"
|
|
36
36
|
},
|
|
37
37
|
"type": "module",
|
|
@@ -42,6 +42,7 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
42
42
|
$documentFilter?: FilterInput | ((args: {
|
|
43
43
|
crud: MongoService.CrudOp;
|
|
44
44
|
method: string;
|
|
45
|
+
byId: boolean;
|
|
45
46
|
documentId?: AnyId;
|
|
46
47
|
itemId?: AnyId;
|
|
47
48
|
input?: Record<string, any>;
|
|
@@ -55,6 +56,7 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
55
56
|
$arrayFilter?: FilterInput | ((args: {
|
|
56
57
|
crud: MongoService.CrudOp;
|
|
57
58
|
method: string;
|
|
59
|
+
byId: boolean;
|
|
58
60
|
documentId?: AnyId;
|
|
59
61
|
itemId?: AnyId;
|
|
60
62
|
input?: Record<string, any>;
|
|
@@ -63,21 +65,23 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
63
65
|
/**
|
|
64
66
|
* Interceptor function for handling callback execution with provided arguments.
|
|
65
67
|
*
|
|
66
|
-
* @param
|
|
67
|
-
* @param
|
|
68
|
-
* @param
|
|
69
|
-
* @param
|
|
70
|
-
* @param
|
|
71
|
-
* @param
|
|
72
|
-
* @param
|
|
73
|
-
* @param
|
|
74
|
-
*
|
|
75
|
-
* @
|
|
68
|
+
* @param callback - The callback function to be intercepted.
|
|
69
|
+
* @param args - The arguments object containing the following properties:
|
|
70
|
+
* @param crud - The CRUD operation type.
|
|
71
|
+
* @param method - The method name.
|
|
72
|
+
* @param byId - True if current operation affects byId records (updateMany, deleteMany etc)
|
|
73
|
+
* @param documentId - The document ID (optional).
|
|
74
|
+
* @param itemId - ID for an item. (optional).
|
|
75
|
+
* @param input - The input object (optional).
|
|
76
|
+
* @param options - Additional options (optional).
|
|
77
|
+
* @param _this - The reference to the current object.
|
|
78
|
+
* @returns - The promise that resolves to the result of the callback execution.
|
|
76
79
|
*/
|
|
77
80
|
$interceptor?: (callback: (...args: any[]) => any, args: {
|
|
78
81
|
crud: MongoService.CrudOp;
|
|
79
82
|
method: string;
|
|
80
83
|
documentId?: AnyId;
|
|
84
|
+
byId: boolean;
|
|
81
85
|
itemId?: AnyId;
|
|
82
86
|
input?: Record<string, any>;
|
|
83
87
|
options?: Record<string, any>;
|
|
@@ -263,6 +267,7 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
263
267
|
protected _getDocumentFilter(args: {
|
|
264
268
|
crud: MongoService.CrudOp;
|
|
265
269
|
method: string;
|
|
270
|
+
byId: boolean;
|
|
266
271
|
documentId?: AnyId;
|
|
267
272
|
itemId?: AnyId;
|
|
268
273
|
input?: Object;
|
|
@@ -279,6 +284,7 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
279
284
|
protected _getArrayFilter(args: {
|
|
280
285
|
crud: MongoService.CrudOp;
|
|
281
286
|
method: string;
|
|
287
|
+
byId: boolean;
|
|
282
288
|
documentId?: AnyId;
|
|
283
289
|
itemId?: AnyId;
|
|
284
290
|
input?: Object;
|
|
@@ -287,6 +293,7 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
287
293
|
protected _intercept(callback: (...args: any[]) => any, args: {
|
|
288
294
|
crud: MongoService.CrudOp;
|
|
289
295
|
method: string;
|
|
296
|
+
byId: boolean;
|
|
290
297
|
documentId?: AnyId;
|
|
291
298
|
itemId?: AnyId;
|
|
292
299
|
input?: Object;
|