@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.
@@ -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 {PartialDTO<T>} input
29
- * @param {MongoEntityService.CreateOptions} options
28
+ * @param {MongoEntityService.CreateCommand} command
30
29
  * @protected
31
30
  */
32
- async _create(input, options) {
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
- assert.ok(doc._id, 'You must provide the "_id" field');
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 out = await this._findById(doc._id, (0, lodash_omit_1.default)(options, 'filter'));
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.CountOptions<T>} options - The options for the count operation.
51
- * @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
58
+ * @param {MongoEntityService.CountCommand<T>} command
59
+ * @protected
52
60
  */
53
- async _count(options) {
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 {MongoAdapter.AnyId} id - The ID of the document to delete.
61
- * @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
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(id, options) {
65
- assert.ok(id, 'You must provide an id');
66
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, ['_id']), options?.filter]);
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.DeleteManyOptions<T>} options - The options for the delete operation.
74
- * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
85
+ * @param {MongoEntityService.DeleteCommand<T>} command
86
+ * @protected
75
87
  */
76
- async _deleteMany(options) {
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
- * @param {string} field
84
- * @param {MongoEntityService.DistinctOptions<T>} options
96
+ *
97
+ * @param {MongoEntityService.DistinctCommand<T>} command
85
98
  * @protected
86
99
  */
87
- async _distinct(field, options) {
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 {MongoAdapter.AnyId} id - The ID of the document.
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(id, options) {
99
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, ['_id']), options?.filter]);
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.FindOneOptions} [options] - The options for the query.
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(options) {
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.FindManyOptions<T>} [options] - The options for the find operation.
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(options) {
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.FindManyOptions<T>} [options] - The options for the find operation.
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(options) {
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 {AnyId} id - The id of the document to update.
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(id, input, options) {
240
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
241
- const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
242
- if (isUpdateFilter && isDocument) {
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 (isDocument) {
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 = input;
255
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, ['_id']), options?.filter]);
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 {MongoAdapter.AnyId} id - The ID of the document to update.
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(id, input, options) {
276
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
277
- const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
278
- if (isUpdateFilter && isDocument) {
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 (isDocument) {
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 = input;
292
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([mongo_adapter_js_1.MongoAdapter.prepareKeyValues(id, ['_id']), options?.filter]);
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 {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
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(input, options) {
310
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
311
- const isDocument = !Array.isArray(input) && !!Object.keys(input).find(x => !x.startsWith('$'));
312
- if (isUpdateFilter && isDocument) {
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 (isDocument) {
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 = input;
342
+ update = inputRaw;
326
343
  const mongoOptions = {
327
344
  ...(0, lodash_omit_1.default)(options, 'filter'),
328
345
  upsert: undefined,