@opra/mongodb 1.0.0-alpha.17 → 1.0.0-alpha.18

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.
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = prepareFilter;
3
4
  const common_1 = require("@opra/common");
4
5
  const opMap = {
5
6
  '=': '$eq',
@@ -65,7 +66,6 @@ function prepareFilter(filters, options) {
65
66
  }
66
67
  return i ? (options?.fieldPrefix ? addPrefix(out, options.fieldPrefix) : out) : undefined;
67
68
  }
68
- exports.default = prepareFilter;
69
69
  function addPrefix(source, prefix) {
70
70
  if (typeof source !== 'object')
71
71
  return source;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = prepareKeyValues;
3
4
  const tslib_1 = require("tslib");
4
5
  const putil_isplainobject_1 = tslib_1.__importDefault(require("putil-isplainobject"));
5
6
  const defaultPrimaryKey = ['_id'];
@@ -19,4 +20,3 @@ function prepareKeyValues(keyValue, primaryKey) {
19
20
  }
20
21
  return { [primaryKey[0]]: keyValue };
21
22
  }
22
- exports.default = prepareKeyValues;
@@ -1,12 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = preparePatch;
3
4
  function preparePatch(doc, options) {
4
5
  const trg = {};
5
6
  _preparePatch(doc, trg, '', options);
6
7
  trg.$set = trg.$set || {};
7
8
  return trg;
8
9
  }
9
- exports.default = preparePatch;
10
10
  function _preparePatch(src, trg, path, options) {
11
11
  let f;
12
12
  let key;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.prepare = void 0;
3
+ exports.default = prepareProjection;
4
+ exports.prepare = prepare;
4
5
  const common_1 = require("@opra/common");
5
6
  function prepareProjection(dataType, projection) {
6
7
  if (projection && typeof projection === 'object' && !Array.isArray(projection))
@@ -11,7 +12,6 @@ function prepareProjection(dataType, projection) {
11
12
  prepare(dataType, out, projection_);
12
13
  return Object.keys(out).length ? out : undefined;
13
14
  }
14
- exports.default = prepareProjection;
15
15
  function prepare(dataType, target, projection) {
16
16
  const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
17
17
  const projectionKeys = projection && Object.keys(projection).map(x => x.toLowerCase());
@@ -50,4 +50,3 @@ function prepare(dataType, target, projection) {
50
50
  }
51
51
  }
52
52
  }
53
- exports.prepare = prepare;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = prepareSort;
3
4
  function prepareSort(sort) {
4
5
  if (!(sort && sort.length))
5
6
  return;
@@ -14,4 +15,3 @@ function prepareSort(sort) {
14
15
  });
15
16
  return out;
16
17
  }
17
- exports.default = prepareSort;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MongoCollectionService = void 0;
4
4
  const common_1 = require("@opra/common");
5
- const mongodb_1 = require("mongodb");
6
5
  const mongo_adapter_js_1 = require("./mongo-adapter.js");
7
6
  const mongo_entity_service_js_1 = require("./mongo-entity-service.js");
8
7
  /**
@@ -26,7 +25,7 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
26
25
  * Throws a ResourceNotFoundError if the resource does not exist.
27
26
  *
28
27
  * @param {MongoAdapter.AnyId} id - The ID of the resource to assert.
29
- * @param {MongoCollectionService.ExistsOptions<T>} [options] - Optional options for checking the existence.
28
+ * @param {MongoEntityService.ExistsOptions<T>} [options] - Optional options for checking the existence.
30
29
  * @returns {Promise<void>} - A Promise that resolves when the resource exists.
31
30
  * @throws {ResourceNotAvailableError} - If the resource does not exist.
32
31
  */
@@ -39,112 +38,126 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
39
38
  * Interceptors will be called before performing db operation
40
39
  *
41
40
  * @param {PartialDTO<T>} input - The input data for creating the document.
42
- * @param {MongoCollectionService.CreateOptions} [options] - The options for creating the document.
41
+ * @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
43
42
  * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
44
43
  * @throws {Error} if an unknown error occurs while creating the document.
45
44
  */
