@opra/mongodb 0.33.13 → 1.0.0-alpha.10

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 (37) hide show
  1. package/cjs/adapter-utils/prepare-filter.js +22 -28
  2. package/cjs/adapter-utils/prepare-key-values.js +4 -3
  3. package/cjs/adapter-utils/prepare-patch.js +2 -1
  4. package/cjs/adapter-utils/prepare-projection.js +40 -39
  5. package/cjs/index.js +1 -2
  6. package/cjs/mongo-adapter.js +71 -1
  7. package/cjs/mongo-collection-service.js +73 -314
  8. package/cjs/mongo-entity-service.js +335 -0
  9. package/cjs/{mongo-array-service.js → mongo-nested-service.js} +252 -244
  10. package/cjs/mongo-service.js +146 -202
  11. package/cjs/mongo-singleton-service.js +29 -125
  12. package/esm/adapter-utils/prepare-filter.js +22 -28
  13. package/esm/adapter-utils/prepare-key-values.js +4 -3
  14. package/esm/adapter-utils/prepare-patch.js +2 -1
  15. package/esm/adapter-utils/prepare-projection.js +39 -38
  16. package/esm/index.js +1 -2
  17. package/esm/mongo-adapter.js +71 -1
  18. package/esm/mongo-collection-service.js +73 -313
  19. package/esm/mongo-entity-service.js +330 -0
  20. package/esm/mongo-nested-service.js +571 -0
  21. package/esm/mongo-service.js +147 -203
  22. package/esm/mongo-singleton-service.js +29 -125
  23. package/package.json +16 -10
  24. package/types/adapter-utils/prepare-filter.d.ts +2 -3
  25. package/types/adapter-utils/prepare-projection.d.ts +4 -13
  26. package/types/index.d.ts +1 -2
  27. package/types/mongo-adapter.d.ts +14 -1
  28. package/types/mongo-collection-service.d.ts +88 -251
  29. package/types/mongo-entity-service.d.ts +149 -0
  30. package/types/mongo-nested-service.d.ts +258 -0
  31. package/types/mongo-service.d.ts +218 -91
  32. package/types/mongo-singleton-service.d.ts +39 -148
  33. package/cjs/types.js +0 -2
  34. package/esm/mongo-array-service.js +0 -563
  35. package/esm/types.js +0 -1
  36. package/types/mongo-array-service.d.ts +0 -409
  37. package/types/types.d.ts +0 -3
@@ -1,27 +1,156 @@
1
- import mongodb, { TransactionOptions } from 'mongodb';
2
- import { StrictOmit, Type } from 'ts-gems';
1
+ import * as OpraCommon from '@opra/common';
2
+ import { ComplexType } from '@opra/common';
3
+ import { ServiceBase } from '@opra/core';
4
+ import mongodb, { Document, TransactionOptions } from 'mongodb';
5
+ import { PartialDTO, StrictOmit, Type } from 'ts-gems';
3
6
  import { IsObject } from 'valgen';
