@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.
@@ -7,45 +7,93 @@ import type { MongoPatchDTO } from '../types.js';
7
7
  import type { MongoEntityService } from './mongo-entity-service.js';
8
8
  import { MongoService } from './mongo-service.js';
9
9
  /**
10
- *
11
- * @namespace MongoNestedService
10
+ * Options for MongoNestedService.
12
11
  */
13
12
  export declare namespace MongoNestedService {
14
13
  /**
15
- * The constructor options of MongoArrayService.
14
+ * Configuration options for MongoNestedService.
16
15
  */
17
16
  interface Options extends MongoService.Options {
17
+ /**
18
+ * Default maximum number of records returned by `findMany`.
19
+ */
18
20
  defaultLimit?: number;
21
+ /**
22
+ * The field name of the primary key in the nested collection.
23
+ */
19
24
  nestedKey?: string;
25
+ /**
26
+ * Optional filter for nested operations.
27
+ */
20
28
  nestedFilter?: MongoAdapter.FilterInput | ((args: MongoService.CommandInfo, _this: this) => MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined);
21
29
  }
30
+ /**
31
+ * Information about the command being executed.
32
+ */
22
33
  interface CommandInfo extends MongoService.CommandInfo {
23
34
  }
24
35
  interface CreateOptions extends MongoService.CreateOptions {
25
36
  }
37
+ /**
38
+ * Options for the `count` operation.
39
+ * @template T - The type of the document.
40
+ */
26
41
  interface CountOptions<T> extends MongoService.CountOptions<T> {
27
42
  documentFilter?: MongoAdapter.FilterInput;
28
43
  }
44
+ /**
45
+ * Options for the `delete` operation.
46
+ * @template T - The type of the document.
47
+ */
29
48
  interface DeleteOptions<T> extends MongoService.DeleteOptions<T> {
30
49
  documentFilter?: MongoAdapter.FilterInput;
31
50
  }
51
+ /**
52
+ * Options for the `deleteMany` operation.
53
+ * @template T - The type of the document.
54
+ */
32
55
  interface DeleteManyOptions<T> extends MongoService.DeleteManyOptions<T> {
33
56
  documentFilter?: MongoAdapter.FilterInput;
34
57
  }
58
+ /**
59
+ * Options for the `exists` operation.
60
+ * @template T - The type of the document.
61
+ */
35
62
  interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
36
63
  }
64
+ /**
65
+ * Options for the `findOne` / `findById` operations.
66
+ * @template T - The type of the document.
67
+ */
37
68
  interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
38
69
  documentFilter?: MongoAdapter.FilterInput;
39
70
  }
71
+ /**
72
+ * Options for the `findMany` operation.
73
+ *
74
+ * @template T - The type of the document.
75
+ */
40
76
  interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
41
77
  documentFilter?: MongoAdapter.FilterInput;
42
78
  nestedFilter?: MongoAdapter.FilterInput;
43
79
  }
80
+ /**
81
+ * Options for the `update` / `updateOnly` operations.
82
+ *
83
+ * @template T - The type of the document.
84
+ */
44
85
  interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
45
86
  documentFilter?: MongoAdapter.FilterInput;
87
+ initArrayFields?: string[];
46
88
  }
89
+ /**
90
+ * Options for the `updateMany` operation.
91
+ *
92
+ * @template T - The type of the document.
93
+ */
47
94
  interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
48
95
  documentFilter?: MongoAdapter.FilterInput;
96
+ initArrayFields?: string[];
49
97
  }
