@opra/mongodb 0.32.5 → 0.33.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.
@@ -1,6 +1,6 @@
1
1
  import mongodb from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
- import { DTO, PartialDTO, PatchDTO } from '@opra/common';
3
+ import { PartialDTO, PatchDTO } from '@opra/common';
4
4
  import * as OpraCommon from '@opra/common';
5
5
  import { FilterInput } from './adapter-utils/prepare-filter.js';
6
6
  import { MongoService } from './mongo-service.js';
@@ -27,7 +27,13 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
27
27
  *
28
28
  * @type {FilterInput | Function}
29
29
  */
30
- $documentFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
30
+ $documentFilter?: FilterInput | ((args: {
31
+ crud: MongoService.CrudOp;
32
+ method: string;
33
+ documentId?: AnyId;
34
+ input?: Record<string, any>;
35
+ options?: Record<string, any>;
36
+ }, _this: this) => FilterInput | Promise<FilterInput> | undefined);
31
37
  /**
32
38
  * Interceptor function for handling callback execution with provided arguments.
33
39
  *
@@ -41,11 +47,11 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
41
47
  * @param {Object} _this - The reference to the current object.
42
48
  * @returns {Promise<any>} - The promise that resolves to the result of the callback execution.
43
49
  */
44
- $interceptor?: (callback: (...args: any[]) => any, args: {
50
+ $interceptor?: (callback: () => any, args: {
45
51
  crud: MongoService.CrudOp;
46
52
  method: string;
47
53
  documentId?: AnyId;
48
- input?: Object;
54
+ input?: Record<string, any>;
49
55
  options?: Record<string, any>;
50
56
  }, _this: any) => Promise<any>;
51
57
  /**
@@ -63,7 +69,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
63
69
  * @param {AnyId} id - The ID of the resource to assert.
64
70
  * @param {MongoCollectionService.ExistsOptions} [options] - Optional options for checking the existence.
65
71
  * @returns {Promise<void>} - A Promise that resolves when the resource exists.
66
- * @throws {ResourceNotFoundError} - If the resource does not exist.
72
+ * @throws {ResourceNotAvailableError} - If the resource does not exist.
67
73
  */
68
74
  assert(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<void>;
69
75
  /**
@@ -74,7 +80,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
74
80
  * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
75
81
  * @throws {Error} if an unknown error occurs while creating the document.
76
82
  */
77
- create(input: DTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
83
+ create(input: PartialDTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
84
+ protected _create(input: PartialDTO<T>, options: MongoCollectionService.CreateOptions | undefined): Promise<PartialDTO<T>>;
78
85
  /**
79
86
  * Returns the count of documents in the collection based on the provided options.
80
87
  *
@@ -82,6 +89,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
82
89
  * @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
83
90
  */
84
91
  count(options?: MongoCollectionService.CountOptions<T>): Promise<number>;
92
+ protected _count(options?: MongoCollectionService.CountOptions<T>): Promise<number>;
85
93
  /**
86
94
  * Deletes a document from the collection.
87
95
  *
@@ -90,6 +98,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
90
98
  * @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
91
99
  */
92
100
  delete(id: AnyId, options?: MongoCollectionService.DeleteOptions<T>): Promise<number>;
101
+ protected _delete(id: AnyId, options?: MongoCollectionService.DeleteOptions<T>): Promise<number>;
93
102
  /**
94
103
  * Deletes multiple documents from the collection that meet the specified filter criteria.
95
104
  *
@@ -97,14 +106,22 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
97
106
  * @return {Promise<number>} - A promise that resolves to the number of documents deleted.
98
107
  */
99
108
  deleteMany(options?: MongoCollectionService.DeleteManyOptions<T>): Promise<number>;
109
+ protected _deleteMany(options?: MongoCollectionService.DeleteManyOptions<T>): Promise<number>;
100
110
  /**
101
111
  * Checks if an object with the given id exists.
102
112
  *
103
113
  * @param {AnyId} id - The id of the object to check.
104
- * @param {MongoCollectionService.ExistsOptions} [options] - The options for the aggregation query (optional).
114
+ * @param {MongoCollectionService.ExistsOptions} [options] - The options for the query (optional).
105
115
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
106
116
  */
107
117
  exists(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<boolean>;
118
+ /**
119
+ * Checks if an object with the given arguments exists.
120
+ *
121
+ * @param {MongoCollectionService.ExistsOneOptions} [options] - The options for the query (optional).
122
+ * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
123
+ */
124
+ existsOne(options?: MongoCollectionService.ExistsOneOptions<T>): Promise<boolean>;
108
125
  /**
109
126
  * Finds a document by its ID.
110
127
  *
@@ -113,6 +130,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
113
130
  * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
114
131
  */
115
132
  findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
133
+ protected _findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
116
134
  /**
117
135
  * Finds a document in the collection that matches the specified options.
118
136
  *
@@ -120,6 +138,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
120
138
  * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
121
139
  */
122
140
  findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
141
+ protected _findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
123
142
  /**
124
143
  * Finds multiple documents in the MongoDB collection.
125
144
  *
@@ -136,6 +155,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
136
155
  * @return A Promise that resolves to an array of partial outputs of type T.
137
156
  */
138
157
  findMany(options?: MongoCollectionService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
158
+ protected _findMany(options?: MongoCollectionService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
139
159
  /**
140
160
  * Retrieves a document from the collection by its ID. Throws error if not found.
141
161
  *
@@ -143,7 +163,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
143
163
  * @param {MongoCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
144
164
  * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
145
165
  * or rejects with a ResourceNotFoundError if the document does not exist.
146
- * @throws {ResourceNotFoundError} - If the document with the specified ID does not exist.
166
+ * @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
147
167
  */
148
168
  get(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T>>;
149
169
  /**
@@ -156,6 +176,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
156
176
  * undefined if the document was not found.
157
177
  */
158
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>;
159
180
  /**
160
181
  * Updates a document in the collection with the specified ID.
161
182
  *
@@ -165,6 +186,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
165
186
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
166
187
  */
167
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>;
168
190
  /**
169
191
  * Updates multiple documents in the collection based on the specified input and options.
170
192
  *
@@ -173,6 +195,7 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
173
195
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
174
196
  */
175
197
  updateMany(input: PatchDTO<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
198
+ protected _updateMany(input: PatchDTO<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
176
199
  /**
177
200
  * Generates an ID.
178
201
  *
@@ -188,7 +211,20 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
188
211
  * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
189
212
  * that resolves to the common filter, or undefined if not available.
190
213
  */
191
- protected _getDocumentFilter(): FilterInput | Promise<FilterInput> | undefined;
214
+ protected _getDocumentFilter(args: {
215
+ crud: MongoService.CrudOp;
216
+ method: string;
217
+ documentId?: AnyId;
218
+ input?: Object;
219
+ options?: Record<string, any>;
220
+ }): FilterInput | Promise<FilterInput> | undefined;
221
+ protected _intercept(callback: (...args: any[]) => any, args: {
222
+ crud: MongoService.CrudOp;
223
+ method: string;
224
+ documentId?: AnyId;
225
+ input?: Object;
226
+ options?: Record<string, any>;
227
+ }): Promise<any>;
192
228
  }
193
229
  /**
194
230
  *
@@ -296,4 +332,19 @@ export declare namespace MongoCollectionService {
296
332
  */
297
333
  interface ExistsOptions extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
298
334
  }
335
+ /**
336
+ * Represents options for checking the document exists
337
+ *
338
+ * @interface
339
+ */
340
+ interface ExistsOneOptions<T> extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
341
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
342
+ }
343
+ interface OperationInfo {
344
+ crud: MongoService.CrudOp;
345
+ method: string;
346
+ documentId?: AnyId;
347
+ input?: Record<string, any>;
348
+ options?: Record<string, any>;
349
+ }
299
350
  }
@@ -1,17 +1,17 @@
1
- import mongodb from 'mongodb';
1
+ import mongodb, { TransactionOptions } from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
- import vg from 'valgen';
3
+ import { IsObject } from 'valgen';
4
4
  import { ComplexType, PartialDTO } from '@opra/common';
5
- import { ApiService, RequestContext } from '@opra/core';
6
- import { AnyId } from './types.js';
5
+ import { ApiService } from '@opra/core';
6
+ import { AnyId, WithTransactionCallback } from './types.js';
7
7
  /**
8
8
  * Class representing a MongoDB service for interacting with a collection.
9
9
  * @extends ApiService
10
10
  * @template T - The type of the documents in the collection.
11
11
  */
12
12
  export declare class MongoService<T extends mongodb.Document> extends ApiService {
13
- protected _encoders: Record<string, vg.ObjectValidator<T>>;
14
- protected _decoder?: vg.ObjectValidator<T>;
13
+ protected _encoders: Record<string, IsObject.Validator<T>>;
14
+ protected _decoder?: IsObject.Validator<T>;
15
15
  protected _dataType: Type | string;
16
16
  /**
17
17
  * Represents the name of a collection in MongoDB
@@ -70,15 +70,23 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
70
70
  * Retrieves the encoder for the specified operation.
71
71
  *
72
72
  * @param {String} operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
73
- * @returns {vg.ObjectValidator<T>} - The encoder for the specified operation.
73
+ * @returns {IsObject.Validator<T>} - The encoder for the specified operation.
74
74
  */
75
- getEncoder(operation: 'create' | 'update'): vg.ObjectValidator<T>;
75
+ getEncoder(operation: 'create' | 'update'): IsObject.Validator<T>;
76
76
  /**
77
77
  * Retrieves the decoder.
78
78
  *
79
- * @returns {vg.ObjectValidator<T>} - The encoder for the specified operation.
79
+ * @returns {IsObject.Validator<T>} - The encoder for the specified operation.
80
80
  */
81
- getDecoder(): vg.ObjectValidator<T>;
81
+ getDecoder(): IsObject.Validator<T>;
82
+ /**
83
+ * Executes the provided function within a transaction.
84
+ *
85
+ * @param {WithTransactionCallback} callback - The function to be executed within the transaction.
86
+ * @param {TransactionOptions} [options] - Optional options for the transaction.
87
+ * @returns {Promise<any>} - A promise that resolves with the result of the function execution within the transaction.
88
+ */
89
+ withTransaction(callback: WithTransactionCallback, options?: TransactionOptions): Promise<any>;
82
90
  /**
83
91
  * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field,
84
92
  * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
@@ -217,32 +225,21 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
217
225
  * @throws {Error} If the collection name is not defined.
218
226
  */
219
227
  getResourceName(): string;
220
- /**
221
- * Compares the current instance with the provided attributes and returns true if they match, false otherwise.
222
- * This method is protected and should only be called by subclasses.
223
- *
224
- * @param {MongoService<any>} service - The service instance to compare.
225
- * @param {RequestContext} context - The request context.
226
- * @param {Object} attributes - Optional attributes object for comparison.
227
- * @return {boolean} - True if the instance matches the provided attributes, false otherwise.
228
- * @protected
229
- */
230
- protected _instanceCompare(service: MongoService<any>, context: RequestContext, attributes?: any): boolean;
231
228
  /**
232
229
  * Generates an encoder for the specified operation.
233
230
  *
234
231
  * @param {string} operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
235
232
  * @protected
236
- * @returns {vg.Validator} - The generated encoder for the specified operation.
233
+ * @returns {IsObject.Validator} - The generated encoder for the specified operation.
237
234
  */
238
- protected _generateEncoder(operation: 'create' | 'update'): vg.ObjectValidator<T>;
235
+ protected _generateEncoder(operation: 'create' | 'update'): IsObject.Validator<T>;
239
236
  /**
240
237
  * Generates an encoder for the specified operation.
241
238
  *
242
239
  * @protected
243
- * @returns {vg.Validator} - The generated encoder for the specified operation.
240
+ * @returns {IsObject.Validator} - The generated encoder for the specified operation.
244
241
  */
245
- protected _generateDecoder(): vg.ObjectValidator<T>;
242
+ protected _generateDecoder(): IsObject.Validator<T>;
246
243
  }
247
244
  /**
248
245
  * The namespace for the MongoService.
@@ -1,6 +1,6 @@
1
1
  import mongodb from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
- import { DTO, PartialDTO, PatchDTO } from '@opra/common';
3
+ import { PartialDTO, PatchDTO } from '@opra/common';
4
4
  import * as OpraCommon from '@opra/common';
5
5
  import { FilterInput } from './adapter-utils/prepare-filter.js';
6
6
  import { MongoService } from './mongo-service.js';
@@ -28,7 +28,13 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
28
28
  *
29
29
  * @type {FilterInput | Function}
30
30
  */
31
- $documentFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
31
+ $documentFilter?: FilterInput | ((args: {
32
+ crud: MongoService.CrudOp;
33
+ method: string;
34
+ documentId?: AnyId;
35
+ input?: Record<string, any>;
36
+ options?: Record<string, any>;
37
+ }, _this: this) => FilterInput | Promise<FilterInput> | undefined);
32
38
  /**
33
39
  * Interceptor function for handling callback execution with provided arguments.
34
40
  *
@@ -46,7 +52,7 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
46
52
  crud: MongoService.CrudOp;
47
53
  method: string;
48
54
  documentId?: AnyId;
49
- input?: Object;
55
+ input?: Record<string, any>;
50
56
  options?: Record<string, any>;
51
57
  }, _this: any) => Promise<any>;
52
58
  /**
@@ -61,7 +67,7 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
61
67
  * Asserts the existence of a resource based on the given options.
62
68
  *
63
69
  * @returns {Promise<void>} A Promise that resolves when the resource exists.
64
- * @throws {ResourceNotFoundError} If the resource does not exist.
70
+ * @throws {ResourceNotAvailableError} If the resource does not exist.
65
71
  */
66
72
  assert(options?: MongoSingletonService.ExistsOptions): Promise<void>;
67
73
  /**
@@ -72,7 +78,8 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
72
78
  * @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
73
79
  * @throws {Error} Throws an error if an unknown error occurs while creating the document.
74
80
  */
75
- create(input: DTO<T>, options?: MongoSingletonService.CreateOptions): Promise<PartialDTO<T>>;
81
+ create(input: PartialDTO<T>, options?: MongoSingletonService.CreateOptions): Promise<PartialDTO<T>>;
82
+ protected _create(input: PartialDTO<T>, options?: MongoSingletonService.CreateOptions): Promise<PartialDTO<T>>;
76
83
  /**
77
84
  * Deletes a record from the database
78
85
  *
@@ -80,6 +87,7 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
80
87
  * @returns {Promise<number>} The number of records deleted
81
88
  */
82
89
  delete(options?: MongoSingletonService.DeleteOptions<T>): Promise<number>;
90
+ protected _delete(options?: MongoSingletonService.DeleteOptions<T>): Promise<number>;
83
91
  /**
84
92
  * Checks if the document exists in the database.
85
93
  *
@@ -93,12 +101,13 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
93
101
  * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
94
102
  */
95
103
  findOne(options?: MongoSingletonService.FindOptions<T>): Promise<PartialDTO<T> | undefined>;
104
+ protected _findOne(options?: MongoSingletonService.FindOptions<T>): Promise<PartialDTO<T> | undefined>;
96
105
  /**
97
106
  * Fetches the document from the Mongo collection service. Throws error if not found.
98
107
  *
99
108
  * @param {MongoCollectionService.FindOneOptions} options - The options to customize the query.
100
109
  * @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
101
- * @throws {ResourceNotFoundError} - If the document is not found in the collection.
110
+ * @throws {ResourceNotAvailableError} - If the document is not found in the collection.
102
111
  */
103
112
  get(options?: MongoSingletonService.FindOptions<T>): Promise<PartialDTO<T>>;
104
113
  /**
@@ -110,6 +119,7 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
110
119
  * @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
111
120
  */
112
121
  updateOnly(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<number>;
122
+ protected _updateOnly(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<number>;
113
123
  /**
114
124
  * Updates a document in the MongoDB collection.
115
125
  *
@@ -119,6 +129,7 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
119
129
  * @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
120
130
  */
121
131
  update(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
132
+ protected _update(input: PatchDTO<T>, options?: MongoSingletonService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
122
133
  /**
123
134
  * Retrieves the common filter used for querying the document.
124
135
  * This method is mostly used for security issues like securing multi-tenant applications.
@@ -127,7 +138,20 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
127
138
  * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
128
139
  * that resolves to the common filter, or undefined if not available.
129
140
  */
130
- protected _getDocumentFilter(): FilterInput | Promise<FilterInput> | undefined;
141
+ protected _getDocumentFilter(args: {
142
+ crud: MongoService.CrudOp;
143
+ method: string;
144
+ documentId?: AnyId;
145
+ input?: Object;
146
+ options?: Record<string, any>;
147
+ }): FilterInput | Promise<FilterInput> | undefined;
148
+ protected _intercept(callback: (...args: any[]) => any, args: {
149
+ crud: MongoService.CrudOp;
150
+ method: string;
151
+ documentId?: AnyId;
152
+ input?: Object;
153
+ options?: Record<string, any>;
154
+ }): Promise<any>;
131
155
  }
132
156
  /**
133
157
  *
package/types/types.d.ts CHANGED
@@ -1,2 +1,3 @@
1
- import { ObjectId } from 'mongodb';
1
+ import { ClientSession, ObjectId } from 'mongodb';
2
2
  export type AnyId = string | number | ObjectId;
3
+ export type WithTransactionCallback = (session: ClientSession) => any;