@opra/mongodb 1.0.0-alpha.17 → 1.0.0-alpha.19
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
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MongoEntityService = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
-
const assert = tslib_1.__importStar(require("node:assert"));
|
|
6
5
|
const common_1 = require("@opra/common");
|
|
7
6
|
const lodash_omit_1 = tslib_1.__importDefault(require("lodash.omit"));
|
|
7
|
+
const valgen_1 = require("valgen");
|
|
8
8
|
const mongo_adapter_js_1 = require("./mongo-adapter.js");
|
|
9
9
|
const mongo_service_js_1 = require("./mongo-service.js");
|
|
10
10
|
/**
|
|
@@ -25,19 +25,27 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
25
25
|
/**
|
|
26
26
|
* Creates a new document in the MongoDB collection.
|
|
27
27
|
*
|
|
28
|
-
* @param {
|
|
29
|
-
* @param {MongoEntityService.CreateOptions} options
|
|
28
|
+
* @param {MongoEntityService.CreateCommand} command
|
|
30
29
|
* @protected
|
|
31
30
|
*/
|
|
32
|
-
async _create(
|
|
31
|
+
async _create(command) {
|
|
32
|
+
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
33
|
+
(0, valgen_1.isNotNullish)(command.input._id, { label: 'input._id' });
|
|
33
34
|
const inputCodec = this.getInputCodec('create');
|
|
34
|
-
const doc = inputCodec(input);
|
|
35
|
-
|
|
35
|
+
const doc = inputCodec(command.input);
|
|
36
|
+
const { options } = command;
|
|
36
37
|
const r = await this._dbInsertOne(doc, options);
|
|
37
38
|
if (r.insertedId) {
|
|
38
|
-
if (!options)
|
|
39
|
+
if (!command.options)
|
|
39
40
|
return doc;
|
|
40
|
-
const
|
|
41
|
+
const findCommand = {
|
|
42
|
+
...command,
|
|
43
|
+
crud: 'read',
|
|
44
|
+
byId: true,
|
|
45
|
+
documentId: doc._id,
|
|
46
|
+
options: (0, lodash_omit_1.default)(options, 'filter'),
|
|
47
|
+
};
|
|
48
|
+
const out = await this._findById(findCommand);
|
|
41
49
|
if (out)
|
|
42
50
|
return out;
|
|
43
51
|
}
|
|
@@ -47,56 +55,65 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
47
55
|
/**
|
|
48
56
|
* Returns the count of documents in the collection based on the provided options.
|
|
49
57
|
*
|
|
50
|
-
* @param {MongoEntityService.
|
|
51
|
-
* @
|
|
58
|
+
* @param {MongoEntityService.CountCommand<T>} command
|
|
59
|
+
* @protected
|
|
52
60
|
*/
|
|
53
|
-
async _count(
|
|
61
|
+
async _count(command) {
|
|
62
|
+
const { options } = command;
|
|
54
63
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
55
64
|
return this._dbCountDocuments(filter, (0, lodash_omit_1.default)(options, 'filter'));
|
|
56
65
|
}
|
|
57
66
|
/**
|
|
58
67
|
* Deletes a document from the collection.
|
|
59
68
|
*
|
|
60
|
-
* @param {
|
|
61
|
-
* @
|
|
62
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
69
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
70
|
+
* @protected
|
|
63
71
|
*/
|
|
64
|
-
async _delete(
|
|
65
|
-
|
|
66
|
-
const
|
|
72
|
+
async _delete(command) {
|
|
73
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
74
|
+
const { options } = command;
|
|
75
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
76
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
77
|
+
options?.filter,
|
|
78
|
+
]);
|
|
67
79
|
const r = await this._dbDeleteOne(filter, options);
|
|
68
80
|
return r.deletedCount;
|
|
69
81
|
}
|
|
70
82
|
/**
|
|
71
83
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
72
84
|
*
|
|
73
|
-
* @param {MongoEntityService.
|
|
74
|
-
* @
|
|
85
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
86
|
+
* @protected
|
|
75
87
|
*/
|
|
76
|
-
async _deleteMany(
|
|
88
|
+
async _deleteMany(command) {
|
|
89
|
+
const { options } = command;
|
|
77
90
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
78
91
|
const r = await this._dbDeleteMany(filter, (0, lodash_omit_1.default)(options, 'filter'));
|
|
79
92
|
return r.deletedCount;
|
|
80
93
|
}
|
|
81
94
|
/**
|
|
82
95
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
83
|
-
*
|
|
84
|
-
* @param {MongoEntityService.
|
|
96
|
+
*
|
|
97
|
+
* @param {MongoEntityService.DistinctCommand<T>} command
|
|
85
98
|
* @protected
|
|
86
99
|
*/
|
|
87
|
-
async _distinct(
|
|
100
|
+
async _distinct(command) {
|
|
101
|
+
const { options, field } = command;
|
|
88
102
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
89
103
|
return await this._dbDistinct(field, filter, (0, lodash_omit_1.default)(options, 'filter'));
|
|
90
104
|
}
|
|
91
105
|
/**
|
|
92
106
|
* Finds a document by its ID.
|
|
93
107
|
*
|
|
94
|
-
* @param {
|
|
95
|
-
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
96
|
-
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
108
|
+
* @param { MongoEntityService.FindOneCommand<T>} command
|
|
97
109
|
*/
|
|
98
|
-
async _findById(
|
|
99
|
-
|
|
110
|
+
async _findById(command) {
|
|
111
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
112
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
113
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
114
|
+
command.options?.filter,
|
|
115
|
+
]);
|
|
116
|
+
const { options } = command;
|
|
100
117
|
const mongoOptions = {
|
|
101
118
|
...options,
|
|
102
119
|
projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
@@ -112,10 +129,10 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
112
129
|
/**
|
|
113
130
|
* Finds a document in the collection that matches the specified options.
|
|
114
131
|
*
|
|
115
|
-
* @param {MongoEntityService.
|
|
116
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
132
|
+
* @param {MongoEntityService.FindOneCommand<T>} command
|
|
117
133
|
*/
|
|
118
|
-
async _findOne(
|
|
134
|
+
async _findOne(command) {
|
|
135
|
+
const { options } = command;
|
|
119
136
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
120
137
|
const mongoOptions = {
|
|
121
138
|
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
@@ -131,10 +148,10 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
131
148
|
/**
|
|
132
149
|
* Finds multiple documents in the MongoDB collection.
|
|
133
150
|
*
|
|
134
|
-
* @param {MongoEntityService.
|
|
135
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
151
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
136
152
|
*/
|
|
137
|
-
async _findMany(
|
|
153
|
+
async _findMany(command) {
|
|
154
|
+
const { options } = command;
|
|
138
155
|
const mongoOptions = {
|
|
139
156
|
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
140
157
|
};
|
|
@@ -173,10 +190,10 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
173
190
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
174
191
|
* and total count that matched the given criteria
|
|
175
192
|
*
|
|
176
|
-
* @param {MongoEntityService.
|
|
177
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
193
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
178
194
|
*/
|
|
179
|
-
async _findManyWithCount(
|
|
195
|
+
async _findManyWithCount(command) {
|
|
196
|
+
const { options } = command;
|
|
180
197
|
const mongoOptions = {
|
|
181
198
|
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
182
199
|
};
|
|
@@ -230,20 +247,17 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
230
247
|
/**
|
|
231
248
|
* Updates a document with the given id in the collection.
|
|
232
249
|
*
|
|
233
|
-
* @param {
|
|
234
|
-
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
235
|
-
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for the update operation.
|
|
236
|
-
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
237
|
-
* undefined if the document was not found.
|
|
250
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
238
251
|
*/
|
|
239
|
-
async _update(
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
|
|
252
|
+
async _update(command) {
|
|
253
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
254
|
+
const { input, inputRaw, options } = command;
|
|
255
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
256
|
+
if (input && inputRaw) {
|
|
243
257
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
244
258
|
}
|
|
245
259
|
let update;
|
|
246
|
-
if (
|
|
260
|
+
if (input) {
|
|
247
261
|
const inputCodec = this.getInputCodec('update');
|
|
248
262
|
const doc = inputCodec(input);
|
|
249
263
|
delete doc._id;
|
|
@@ -251,8 +265,11 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
251
265
|
update.$set = update.$set || {};
|
|
252
266
|
}
|
|
253
267
|
else
|
|
254
|
-
update =
|
|
255
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
268
|
+
update = inputRaw;
|
|
269
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
270
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
271
|
+
options?.filter,
|
|
272
|
+
]);
|
|
256
273
|
const mongoOptions = {
|
|
257
274
|
...options,
|
|
258
275
|
includeResultMetadata: false,
|
|
@@ -267,19 +284,17 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
267
284
|
/**
|
|
268
285
|
* Updates a document in the collection with the specified ID.
|
|
269
286
|
*
|
|
270
|
-
* @param {
|
|
271
|
-
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
272
|
-
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for updating the document.
|
|
273
|
-
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
287
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
274
288
|
*/
|
|
275
|
-
async _updateOnly(
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
289
|
+
async _updateOnly(command) {
|
|
290
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
291
|
+
const { input, inputRaw, options } = command;
|
|
292
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
293
|
+
if (input && inputRaw) {
|
|
279
294
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
280
295
|
}
|
|
281
296
|
let update;
|
|
282
|
-
if (
|
|
297
|
+
if (input) {
|
|
283
298
|
const inputCodec = this.getInputCodec('update');
|
|
284
299
|
const doc = inputCodec(input);
|
|
285
300
|
delete doc._id;
|
|
@@ -288,8 +303,11 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
288
303
|
return 0;
|
|
289
304
|
}
|
|
290
305
|
else
|
|
291
|
-
update =
|
|
292
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
306
|
+
update = inputRaw;
|
|
307
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
308
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
309
|
+
options?.filter,
|
|
310
|
+
]);
|
|
293
311
|
const mongoOptions = {
|
|
294
312
|
...options,
|
|
295
313
|
includeResultMetadata: false,
|
|
@@ -302,18 +320,17 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
302
320
|
/**
|
|
303
321
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
304
322
|
*
|
|
305
|
-
* @param {
|
|
306
|
-
* @param {MongoEntityService.UpdateManyOptions<T>} [options] - The options for updating the documents.
|
|
307
|
-
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
323
|
+
* @param {MongoEntityService.UpdateManyCommand<T>} command
|
|
308
324
|
*/
|
|
309
|
-
async _updateMany(
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
|
|
325
|
+
async _updateMany(command) {
|
|
326
|
+
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
327
|
+
const { input, inputRaw, options } = command;
|
|
328
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
329
|
+
if (input && inputRaw) {
|
|
313
330
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
314
331
|
}
|
|
315
332
|
let update;
|
|
316
|
-
if (
|
|
333
|
+
if (input) {
|
|
317
334
|
const inputCodec = this.getInputCodec('update');
|
|
318
335
|
const doc = inputCodec(input);
|
|
319
336
|
delete doc._id;
|
|
@@ -322,7 +339,7 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
322
339
|
return 0;
|
|
323
340
|
}
|
|
324
341
|
else
|
|
325
|
-
update =
|
|
342
|
+
update = inputRaw;
|
|
326
343
|
const mongoOptions = {
|
|
327
344
|
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
328
345
|
upsert: undefined,
|