50
98
  interface CreateCommand<T> extends RequiredSome<CommandInfo, 'documentId' | 'input'> {
51
99
  crud: 'create';
@@ -91,51 +139,43 @@ export declare namespace MongoNestedService {
91
139
  export declare class MongoNestedService<T extends mongodb.Document> extends MongoService<T> {
92
140
  /**
93
141
  * Represents the name of the array field in parent document
94
- *
95
- * @type {string}
96
142
  */
97
143
  fieldName: string;
98
144
  /**
99
145
  * Represents the value of a nested array key field
100
- *
101
- * @type {string}
102
146
  */
103
147
  nestedKey: string;
104
148
  /**
105
149
  * Represents the default limit value for a certain operation.
106
- *
107
- * @type {number}
108
150
  */
109
151
  defaultLimit: number;
110
152
  /**
111
153
  * Represents a common array filter function
112
- *
113
- * @type {FilterInput | Function}
114
154
  */
115
155
  nestedFilter?: MongoAdapter.FilterInput | ((args: MongoService.CommandInfo, _this: this) => MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined);
116
156
  /**
117
157
  * Constructs a new instance
118
158
  *
119
- * @param {Type | string} dataType - The data type of the array elements.
120
- * @param {string} fieldName - The name of the field in the document representing the array.
121
- * @param {MongoNestedService.Options} [options] - The options for the array service.
159
+ * @param dataType - The data type of the array elements.
160
+ * @param fieldName - The name of the field in the document representing the array.
161
+ * @param [options] - The options for the array service.
122
162
  * @constructor
123
163
  */
124
164
  constructor(dataType: Type | string, fieldName: string, options?: MongoNestedService.Options);
125
165
  /**
126
- * Retrieves the data type of the array field
166
+ * Retrieves the data type of the array field.
127
167
  *
128
- * @returns {ComplexType} The complex data type of the field.
129
- * @throws {NotAcceptableError} If the data type is not a ComplexType.
168
+ * @returns The complex data type of the field.
169
+ * @throws {@link NotAcceptableError} If the data type is not a ComplexType.
130
170
  */
131
171
  get dataType(): ComplexType;
132
172
  /**
133
173
  * Create a copy of this instance with given properties and context applied.
134
174
  *
135
- * @param {C | ServiceBase} context - The execution context or service base to associate with this instance.
136
- * @param {Nullish<P>} [overwriteProperties] - An optional object containing properties to overwrite in the current instance.
137
- * @param {Partial<C>} [overwriteContext] - An optional partial context to apply and potentially overwrite parts of the provided execution context.
138
- * @return {this & Required<P>} The current instance with the specified properties and context applied.
175
+ * @param context - The execution context or service base to associate with this instance.
176
+ * @param [overwriteProperties] - An optional object containing properties to overwrite in the current instance.
177
+ * @param [overwriteContext] - An optional partial context to apply and potentially overwrite parts of the provided execution context.
178
+ * @returns The current instance with the specified properties and context applied.
139
179
  * @template P
140
180
  * @template C
141
181
  */
@@ -144,87 +184,87 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
144
184
  * Asserts whether a resource with the specified parentId and id exists.
145
185
  * Throws a ResourceNotFoundError if the resource does not exist.
146
186
  *
147
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
148
- * @param {MongoAdapter.AnyId} id - The ID of the resource.
149
- * @param {MongoNestedService.ExistsOptions<T>} [options] - Optional parameters for checking resource existence.
150
- * @return {Promise<void>} - A promise that resolves with no value upon success.
151
- * @throws {ResourceNotAvailableError} - If the resource does not exist.
187
+ * @param documentId - The ID of the parent document.
188
+ * @param id - The ID of the resource.
189
+ * @param options - Optional parameters for checking resource existence.
190
+ * @returns A promise that resolves with no value upon success.
191
+ * @throws {@link ResourceNotAvailableError} - If the resource does not exist.
152
192
  */
153
193
  assert(documentId: MongoAdapter.AnyId, id: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOptions<T>): Promise<void>;
154
194
  /**
155
195
  * Adds a single item into the array field.
156
196
  *
157
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
158
- * @param {PartialDTO<T>} input - The item to be added to the array field.
159
- * @param {MongoNestedService.CreateOptions<T>} [options] - Optional options for the create operation.
160
- * @return {Promise<PartialDTO<T>>} - A promise that resolves with the partial output of the created item.
161
- * @throws {ResourceNotAvailableError} - If the parent document is not found.
197
+ * @param documentId - The ID of the parent document.
198
+ * @param input - The item to be added to the array field.
199
+ * @param options - Optional options for the create operation.
200
+ * @returns A promise that resolves with the partial output of the created item.
201
+ * @throws {@link ResourceNotAvailableError} - If the parent document is not found.
162
202
  */
163
203
  create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options: RequiredSome<MongoNestedService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
164
204
  create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<T>;
165
205
  /**
166
206
  * Adds a single item into the array field.
167
207
  *
168
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
169
- * @param {DTO<T>} input - The item to be added to the array field.
170
- * @param {MongoNestedService.CreateOptions} [options] - Optional options for the create operation.
171
- * @return {Promise<PartialDTO<T>>} - A promise that resolves create operation result
172
- * @throws {ResourceNotAvailableError} - If the parent document is not found.
208
+ * @param documentId - The ID of the parent document.
209
+ * @param input - The item to be added to the array field.
210
+ * @param [options] - Optional options for the create operation.
211
+ * @returns A promise that resolves create operation result
212
+ * @throws {@link ResourceNotAvailableError} - If the parent document is not found.
173
213
  */
174
214
  createOnly(documentId: MongoAdapter.AnyId, input: DTO<T>, options?: MongoNestedService.CreateOptions): Promise<PartialDTO<T>>;
175
215
  protected _create(command: MongoNestedService.CreateCommand<T>): Promise<T>;
176
216
  /**
177
217
  * Counts the number of documents in the collection that match the specified parentId and options.
178
218
  *
179
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
180
- * @param {MongoNestedService.CountOptions<T>} [options] - Optional parameters for counting.
181
- * @returns {Promise<number>} - A promise that resolves to the count of documents.
219
+ * @param documentId - The ID of the parent document.
220
+ * @param [options] - Optional parameters for counting.
221
+ * @returns A promise that resolves to the count of documents.
182
222
  */
183
223
  count(documentId: MongoAdapter.AnyId, options?: MongoNestedService.CountOptions<T>): Promise<number>;
184
224
  protected _count(command: MongoNestedService.CountCommand<T>): Promise<number>;
185
225
  /**
186
226
  * Deletes an element from an array within a document in the MongoDB collection.
187
227
  *
188
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
189
- * @param {MongoAdapter.AnyId} nestedId - The ID of the element to delete from the nested array.
190
- * @param {MongoNestedService.DeleteOptions<T>} [options] - Additional options for the delete operation.
191
- * @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
228
+ * @param documentId - The ID of the parent document.
229
+ * @param nestedId - The ID of the element to delete from the nested array.
230
+ * @param [options] - Additional options for the delete operation.
231
+ * @returns A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
192
232
  */
193
233
  delete(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteOptions<T>): Promise<number>;
194
234
  protected _delete(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
195
235
  /**
196
236
  * Deletes multiple items from a collection based on the parent ID and optional filter.
197
237
  *
198
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
199
- * @param {MongoNestedService.DeleteManyOptions<T>} [options] - Optional options to specify a filter.
200
- * @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
238
+ * @param documentId - The ID of the parent document.
239
+ * @param [options] - Optional options to specify a filter.
240
+ * @returns A Promise that resolves to the number of items deleted.
201
241
  */
202
242
  deleteMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteManyOptions<T>): Promise<number>;
203
243
  protected _deleteMany(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
204
244
  /**
205
245
  * Checks if an array element with the given parentId and id exists.
206
246
  *
207
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
208
- * @param {MongoAdapter.AnyId} nestedId - The id of the record.
209
- * @param {MongoNestedService.ExistsOptions<T>} [options] - The options for the exists method.
210
- * @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the record exists or not.
247
+ * @param documentId - The ID of the parent document.
248
+ * @param nestedId - The id of the record.
249
+ * @param [options] - The options for the exists method.
250
+ * @returns A promise that resolves to a boolean indicating if the record exists or not.
211
251
  */
212
252
  exists(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOptions<T>): Promise<boolean>;
213
253
  /**
214
254
  * Checks if an object with the given arguments exists.
215
255
  *
216
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
217
- * @param {MongoNestedService.ExistsOptions} [options] - The options for the query (optional).
218
- * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
256
+ * @param documentId - The ID of the parent document.
257
+ * @param [options] - The options for the query (optional).
258
+ * @returns A Promise that resolves to a boolean indicating whether the object exists or not.
219
259
  */
220
260
  existsOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOptions<T>): Promise<boolean>;
221
261
  /**
222
262
  * Finds an element in array field by its parent ID and ID.
223
263
  *
224
- * @param {MongoAdapter.AnyId} documentId - The ID of the document.
225
- * @param {MongoAdapter.AnyId} nestedId - The ID of the document.
226
- * @param {MongoNestedService.FindOneOptions<T>} [options] - The optional options for the operation.
227
- * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
264
+ * @param documentId - The ID of the document.
265
+ * @param nestedId - The ID of the document.
266
+ * @param [options] - The optional options for the operation.
267
+ * @returns A promise that resolves to the found document or undefined if not found.
228
268
  */
229
269
  findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options: RequiredSome<MongoNestedService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
230
270
  findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<T | undefined>;
@@ -232,9 +272,9 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
232
272
  /**
233
273
  * Finds the first array element that matches the given parentId.
234
274
  *
235
- * @param {MongoAdapter.AnyId} documentId - The ID of the document.
236
- * @param {MongoNestedService.FindOneOptions<T>} [options] - Optional options to customize the query.
237
- * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
275
+ * @param documentId - The ID of the document.
276
+ * @param [options] - Optional options to customize the query.
277
+ * @returns A promise that resolves to the first matching document, or `undefined` if no match is found.
238
278
  */
239
279
  findOne(documentId: MongoAdapter.AnyId, options: RequiredSome<MongoNestedService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
240
280
  findOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<T | undefined>;
@@ -242,9 +282,9 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
242
282
  /**
243
283
  * Finds multiple elements in an array field.
244
284
  *
245
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
246
- * @param {MongoNestedService.FindManyOptions<T>} [options] - The options for finding the documents.
247
- * @returns {Promise<PartialDTO<T>[]>} - The found documents.
285
+ * @param documentId - The ID of the parent document.
286
+ * @param [options] - The options for finding the documents.
287
+ * @returns The found documents.
248
288
  */
249
289
  findMany(documentId: MongoAdapter.AnyId, options: RequiredSome<MongoNestedService.FindManyOptions<T>, 'projection'>): Promise<PartialDTO<T>[]>;
250
290
  findMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindManyOptions<T>): Promise<T[]>;
@@ -252,9 +292,9 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
252
292
  /**
253
293
  * Finds multiple elements in an array field.
254
294
  *
255
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
256
- * @param {MongoNestedService.FindManyOptions<T>} [options] - The options for finding the documents.
257
- * @returns {Promise<PartialDTO<T>[]>} - The found documents.
295
+ * @param documentId - The ID of the parent document.
296
+ * @param [options] - The options for finding the documents.
297
+ * @returns The found documents.
258
298
  */
259
299
  findManyWithCount(documentId: MongoAdapter.AnyId, options: RequiredSome<MongoNestedService.FindManyOptions<T>, 'projection'>): Promise<{
260
300
  count: number;
@@ -271,44 +311,53 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
271
311
  /**
272
312
  * Retrieves a specific item from the array of a document.
273
313
  *
274
- * @param {MongoAdapter.AnyId} documentId - The ID of the document.
275
- * @param {MongoAdapter.AnyId} nestedId - The ID of the item.
276
- * @param {MongoNestedService.FindOneOptions<T>} [options] - The options for finding the item.
277
- * @returns {Promise<PartialDTO<T>>} - The item found.
278
- * @throws {ResourceNotAvailableError} - If the item is not found.
314
+ * @param documentId - The ID of the document.
315
+ * @param nestedId - The ID of the item.
316
+ * @param options - Options including a required `projection`.
317
+ * @returns A promise that resolves to the retrieved document as a partial DTO.
318
+ * @throws {@link ResourceNotAvailableError} - If the item is not found.
279
319
  */
280
320
  get(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options: RequiredSome<MongoNestedService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>;
321
+ /**
322
+ * Retrieves a specific item from the array of a document and returns it as a full DTO.
323
+ *
324
+ * @param documentId - The ID of the document.
325
+ * @param nestedId - The ID of the item.
326
+ * @param options - Optional query options.
327
+ * @returns A promise that resolves to the retrieved document as a full DTO.
328
+ * @throws {@link ResourceNotAvailableError} - If the item is not found.
329
+ */
281
330
  get(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<T>;
282
331
  /**
283
332
  * Updates an array element with new data and returns the updated element
284
333
  *
285
- * @param {AnyId} documentId - The ID of the document to update.
286
- * @param {AnyId} nestedId - The ID of the item to update within the document.
287
- * @param {MongoPatchDTO<T>} input - The new data to update the item with.
288
- * @param {MongoNestedService.UpdateOneOptions<T>} [options] - Additional update options.
289
- * @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
290
- * @throws {Error} If an error occurs while updating the item.
334
+ * @param documentId - The ID of the document to update.
335
+ * @param nestedId - The ID of the item to update within the document.
336
+ * @param input - The new data to update the item with.
337
+ * @param [options] - Additional update options.
338
+ * @returns The updated item or undefined if it does not exist.
339
+ * @throws {@link Error} If an error occurs while updating the item.
291
340
  */
292
341
  update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: MongoPatchDTO<T>, options: RequiredSome<MongoNestedService.UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
293
342
  update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: MongoPatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<T | undefined>;
294
343
  /**
295
344
  * Update an array element with new data. Returns 1 if document updated 0 otherwise.
296
345
  *
297
- * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
298
- * @param {MongoAdapter.AnyId} nestedId - The ID of the document to update.
299
- * @param {MongoPatchDTO<T>} input - The partial input object containing the fields to update.
300
- * @param {MongoNestedService.UpdateOneOptions<T>} [options] - Optional update options.
301
- * @returns {Promise<number>} - A promise that resolves to the number of elements updated.
346
+ * @param documentId - The ID of the parent document.
347
+ * @param nestedId - The ID of the document to update.
348
+ * @param input - The partial input object containing the fields to update.
349
+ * @param [options] - Optional update options.
350
+ * @returns A promise that resolves to the number of elements updated.
302
351
  */
303
352
  updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: MongoPatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<number>;
304
353
  protected _updateOnly(command: MongoNestedService.UpdateOneCommand<T>): Promise<number>;
305
354
  /**
306
355
  * Updates multiple array elements in document
307
356
  *
308
- * @param {MongoAdapter.AnyId} documentId - The ID of the document to update.
309
- * @param {MongoPatchDTO<T>} input - The updated data for the document(s).
310
- * @param {MongoNestedService.UpdateManyOptions<T>} [options] - Additional options for the update operation.
311
- * @returns {Promise<number>} - A promise that resolves to the number of documents updated.
357
+ * @param documentId - The ID of the document to update.
358
+ * @param input - The updated data for the document(s).
359
+ * @param [options] - Additional options for the update operation.
360
+ * @returns A promise that resolves to the number of documents updated.
312
361
  */
313
362
  updateMany(documentId: MongoAdapter.AnyId, input: MongoPatchDTO<T>, options?: MongoNestedService.UpdateManyOptions<T>): Promise<number>;
314
363
  protected _updateMany(command: MongoNestedService.UpdateManyCommand<T>): Promise<number>;
@@ -317,7 +366,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
317
366
  * This method is mostly used for security issues like securing multi-tenant applications.
318
367
  *
319
368
  * @protected
320
- * @returns {MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined} The common filter or a Promise
369
+ * @returns The common filter or a Promise
321
370
  * that resolves to the common filter, or undefined if not available.
322
371
  */
323
372
  protected _getNestedFilter(args: MongoService.CommandInfo): MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined;