@opra/mongodb 0.32.3 → 0.32.5

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,7 +1,8 @@
1
1
  import mongodb from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
+ import { DTO, PartialDTO, PatchDTO } from '@opra/common';
3
4
  import * as OpraCommon from '@opra/common';
4
- import { PartialInput, PartialOutput } from '@opra/core';
5
+ import { FilterInput } from './adapter-utils/prepare-filter.js';
5
6
  import { MongoService } from './mongo-service.js';
6
7
  import { AnyId } from './types.js';
7
8
  /**
@@ -21,25 +22,59 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
21
22
  * @type {number}
22
23
  */
23
24
  defaultLimit: number;
25
+ /**
26
+ * Represents a common filter function for a MongoCollectionService.
27
+ *
28
+ * @type {FilterInput | Function}
29
+ */
30
+ $documentFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
31
+ /**
32
+ * Interceptor function for handling callback execution with provided arguments.
33
+ *
34
+ * @param {Function} callback - The callback function to be intercepted.
35
+ * @param {Object} args - The arguments object containing the following properties:
36
+ * @param {string} crud - The CRUD operation type.
37
+ * @param {string} method - The method name.
38
+ * @param {AnyId} documentId - The document ID (optional).
39
+ * @param {Object} input - The input object (optional).
40
+ * @param {Object} options - Additional options (optional).
41
+ * @param {Object} _this - The reference to the current object.
42
+ * @returns {Promise<any>} - The promise that resolves to the result of the callback execution.
43
+ */
44
+ $interceptor?: (callback: (...args: any[]) => any, args: {
45
+ crud: MongoService.CrudOp;
46
+ method: string;
47
+ documentId?: AnyId;
48
+ input?: Object;
49
+ options?: Record<string, any>;
50
+ }, _this: any) => Promise<any>;
51
+ /**
52
+ * Constructs a new instance
53
+ *
54
+ * @param {Type | string} dataType - The data type of the array elements.
55
+ * @param {MongoCollectionService.Options} [options] - The options for the array service.
56
+ * @constructor
57
+ */
24
58
  constructor(dataType: Type | string, options?: MongoCollectionService.Options);
25
59
  /**
26
60
  * Asserts the existence of a resource with the given ID.
27
61
  * Throws a ResourceNotFoundError if the resource does not exist.
28
62
  *
29
63
  * @param {AnyId} id - The ID of the resource to assert.
64
+ * @param {MongoCollectionService.ExistsOptions} [options] - Optional options for checking the existence.
30
65
  * @returns {Promise<void>} - A Promise that resolves when the resource exists.
31
66
  * @throws {ResourceNotFoundError} - If the resource does not exist.
32
67
  */