4
- import { ComplexType, PartialDTO } from '@opra/common';
5
- import { ApiService } from '@opra/core';
6
- import { AnyId, WithTransactionCallback } from './types.js';
7
+ import { MongoAdapter } from './mongo-adapter.js';
8
+ /**
9
+ * The namespace for the MongoService.
10
+ *
11
+ * @namespace MongoService
12
+ */
13
+ export declare namespace MongoService {
14
+ interface Options {
15
+ db?: MongoService<any>['db'];
16
+ session?: MongoService<any>['session'];
17
+ collectionName?: MongoService<any>['$collectionName'];
18
+ resourceName?: MongoService<any>['$resourceName'];
19
+ documentFilter?: MongoService<any>['$documentFilter'];
20
+ interceptor?: MongoService<any>['$interceptor'];
21
+ idGenerator?: MongoService<any>['$idGenerator'];
22
+ onError?: MongoService<any>['$onError'];
23
+ }
24
+ type CrudOp = 'create' | 'read' | 'update' | 'delete';
25
+ interface CommandInfo {
26
+ crud: CrudOp;
27
+ method: string;
28
+ byId: boolean;
29
+ documentId?: MongoAdapter.AnyId;
30
+ nestedId?: MongoAdapter.AnyId;
31
+ input?: Record<string, any>;
32
+ options?: Record<string, any>;
33
+ }
34
+ /**
35
+ * Represents options for "create" operation
36
+ *
37
+ * @interface
38
+ */
39
+ interface CreateOptions extends mongodb.InsertOneOptions {
40
+ projection?: string[];
41
+ }
42
+ /**
43
+ * Represents options for "count" operation
44
+ *
45
+ * @interface
46
+ * @template T - The type of the document.
47
+ */
48
+ interface CountOptions<T> extends mongodb.CountOptions {
49
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
50
+ }
51
+ /**
52
+ * Represents options for "delete" operation
53
+ *
54
+ * @interface
55
+ * @template T - The type of the document.
56
+ */
57
+ interface DeleteOptions<T> extends mongodb.DeleteOptions {
58
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
59
+ }
60
+ /**
61
+ * Represents options for "deleteMany" operation
62
+ *
63
+ * @interface
64
+ * @template T - The type of the document.
65
+ */
66
+ interface DeleteManyOptions<T> extends mongodb.DeleteOptions {
67
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
68
+ }
69
+ /**
70
+ * Represents options for "distinct" operation
71
+ *
72
+ * @interface
73
+ * @template T - The type of the document.
74
+ */
75
+ interface DistinctOptions<T> extends mongodb.DistinctOptions {
76
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
77
+ }
78
+ /**
79
+ * Represents options for "exists" operation
80
+ *
81
+ * @interface
82
+ */
83
+ interface ExistsOptions<T> extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
84
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
85
+ }
86
+ /**
87
+ * Represents options for checking the document exists
88
+ *
89
+ * @interface
90
+ */
91
+ interface ExistsOneOptions<T> extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
92
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
93
+ }
94
+ /**
95
+ * Represents options for "findOne" operation
96
+ *
97
+ * @interface
98
+ * @template T - The type of the document.
99
+ */
100
+ interface FindOneOptions<T> extends StrictOmit<FindManyOptions<T>, 'limit'> {
101
+ }
102
+ /**
103
+ * Represents options for "findMany" operation
104
+ *
105
+ * @interface
106
+ * @template T - The type of the document.
107
+ */
108
+ interface FindManyOptions<T> extends mongodb.AggregateOptions {
109
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
110
+ projection?: string | string[] | Document;
111
+ sort?: string[];
112
+ limit?: number;
113
+ skip?: number;
114
+ }
115
+ /**
116
+ * Represents options for "update" operation
117
+ *
118
+ * @interface
119
+ * @template T - The type of the document.
120
+ */
121
+ interface UpdateOptions<T> extends StrictOmit<mongodb.FindOneAndUpdateOptions, 'projection' | 'returnDocument' | 'includeResultMetadata'> {
122
+ projection?: string | string[] | Document;
123
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
124
+ }
125
+ /**
126
+ * Represents options for "updateMany" operation
127
+ *
128
+ * @interface
129
+ * @template T - The type of the document.
130
+ */
131
+ interface UpdateManyOptions<T> extends StrictOmit<mongodb.UpdateOptions, 'upsert'> {
132
+ filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
133
+ }
134
+ }
7
135
  /**
8
136
  * Class representing a MongoDB service for interacting with a collection.
9
- * @extends ApiService
137
+ * @extends ServiceBase
10
138
  * @template T - The type of the documents in the collection.
11
139
  */
