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