@opra/mongodb 1.26.3 → 1.26.4

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.
@@ -3,42 +3,91 @@ import type { PartialDTO, StrictOmit, Type } from 'ts-gems';
3
3
  import type { MongoPatchDTO } from '../types.js';
4
4
  import { MongoService } from './mongo-service.js';
5
5
  /**
6
- *
7
- * @namespace MongoEntityService
6
+ * Options for MongoEntityService.
8
7
  */
9
8
  export declare namespace MongoEntityService {
10
9
  /**
11
- * The constructor options of MongoEntityService.
12
- *
13
- * @interface Options
14
- * @extends MongoService.Options
10
+ * Configuration options for MongoEntityService.
15
11
  */
16
12
  interface Options extends MongoService.Options {
17
13
  }
14
+ /**
15
+ * Information about the command being executed.
16
+ */
18
17
  interface CommandInfo extends MongoService.CommandInfo {
19
18
  }
20
19
  interface CreateOptions extends MongoService.CreateOptions {
21
20
  }
21
+ /**
22
+ * Options for the `count` operation.
23
+ *
24
+ * @template T - The type of the document.
25
+ */
22
26
  interface CountOptions<T> extends MongoService.CountOptions<T> {
23
27
  }
28
+ /**
29
+ * Options for the `delete` operation.
30
+ *
31
+ * @template T - The type of the document.
32
+ */
24
33
  interface DeleteOptions<T> extends MongoService.DeleteOptions<T> {
25
34
  }
35
+ /**
36
+ * Options for the `deleteMany` operation.
37
+ *
38
+ * @template T - The type of the document.
39
+ */
26
40
  interface DeleteManyOptions<T> extends MongoService.DeleteManyOptions<T> {
27
41
  }
42
+ /**
43
+ * Options for the `distinct` operation.
44
+ *
45
+ * @template T - The type of the document.
46
+ */
28
47
  interface DistinctOptions<T> extends MongoService.DistinctOptions<T> {
29
48
  }
49
+ /**
50
+ * Options for the `exists` operation.
51
+ *
52
+ * @template T - The type of the document.
53
+ */
30
54
  interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
31
55
  }
56
+ /**
57
+ * Options for the `findOne` / `findById` operations.
58
+ *
59
+ * @template T - The type of the document.
60
+ */
32
61
  interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
33
62
  }
63
+ /**
64
+ * Options for the `findMany` operation.
65
+ *
66
+ * @template T - The type of the document.
67
+ */
34
68
  interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
35
69
  noDecode?: boolean;
36
70
  }
71
+ /**
72
+ * Options for the `replace` operation.
73
+ *
74
+ * @template T - The type of the document.
75
+ */
37
76
  interface ReplaceOptions<T> extends MongoService.ReplaceOptions<T> {
38
77
  }
78
+ /**
79
+ * Options for the `update` / `updateOnly` operations.
80
+ *
81
+ * @template T - The type of the document.
82
+ */
39
83
  interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
40
84
  initArrayFields?: string[];
41
85
  }
86
+ /**
87
+ * Options for the `updateMany` operation.
88
+ *
89
+ * @template T - The type of the document.
90
+ */
42
91
  interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
43
92
  initArrayFields?: string[];
44
93
  }