12
- export declare class MongoService<T extends mongodb.Document> extends ApiService {
13
- protected _encoders: Record<string, IsObject.Validator<T>>;
14
- protected _decoder?: IsObject.Validator<T>;
15
- protected _dataType: Type | string;
140
+ export declare class MongoService<T extends mongodb.Document = mongodb.Document> extends ServiceBase {
141
+ protected _dataType_: Type | string;
142
+ protected _dataType: ComplexType;
143
+ protected _inputCodecs: Record<string, IsObject.Validator<T>>;
144
+ protected _outputCodecs: Record<string, IsObject.Validator<T>>;
16
145
  /**
17
146
  * Represents the name of a collection in MongoDB
18
147
  */
19
- collectionName?: string | ((_this: any) => string);
148
+ $collectionName?: string | ((_this: any) => string);
20
149
  /**
21
150
  * Represents the name of a resource.
22
151
  * @type {string}
23
152
  */
24
- resourceName?: string | ((_this: any) => string);
153
+ $resourceName?: string | ((_this: any) => string);
25
154
  /**
26
155
  * Represents a MongoDB database object.
27
156
  */
@@ -33,9 +162,8 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
33
162
  /**
34
163
  * Generates a new id for new inserting Document.
35
164
  *
36
- * @protected
37
165
  */
38
- $idGenerator?: (_this: any) => AnyId;
166
+ $idGenerator?: (_this: any) => MongoAdapter.AnyId;
39
167
  /**
40
168
  * Callback function for handling errors.
41
169
  *
@@ -43,47 +171,68 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
43
171
  * @param _this - The context object.
44
172
  */
45
173
  $onError?: (error: unknown, _this: any) => void | Promise<void>;
174
+ /**
175
+ * Represents a common filter function for a MongoService.
176
+ *
177
+ * @type {FilterInput | Function}
178
+ */
179
+ $documentFilter?: MongoAdapter.FilterInput | ((args: MongoService.CommandInfo, _this: this) => MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined);
180
+ /**
181
+ * Interceptor function for handling callback execution with provided arguments.
182
+ *
183
+ * @param callback - The callback function to be intercepted.
184
+ * @param {MongoService.CommandInfo} info - The arguments object containing the following properties:
185
+ * @param _this - The reference to the current object.
186
+ * @returns - The promise that resolves to the result of the callback execution.
187
+ */
188
+ $interceptor?: (callback: () => any, info: MongoService.CommandInfo, _this: any) => Promise<any>;
46
189
  /**
47
190
  * Constructs a new instance
48
191
  *
49
- * @param dataType - The data type of the array elements.
50
- * @param [options] - The options for the array service.
192
+ * @param dataType - The data type of the returning results
193
+ * @param [options] - The options for the service
51
194
  * @constructor
52
195
  */
53
196
  constructor(dataType: Type | string, options?: MongoService.Options);
54
197
  /**
55
- * Retrieves the data type of the document
198
+ * Retrieves the collection name.
199
+ *
200
+ * @protected
201
+ * @returns The collection name.
202
+ * @throws {Error} If the collection name is not defined.
203
+ */
204
+ getCollectionName(): string;
205
+ /**
206
+ * Retrieves the resource name.
207
+ *
208
+ * @protected
209
+ * @returns {string} The resource name.
210
+ * @throws {Error} If the resource name is not defined.
211
+ */
212
+ getResourceName(): string;
213
+ /**
214
+ * Retrieves the OPRA data type
56
215
  *
57
216
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
58
217
  */
59
- getDataType(): ComplexType;
218
+ get dataType(): ComplexType;
60
219
  /**
61
- * Retrieves the encoder for the specified operation.
220
+ * Retrieves the codec for the specified operation.
62
221
  *
63
222
  * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
64
223
  */
65
- getEncoder(operation: 'create' | 'update'): IsObject.Validator<T>;
224
+ getInputCodec(operation: string): IsObject.Validator<T>;
66
225
  /**
67
- * Retrieves the decoder.
226
+ * Retrieves the codec.
68
227
  */
69
- getDecoder(): IsObject.Validator<T>;
228
+ getOutputCodec(operation: string): IsObject.Validator<T>;
70
229
  /**
71
230
  * Executes the provided function within a transaction.
72
231
  *
73
232
  * @param callback - The function to be executed within the transaction.
74
233
  * @param [options] - Optional options for the transaction.
75
234
  */
76
- withTransaction(callback: WithTransactionCallback, options?: TransactionOptions): Promise<any>;
77
- /**
78
- * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field,
79
- * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
80
- * can be overridden by setting the **forceServerObjectId** flag.
81
- *
82
- * @param doc - The document to insert
83
- * @param options - Optional settings for the command
84
- * @protected
85
- */
86
- protected __insertOne(doc: mongodb.OptionalUnlessRequiredId<T>, options?: mongodb.InsertOneOptions): Promise<mongodb.InsertOneResult<T>>;
235
+ withTransaction(callback: MongoAdapter.WithTransactionCallback, options?: TransactionOptions): Promise<any>;
87
236
  /**
88
237
  * Gets the number of documents matching the filter.
89
238
  *
@@ -91,84 +240,92 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
91
240
  * @param options - The options for counting documents.
92
241
  * @protected
93
242
  */
94
- protected __countDocuments(filter?: mongodb.Filter<T>, options?: mongodb.CountOptions): Promise<number>;
243
+ protected _dbCountDocuments(filter?: mongodb.Filter<T>, options?: mongodb.CountOptions): Promise<number>;
95
244
  /**
96
- * Delete a document from a collection
245
+ * Acquires a connection and performs Collection.deleteOne operation
97
246
  *
98
247
  * @param filter - The filter used to select the document to remove
99
248
  * @param options - Optional settings for the command
100
249
  * @protected
101
250
  */
102
- protected __deleteOne(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
251
+ protected _dbDeleteOne(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
103
252
  /**
104
- * Delete multiple documents from a collection
253
+ * Acquires a connection and performs Collection.deleteMany operation
105
254
  *
106
255
  * @param filter - The filter used to select the documents to remove
107
256
  * @param options - Optional settings for the command
108
257
  * @protected
109
258
  */
110
- protected __deleteMany(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
259
+ protected _dbDeleteMany(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
111
260
  /**
112
- * Gets the number of documents matching the filter.
261
+ * Acquires a connection and performs Collection.distinct operation
113
262
  *
114
263
  * @param field - Field of the document to find distinct values for
115
264
  * @param filter - The filter for filtering the set of documents to which we apply the distinct filter.
116
265
  * @param options - Optional settings for the command
117
266
  * @protected
118
267
  */
119
- protected __distinct(field: string, filter?: mongodb.Filter<T>, options?: mongodb.DistinctOptions): Promise<any[]>;
268
+ protected _dbDistinct(field: string, filter?: mongodb.Filter<T>, options?: mongodb.DistinctOptions): Promise<any[]>;
120
269
  /**
121
- * Execute an aggregation framework pipeline against the collection, needs MongoDB \>= 2.2
270
+ * Acquires a connection and performs Collection.aggregate operation
122
271
  *
123
272
  * @param pipeline - An array of aggregation pipelines to execute
124
273
  * @param options - Optional settings for the command
125
274
  * @protected
126
275
  */
127
- protected __aggregate(pipeline?: mongodb.Document[], options?: mongodb.AggregateOptions): Promise<mongodb.AggregationCursor<T>>;
276
+ protected _dbAggregate(pipeline?: mongodb.Document[], options?: mongodb.AggregateOptions): Promise<mongodb.AggregationCursor<T>>;
128
277
  /**
129
- * Fetches the first document that matches the filter
278
+ * Acquires a connection and performs Collection.findOne operation
130
279
  *
131
280
  * @param filter - Query for find Operation
132
281
  * @param options - Optional settings for the command
133
282
  * @protected
134
283
  */
135
- protected __findOne(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<PartialDTO<T> | undefined>;
284
+ protected _dbFindOne(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<PartialDTO<T> | undefined>;
136
285
  /**
137
- * Creates a cursor for a filter that can be used to iterate over results from MongoDB
286
+ * Acquires a connection and performs Collection.find operation
138
287
  *
139
288
  * @param filter - The filter predicate. If unspecified,
140
289
  * then all documents in the collection will match the predicate
141
290
  * @param options - Optional settings for the command
142
291
  * @protected
143
292
  */
144
- protected __find(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<mongodb.FindCursor<T>>;
293
+ protected _dbFind(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<mongodb.FindCursor<T>>;
294
+ /**
295
+ * Acquires a connection and performs Collection.insertOne operation
296
+ *
297
+ * @param doc - The document to insert
298
+ * @param options - Optional settings for the command
299
+ * @protected
300
+ */
301
+ protected _dbInsertOne(doc: mongodb.OptionalUnlessRequiredId<T>, options?: mongodb.InsertOneOptions): Promise<mongodb.InsertOneResult<T>>;
145
302
  /**
146
- * Update a single document in a collection
303
+ * Acquires a connection and performs Collection.updateOne operation
147
304
  *
148
305
  * @param filter - The filter used to select the document to update
149
306
  * @param update - The update operations to be applied to the document
150
307
  * @param options - Optional settings for the command
151
308
  * @protected
152
309
  */
153
- protected __updateOne(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T>, options?: mongodb.UpdateOptions): Promise<mongodb.UpdateResult<T>>;
310
+ protected _dbUpdateOne(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T>, options?: mongodb.UpdateOptions): Promise<mongodb.UpdateResult<T>>;
154
311
  /**
155
- * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.
312
+ * Acquires a connection and performs Collection.findOneAndUpdate operation
156
313
  *
157
314
  * @param filter - The filter used to select the document to update
158
315
  * @param update - Update operations to be performed on the document
159
316
  * @param options - Optional settings for the command
160
317
  * @protected
161
318
  */
162
- protected __findOneAndUpdate(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T>, options?: mongodb.FindOneAndUpdateOptions): Promise<mongodb.WithId<T> | null>;
319
+ protected _dbFindOneAndUpdate(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T>, options?: mongodb.FindOneAndUpdateOptions): Promise<mongodb.WithId<T> | null>;
163
320
  /**
164
- * Update multiple documents in a collection
321
+ * Acquires a connection and performs Collection.updateMany operation
165
322
  *
166
323
  * @param filter - The filter used to select the documents to update
167
324
  * @param update - The update operations to be applied to the documents
168
325
  * @param options - Optional settings for the command
169
326
  * @protected
170
327
  */
171
- protected __updateMany(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<mongodb.UpdateResult<T>>;
328
+ protected _dbUpdateMany(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<mongodb.UpdateResult<T>>;
172
329
  /**
173
330
  * Retrieves the database connection.
174
331
  *
@@ -193,50 +350,20 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
193
350
  */
194
351
  protected getCollection(db: mongodb.Db): Promise<mongodb.Collection<T>>;
195
352
  /**
196
- * Retrieves the collection name.
197
- *
198
- * @protected
199
- * @returns The collection name.
200
- * @throws {Error} If the collection name is not defined.
201
- */
202
- getCollectionName(): string;
203
- /**
204
- * Retrieves the resource name.
353
+ * Generates an ID.
205
354
  *
206
355
  * @protected
207
- * @returns {string} The resource name.
208
- * @throws {Error} If the collection name is not defined.
356
+ * @returns {MongoAdapter.AnyId} The generated ID.
209
357
  */
210
- getResourceName(): string;
358
+ protected _generateId(): MongoAdapter.AnyId;
211
359
  /**
212
- * Generates an encoder for the specified operation.
360
+ * Retrieves the common filter used for querying documents.
361
+ * This method is mostly used for security issues like securing multi-tenant applications.
213
362
  *
214
- * @param operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
215
363
  * @protected
216
- * @returns - The generated encoder for the specified operation.
364
+ * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
365
+ * that resolves to the common filter, or undefined if not available.
217
366
  */
218
- protected _generateEncoder(operation: 'create' | 'update'): IsObject.Validator<T>;
219
- /**
220
- * Generates an encoder for the specified operation.
221
- *
222
- * @protected
223
- * @returns - The generated encoder for the specified operation.
224
- */
225
- protected _generateDecoder(): IsObject.Validator<T>;
226
- }
227
- /**
228
- * The namespace for the MongoService.
229
- *
230
- * @namespace MongoService
231
- */
232
- export declare namespace MongoService {
233
- interface Options {
234
- db?: MongoService<any>['db'];
235
- collectionName?: MongoService<any>['collectionName'];
236
- resourceName?: MongoService<any>['resourceName'];
237
- session?: MongoService<any>['session'];
238
- idGenerator?: MongoService<any>['$idGenerator'];
239
- onError?: MongoService<any>['$onError'];
240
- }
241
- type CrudOp = 'create' | 'read' | 'update' | 'delete';
367
+ protected _getDocumentFilter(info: MongoService.CommandInfo): MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined;
368
+ protected _intercept(callback: (...args: any[]) => any, info: MongoService.CommandInfo): Promise<any>;
242
369
  }