46
45
  async create(input, options) {
47
- const id = input._id || this._generateId();
48
- if (id != null)
49
- input._id = id;
50
- const info = {
46
+ const command = {
51
47
  crud: 'create',
52
48
  method: 'create',
53
49
  byId: false,
54
- documentId: id,
55
50
  input,
56
51
  options,
57
52
  };
58
- return this._intercept(() => this._create(input, options), info);
53
+ return this._executeCommand(command, () => this._create(command));
59
54
  }
60
55
  /**
61
56
  * Returns the count of documents in the collection based on the provided options.
62
57
  *
63
- * @param {MongoCollectionService.CountOptions<T>} options - The options for the count operation.
58
+ * @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
64
59
  * @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
65
60
  */
66
61
  async count(options) {
67
- const info = {
62
+ const command = {
68
63
  crud: 'read',
69
64
  method: 'count',
70
65
  byId: false,
71
66
  options,
72
67
  };
73
- return this._intercept(async () => {
74
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
75
- return this._count({ ...options, filter });
76
- }, info);
68
+ return this._executeCommand(command, async () => {
69
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
70
+ command.options = { ...command.options, filter };
71
+ return this._count(command);
72
+ });
77
73
  }
78
74
  /**
79
75
  * Deletes a document from the collection.
80
76
  *
81
77
  * @param {MongoAdapter.AnyId} id - The ID of the document to delete.
82
- * @param {MongoCollectionService.DeleteOptions<T>} [options] - Optional delete options.
78
+ * @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
83
79
  * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
84
80
  */
85
81
  async delete(id, options) {
86
- const info = {
82
+ const command = {
87
83
  crud: 'delete',
88
84
  method: 'delete',
89
85
  byId: true,
90
86
  documentId: id,
91
87
  options,
92
88
  };
93
- return this._intercept(async () => {
94
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
95
- return this._delete(id, { ...options, filter });
96
- }, info);
89
+ return this._executeCommand(command, async () => {
90
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
91
+ command.options = { ...command.options, filter };
92
+ return this._delete(command);
93
+ });
97
94
  }
98
95
  /**
99
96
  * Deletes multiple documents from the collection that meet the specified filter criteria.
100
97
  *
101
- * @param {MongoCollectionService.DeleteManyOptions<T>} options - The options for the delete operation.
98
+ * @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
102
99
  * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
103
100
  */
104
101
  async deleteMany(options) {
105
- const info = {
102
+ const command = {
106
103
  crud: 'delete',
107
104
  method: 'deleteMany',
108
105
  byId: false,
109
106
  options,
110
107
  };
111
- return this._intercept(async () => {
112
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
113
- return this._deleteMany({ ...options, filter });
114
- }, info);
108
+ return this._executeCommand(command, async () => {
109
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
110
+ command.options = { ...command.options, filter };
111
+ return this._deleteMany(command);
112
+ });
115
113
  }
116
114
  /**
117
115
  * The distinct command returns a list of distinct values for the given key across a collection.
118
116
  * @param {string} field
119
- * @param {MongoCollectionService.DistinctOptions<T>} [options]
117
+ * @param {MongoEntityService.DistinctOptions<T>} [options]
120
118
  * @protected
121
119
  */
122
120
  async distinct(field, options) {
123
- const info = {
121
+ const command = {
124
122
  crud: 'read',
125
123
  method: 'distinct',
126
- byId: true,
124
+ byId: false,
125
+ field,
127
126
  options,
128
127
  };
129
- return this._intercept(async () => {
130
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
131
- return this._distinct(field, { ...options, filter });
132
- }, info);
128
+ return this._executeCommand(command, async () => {
129
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
130
+ command.options = { ...command.options, filter };
131
+ return this._distinct(command);
132
+ });
133
133
  }
134
134
  /**
135
135
  * Checks if an object with the given id exists.
136
136
  *
137
137
  * @param {MongoAdapter.AnyId} id - The id of the object to check.
138
- * @param {MongoCollectionService.ExistsOptions<T>} [options] - The options for the query (optional).
138
+ * @param {MongoEntityService.ExistsOptions<T>} [options] - The options for the query (optional).
139
139
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
140
140
  */
141
141
  async exists(id, options) {
142
- return !!(await this.findById(id, { ...options, projection: ['_id'] }));
142
+ const command = {
143
+ crud: 'read',
144
+ method: 'exists',
145
+ byId: true,
146
+ documentId: id,
147
+ options,
148
+ };
149
+ return this._executeCommand(command, async () => {
150
+ const documentFilter = await this._getDocumentFilter(command);
151
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
152
+ const findCommand = command;
153
+ findCommand.options = { ...command.options, filter, projection: ['_id'] };
154
+ return !!(await this._findById(findCommand));
155
+ });
143
156
  }
144
157
  /**
145
158
  * Checks if an object with the given arguments exists.
146
159
  *
147
- * @param {MongoCollectionService.ExistsOneOptions} [options] - The options for the query (optional).
160
+ * @param {MongoEntityService.ExistsOneOptions} [options] - The options for the query (optional).
148
161
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
149
162
  */
150
163
  async existsOne(options) {
@@ -154,83 +167,87 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
154
167
  * Finds a document by its ID.
155
168
  *
156
169
  * @param {MongoAdapter.AnyId} id - The ID of the document.
157
- * @param {MongoCollectionService.FindOneOptions<T>} [options] - The options for the find query.
170
+ * @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
158
171
  * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
159
172
  */
160
173
  async findById(id, options) {
161
- const info = {
174
+ const command = {
162
175
  crud: 'read',
163
176
  method: 'findById',
164
177
  byId: true,
165
178
  documentId: id,
166
179
  options,
167
180
  };
168
- return this._intercept(async () => {
169
- const documentFilter = await this._getDocumentFilter(info);
170
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, options?.filter]);
171
- return this._findById(id, { ...options, filter });
172
- }, info);
181
+ return this._executeCommand(command, async () => {
182
+ const documentFilter = await this._getDocumentFilter(command);
183
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
184
+ command.options = { ...command.options, filter };
185
+ return this._findById(command);
186
+ });
173
187
  }
174
188
  /**
175
189
  * Finds a document in the collection that matches the specified options.
176
190
  *
177
- * @param {MongoCollectionService.FindOneOptions<T>} [options] - The options for the query.
191
+ * @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the query.
178
192
  * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
179
193
  */
180
194
  async findOne(options) {
181
- const info = {
195
+ const command = {
182
196
  crud: 'read',
183
197
  method: 'findOne',
184
198
  byId: false,
185
199
  options,
186
200
  };
187
- return this._intercept(async () => {
188
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
189
- return this._findOne({ ...options, filter });
190
- }, info);
201
+ return this._executeCommand(command, async () => {
202
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
203
+ command.options = { ...command.options, filter };
204
+ return this._findOne(command);
205
+ });
191
206
  }
192
207
  /**
193
208
  * Finds multiple documents in the MongoDB collection.
194
209
  *
195
- * @param {MongoCollectionService.FindManyOptions<T>} options - The options for the find operation.
210
+ * @param {MongoEntityService.FindManyOptions<T>} options - The options for the find operation.
196
211
  * @return A Promise that resolves to an array of partial outputs of type T.
197
212
  */
198
213
  async findMany(options) {
199
- const info = {
214
+ const command = {
200
215
  crud: 'read',
201
216
  method: 'findMany',
202
217
  byId: false,
203
218
  options,
204
219
  };
205
- return this._intercept(async () => {
206
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
207
- return this._findMany({ ...options, filter, limit: options?.limit || this.defaultLimit });
208
- }, info);
220
+ return this._executeCommand(command, async () => {
221
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
222
+ command.options = { ...command.options, filter, limit: command.options?.limit || this.defaultLimit };
223
+ return this._findMany(command);
224
+ });
209
225
  }
210
226
  /**
211
227
  * Finds multiple documents in the collection and returns both records (max limit)
212
228
  * and total count that matched the given criteria
213
229
  *
214
- * @param {MongoCollectionService.FindManyOptions<T>} [options] - The options for the find operation.
230
+ * @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
215
231
  * @return A Promise that resolves to an array of partial outputs of type T.
216
232
  */
217
233
  async findManyWithCount(options) {
218
- const info = {
234
+ const command = {
219
235
  crud: 'read',
220
236
  method: 'findManyWithCount',
221
237
  byId: false,
222
238
  options,
223
239
  };
224
- return this._intercept(async () => {
225
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
226
- return this._findManyWithCount({ ...options, filter, limit: options?.limit || this.defaultLimit });
227
- }, info);
240
+ return this._executeCommand(command, async () => {
241
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
242
+ command.options = { ...command.options, filter, limit: command.options?.limit || this.defaultLimit };
243
+ return this._findManyWithCount(command);
244
+ });
228
245
  }
229
246
  /**
230
247
  * Retrieves a document from the collection by its ID. Throws error if not found.
231
248
  *
232
249
  * @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
233
- * @param {MongoCollectionService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
250
+ * @param {MongoEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
234
251
  * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
235
252
  * or rejects with a ResourceNotFoundError if the document does not exist.
236
253
  * @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
@@ -246,74 +263,74 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
246
263
  *
247
264
  * @param {MongoAdapter.AnyId} id - The id of the document to update.
248
265
  * @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
249
- * @param {MongoCollectionService.UpdateOptions<T>} [options] - The options for the update operation.
266
+ * @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for the update operation.
250
267
  * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
251
268
  * undefined if the document was not found.
252
269
  */
253
270
  async update(id, input, options) {
254
- const info = {
271
+ const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
272
+ const command = {
255
273
  crud: 'update',
256
274
  method: 'update',
257
275
  documentId: id,
258
276
  byId: true,
259
- input,
277
+ input: isUpdateFilter ? undefined : input,
278
+ inputRaw: isUpdateFilter ? input : undefined,
260
279
  options,
261
280
  };
262
- return this._intercept(async () => {
263
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
264
- return this._update(id, input, { ...options, filter });
265
- }, info);
281
+ return this._executeCommand(command, async () => {
282
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
283
+ command.options = { ...command.options, filter };
284
+ return this._update(command);
285
+ });
266
286
  }
267
287
  /**
268
288
  * Updates a document in the collection with the specified ID.
269
289
  *
270
290
  * @param {MongoAdapter.AnyId} id - The ID of the document to update.
271
291
  * @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
272
- * @param {MongoCollectionService.UpdateOptions<T>} [options] - The options for updating the document.
292
+ * @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
273
293
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
274
294
  */
275
295
  async updateOnly(id, input, options) {
276
- const info = {
296
+ const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
297
+ const command = {
277
298
  crud: 'update',
278
- method: 'update',
299
+ method: 'updateOnly',
279
300
  documentId: id,
280
301
  byId: true,
281
- input,
302
+ input: isUpdateFilter ? undefined : input,
303
+ inputRaw: isUpdateFilter ? input : undefined,
282
304
  options,
283
305
  };
284
- return this._intercept(async () => {
285
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
286
- return this._updateOnly(id, input, { ...options, filter });
287
- }, info);
306
+ return this._executeCommand(command, async () => {
307
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
308
+ command.options = { ...command.options, filter };
309
+ return this._updateOnly(command);
310
+ });
288
311
  }
289
312
  /**
290
313
  * Updates multiple documents in the collection based on the specified input and options.
291
314
  *
292
315
  * @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
293
- * @param {MongoCollectionService.UpdateManyOptions<T>} options - The options for updating the documents.
316
+ * @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
294
317
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
295
318
  */
296
319
  async updateMany(input, options) {
297
- const info = {
320
+ const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
321
+ const command = {
298
322
  crud: 'update',
299
323
  method: 'updateMany',
300
324
  byId: false,
301
- input,
325
+ input: isUpdateFilter ? undefined : input,
326
+ inputRaw: isUpdateFilter ? input : undefined,
302
327
  options,
303
328
  };
304
- return this._intercept(async () => {
305
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(info), options?.filter]);
306
- return this._updateMany(input, { ...options, filter });
307
- }, info);
308
- }
309
- /**
310
- * Generates an ID.
311
- *
312
- * @protected
313
- * @returns {MongoAdapter.AnyId} The generated ID.
314
- */
315
- _generateId() {
316
- return typeof this.$idGenerator === 'function' ? this.$idGenerator(this) : new mongodb_1.ObjectId();
329
+ return this._executeCommand(command, async () => {
330
+ const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
331
+ command.options = { ...command.options, filter };
332
+ return this._updateMany(command);
333
+ });
317
334
  }
318
335
  }
319
336
  exports.MongoCollectionService = MongoCollectionService;