@@ -96,71 +145,70 @@ export declare namespace MongoEntityService {
96
145
  */
97
146
  export declare class MongoEntityService<T extends mongodb.Document> extends MongoService<T> {
98
147
  /**
99
- * Constructs a new instance
148
+ * Constructs a new instance.
100
149
  *
101
- * @param {Type | string} dataType - The data type of the array elements.
102
- * @param {MongoEntityService.Options} [options] - The options for the array service.
103
- * @constructor
150
+ * @param dataType - The data type of the managed entities.
151
+ * @param options - The options for the entity service.
104
152
  */
105
153
  constructor(dataType: Type | string, options?: MongoEntityService.Options);
106
154
  /**
107
155
  * Creates a new document in the MongoDB collection
108
156
  *
109
- * @param {MongoEntityService.CreateCommand} command
157
+ * @param command - The command information.
110
158
  * @protected
111
159
  */
112
160
  protected _create(command: MongoEntityService.CreateCommand<T>): Promise<T>;
113
161
  /**
114
162
  * Returns the count of documents in the collection based on the provided options.
115
163
  *
116
- * @param {MongoEntityService.CountCommand<T>} command
164
+ * @param command - The command information.
117
165
  * @protected
118
166
  */
119
167
  protected _count(command: MongoEntityService.CountCommand<T>): Promise<number>;
120
168
  /**
121
169
  * Deletes a document from the collection
122
170
  *
123
- * @param {MongoEntityService.DeleteCommand<T>} command
171
+ * @param command - The command information.
124
172
  * @protected
125
173
  */
126
174
  protected _delete(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
127
175
  /**
128
176
  * Deletes multiple documents from the collection that meet the specified filter criteria.
129
177
  *
130
- * @param {MongoEntityService.DeleteCommand<T>} command
178
+ * @param command - The command information.
131
179
  * @protected
132
180
  */
133
181
  protected _deleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
134
182
  /**
135
183
  * The distinct command returns a list of distinct values for the given key across a collection
136
184
  *
137
- * @param {MongoEntityService.DistinctCommand<T>} command
185
+ * @param command - The command information.
138
186
  * @protected
139
187
  */
140
188
  protected _distinct(command: MongoEntityService.DistinctCommand<T>): Promise<any[]>;
141
189
  /**
142
190
  * Finds a document by its ID.
143
191
  *
144
- * @param { MongoEntityService.FindOneCommand<T>} command
192
+ * @param command - The command information.
145
193
  */
146
194
  protected _findById(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
147
195
  /**
148
196
  * Finds a document in the collection that matches the specified options.
149
197
  *
150
- * @param {MongoEntityService.FindOneCommand<T>} command
198
+ * @param command - The command information.
151
199
  */
152
200
  protected _findOne(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
153
201
  /**
154
202
  * Finds multiple documents in the MongoDB collection
155
203
  *
156
- * @param {MongoEntityService.FindManyCommand<T>} command
204
+ * @param command - The command information.
157
205
  */
158
206
  protected _findMany(command: MongoEntityService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
159
207
  /**
160
208
  * Finds multiple documents in the collection and returns both records (max limit)
161
209
  * and total count that matched the given criteria
162
210
  *
163
- * @param {MongoEntityService.FindManyCommand<T>} command
211
+ * @param command - The command information.
164
212
  */
165
213
  protected _findManyWithCount(command: MongoEntityService.FindManyCommand<T>): Promise<{
166
214
  count: number;
@@ -169,25 +217,25 @@ export declare class MongoEntityService<T extends mongodb.Document> extends Mong
169
217
  /**
170
218
  * Updates a document with the given id in the collection
171
219
  *
172
- * @param {MongoEntityService.UpdateOneCommand<T>} command
220
+ * @param command - The command information.
173
221
  */
174
222
  protected _update(command: MongoEntityService.UpdateOneCommand<T>): Promise<PartialDTO<T> | undefined>;
175
223
  /**
176
224
  * Updates a document in the collection with the specified ID.
177
225
  *
178
- * @param {MongoEntityService.UpdateOneCommand<T>} command
226
+ * @param command - The command information.
179
227
  */
180
228
  protected _updateOnly(command: MongoEntityService.UpdateOneCommand<T>): Promise<number>;
181
229
  /**
182
230
  * Updates multiple documents in the collection based on the specified input and options.
183
231
  *
184
- * @param {MongoEntityService.UpdateManyCommand<T>} command
232
+ * @param command - The command information.
185
233
  */
186
234
  protected _updateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<number>;
187
235
  /**
188
236
  * Replaces a document with the given id in the collection
189
237
  *
190
- * @param {MongoEntityService.ReplaceCommand<T>} command
238
+ * @param command - The command information.
191
239
  */
192
240
  protected _replace(command: MongoEntityService.ReplaceCommand<T>): Promise<PartialDTO<T> | undefined>;
193
241
  protected _prepareUpdate(command: MongoEntityService.UpdateOneCommand<T> | MongoEntityService.UpdateManyCommand<T>): UpdateFilter<T>;
@@ -10,11 +10,10 @@ import { MongoService } from './mongo-service.js';
10
10
  */
11
11
  export class MongoEntityService extends MongoService {
12
12
  /**
13
- * Constructs a new instance
13
+ * Constructs a new instance.
14
14
  *
15
- * @param {Type | string} dataType - The data type of the array elements.
16
- * @param {MongoEntityService.Options} [options] - The options for the array service.
17
- * @constructor
15
+ * @param dataType - The data type of the managed entities.
16
+ * @param options - The options for the entity service.
18
17
  */
19
18
  constructor(dataType, options) {
20
19
  super(dataType, options);
@@ -22,7 +21,7 @@ export class MongoEntityService extends MongoService {
22
21
  /**
23
22
  * Creates a new document in the MongoDB collection
24
23
  *
25
- * @param {MongoEntityService.CreateCommand} command
24
+ * @param command - The command information.
26
25
  * @protected
27
26
  */
28
27
  async _create(command) {
@@ -47,7 +46,7 @@ export class MongoEntityService extends MongoService {
47
46
  /**
48
47
  * Returns the count of documents in the collection based on the provided options.
49
48
  *
50
- * @param {MongoEntityService.CountCommand<T>} command
49
+ * @param command - The command information.
51
50
  * @protected
52
51
  */
53
52
  async _count(command) {
@@ -64,7 +63,7 @@ export class MongoEntityService extends MongoService {
64
63
  /**
65
64
  * Deletes a document from the collection
66
65
  *
67
- * @param {MongoEntityService.DeleteCommand<T>} command
66
+ * @param command - The command information.
68
67
  * @protected
69
68
  */
70
69
  async _delete(command) {
@@ -85,7 +84,7 @@ export class MongoEntityService extends MongoService {
85
84
  /**
86
85
  * Deletes multiple documents from the collection that meet the specified filter criteria.
87
86
  *
88
- * @param {MongoEntityService.DeleteCommand<T>} command
87
+ * @param command - The command information.
89
88
  * @protected
90
89
  */
91
90
  async _deleteMany(command) {
@@ -101,7 +100,7 @@ export class MongoEntityService extends MongoService {
101
100
  /**
102
101
  * The distinct command returns a list of distinct values for the given key across a collection
103
102
  *
104
- * @param {MongoEntityService.DistinctCommand<T>} command
103
+ * @param command - The command information.
105
104
  * @protected
106
105
  */
107
106
  async _distinct(command) {
@@ -117,7 +116,7 @@ export class MongoEntityService extends MongoService {
117
116
  /**
118
117
  * Finds a document by its ID.
119
118
  *
120
- * @param { MongoEntityService.FindOneCommand<T>} command
119
+ * @param command - The command information.
121
120
  */
122
121
  async _findById(command) {
123
122
  isNotNullish(command.documentId, { label: 'documentId' });
@@ -141,7 +140,7 @@ export class MongoEntityService extends MongoService {
141
140
  /**
142
141
  * Finds a document in the collection that matches the specified options.
143
142
  *
144
- * @param {MongoEntityService.FindOneCommand<T>} command
143
+ * @param command - The command information.
145
144
  */
146
145
  async _findOne(command) {
147
146
  const { options } = command;
@@ -158,32 +157,32 @@ export class MongoEntityService extends MongoService {
158
157
  /**
159
158
  * Finds multiple documents in the MongoDB collection
160
159
  *
161
- * @param {MongoEntityService.FindManyCommand<T>} command
160
+ * @param command - The command information.
162
161
  */
163
162
  async _findMany(command) {
164
163
  const { options } = command;
165
164
  const stages = [];
166
- /** Pre-Stages */
165
+ /* Pre-Stages */
167
166
  if (options?.preStages)
168
167
  stages.push(...options.preStages);
169
- /** "Filter" stage */
168
+ /* "Filter" stage */
170
169
  let filter;
171
170
  if (options?.filter)
172
171
  filter = MongoAdapter.prepareFilter(options?.filter);
173
172
  if (filter)
174
173
  stages.push({ $match: filter });
175
- /** "Skip" stage */
174
+ /* "Skip" stage */
176
175
  if (options?.skip)
177
176
  stages.push({ $skip: options.skip });
178
- /** "Sort" stage */
177
+ /* "Sort" stage */
179
178
  if (options?.sort) {
180
179
  const sort = MongoAdapter.prepareSort(options.sort);
181
180
  if (sort)
182
181
  stages.push({ $sort: sort });
183
182
  }
184
- /** "Limit" stage */
183
+ /* "Limit" stage */
185
184
  stages.push({ $limit: options?.limit || 10 });
186
- /** Post-Stages */
185
+ /* Post-Stages */
187
186
  if (options?.postStages)
188
187
  stages.push(...options.postStages);
189
188
  const dataType = this.dataType;
@@ -196,12 +195,12 @@ export class MongoEntityService extends MongoService {
196
195
  ...omit(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
197
196
  session: options?.session ?? this.getSession(),
198
197
  });
199
- /** Execute db command */
198
+ /* Execute db command */
200
199
  try {
201
- /** Fetch the cursor */
200
+ /* Fetch the cursor */
202
201
  if (options?.noDecode)
203
202
  return cursor.toArray();
204
- /** Decode result objects */
203
+ /* Decode result objects */
205
204
  const outputCodec = this._getOutputCodec('find');
206
205
  return (await cursor.toArray()).map((r) => outputCodec(r));
207
206
  }
@@ -214,7 +213,7 @@ export class MongoEntityService extends MongoService {
214
213
  * Finds multiple documents in the collection and returns both records (max limit)
215
214
  * and total count that matched the given criteria
216
215
  *
217
- * @param {MongoEntityService.FindManyCommand<T>} command
216
+ * @param command - The command information.
218
217
  */
219
218
  async _findManyWithCount(command) {
220
219
  const { options } = command;
@@ -232,39 +231,39 @@ export class MongoEntityService extends MongoService {
232
231
  },
233
232
  },
234
233
  ];
235
- /** Pre-Stages */
234
+ /* Pre-Stages */
236
235
  if (options?.preStages)
237
236
  dataStages.push(...options.preStages);
238
- /** Filter */
237
+ /* Filter */
239
238
  if (filter) {
240
239
  countStages.push({ $match: filter });
241
240
  dataStages.push({ $match: filter });
242
241
  }
243
242
  countStages.push({ $count: 'totalMatches' });
244
- /** Sort */
243
+ /* Sort */
245
244
  if (options?.sort) {
246
245
  const sort = MongoAdapter.prepareSort(options.sort);
247
246
  if (sort)
248
247
  dataStages.push({ $sort: sort });
249
248
  }
250
- /** Skip */
249
+ /* Skip */
251
250
  if (options?.skip)
252
251
  dataStages.push({ $skip: options.skip });
253
- /** Limit */
252
+ /* Limit */
254
253
  dataStages.push({ $limit: limit });
255
254
  const dataType = this.dataType;
256
255
  const projection = MongoAdapter.prepareProjection(dataType, options?.projection, this._dataTypeScope);
257
256
  if (projection)
258
257
  dataStages.push({ $project: projection });
259
258
  const outputCodec = this._getOutputCodec('find');
260
- /** Execute db command */
259
+ /* Execute db command */
261
260
  const db = this.getDatabase();
262
261
  const collection = await this.getCollection(db);
263
262
  const cursor = collection.aggregate(stages, {
264
263
  ...omit(options, ['projection', 'sort', 'skip', 'limit', 'filter']),
265
264
  session: options?.session ?? this.getSession(),
266
265
  });
267
- /** Fetch the cursor and decode the result objects */
266
+ /* Fetch the cursor and decode the result objects */
268
267
  try {
269
268
  const facetResult = await cursor.toArray();
270
269
  return {
@@ -282,7 +281,7 @@ export class MongoEntityService extends MongoService {
282
281
  /**
283
282
  * Updates a document with the given id in the collection
284
283
  *
285
- * @param {MongoEntityService.UpdateOneCommand<T>} command
284
+ * @param command - The command information.
286
285
  */
287
286
  async _update(command) {
288
287
  isNotNullish(command.documentId, { label: 'documentId' });
@@ -314,7 +313,7 @@ export class MongoEntityService extends MongoService {
314
313
  /**
315
314
  * Updates a document in the collection with the specified ID.
316
315
  *
317
- * @param {MongoEntityService.UpdateOneCommand<T>} command
316
+ * @param command - The command information.
318
317
  */
319
318
  async _updateOnly(command) {
320
319
  isNotNullish(command.documentId, { label: 'documentId' });
@@ -331,7 +330,7 @@ export class MongoEntityService extends MongoService {
331
330
  ]);
332
331
  const db = this.getDatabase();
333
332
  const collection = await this.getCollection(db);
334
- /** Create array fields if not exists */
333
+ /* Create array fields if not exists */
335
334
  if (options?.initArrayFields) {
336
335
  const $set = options.initArrayFields.reduce((a, k) => {
337
336
  a[k] = { $ifNull: ['$' + k, []] };
@@ -345,7 +344,7 @@ export class MongoEntityService extends MongoService {
345
344
  });
346
345
  delete options.initArrayFields;
347
346
  }
348
- /** Execute update operation */
347
+ /* Execute update operation */
349
348
  return (await collection.updateOne(filter || {}, update, {
350
349
  ...options,
351
350
  session: options?.session ?? this.getSession(),
@@ -355,7 +354,7 @@ export class MongoEntityService extends MongoService {
355
354
  /**
356
355
  * Updates multiple documents in the collection based on the specified input and options.
357
356
  *
358
- * @param {MongoEntityService.UpdateManyCommand<T>} command
357
+ * @param command - The command information.
359
358
  */
360
359
  async _updateMany(command) {
361
360
  isNotNullish(command.input, { label: 'input' });
@@ -369,7 +368,7 @@ export class MongoEntityService extends MongoService {
369
368
  const filter = MongoAdapter.prepareFilter(options?.filter);
370
369
  const db = this.getDatabase();
371
370
  const collection = await this.getCollection(db);
372
- /** Create array fields if not exists */
371
+ /* Create array fields if not exists */
373
372
  if (options?.initArrayFields) {
374
373
  const $set = options.initArrayFields.reduce((a, k) => {
375
374
  a[k] = { $ifNull: ['$' + k, []] };
@@ -383,7 +382,7 @@ export class MongoEntityService extends MongoService {
383
382
  });
384
383
  delete options.initArrayFields;
385
384
  }
386
- /** Execute update operation */
385
+ /* Execute update operation */
387
386
  return (await collection.updateMany(filter || {}, update, {
388
387
  ...omit(options, ['filter']),
389
388
  session: options?.session ?? this.getSession(),
@@ -393,7 +392,7 @@ export class MongoEntityService extends MongoService {
393
392
  /**
394
393
  * Replaces a document with the given id in the collection
395
394
  *
396
- * @param {MongoEntityService.ReplaceCommand<T>} command
395
+ * @param command - The command information.
397
396
  */
398
397
  async _replace(command) {
399
398
  const input = command.input;
@@ -452,7 +451,7 @@ export class MongoEntityService extends MongoService {
452
451
  async _executeCommand(command, commandFn) {
453
452
  try {
454
453
  const result = await super._executeCommand(command, async () => {
455
- /** Call before[X] hooks */
454
+ /* Call before[X] hooks */
456
455
  if (command.crud === 'create')
457
456
  await this._beforeCreate(command);
458
457
  else if (command.crud === 'delete' && command.byId) {
@@ -470,10 +469,10 @@ export class MongoEntityService extends MongoService {
470
469
  else if (command.crud === 'update' && !command.byId) {
471
470
  await this._beforeUpdateMany(command);
472
471
  }
473
- /** Call command function */
472
+ /* Call command function */
474
473
  return commandFn();
475
474
  });
476
- /** Call after[X] hooks */
475
+ /* Call after[X] hooks */
477
476
  if (command.crud === 'create')
478
477
  await this._afterCreate(command, result);
479
478
  else if (command.crud === 'delete' && command.byId) {