@opra/mongodb 0.33.13 → 1.0.0-alpha.1

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