@opra/mongodb 0.33.3 → 0.33.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.
@@ -9,8 +9,8 @@ export class MongoService extends ApiService {
9
9
  /**
10
10
  * Constructs a new instance
11
11
  *
12
- * @param {Type | string} dataType - The data type of the array elements.
13
- * @param {MongoService.Options} [options] - The options for the array service.
12
+ * @param dataType - The data type of the array elements.
13
+ * @param [options] - The options for the array service.
14
14
  * @constructor
15
15
  */
16
16
  constructor(dataType, options) {
@@ -34,7 +34,6 @@ export class MongoService extends ApiService {
34
34
  /**
35
35
  * Retrieves the data type of the document
36
36
  *
37
- * @returns {ComplexType} The complex data type of the field.
38
37
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
39
38
  */
40
39
  getDataType() {
@@ -43,8 +42,7 @@ export class MongoService extends ApiService {
43
42
  /**
44
43
  * Retrieves the encoder for the specified operation.
45
44
  *
46
- * @param {String} operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
47
- * @returns {IsObject.Validator<T>} - The encoder for the specified operation.
45
+ * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
48
46
  */
49
47
  getEncoder(operation) {
50
48
  let encoder = this._encoders[operation];
@@ -56,8 +54,6 @@ export class MongoService extends ApiService {
56
54
  }
57
55
  /**
58
56
  * Retrieves the decoder.
59
- *
60
- * @returns {IsObject.Validator<T>} - The encoder for the specified operation.
61
57
  */
62
58
  getDecoder() {
63
59
  let decoder = this._decoder;
@@ -70,9 +66,8 @@ export class MongoService extends ApiService {
70
66
  /**
71
67
  * Executes the provided function within a transaction.
72
68
  *
73
- * @param {WithTransactionCallback} callback - The function to be executed within the transaction.
74
- * @param {TransactionOptions} [options] - Optional options for the transaction.
75
- * @returns {Promise<any>} - A promise that resolves with the result of the function execution within the transaction.
69
+ * @param callback - The function to be executed within the transaction.
70
+ * @param [options] - Optional options for the transaction.
76
71
  */
77
72
  async withTransaction(callback, options) {
78
73
  let session = this.getSession();
@@ -101,12 +96,12 @@ export class MongoService extends ApiService {
101
96
  }
102
97
  finally {
103
98
  // Restore old session property
104
- if (hasOldSession) {
99
+ if (hasOldSession)
105
100
  this.session = oldSessionGetter;
106
- await session.endSession();
107
- }
108
101
  else
109
102
  delete this.session;
103
+ if (!oldInTransaction)
104
+ await session.endSession();
110
105
  }
111
106
  }
112
107
  /**
@@ -114,9 +109,8 @@ export class MongoService extends ApiService {
114
109
  * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
115
110
  * can be overridden by setting the **forceServerObjectId** flag.
116
111
  *
117
- * @param {T} doc - The document to be inserted.
118
- * @param {mongodb.InsertOneOptions} options - The options for the insert operation.
119
- * @returns {Promise<mongodb.InsertOneWriteOpResult<mongodb.OptionalId<T>>>} - A promise that resolves with the result of the insert operation.
112
+ * @param doc - The document to insert
113
+ * @param options - Optional settings for the command
120
114
  * @protected
121
115
  */
122
116
  async __insertOne(doc, options) {
@@ -137,9 +131,8 @@ export class MongoService extends ApiService {
137
131
  /**
138
132
  * Gets the number of documents matching the filter.
139
133
  *
140
- * @param {mongodb.Filter<T>} filter - The filter used to match documents.
141
- * @param {mongodb.CountOptions} options - The options for counting documents.
142
- * @returns {Promise<number>} - The number of documents matching the filter.
134
+ * @param filter - The filter used to match documents.
135
+ * @param options - The options for counting documents.
143
136
  * @protected
144
137
  */
145
138
  async __countDocuments(filter, options) {
@@ -161,9 +154,9 @@ export class MongoService extends ApiService {
161
154
  /**
162
155
  * Delete a document from a collection
163
156
  *
164
- * @param {mongodb.Filter<T>} filter - The filter used to select the document to remove
165
- * @param {mongodb.DeleteOptions} options - Optional settings for the command
166
- * @return {Promise<mongodb.DeleteResult>} A Promise that resolves to the result of the delete operation
157
+ * @param filter - The filter used to select the document to remove
158
+ * @param options - Optional settings for the command
159
+ * @protected
167
160
  */
168
161
  async __deleteOne(filter, options) {
169
162
  const db = this.getDatabase();
@@ -181,12 +174,10 @@ export class MongoService extends ApiService {
181
174
  }
182
175
  }
183
176
  /**
184
- * Deletes multiple documents from a collection.
177
+ * Delete multiple documents from a collection
185
178
  *
186
- * @param {mongodb.Filter<T>} [filter] - The filter object specifying the documents to delete.
187
- * If not provided, all documents in the collection will be deleted.
188
- * @param {mongodb.DeleteOptions} [options] - The options for the delete operation.
189
- * @returns {Promise<mongodb.DeleteResult>} A promise that resolves with the delete result object.
179
+ * @param filter - The filter used to select the documents to remove
180
+ * @param options - Optional settings for the command
190
181
  * @protected
191
182
  */
192
183
  async __deleteMany(filter, options) {
@@ -205,12 +196,33 @@ export class MongoService extends ApiService {
205
196
  }
206
197
  }
207
198
  /**
208
- * Create a new Change Stream, watching for new changes
209
- * (insertions, updates, replacements, deletions, and invalidations) in this collection.
199
+ * Gets the number of documents matching the filter.
200
+ *
201
+ * @param field - Field of the document to find distinct values for
202
+ * @param filter - The filter for filtering the set of documents to which we apply the distinct filter.
203
+ * @param options - Optional settings for the command
204
+ * @protected
205
+ */
206
+ async __distinct(field, filter, options) {
207
+ const db = this.getDatabase();
208
+ const collection = await this.getCollection(db);
209
+ options = {
210
+ ...options,
211
+ session: options?.session || this.getSession()
212
+ };
213
+ try {
214
+ return await collection.distinct(field, filter || {}, options);
215
+ }
216
+ catch (e) {
217
+ await this.$onError?.(e, this);
218
+ throw e;
219
+ }
220
+ }
221
+ /**
222
+ * Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2
210
223
  *
211
- * @param {mongodb.Document[]} pipeline - The pipeline of aggregation stages to apply to the collection.
212
- * @param {mongodb.AggregateOptions} options - The options to customize the aggregation.
213
- * @returns {Promise<mongodb.ChangeStream<T>>} - A promise that resolves to a Change Stream that represents the result of the aggregation.
224
+ * @param pipeline - An array of aggregation pipelines to execute
225
+ * @param options - Optional settings for the command
214
226
  * @protected
215
227
  */
216
228
  async __aggregate(pipeline, options) {
@@ -231,10 +243,9 @@ export class MongoService extends ApiService {
231
243
  /**
232
244
  * Fetches the first document that matches the filter
233
245
  *
234
- * @param {mongodb.Filter<T>} filter - The filter object to match documents against
235
- * @param {mongodb.FindOptions} [options] - The options to use when querying the collection
246
+ * @param filter - Query for find Operation
247
+ * @param options - Optional settings for the command
236
248
  * @protected
237
- * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the first matching document, or undefined if no match is found
238
249
  */
239
250
  async __findOne(filter, options) {
240
251
  const db = this.getDatabase();
@@ -252,11 +263,11 @@ export class MongoService extends ApiService {
252
263
  }
253
264
  }
254
265
  /**
255
- * Creates a cursor for a filter that can be used to iterate over results from MongoDB.
266
+ * Creates a cursor for a filter that can be used to iterate over results from MongoDB
256
267
  *
257
- * @param {mongodb.Filter<T>} filter - The filter to apply when searching for results.
258
- * @param {mongodb.FindOptions} [options] - The options to customize the search behavior.
259
- * @returns {mongodb.Cursor<T>} - The cursor object that can be used to iterate over the results.
268
+ * @param filter - The filter predicate. If unspecified,
269
+ * then all documents in the collection will match the predicate
270
+ * @param options - Optional settings for the command
260
271
  * @protected
261
272
  */
262
273
  async __find(filter, options) {
@@ -275,13 +286,12 @@ export class MongoService extends ApiService {
275
286
  }
276
287
  }
277
288
  /**
278
- * Update a single document in a collection.
289
+ * Update a single document in a collection
279
290
  *
280
- * @param {mongodb.Filter<T>} filter - The filter to select the document(s) to update.
281
- * @param {mongodb.UpdateFilter<T>} update - The update operation to be applied on the selected document(s).
282
- * @param {mongodb.UpdateOptions} [options] - Optional settings for the update operation.
291
+ * @param filter - The filter used to select the document to update
292
+ * @param update - The update operations to be applied to the document
293
+ * @param options - Optional settings for the command
283
294
  * @protected
284
- * @returns {Promise<mongodb.UpdateResult>} - A promise that resolves to the result of the update operation.
285
295
  */
286
296
  async __updateOne(filter, update, options) {
287
297
  const db = this.getDatabase();
@@ -301,13 +311,12 @@ export class MongoService extends ApiService {
301
311
  /**
302
312
  * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.
303
313
  *
304
- * @param {mongodb.Filter<T>} filter - The filter to select the document to be updated.
305
- * @param {mongodb.UpdateFilter<T>} doc - The update document.
306
- * @param {mongodb.FindOneAndUpdateOptions} [options] - Optional options for the find one and update operation.
307
- * @returns {Promise<T | null>} - A promise that resolves to the updated document or null if no document matched the filter.
314
+ * @param filter - The filter used to select the document to update
315
+ * @param update - Update operations to be performed on the document
316
+ * @param options - Optional settings for the command
308
317
  * @protected
309
318
  */
310
- async __findOneAndUpdate(filter, doc, options) {
319
+ async __findOneAndUpdate(filter, update, options) {
311
320
  const db = this.getDatabase();
312
321
  const collection = await this.getCollection(db);
313
322
  const opts = {
@@ -317,7 +326,7 @@ export class MongoService extends ApiService {
317
326
  session: options?.session || this.getSession(),
318
327
  };
319
328
  try {
320
- return await collection.findOneAndUpdate(filter || {}, doc, opts);
329
+ return await collection.findOneAndUpdate(filter || {}, update, opts);
321
330
  }
322
331
  catch (e) {
323
332
  await this.$onError?.(e, this);
@@ -325,15 +334,14 @@ export class MongoService extends ApiService {
325
334
  }
326
335
  }
327
336
  /**
328
- * Update multiple documents in a collection.
337
+ * Update multiple documents in a collection
329
338
  *
330
- * @param {mongodb.Filter<T>} filter - The filter used to select the documents to be updated.
331
- * @param {mongodb.UpdateFilter<T> | Partial<T>} doc - The updates to be applied to the selected documents.
332
- * @param {StrictOmit<mongodb.UpdateOptions, 'upsert'>} [options] - The optional settings for the update operation.
333
- * @return {Promise<mongodb.UpdateResult>} - A Promise that resolves to the result of the update operation.
339
+ * @param filter - The filter used to select the documents to update
340
+ * @param update - The update operations to be applied to the documents
341
+ * @param options - Optional settings for the command
334
342
  * @protected
335
343
  */
336
- async __updateMany(filter, doc, options) {
344
+ async __updateMany(filter, update, options) {
337
345
  const db = this.getDatabase();
338
346
  const collection = await this.getCollection(db);
339
347
  options = {
@@ -342,7 +350,7 @@ export class MongoService extends ApiService {
342
350
  upsert: false
343
351
  };
344
352
  try {
345
- return await collection.updateMany(filter || {}, doc, options);
353
+ return await collection.updateMany(filter || {}, update, options);
346
354
  }
347
355
  catch (e) {
348
356
  await this.$onError?.(e, this);
@@ -354,7 +362,6 @@ export class MongoService extends ApiService {
354
362
  *
355
363
  * @protected
356
364
  *
357
- * @returns {Promise<mongodb.Db>} The database connection.
358
365
  * @throws {Error} If the context or database is not set.
359
366
  */
360
367
  getDatabase() {
@@ -370,7 +377,6 @@ export class MongoService extends ApiService {
370
377
  *
371
378
  * @protected
372
379
  *
373
- * @returns {Promise<mongodb.ClientSession>} The database connection.
374
380
  * @throws {Error} If the context or database is not set.
375
381
  */
376
382
  getSession() {
@@ -381,9 +387,8 @@ export class MongoService extends ApiService {
381
387
  /**
382
388
  * Retrieves a MongoDB collection from the given database.
383
389
  *
384
- * @param {mongodb.Db} db - The MongoDB database.
390
+ * @param db - The MongoDB database.
385
391
  * @protected
386
- * @returns {Promise<mongodb.Collection<T>>} A promise that resolves to the MongoDB collection.
387
392
  */
388
393
  async getCollection(db) {
389
394
  return db.collection(this.getCollectionName());
@@ -392,7 +397,7 @@ export class MongoService extends ApiService {
392
397
  * Retrieves the collection name.
393
398
  *
394
399
  * @protected
395
- * @returns {string} The collection name.
400
+ * @returns The collection name.
396
401
  * @throws {Error} If the collection name is not defined.
397
402
  */
398
403
  getCollectionName() {
@@ -407,7 +412,7 @@ export class MongoService extends ApiService {
407
412
  * Retrieves the resource name.
408
413
  *
409
414
  * @protected
410
- * @returns {string} The collection name.
415
+ * @returns {string} The resource name.
411
416
  * @throws {Error} If the collection name is not defined.
412
417
  */
413
418
  getResourceName() {
@@ -421,9 +426,9 @@ export class MongoService extends ApiService {
421
426
  /**
422
427
  * Generates an encoder for the specified operation.
423
428
  *
424
- * @param {string} operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
429
+ * @param operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
425
430
  * @protected
426
- * @returns {IsObject.Validator} - The generated encoder for the specified operation.
431
+ * @returns - The generated encoder for the specified operation.
427
432
  */
428
433
  _generateEncoder(operation) {
429
434
  const dataType = this.getDataType();
@@ -438,7 +443,7 @@ export class MongoService extends ApiService {
438
443
  * Generates an encoder for the specified operation.
439
444
  *
440
445
  * @protected
441
- * @returns {IsObject.Validator} - The generated encoder for the specified operation.
446
+ * @returns - The generated encoder for the specified operation.
442
447
  */
443
448
  _generateDecoder() {
444
449
  const dataType = this.getDataType();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "0.33.3",
3
+ "version": "0.33.4",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -30,8 +30,8 @@
30
30
  "ts-gems": "^3.1.0"
31
31
  },
32
32
  "peerDependencies": {
33
- "@opra/common": "^0.33.3",
34
- "@opra/core": "^0.33.3",
33
+ "@opra/common": "^0.33.4",
34
+ "@opra/core": "^0.33.4",
35
35
  "mongodb": ">=6.x.x"
36
36
  },
37
37
  "type": "module",
@@ -81,7 +81,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
81
81
  * @throws {Error} if an unknown error occurs while creating the document.
82
82
  */
83
83
  create(input: PartialDTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
84
- protected _create(input: PartialDTO<T>, options: MongoCollectionService.CreateOptions | undefined): Promise<PartialDTO<T>>;
84
+ protected _create(input: PartialDTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
85
85
  /**
86
86
  * Returns the count of documents in the collection based on the provided options.
87
87
  *
@@ -107,6 +107,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
107
107
  */
108
108
  deleteMany(options?: MongoCollectionService.DeleteManyOptions<T>): Promise<number>;
109
109
  protected _deleteMany(options?: MongoCollectionService.DeleteManyOptions<T>): Promise<number>;
110
+ distinct(field: string, options?: MongoCollectionService.DistinctOptions<T>): Promise<any[]>;
111
+ protected _distinct(field: string, options?: MongoCollectionService.DistinctOptions<T>): Promise<any[]>;
110
112
  /**
111
113
  * Checks if an object with the given id exists.
112
114
  *
@@ -175,8 +177,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
175
177
  * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
176
178
  * undefined if the document was not found.
177
179
  */
178
- update(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
179
- protected _update(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
180
+ update(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
181
+ protected _update(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
180
182
  /**
181
183
  * Updates a document in the collection with the specified ID.
182
184
  *
@@ -185,8 +187,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
185
187
  * @param {MongoCollectionService.UpdateOptions<T>} options - The options for updating the document.
186
188
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
187
189
  */
188
- updateOnly(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
189
- protected _updateOnly(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
190
+ updateOnly(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
191
+ protected _updateOnly(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
190
192
  /**
191
193
  * Updates multiple documents in the collection based on the specified input and options.
192
194
  *
@@ -194,8 +196,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
194
196
  * @param {MongoCollectionService.UpdateManyOptions<T>} options - The options for updating the documents.
195
197
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
196
198
  */
197
- updateMany(input: PatchDTO<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
198
- protected _updateMany(input: PatchDTO<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
199
+ updateMany(input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
200
+ protected _updateMany(input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
199
201
  /**
200
202
  * Generates an ID.
201
203
  *
@@ -244,7 +246,7 @@ export declare namespace MongoCollectionService {
244
246
  interceptor?: MongoCollectionService<any>['$interceptor'];
245
247
  }
246
248
  /**
247
- * Represents options for creating a new document.
249
+ * Represents options for "create" operation
248
250
  *
249
251
  * @interface
250
252
  */
@@ -254,7 +256,7 @@ export declare namespace MongoCollectionService {
254
256
  include?: string[];
255
257
  }
256
258
  /**
257
- * Represents options for counting document.
259
+ * Represents options for "count" operation
258
260
  *
259
261
  * @interface
260
262
  * @template T - The type of the document.
@@ -263,7 +265,7 @@ export declare namespace MongoCollectionService {
263
265
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
264
266
  }
265
267
  /**
266
- * Represents options for deleting single document.
268
+ * Represents options for "delete" operation
267
269
  *
268
270
  * @interface
269
271
  * @template T - The type of the document.
@@ -272,7 +274,7 @@ export declare namespace MongoCollectionService {
272
274
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
273
275
  }
274
276
  /**
275
- * Represents options for deleting multiple documents.
277
+ * Represents options for "deleteMany" operation
276
278
  *
277
279
  * @interface
278
280
  * @template T - The type of the document.
@@ -281,7 +283,16 @@ export declare namespace MongoCollectionService {
281
283
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
282
284
  }
283
285
  /**
284
- * Represents options for finding single document.
286
+ * Represents options for "distinct" operation
287
+ *
288
+ * @interface
289
+ * @template T - The type of the document.
290
+ */
291
+ interface DistinctOptions<T> extends mongodb.DistinctOptions {
292
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
293
+ }
294
+ /**
295
+ * Represents options for "findOne" operation
285
296
  *
286
297
  * @interface
287
298
  * @template T - The type of the document.
@@ -289,7 +300,7 @@ export declare namespace MongoCollectionService {
289
300
  interface FindOneOptions<T = any> extends StrictOmit<FindManyOptions<T>, 'count' | 'limit'> {
290
301
  }
291
302
  /**
292
- * Represents options for finding multiple documents.
303
+ * Represents options for "findMany" operation
293
304
  *
294
305
  * @interface
295
306
  * @template T - The type of the document.
@@ -305,7 +316,7 @@ export declare namespace MongoCollectionService {
305
316
  count?: boolean;
306
317
  }
307
318
  /**
308
- * Represents options for updating single document.
319
+ * Represents options for "update" operation
309
320
  *
310
321
  * @interface
311
322
  * @template T - The type of the document.
@@ -317,7 +328,7 @@ export declare namespace MongoCollectionService {
317
328
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
318
329
  }
319
330
  /**
320
- * Represents options for updating multiple documents.
331
+ * Represents options for "updateMany" operation
321
332
  *
322
333
  * @interface
323
334
  * @template T - The type of the document.
@@ -326,7 +337,7 @@ export declare namespace MongoCollectionService {
326
337
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
327
338
  }
328
339
  /**
329
- * Represents options for checking the document exists
340
+ * Represents options for "exists" operation
330
341
  *
331
342
  * @interface
332
343
  */