@opra/mongodb 1.0.0-alpha.9 → 1.0.0-beta.2
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 +6 -7
- package/cjs/adapter-utils/prepare-sort.js +1 -1
- package/cjs/index.js +1 -0
- package/cjs/mongo-adapter.js +4 -4
- package/cjs/mongo-collection-service.js +123 -141
- package/cjs/mongo-entity-service.js +259 -131
- package/cjs/mongo-nested-service.js +416 -211
- package/cjs/mongo-service.js +79 -239
- package/cjs/mongo-singleton-service.js +101 -63
- package/esm/adapter-utils/prepare-projection.js +4 -4
- package/esm/index.js +1 -0
- package/esm/mongo-adapter.js +4 -4
- package/esm/mongo-collection-service.js +122 -141
- package/esm/mongo-entity-service.js +259 -131
- package/esm/mongo-nested-service.js +416 -211
- package/esm/mongo-service.js +79 -239
- package/esm/mongo-singleton-service.js +100 -63
- package/esm/package.json +3 -0
- package/package.json +27 -33
- package/types/adapter-utils/prepare-filter.d.ts +2 -2
- package/types/adapter-utils/prepare-projection.d.ts +1 -1
- package/types/index.d.cts +6 -0
- package/types/index.d.ts +1 -0
- package/types/mongo-adapter.d.ts +1 -1
- package/types/mongo-collection-service.d.ts +44 -64
- package/types/mongo-entity-service.d.ts +91 -53
- package/types/mongo-nested-service.d.ts +107 -43
- package/types/mongo-service.d.ts +55 -153
- package/types/mongo-singleton-service.d.ts +33 -30
|
@@ -2,14 +2,14 @@
|
|
|
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
|
/**
|
|
11
11
|
* @class MongoEntityService
|
|
12
|
-
* @template T - The type of the documents in the collection
|
|
12
|
+
* @template T - The type of the documents in the collection
|
|
13
13
|
*/
|
|
14
14
|
class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
15
15
|
/**
|
|
@@ -23,121 +23,155 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
23
23
|
super(dataType, options);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
|
-
* Creates a new document in the MongoDB collection
|
|
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(
|
|
33
|
-
const
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
31
|
+
async _create(command) {
|
|
32
|
+
const input = command.input;
|
|
33
|
+
(0, valgen_1.isNotNullish)(input, { label: 'input' });
|
|
34
|
+
(0, valgen_1.isNotNullish)(input._id, { label: 'input._id' });
|
|
35
|
+
const inputCodec = this._getInputCodec('create');
|
|
36
|
+
const document = inputCodec(input);
|
|
37
|
+
const { options } = command;
|
|
38
|
+
const db = this.getDatabase();
|
|
39
|
+
const collection = await this.getCollection(db);
|
|
40
|
+
const r = await collection.insertOne(document, {
|
|
41
|
+
...options,
|
|
42
|
+
session: options?.session || this.getSession(),
|
|
43
|
+
});
|
|
44
44
|
/* istanbul ignore next */
|
|
45
|
-
|
|
45
|
+
if (!r.insertedId) {
|
|
46
|
+
throw new common_1.InternalServerError(`Unknown error while creating document for "${this.getResourceName()}"`);
|
|
47
|
+
}
|
|
48
|
+
return document;
|
|
46
49
|
}
|
|
47
50
|
/**
|
|
48
51
|
* Returns the count of documents in the collection based on the provided options.
|
|
49
52
|
*
|
|
50
|
-
* @param {MongoEntityService.
|
|
51
|
-
* @
|
|
53
|
+
* @param {MongoEntityService.CountCommand<T>} command
|
|
54
|
+
* @protected
|
|
52
55
|
*/
|
|
53
|
-
async _count(
|
|
56
|
+
async _count(command) {
|
|
57
|
+
const { options } = command;
|
|
54
58
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
55
|
-
|
|
59
|
+
const db = this.getDatabase();
|
|
60
|
+
const collection = await this.getCollection(db);
|
|
61
|
+
return ((await collection.countDocuments(filter || {}, {
|
|
62
|
+
...options,
|
|
63
|
+
limit: undefined,
|
|
64
|
+
session: options?.session || this.getSession(),
|
|
65
|
+
})) || 0);
|
|
56
66
|
}
|
|
57
67
|
/**
|
|
58
|
-
* Deletes a document from the collection
|
|
68
|
+
* Deletes a document from the collection
|
|
59
69
|
*
|
|
60
|
-
* @param {
|
|
61
|
-
* @
|
|
62
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
70
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
71
|
+
* @protected
|
|
63
72
|
*/
|
|
64
|
-
async _delete(
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
const
|
|
68
|
-
|
|
73
|
+
async _delete(command) {
|
|
74
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
75
|
+
const { options } = command;
|
|
76
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
77
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
78
|
+
options?.filter,
|
|
79
|
+
]);
|
|
80
|
+
const db = this.getDatabase();
|
|
81
|
+
const collection = await this.getCollection(db);
|
|
82
|
+
return (await collection.deleteOne(filter || {}, {
|
|
83
|
+
...options,
|
|
84
|
+
session: options?.session || this.getSession(),
|
|
85
|
+
})).deletedCount;
|
|
69
86
|
}
|
|
70
87
|
/**
|
|
71
88
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
72
89
|
*
|
|
73
|
-
* @param {MongoEntityService.
|
|
74
|
-
* @
|
|
90
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
91
|
+
* @protected
|
|
75
92
|
*/
|
|
76
|
-
async _deleteMany(
|
|
93
|
+
async _deleteMany(command) {
|
|
94
|
+
const { options } = command;
|
|
77
95
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
78
|
-
const
|
|
79
|
-
|
|
96
|
+
const db = this.getDatabase();
|
|
97
|
+
const collection = await this.getCollection(db);
|
|
98
|
+
return (await collection.deleteMany(filter || {}, {
|
|
99
|
+
...options,
|
|
100
|
+
session: options?.session || this.getSession(),
|
|
101
|
+
})).deletedCount;
|
|
80
102
|
}
|
|
81
103
|
/**
|
|
82
|
-
* The distinct command returns a list of distinct values for the given key across a collection
|
|
83
|
-
*
|
|
84
|
-
* @param {MongoEntityService.
|
|
104
|
+
* The distinct command returns a list of distinct values for the given key across a collection
|
|
105
|
+
*
|
|
106
|
+
* @param {MongoEntityService.DistinctCommand<T>} command
|
|
85
107
|
* @protected
|
|
86
108
|
*/
|
|
87
|
-
async _distinct(
|
|
109
|
+
async _distinct(command) {
|
|
110
|
+
const { options, field } = command;
|
|
88
111
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
89
|
-
|
|
112
|
+
const db = this.getDatabase();
|
|
113
|
+
const collection = await this.getCollection(db);
|
|
114
|
+
return await collection.distinct(field, filter || {}, {
|
|
115
|
+
...options,
|
|
116
|
+
session: options?.session || this.getSession(),
|
|
117
|
+
});
|
|
90
118
|
}
|
|
91
119
|
/**
|
|
92
120
|
* Finds a document by its ID.
|
|
93
121
|
*
|
|
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.
|
|
122
|
+
* @param { MongoEntityService.FindOneCommand<T>} command
|
|
97
123
|
*/
|
|
98
|
-
async _findById(
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
124
|
+
async _findById(command) {
|
|
125
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
126
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
127
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
128
|
+
command.options?.filter,
|
|
129
|
+
]);
|
|
130
|
+
const { options } = command;
|
|
131
|
+
const db = this.getDatabase();
|
|
132
|
+
const collection = await this.getCollection(db);
|
|
133
|
+
const out = await collection.findOne(filter || {}, {
|
|
134
|
+
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
135
|
+
session: options?.session || this.getSession(),
|
|
102
136
|
projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
103
137
|
limit: undefined,
|
|
104
138
|
skip: undefined,
|
|
105
139
|
sort: undefined,
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
if (out)
|
|
140
|
+
});
|
|
141
|
+
if (out) {
|
|
142
|
+
const outputCodec = this._getOutputCodec('find');
|
|
110
143
|
return outputCodec(out);
|
|
144
|
+
}
|
|
111
145
|
}
|
|
112
146
|
/**
|
|
113
147
|
* Finds a document in the collection that matches the specified options.
|
|
114
148
|
*
|
|
115
|
-
* @param {MongoEntityService.
|
|
116
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
149
|
+
* @param {MongoEntityService.FindOneCommand<T>} command
|
|
117
150
|
*/
|
|
118
|
-
async _findOne(
|
|
151
|
+
async _findOne(command) {
|
|
152
|
+
const { options } = command;
|
|
119
153
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
120
|
-
const
|
|
154
|
+
const db = this.getDatabase();
|
|
155
|
+
const collection = await this.getCollection(db);
|
|
156
|
+
const out = await collection.findOne(filter || {}, {
|
|
121
157
|
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
158
|
+
session: options?.session || this.getSession(),
|
|
122
159
|
sort: options?.sort ? mongo_adapter_js_1.MongoAdapter.prepareSort(options.sort) : undefined,
|
|
123
160
|
projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
124
161
|
limit: undefined,
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (out)
|
|
162
|
+
});
|
|
163
|
+
if (out) {
|
|
164
|
+
const outputCodec = this._getOutputCodec('find');
|
|
129
165
|
return outputCodec(out);
|
|
166
|
+
}
|
|
130
167
|
}
|
|
131
168
|
/**
|
|
132
|
-
* Finds multiple documents in the MongoDB collection
|
|
169
|
+
* Finds multiple documents in the MongoDB collection
|
|
133
170
|
*
|
|
134
|
-
* @param {MongoEntityService.
|
|
135
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
171
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
136
172
|
*/
|
|
137
|
-
async _findMany(
|
|
138
|
-
const
|
|
139
|
-
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
140
|
-
};
|
|
173
|
+
async _findMany(command) {
|
|
174
|
+
const { options } = command;
|
|
141
175
|
const limit = options?.limit || 10;
|
|
142
176
|
const stages = [];
|
|
143
177
|
let filter;
|
|
@@ -157,11 +191,16 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
157
191
|
const projection = mongo_adapter_js_1.MongoAdapter.prepareProjection(dataType, options?.projection);
|
|
158
192
|
if (projection)
|
|
159
193
|
stages.push({ $project: projection });
|
|
160
|
-
const
|
|
194
|
+
const db = this.getDatabase();
|
|
195
|
+
const collection = await this.getCollection(db);
|
|
196
|
+
const cursor = collection.aggregate(stages, {
|
|
197
|
+
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
198
|
+
session: options?.session || this.getSession(),
|
|
199
|
+
});
|
|
161
200
|
/** Execute db command */
|
|
162
201
|
try {
|
|
163
202
|
/** Fetch the cursor and decode the result objects */
|
|
164
|
-
const outputCodec = this.
|
|
203
|
+
const outputCodec = this._getOutputCodec('find');
|
|
165
204
|
return (await cursor.toArray()).map((r) => outputCodec(r));
|
|
166
205
|
}
|
|
167
206
|
finally {
|
|
@@ -173,13 +212,10 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
173
212
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
174
213
|
* and total count that matched the given criteria
|
|
175
214
|
*
|
|
176
|
-
* @param {MongoEntityService.
|
|
177
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
215
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
178
216
|
*/
|
|
179
|
-
async _findManyWithCount(
|
|
180
|
-
const
|
|
181
|
-
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
182
|
-
};
|
|
217
|
+
async _findManyWithCount(command) {
|
|
218
|
+
const { options } = command;
|
|
183
219
|
const limit = options?.limit || 10;
|
|
184
220
|
let filter;
|
|
185
221
|
if (options?.filter)
|
|
@@ -211,11 +247,16 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
211
247
|
const projection = mongo_adapter_js_1.MongoAdapter.prepareProjection(dataType, options?.projection);
|
|
212
248
|
if (projection)
|
|
213
249
|
dataStages.push({ $project: projection });
|
|
214
|
-
const outputCodec = this.
|
|
250
|
+
const outputCodec = this._getOutputCodec('find');
|
|
215
251
|
/** Execute db command */
|
|
216
|
-
const
|
|
252
|
+
const db = this.getDatabase();
|
|
253
|
+
const collection = await this.getCollection(db);
|
|
254
|
+
const cursor = collection.aggregate(stages, {
|
|
255
|
+
...(0, lodash_omit_1.default)(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
|
|
256
|
+
session: options?.session || this.getSession(),
|
|
257
|
+
});
|
|
258
|
+
/** Fetch the cursor and decode the result objects */
|
|
217
259
|
try {
|
|
218
|
-
/** Fetch the cursor and decode the result objects */
|
|
219
260
|
const facetResult = await cursor.toArray();
|
|
220
261
|
return {
|
|
221
262
|
count: facetResult[0].count[0]?.totalMatches || 0,
|
|
@@ -228,59 +269,60 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
228
269
|
}
|
|
229
270
|
}
|
|
230
271
|
/**
|
|
231
|
-
* Updates a document with the given id in the collection
|
|
272
|
+
* Updates a document with the given id in the collection
|
|
232
273
|
*
|
|
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.
|
|
274
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
238
275
|
*/
|
|
239
|
-
async _update(
|
|
240
|
-
|
|
241
|
-
const
|
|
242
|
-
|
|
276
|
+
async _update(command) {
|
|
277
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
278
|
+
const { input, inputRaw, options } = command;
|
|
279
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
280
|
+
if (input && inputRaw) {
|
|
243
281
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
244
282
|
}
|
|
245
283
|
let update;
|
|
246
|
-
if (
|
|
247
|
-
const inputCodec = this.
|
|
284
|
+
if (input) {
|
|
285
|
+
const inputCodec = this._getInputCodec('update');
|
|
248
286
|
const doc = inputCodec(input);
|
|
249
287
|
delete doc._id;
|
|
250
288
|
update = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
|
|
251
289
|
update.$set = update.$set || {};
|
|
252
290
|
}
|
|
253
291
|
else
|
|
254
|
-
update =
|
|
255
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
256
|
-
|
|
292
|
+
update = inputRaw;
|
|
293
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
294
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
295
|
+
options?.filter,
|
|
296
|
+
]);
|
|
297
|
+
const db = this.getDatabase();
|
|
298
|
+
const collection = await this.getCollection(db);
|
|
299
|
+
const out = await collection.findOneAndUpdate(filter || {}, update, {
|
|
300
|
+
upsert: undefined,
|
|
257
301
|
...options,
|
|
302
|
+
returnDocument: 'after',
|
|
258
303
|
includeResultMetadata: false,
|
|
259
|
-
|
|
304
|
+
session: options?.session || this.getSession(),
|
|
260
305
|
projection: mongo_adapter_js_1.MongoAdapter.prepareProjection(this.dataType, options?.projection),
|
|
261
|
-
};
|
|
262
|
-
const
|
|
263
|
-
const outputCodec = this.getOutputCodec('update');
|
|
306
|
+
});
|
|
307
|
+
const outputCodec = this._getOutputCodec('update');
|
|
264
308
|
if (out)
|
|
265
309
|
return outputCodec(out);
|
|
266
310
|
}
|
|
267
311
|
/**
|
|
268
312
|
* Updates a document in the collection with the specified ID.
|
|
269
313
|
*
|
|
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.
|
|
314
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
274
315
|
*/
|
|
275
|
-
async _updateOnly(
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
316
|
+
async _updateOnly(command) {
|
|
317
|
+
(0, valgen_1.isNotNullish)(command.documentId, { label: 'documentId' });
|
|
318
|
+
const { input, inputRaw, options } = command;
|
|
319
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
320
|
+
if (input && inputRaw) {
|
|
279
321
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
280
322
|
}
|
|
281
323
|
let update;
|
|
282
|
-
if (
|
|
283
|
-
const inputCodec = this.
|
|
324
|
+
if (input) {
|
|
325
|
+
const inputCodec = this._getInputCodec('update');
|
|
284
326
|
const doc = inputCodec(input);
|
|
285
327
|
delete doc._id;
|
|
286
328
|
update = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
|
|
@@ -288,33 +330,34 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
288
330
|
return 0;
|
|
289
331
|
}
|
|
290
332
|
else
|
|
291
|
-
update =
|
|
292
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
293
|
-
|
|
333
|
+
update = inputRaw;
|
|
334
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
|
|
335
|
+
mongo_adapter_js_1.MongoAdapter.prepareKeyValues(command.documentId, ['_id']),
|
|
336
|
+
options?.filter,
|
|
337
|
+
]);
|
|
338
|
+
const db = this.getDatabase();
|
|
339
|
+
const collection = await this.getCollection(db);
|
|
340
|
+
return (await collection.updateOne(filter || {}, update, {
|
|
294
341
|
...options,
|
|
295
|
-
|
|
342
|
+
session: options?.session || this.getSession(),
|
|
296
343
|
upsert: undefined,
|
|
297
|
-
|
|
298
|
-
};
|
|
299
|
-
const out = await this._dbUpdateOne(filter, update, mongoOptions);
|
|
300
|
-
return out.matchedCount;
|
|
344
|
+
})).matchedCount;
|
|
301
345
|
}
|
|
302
346
|
/**
|
|
303
347
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
304
348
|
*
|
|
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.
|
|
349
|
+
* @param {MongoEntityService.UpdateManyCommand<T>} command
|
|
308
350
|
*/
|
|
309
|
-
async _updateMany(
|
|
310
|
-
|
|
311
|
-
const
|
|
312
|
-
|
|
351
|
+
async _updateMany(command) {
|
|
352
|
+
(0, valgen_1.isNotNullish)(command.input, { label: 'input' });
|
|
353
|
+
const { input, inputRaw, options } = command;
|
|
354
|
+
(0, valgen_1.isNotNullish)(input || inputRaw, { label: 'input' });
|
|
355
|
+
if (input && inputRaw) {
|
|
313
356
|
throw new TypeError('You must pass one of MongoDB UpdateFilter or a partial document, not both');
|
|
314
357
|
}
|
|
315
358
|
let update;
|
|
316
|
-
if (
|
|
317
|
-
const inputCodec = this.
|
|
359
|
+
if (input) {
|
|
360
|
+
const inputCodec = this._getInputCodec('update');
|
|
318
361
|
const doc = inputCodec(input);
|
|
319
362
|
delete doc._id;
|
|
320
363
|
update = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
|
|
@@ -322,14 +365,99 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
|
|
|
322
365
|
return 0;
|
|
323
366
|
}
|
|
324
367
|
else
|
|
325
|
-
update =
|
|
326
|
-
const mongoOptions = {
|
|
327
|
-
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
328
|
-
upsert: undefined,
|
|
329
|
-
};
|
|
368
|
+
update = inputRaw;
|
|
330
369
|
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter(options?.filter);
|
|
331
|
-
const
|
|
332
|
-
|
|
370
|
+
const db = this.getDatabase();
|
|
371
|
+
const collection = await this.getCollection(db);
|
|
372
|
+
return (await collection.updateMany(filter || {}, update, {
|
|
373
|
+
...(0, lodash_omit_1.default)(options, 'filter'),
|
|
374
|
+
session: options?.session || this.getSession(),
|
|
375
|
+
upsert: false,
|
|
376
|
+
})).matchedCount;
|
|
377
|
+
}
|
|
378
|
+
async _executeCommand(command, commandFn) {
|
|
379
|
+
try {
|
|
380
|
+
const result = await super._executeCommand(command, async () => {
|
|
381
|
+
/** Call before[X] hooks */
|
|
382
|
+
if (command.crud === 'create')
|
|
383
|
+
await this._beforeCreate(command);
|
|
384
|
+
else if (command.crud === 'update' && command.byId) {
|
|
385
|
+
await this._beforeUpdate(command);
|
|
386
|
+
}
|
|
387
|
+
else if (command.crud === 'update' && !command.byId) {
|
|
388
|
+
await this._beforeUpdateMany(command);
|
|
389
|
+
}
|
|
390
|
+
else if (command.crud === 'delete' && command.byId) {
|
|
391
|
+
await this._beforeDelete(command);
|
|
392
|
+
}
|
|
393
|
+
else if (command.crud === 'delete' && !command.byId) {
|
|
394
|
+
await this._beforeDeleteMany(command);
|
|
395
|
+
}
|
|
396
|
+
/** Call command function */
|
|
397
|
+
return commandFn();
|
|
398
|
+
});
|
|
399
|
+
/** Call after[X] hooks */
|
|
400
|
+
if (command.crud === 'create')
|
|
401
|
+
await this._afterCreate(command, result);
|
|
402
|
+
else if (command.crud === 'update' && command.byId) {
|
|
403
|
+
await this._afterUpdate(command, result);
|
|
404
|
+
}
|
|
405
|
+
else if (command.crud === 'update' && !command.byId) {
|
|
406
|
+
await this._afterUpdateMany(command, result);
|
|
407
|
+
}
|
|
408
|
+
else if (command.crud === 'delete' && command.byId) {
|
|
409
|
+
await this._afterDelete(command, result);
|
|
410
|
+
}
|
|
411
|
+
else if (command.crud === 'delete' && !command.byId) {
|
|
412
|
+
await this._afterDeleteMany(command, result);
|
|
413
|
+
}
|
|
414
|
+
return result;
|
|
415
|
+
}
|
|
416
|
+
catch (e) {
|
|
417
|
+
Error.captureStackTrace(e, this._executeCommand);
|
|
418
|
+
await this.onError?.(e, this);
|
|
419
|
+
throw e;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
423
|
+
async _beforeCreate(command) {
|
|
424
|
+
// Do nothing
|
|
425
|
+
}
|
|
426
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
427
|
+
async _beforeUpdate(command) {
|
|
428
|
+
// Do nothing
|
|
429
|
+
}
|
|
430
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
431
|
+
async _beforeUpdateMany(command) {
|
|
432
|
+
// Do nothing
|
|
433
|
+
}
|
|
434
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
435
|
+
async _beforeDelete(command) {
|
|
436
|
+
// Do nothing
|
|
437
|
+
}
|
|
438
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
439
|
+
async _beforeDeleteMany(command) {
|
|
440
|
+
// Do nothing
|
|
441
|
+
}
|
|
442
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
443
|
+
async _afterCreate(command, result) {
|
|
444
|
+
// Do nothing
|
|
445
|
+
}
|
|
446
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
447
|
+
async _afterUpdate(command, result) {
|
|
448
|
+
// Do nothing
|
|
449
|
+
}
|
|
450
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
451
|
+
async _afterUpdateMany(command, affected) {
|
|
452
|
+
// Do nothing
|
|
453
|
+
}
|
|
454
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
455
|
+
async _afterDelete(command, affected) {
|
|
456
|
+
// Do nothing
|
|
457
|
+
}
|
|
458
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
459
|
+
async _afterDeleteMany(command, affected) {
|
|
460
|
+
// Do nothing
|
|
333
461
|
}
|
|
334
462
|
}
|
|
335
463
|
exports.MongoEntityService = MongoEntityService;
|