33
- assert(id: AnyId): Promise<void>;
68
+ assert(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<void>;
34
69
  /**
35
70
  * Creates a new document in the MongoDB collection.
36
71
  *
37
- * @param {PartialInput<T>} input - The input data for creating the document.
72
+ * @param {DTO<T>} input - The input data for creating the document.
38
73
  * @param {MongoCollectionService.CreateOptions} [options] - The options for creating the document.
39
- * @returns {Promise<PartialOutput<T>>} A promise that resolves to the created document.
74
+ * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
40
75
  * @throws {Error} if an unknown error occurs while creating the document.
41
76
  */
42
- create(input: PartialInput<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialOutput<T>>;
77
+ create(input: DTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
43
78
  /**
44
79
  * Returns the count of documents in the collection based on the provided options.
45
80
  *
@@ -66,24 +101,25 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
66
101
  * Checks if an object with the given id exists.
67
102
  *
68
103
  * @param {AnyId} id - The id of the object to check.
104
+ * @param {MongoCollectionService.ExistsOptions} [options] - The options for the aggregation query (optional).
69
105
  * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
70
106
  */
71
- exists(id: AnyId): Promise<boolean>;
107
+ exists(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<boolean>;
72
108
  /**
73
109
  * Finds a document by its ID.
74
110
  *
75
111
  * @param {AnyId} id - The ID of the document.
76
112
  * @param {MongoCollectionService.FindOneOptions} [options] - The options for the find query.
77
- * @return {Promise<PartialOutput<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
113
+ * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
78
114
  */
79
- findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialOutput<T | undefined>>;
115
+ findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
80
116
  /**
81
117
  * Finds a document in the collection that matches the specified options.
82
118
  *
83
119
  * @param {MongoCollectionService.FindOneOptions} options - The options for the query.
84
- * @return {Promise<PartialOutput<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
120
+ * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
85
121
  */
86
- findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialOutput<T> | undefined>;
122
+ findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
87
123
  /**
88
124
  * Finds multiple documents in the MongoDB collection.
89
125
  *
@@ -99,51 +135,60 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
99
135
  *
100
136
  * @return A Promise that resolves to an array of partial outputs of type T.
101
137
  */
102
- findMany(options?: MongoCollectionService.FindManyOptions<T>): Promise<PartialOutput<T>[]>;
138
+ findMany(options?: MongoCollectionService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
103
139
  /**
104
140
  * Retrieves a document from the collection by its ID. Throws error if not found.
105
141
  *
106
142
  * @param {*} id - The ID of the document to retrieve.
107
143
  * @param {MongoCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
108
- * @returns {Promise<PartialOutput<T>>} - A promise that resolves to the retrieved document,
144
+ * @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
109
145
  * or rejects with a ResourceNotFoundError if the document does not exist.
110
146
  * @throws {ResourceNotFoundError} - If the document with the specified ID does not exist.
111
147
  */
112
- get(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialOutput<T>>;
148
+ get(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T>>;
113
149
  /**
114
150
  * Updates a document with the given id in the collection.
115
151
  *
116
152
  * @param {any} id - The id of the document to update.
117
- * @param {PartialInput<T>} input - The partial input object containing the fields to update.
153
+ * @param {PatchDTO<T>} input - The partial input object containing the fields to update.
118
154
  * @param {MongoCollectionService.UpdateOptions<T>} [options] - The options for the update operation.
119
- * @returns {Promise<PartialOutput<T> | undefined>} A promise that resolves to the updated document or
155
+ * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
120
156
  * undefined if the document was not found.
121
157
  */
122
- update(id: any, input: PartialInput<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialOutput<T> | undefined>;
158
+ update(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
123
159
  /**
124
160
  * Updates a document in the collection with the specified ID.
125
161
  *
126
162
  * @param {any} id - The ID of the document to update.
127
- * @param {PartialInput<T>} input - The partial input data to update the document with.
163
+ * @param {PatchDTO<T>} input - The partial input data to update the document with.
128
164
  * @param {MongoCollectionService.UpdateOptions<T>} options - The options for updating the document.
129
165
  * @returns {Promise<number>} - A promise that resolves to the number of documents modified.
130
166
  */
131
- updateOnly(id: any, input: PartialInput<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
167
+ updateOnly(id: AnyId, input: PatchDTO<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
132
168
  /**
133
169
  * Updates multiple documents in the collection based on the specified input and options.
134
170
  *
135
- * @param {OpraCommon.PartialInput<T>} input - The partial input to update the documents with.
171
+ * @param {PatchDTO<T>} input - The partial input to update the documents with.
136
172
  * @param {MongoCollectionService.UpdateManyOptions<T>} options - The options for updating the documents.
137
173
  * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
138
174
  */
139
- updateMany(input: OpraCommon.PartialInput<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
175
+ updateMany(input: PatchDTO<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
140
176
  /**
141
- * Generates a new id for new inserting Document.
177
+ * Generates an ID.
142
178
  *
143
179
  * @protected
144
- * @returns {AnyId} A new instance of AnyId.
180
+ * @returns {AnyId} The generated ID.
145
181
  */
146
182
  protected _generateId(): AnyId;
183
+ /**
184
+ * Retrieves the common filter used for querying documents.
185
+ * This method is mostly used for security issues like securing multi-tenant applications.
186
+ *
187
+ * @protected
188
+ * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
189
+ * that resolves to the common filter, or undefined if not available.
190
+ */
191
+ protected _getDocumentFilter(): FilterInput | Promise<FilterInput> | undefined;
147
192
  }
148
193
  /**
149
194
  *
@@ -157,8 +202,10 @@ export declare namespace MongoCollectionService {
157
202
  * @extends MongoService.Options
158
203
  */
159
204
  interface Options extends MongoService.Options {
160
- collectionKey?: string;
161
- defaultLimit?: number;
205
+ collectionKey?: MongoCollectionService<any>['collectionKey'];
206
+ defaultLimit?: MongoCollectionService<any>['defaultLimit'];
207
+ documentFilter?: MongoCollectionService<any>['$documentFilter'];
208
+ interceptor?: MongoCollectionService<any>['$interceptor'];
162
209
  }
163
210
  /**
164
211
  * Represents options for creating a new document.
@@ -242,4 +289,11 @@ export declare namespace MongoCollectionService {
242
289
  interface UpdateManyOptions<T> extends StrictOmit<mongodb.UpdateOptions, 'upsert'> {
243
290
  filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
244
291
  }
292
+ /**
293
+ * Represents options for checking the document exists
294
+ *
295
+ * @interface
296
+ */
297
+ interface ExistsOptions extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
298
+ }
245
299
  }
@@ -1,15 +1,9 @@
1
1
  import mongodb from 'mongodb';
2
2
  import { StrictOmit, Type } from 'ts-gems';
3
3
  import vg from 'valgen';
4
- import { ComplexType } from '@opra/common';
5
- import { ApiService, PartialOutput, RequestContext } from '@opra/core';
6
- export declare namespace MongoService {
7
- interface Options {
8
- db?: mongodb.Db;
9
- collectionName?: string;
10
- resourceName?: string;
11
- }
12
- }
4
+ import { ComplexType, PartialDTO } from '@opra/common';
5
+ import { ApiService, RequestContext } from '@opra/core';
6
+ import { AnyId } from './types.js';
13
7
  /**
14
8
  * Class representing a MongoDB service for interacting with a collection.
15
9
  * @extends ApiService
@@ -19,10 +13,51 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
19
13
  protected _encoders: Record<string, vg.ObjectValidator<T>>;
20
14
  protected _decoder?: vg.ObjectValidator<T>;
21
15
  protected _dataType: Type | string;
22
- collectionName?: string;
23
- db?: mongodb.Db;
24
- session?: mongodb.ClientSession;
25
- resourceName?: string;
16
+ /**
17
+ * Represents the name of a collection in MongoDB
18
+ *
19
+ * @type {string | Function} collectionName
20
+ */
21
+ collectionName?: string | ((_this: any) => string);
22
+ /**
23
+ * Represents the name of a resource.
24
+ *
25
+ * @type {string}
26
+ */
27
+ resourceName?: string | ((_this: any) => string);
28
+ /**
29
+ * Represents a MongoDB database object.
30
+ * @type {mongodb.Db | Function}
31
+ */
32
+ db?: mongodb.Db | ((_this: any) => mongodb.Db);
33
+ /**
34
+ * Represents a MongoDB ClientSession.
35
+ *
36
+ * @type {mongodb.ClientSession}
37
+ */
38
+ session?: mongodb.ClientSession | ((_this: any) => mongodb.ClientSession);
39
+ /**
40
+ * Generates a new id for new inserting Document.
41
+ *
42
+ * @protected
43
+ * @returns {AnyId} A new instance of AnyId.
44
+ */
45
+ $idGenerator?: (_this: any) => AnyId;
46
+ /**
47
+ * Callback function for handling errors.
48
+ *
49
+ * @param {unknown} error - The error object.
50
+ * @param {object} _this - The context object.
51
+ * @return {void|Promise<void>} - A void value or a promise that resolves to void.
52
+ */
53
+ $onError?: (error: unknown, _this: any) => void | Promise<void>;
54
+ /**
55
+ * Constructs a new instance
56
+ *
57
+ * @param {Type | string} dataType - The data type of the array elements.
58
+ * @param {MongoService.Options} [options] - The options for the array service.
59
+ * @constructor
60
+ */
26
61
  constructor(dataType: Type | string, options?: MongoService.Options);
27
62
  /**
28
63
  * Retrieves the data type of the document
@@ -32,64 +67,75 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
32
67
  */
33
68
  getDataType(): ComplexType;
34
69
  /**
35
- * Creates a new instance for given context
70
+ * Retrieves the encoder for the specified operation.
71
+ *
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.
74
+ */
75
+ getEncoder(operation: 'create' | 'update'): vg.ObjectValidator<T>;
76
+ /**
77
+ * Retrieves the decoder.
36
78
  *
37
- * @param source
79
+ * @returns {vg.ObjectValidator<T>} - The encoder for the specified operation.
38
80
  */
39
- forContext(source: ApiService): this;
40
- forContext(context: RequestContext, attributes?: {
41
- db?: mongodb.Db;
42
- session?: mongodb.ClientSession;
43
- }): this;
81
+ getDecoder(): vg.ObjectValidator<T>;
44
82
  /**
45
83
  * Inserts a single document into MongoDB. If documents passed in do not contain the **_id** field,
46
84
  * one will be added to each of the documents missing it by the driver, mutating the document. This behavior
47
85
  * can be overridden by setting the **forceServerObjectId** flag.
48
86
  *
49
- * @param doc
50
- * @param options
87
+ * @param {T} doc - The document to be inserted.
88
+ * @param {mongodb.InsertOneOptions} options - The options for the insert operation.
89
+ * @returns {Promise<mongodb.InsertOneWriteOpResult<mongodb.OptionalId<T>>>} - A promise that resolves with the result of the insert operation.
51
90
  * @protected
52
91
  */
53
92
  protected __insertOne(doc: mongodb.OptionalUnlessRequiredId<T>, options?: mongodb.InsertOneOptions): Promise<mongodb.InsertOneResult<T>>;
54
93
  /**
55
94
  * Gets the number of documents matching the filter.
56
95
  *
57
- * @param filter
58
- * @param options
96
+ * @param {mongodb.Filter<T>} filter - The filter used to match documents.
97
+ * @param {mongodb.CountOptions} options - The options for counting documents.
98
+ * @returns {Promise<number>} - The number of documents matching the filter.
59
99
  * @protected
60
100
  */
61
101
  protected __countDocuments(filter?: mongodb.Filter<T>, options?: mongodb.CountOptions): Promise<number>;
62
102
  /**
63
103
  * Delete a document from a collection
64
104
  *
65
- * @param filter - The filter used to select the document to remove
66
- * @param options - Optional settings for the command
105
+ * @param {mongodb.Filter<T>} filter - The filter used to select the document to remove
106
+ * @param {mongodb.DeleteOptions} options - Optional settings for the command
107
+ * @return {Promise<mongodb.DeleteResult>} A Promise that resolves to the result of the delete operation
67
108
  */
68
109
  protected __deleteOne(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
69
110
  /**
70
- * Delete multiple documents from a collection
111
+ * Deletes multiple documents from a collection.
71
112
  *
72
- * @param filter
73
- * @param options
113
+ * @param {mongodb.Filter<T>} [filter] - The filter object specifying the documents to delete.
114
+ * If not provided, all documents in the collection will be deleted.
115
+ * @param {mongodb.DeleteOptions} [options] - The options for the delete operation.
116
+ * @returns {Promise<mongodb.DeleteResult>} A promise that resolves with the delete result object.
74
117
  * @protected
75
118
  */
76
119
  protected __deleteMany(filter?: mongodb.Filter<T>, options?: mongodb.DeleteOptions): Promise<mongodb.DeleteResult>;
77
120
  /**
78
- * Create a new Change Stream, watching for new changes (insertions, updates, replacements, deletions, and invalidations) in this collection.
121
+ * Create a new Change Stream, watching for new changes
122
+ * (insertions, updates, replacements, deletions, and invalidations) in this collection.
79
123
  *
80
- * @param pipeline
81
- * @param options
124
+ * @param {mongodb.Document[]} pipeline - The pipeline of aggregation stages to apply to the collection.
125
+ * @param {mongodb.AggregateOptions} options - The options to customize the aggregation.
126
+ * @returns {Promise<mongodb.ChangeStream<T>>} - A promise that resolves to a Change Stream that represents the result of the aggregation.
82
127
  * @protected
83
128
  */
84
129
  protected __aggregate(pipeline?: mongodb.Document[], options?: mongodb.AggregateOptions): Promise<mongodb.AggregationCursor<T>>;
85
130
  /**
86
131
  * Fetches the first document that matches the filter
87
132
  *
88
- * @param filter
89
- * @param options
133
+ * @param {mongodb.Filter<T>} filter - The filter object to match documents against
134
+ * @param {mongodb.FindOptions} [options] - The options to use when querying the collection
90
135
  * @protected
136
+ * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the first matching document, or undefined if no match is found
91
137
  */
92
- protected __findOne(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<PartialOutput<T> | undefined>;
138
+ protected __findOne(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<PartialDTO<T> | undefined>;
93
139
  /**
94
140
  * Creates a cursor for a filter that can be used to iterate over results from MongoDB.
95
141
  *
@@ -100,58 +146,93 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
100
146
  */
101
147
  protected __find(filter: mongodb.Filter<T>, options?: mongodb.FindOptions): Promise<mongodb.FindCursor<T>>;
102
148
  /**
103
- * Update a single document in a collection
149
+ * Update a single document in a collection.
104
150
  *
105
- * @param {mongodb.Filter<T>} filter - The filter to select the document(s) to update
106
- * @param {mongodb.UpdateFilter<T>} update - The update operation to be applied on the selected document(s)
107
- * @param {mongodb.UpdateOptions} [options] - Optional settings for the update operation
151
+ * @param {mongodb.Filter<T>} filter - The filter to select the document(s) to update.
152
+ * @param {mongodb.UpdateFilter<T>} update - The update operation to be applied on the selected document(s).
153
+ * @param {mongodb.UpdateOptions} [options] - Optional settings for the update operation.
108
154
  * @protected
109
- * @returns {Promise<mongodb.UpdateResult>} A promise that resolves to the result of the update operation
155
+ * @returns {Promise<mongodb.UpdateResult>} - A promise that resolves to the result of the update operation.
110
156
  */
111
157
  protected __updateOne(filter: mongodb.Filter<T>, update: mongodb.UpdateFilter<T>, options?: mongodb.UpdateOptions): Promise<mongodb.UpdateResult<T>>;
112
158
  /**
113
159
  * Find a document and update it in one atomic operation. Requires a write lock for the duration of the operation.
114
160
  *
115
- * @param filter
116
- * @param doc
117
- * @param options
161
+ * @param {mongodb.Filter<T>} filter - The filter to select the document to be updated.
162
+ * @param {mongodb.UpdateFilter<T>} doc - The update document.
163
+ * @param {mongodb.FindOneAndUpdateOptions} [options] - Optional options for the find one and update operation.
164
+ * @returns {Promise<T | null>} - A promise that resolves to the updated document or null if no document matched the filter.
118
165
  * @protected
119
166
  */
120
167
  protected __findOneAndUpdate(filter: mongodb.Filter<T>, doc: mongodb.UpdateFilter<T>, options?: mongodb.FindOneAndUpdateOptions): Promise<mongodb.WithId<T> | null>;
121
168
  /**
122
- * Update multiple documents in a collection
169
+ * Update multiple documents in a collection.
123
170
  *
124
- * @param filter
125
- * @param doc
126
- * @param options
171
+ * @param {mongodb.Filter<T>} filter - The filter used to select the documents to be updated.
172
+ * @param {mongodb.UpdateFilter<T> | Partial<T>} doc - The updates to be applied to the selected documents.
173
+ * @param {StrictOmit<mongodb.UpdateOptions, 'upsert'>} [options] - The optional settings for the update operation.
174
+ * @return {Promise<mongodb.UpdateResult>} - A Promise that resolves to the result of the update operation.
127
175
  * @protected
128
176
  */
129
177
  protected __updateMany(filter: mongodb.Filter<T>, doc: mongodb.UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<mongodb.UpdateResult<T>>;
130
- protected _onError(error: unknown): Promise<void>;
131
- protected getDatabase(): mongodb.Db | Promise<mongodb.Db>;
178
+ /**
179
+ * Retrieves the database connection.
180
+ *
181
+ * @protected
182
+ *
183
+ * @returns {Promise<mongodb.Db>} The database connection.
184
+ * @throws {Error} If the context or database is not set.
185
+ */
186
+ protected getDatabase(): mongodb.Db;
187
+ /**
188
+ * Retrieves the database session.
189
+ *
190
+ * @protected
191
+ *
192
+ * @returns {Promise<mongodb.ClientSession>} The database connection.
193
+ * @throws {Error} If the context or database is not set.
194
+ */
195
+ protected getSession(): mongodb.ClientSession | undefined;
196
+ /**
197
+ * Retrieves a MongoDB collection from the given database.
198
+ *
199
+ * @param {mongodb.Db} db - The MongoDB database.
200
+ * @protected
201
+ * @returns {Promise<mongodb.Collection<T>>} A promise that resolves to the MongoDB collection.
202
+ */
132
203
  protected getCollection(db: mongodb.Db): Promise<mongodb.Collection<T>>;
133
- protected getCollectionName(): string;
134
- protected _cacheMatch(service: MongoService<any>, context: RequestContext, attributes?: any): boolean;
135
204
  /**
136
- * Retrieves the encoder for the specified operation.
205
+ * Retrieves the collection name.
137
206
  *
138
- * @param {String} operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
139
- * @returns {vg.ObjectValidator<T>} - The encoder for the specified operation.
207
+ * @protected
208
+ * @returns {string} The collection name.
209
+ * @throws {Error} If the collection name is not defined.
140
210
  */
141
- getEncoder(operation: 'create' | 'update'): vg.ObjectValidator<T>;
211
+ getCollectionName(): string;
142
212
  /**
143
- * Retrieves the decoder.
213
+ * Retrieves the resource name.
144
214
  *
145
- * @returns {vg.ObjectValidator<T>} - The encoder for the specified operation.
215
+ * @protected
216
+ * @returns {string} The collection name.
217
+ * @throws {Error} If the collection name is not defined.
146
218
  */
147
- getDecoder(): vg.ObjectValidator<T>;
219
+ 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;
148
231
  /**
149
232
  * Generates an encoder for the specified operation.
150
233
  *
151
234
  * @param {string} operation - The operation to generate the encoder for. Must be either 'create' or 'update'.
152
- *
153
235
  * @protected
154
- *
155
236
  * @returns {vg.Validator} - The generated encoder for the specified operation.
156
237
  */
157
238
  protected _generateEncoder(operation: 'create' | 'update'): vg.ObjectValidator<T>;
@@ -159,9 +240,23 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
159
240
  * Generates an encoder for the specified operation.
160
241
  *
161
242
  * @protected
162
- *
163
243
  * @returns {vg.Validator} - The generated encoder for the specified operation.
164
244
  */
165
245
  protected _generateDecoder(): vg.ObjectValidator<T>;
166
- protected onError?(error: unknown): void | Promise<void>;
246
+ }
247
+ /**
248
+ * The namespace for the MongoService.
249
+ *
250
+ * @namespace MongoService
251
+ */
252
+ export declare namespace MongoService {
253
+ interface Options {
254
+ db?: MongoService<any>['db'];
255
+ collectionName?: MongoService<any>['collectionName'];
256
+ resourceName?: MongoService<any>['resourceName'];
257
+ session?: MongoService<any>['session'];
258
+ idGenerator?: MongoService<any>['$idGenerator'];
259
+ onError?: MongoService<any>['$onError'];
260
+ }
261
+ type CrudOp = 'create' | 'read' | 'update' | 'delete';
167
262
  }