@opra/mongodb 1.20.0 → 1.22.0

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.
Files changed (43) hide show
  1. package/package.json +12 -29
  2. package/{esm/services → services}/mongo-collection-service.js +6 -0
  3. package/{esm/services → services}/mongo-nested-service.js +24 -0
  4. package/{esm/services → services}/mongo-service.js +44 -2
  5. package/{esm/services → services}/mongo-singleton-service.js +5 -0
  6. package/cjs/adapter/mongo-adapter.js +0 -127
  7. package/cjs/adapter/mongo-patch-generator.js +0 -215
  8. package/cjs/adapter/prepare-filter.js +0 -190
  9. package/cjs/adapter/prepare-key-values.js +0 -21
  10. package/cjs/adapter/prepare-projection.js +0 -58
  11. package/cjs/adapter/prepare-sort.js +0 -17
  12. package/cjs/index.js +0 -11
  13. package/cjs/package.json +0 -3
  14. package/cjs/services/mongo-collection-service.js +0 -401
  15. package/cjs/services/mongo-entity-service.js +0 -578
  16. package/cjs/services/mongo-nested-service.js +0 -913
  17. package/cjs/services/mongo-service.js +0 -282
  18. package/cjs/services/mongo-singleton-service.js +0 -213
  19. package/cjs/types.js +0 -2
  20. package/esm/package.json +0 -3
  21. package/types/index.d.cts +0 -8
  22. /package/{types/adapter → adapter}/mongo-adapter.d.ts +0 -0
  23. /package/{esm/adapter → adapter}/mongo-adapter.js +0 -0
  24. /package/{types/adapter → adapter}/mongo-patch-generator.d.ts +0 -0
  25. /package/{esm/adapter → adapter}/mongo-patch-generator.js +0 -0
  26. /package/{types/adapter → adapter}/prepare-filter.d.ts +0 -0
  27. /package/{esm/adapter → adapter}/prepare-filter.js +0 -0
  28. /package/{types/adapter → adapter}/prepare-key-values.d.ts +0 -0
  29. /package/{esm/adapter → adapter}/prepare-key-values.js +0 -0
  30. /package/{types/adapter → adapter}/prepare-projection.d.ts +0 -0
  31. /package/{esm/adapter → adapter}/prepare-projection.js +0 -0
  32. /package/{types/adapter → adapter}/prepare-sort.d.ts +0 -0
  33. /package/{esm/adapter → adapter}/prepare-sort.js +0 -0
  34. /package/{types/index.d.ts → index.d.ts} +0 -0
  35. /package/{esm/index.js → index.js} +0 -0
  36. /package/{types/services → services}/mongo-collection-service.d.ts +0 -0
  37. /package/{types/services → services}/mongo-entity-service.d.ts +0 -0
  38. /package/{esm/services → services}/mongo-entity-service.js +0 -0
  39. /package/{types/services → services}/mongo-nested-service.d.ts +0 -0
  40. /package/{types/services → services}/mongo-service.d.ts +0 -0
  41. /package/{types/services → services}/mongo-singleton-service.d.ts +0 -0
  42. /package/{types/types.d.ts → types.d.ts} +0 -0
  43. /package/{esm/types.js → types.js} +0 -0
