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