@@ -1,401 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoCollectionService = void 0;
4
- const common_1 = require("@opra/common");
5
- const mongo_adapter_js_1 = require("../adapter/mongo-adapter.js");
6
- const mongo_entity_service_js_1 = require("./mongo-entity-service.js");
7
- /**
8
- * @class MongoCollectionService
9
- * @template T - The type of the documents in the collection.
10
- */
11
- class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityService {
12
- /**
13
- * Constructs a new instance
14
- *
15
- * @param {Type | string} dataType - The data type of the array elements.
16
- * @param {MongoCollectionService.Options} [options] - The options for the array service.
17
- * @constructor
18
- */
19
- constructor(dataType, options) {
20
- super(dataType, options);
21
- this.defaultLimit = options?.defaultLimit || 10;
22
- }
23
- /**
24
- * Asserts the existence of a resource with the given ID.
25
- * Throws a ResourceNotFoundError if the resource does not exist.
26
- *
27
- * @param {MongoAdapter.AnyId} id - The ID of the resource to assert.
28
- * @param {MongoEntityService.ExistsOptions<T>} [options] - Optional options for checking the existence.
29
- * @returns {Promise<void>} - A Promise that resolves when the resource exists.
30
- * @throws {ResourceNotAvailableError} - If the resource does not exist.
31
- */
32
- async assert(id, options) {
33
- if (!(await this.exists(id, options)))
34
- throw new common_1.ResourceNotAvailableError(this.getResourceName(), id);
35
- }
36
- async create(input, options) {
37
- const command = {
38
- crud: 'create',
39
- method: 'create',
40
- byId: false,
41
- input,
42
- options,
43
- };
44
- input._id =
45
- input._id == null || input._id === ''
46
- ? this._generateId(command)
47
- : input._id;
48
- return this._executeCommand(command, async () => {
49
- const r = await this._create(command);
50
- const findCommand = {
51
- ...command,
52
- crud: 'read',
53
- byId: true,
54
- documentId: r._id,
55
- options,
56
- };
57
- const out = await this._findById(findCommand);
58
- if (out)
59
- return out;
60
- });
61
- }
62
- /**
63
- * Returns the count of documents in the collection based on the provided options.
64
- *
65
- * @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
66
- * @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
67
- */
68
- async count(options) {
69
- const command = {
70
- crud: 'read',
71
- method: 'count',
72
- byId: false,
73
- options,
74
- };
75
- return this._executeCommand(command, async () => {
76
- const documentFilter = await this._getDocumentFilter(command);
77
- if (documentFilter) {
78
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
79
- documentFilter,
80
- command.options?.filter,
81
- ]);
82
- command.options = { ...command.options, filter };
83
- }
84
- return this._count(command);
85
- });
86
- }
87
- /**
88
- * Deletes a document from the collection.
89
- *
90
- * @param {MongoAdapter.AnyId} id - The ID of the document to delete.
91
- * @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
92
- * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
93
- */
94
- async delete(id, options) {
95
- const command = {
96
- crud: 'delete',
97
- method: 'delete',
98
- byId: true,
99
- documentId: id,
100
- options,
101
- };
102
- return this._executeCommand(command, async () => {
103
- const documentFilter = await this._getDocumentFilter(command);
104
- if (documentFilter) {
105
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
106
- documentFilter,
107
- command.options?.filter,
108
- ]);
109
- command.options = { ...command.options, filter };
110
- }
111
- return this._delete(command);
112
- });
113
- }
114
- /**
115
- * Deletes multiple documents from the collection that meet the specified filter criteria.
116
- *
117
- * @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
118
- * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
119
- */
120
- async deleteMany(options) {
121
- const command = {
122
- crud: 'delete',
123
- method: 'deleteMany',
124
- byId: false,
125
- options,
126
- };
127
- return this._executeCommand(command, async () => {
128
- const documentFilter = await this._getDocumentFilter(command);
129
- if (documentFilter) {
130
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
131
- documentFilter,
132
- command.options?.filter,
133
- ]);
134
- command.options = { ...command.options, filter };
135
- }
136
- return this._deleteMany(command);
137
- });
138
- }
139
- /**
140
- * The distinct command returns a list of distinct values for the given key across a collection.
141
- * @param {string} field
142
- * @param {MongoEntityService.DistinctOptions<T>} [options]
143
- * @protected
144
- */
145
- async distinct(field, options) {
146
- const command = {
147
- crud: 'read',
148
- method: 'distinct',
149
- byId: false,
150
- field,
151
- options,
152
- };
153
- return this._executeCommand(command, async () => {
154
- const documentFilter = await this._getDocumentFilter(command);
155
- if (documentFilter) {
156
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
157
- documentFilter,
158
- command.options?.filter,
159
- ]);
160
- command.options = { ...command.options, filter };
161
- }
162
- return this._distinct(command);
163
- });
164
- }
165
- /**
166
- * Checks if an object with the given id exists.
167
- *
168
- * @param {MongoAdapter.AnyId} id - The id of the object to check.
169
- * @param {MongoEntityService.ExistsOptions<T>} [options] - The options for the query (optional).
170
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
171
- */
172
- async exists(id, options) {
173
- const command = {
174
- crud: 'read',
175
- method: 'exists',
176
- byId: true,
177
- documentId: id,
178
- options,
179
- };
180
- return this._executeCommand(command, async () => {
181
- const findCommand = command;
182
- const documentFilter = await this._getDocumentFilter(command);
183
- if (documentFilter) {
184
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
185
- documentFilter,
186
- command.options?.filter,
187
- ]);
188
- findCommand.options = {
189
- ...command.options,
190
- filter,
191
- projection: ['_id'],
192
- };
193
- }
194
- return !!(await this._findById(findCommand));
195
- });
196
- }
197
- /**
198
- * Checks if an object with the given arguments exists.
199
- *
200
- * @param {MongoEntityService.ExistsOptions} [options] - The options for the query (optional).
201
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
202
- */
203
- async existsOne(options) {
204
- return !!(await this.findOne({ ...options, projection: ['_id'] }));
205
- }
206
- async findById(id, options) {
207
- const command = {
208
- crud: 'read',
209
- method: 'findById',
210
- byId: true,
211
- documentId: id,
212
- options,
213
- };
214
- return this._executeCommand(command, async () => {
215
- const documentFilter = await this._getDocumentFilter(command);
216
- if (documentFilter) {
217
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
218
- documentFilter,
219
- command.options?.filter,
220
- ]);
221
- command.options = { ...command.options, filter };
222
- }
223
- return this._findById(command);
224
- });
225
- }
226
- async findOne(options) {
227
- const command = {
228
- crud: 'read',
229
- method: 'findOne',
230
- byId: false,
231
- options,
232
- };
233
- return this._executeCommand(command, async () => {
234
- const documentFilter = await this._getDocumentFilter(command);
235
- if (documentFilter) {
236
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
237
- documentFilter,
238
- command.options?.filter,
239
- ]);
240
- command.options = { ...command.options, filter };
241
- }
242
- return this._findOne(command);
243
- });
244
- }
245
- async findMany(options) {
246
- const command = {
247
- crud: 'read',
248
- method: 'findMany',
249
- byId: false,
250
- options,
251
- };
252
- return this._executeCommand(command, async () => {
253
- const documentFilter = await this._getDocumentFilter(command);
254
- command.options = command.options || {};
255
- if (documentFilter) {
256
- command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
257
- documentFilter,
258
- command.options?.filter,
259
- ]);
260
- }
261
- const limit = command.options?.limit || this.defaultLimit;
262
- if (limit)
263
- command.options.limit = limit;
264
- return this._findMany(command);
265
- });
266
- }
267
- async findManyWithCount(options) {
268
- const command = {
269
- crud: 'read',
270
- method: 'findManyWithCount',
271
- byId: false,
272
- options,
273
- };
274
- return this._executeCommand(command, async () => {
275
- const documentFilter = await this._getDocumentFilter(command);
276
- command.options = command.options || {};
277
- if (documentFilter) {
278
- command.options.filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
279
- documentFilter,
280
- command.options?.filter,
281
- ]);
282
- }
283
- const limit = command.options?.limit || this.defaultLimit;
284
- if (limit)
285
- command.options.limit = limit;
286
- return this._findManyWithCount(command);
287
- });
288
- }
289
- async get(id, options) {
290
- const out = await this.findById(id, options);
291
- if (!out)
292
- throw new common_1.ResourceNotAvailableError(this.getResourceName(), id);
293
- return out;
294
- }
295
- async replace(id, input, options) {
296
- const command = {
297
- crud: 'replace',
298
- method: 'replace',
299
- documentId: id,
300
- byId: true,
301
- input,
302
- options,
303
- };
304
- input._id = id;
305
- return this._executeCommand(command, async () => {
306
- const documentFilter = await this._getDocumentFilter(command);
307
- if (documentFilter) {
308
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
309
- documentFilter,
310
- command.options?.filter,
311
- ]);
312
- command.options = { ...command.options, filter };
313
- }
314
- return await this._replace(command);
315
- });
316
- }
317
- async update(id, input, options) {
318
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
319
- const command = {
320
- crud: 'update',
321
- method: 'update',
322
- documentId: id,
323
- byId: true,
324
- input: isUpdateFilter ? undefined : input,
325
- inputRaw: isUpdateFilter ? input : undefined,
326
- options,
327
- };
328
- return this._executeCommand(command, async () => {
329
- const documentFilter = await this._getDocumentFilter(command);
330
- if (documentFilter) {
331
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
332
- documentFilter,
333
- command.options?.filter,
334
- ]);
335
- command.options = { ...command.options, filter };
336
- }
337
- return this._update(command);
338
- });
339
- }
340
- /**
341
- * Updates a document in the collection with the specified ID.
342
- *
343
- * @param {MongoAdapter.AnyId} id - The ID of the document to update.
344
- * @param {MongoPatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
345
- * @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
346
- * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
347
- */
348
- async updateOnly(id, input, options) {
349
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
350
- const command = {
351
- crud: 'update',
352
- method: 'updateOnly',
353
- documentId: id,
354
- byId: true,
355
- input: isUpdateFilter ? undefined : input,
356
- inputRaw: isUpdateFilter ? input : undefined,
357
- options,
358
- };
359
- return this._executeCommand(command, async () => {
360
- const documentFilter = await this._getDocumentFilter(command);
361
- if (documentFilter) {
362
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
363
- documentFilter,
364
- command.options?.filter,
365
- ]);
366
- command.options = { ...command.options, filter };
367
- }
368
- return this._updateOnly(command);
369
- });
370
- }
371
- /**
372
- * Updates multiple documents in the collection based on the specified input and options.
373
- *
374
- * @param {MongoPatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
375
- * @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
376
- * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
377
- */
378
- async updateMany(input, options) {
379
- const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
380
- const command = {
381
- crud: 'update',
382
- method: 'updateMany',
383
- byId: false,
384
- input: isUpdateFilter ? undefined : input,
385
- inputRaw: isUpdateFilter ? input : undefined,
386
- options,
387
- };
388
- return this._executeCommand(command, async () => {
389
- const documentFilter = await this._getDocumentFilter(command);
390
- if (documentFilter) {
391
- const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([
392
- documentFilter,
393
- command.options?.filter,
394
- ]);
395
- command.options = { ...command.options, filter };
396
- }
397
- return this._updateMany(command);
398
- });
399
- }
400
- }
401
- exports.MongoCollectionService = MongoCollectionService;