@opra/mongodb 0.33.13 → 1.0.0-alpha.2
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.
- package/cjs/adapter-utils/prepare-filter.js +20 -28
- package/cjs/adapter-utils/prepare-projection.js +40 -39
- package/cjs/index.js +1 -2
- package/cjs/mongo-adapter.js +66 -1
- package/cjs/mongo-collection-service.js +72 -313
- package/cjs/mongo-entity-service.js +329 -0
- package/cjs/{mongo-array-service.js → mongo-nested-service.js} +231 -225
- package/cjs/mongo-service.js +124 -183
- package/cjs/mongo-singleton-service.js +28 -124
- package/esm/adapter-utils/prepare-filter.js +20 -28
- package/esm/adapter-utils/prepare-projection.js +39 -38
- package/esm/index.js +1 -2
- package/esm/mongo-adapter.js +66 -1
- package/esm/mongo-collection-service.js +73 -313
- package/esm/mongo-entity-service.js +324 -0
- package/esm/mongo-nested-service.js +569 -0
- package/esm/mongo-service.js +125 -184
- package/esm/mongo-singleton-service.js +29 -125
- package/package.json +9 -8
- package/types/adapter-utils/prepare-filter.d.ts +2 -3
- package/types/adapter-utils/prepare-projection.d.ts +4 -13
- package/types/index.d.ts +1 -2
- package/types/mongo-adapter.d.ts +14 -1
- package/types/mongo-collection-service.d.ts +88 -251
- package/types/mongo-entity-service.d.ts +149 -0
- package/types/mongo-nested-service.d.ts +258 -0
- package/types/mongo-service.d.ts +208 -82
- package/types/mongo-singleton-service.d.ts +39 -148
- package/cjs/types.js +0 -2
- package/esm/mongo-array-service.js +0 -563
- package/esm/types.js +0 -1
- package/types/mongo-array-service.d.ts +0 -409
- package/types/types.d.ts +0 -3
|
@@ -1,62 +1,56 @@
|
|
|
1
|
-
import mongodb from 'mongodb';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import { FilterInput } from './adapter-utils/prepare-filter.js';
|
|
1
|
+
import mongodb, { UpdateFilter } from 'mongodb';
|
|
2
|
+
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
|
+
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
|
+
import { MongoEntityService } from './mongo-entity-service.js';
|
|
6
5
|
import { MongoService } from './mongo-service.js';
|
|
7
|
-
import { AnyId } from './types.js';
|
|
8
6
|
/**
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
7
|
+
*
|
|
8
|
+
* @namespace MongoCollectionService
|
|
11
9
|
*/
|
|
12
|
-
export declare
|
|
10
|
+
export declare namespace MongoCollectionService {
|
|
13
11
|
/**
|
|
14
|
-
*
|
|
12
|
+
* The constructor options of MongoCollectionService.
|
|
15
13
|
*
|
|
16
|
-
* @
|
|
14
|
+
* @interface Options
|
|
15
|
+
* @extends MongoService.Options
|
|
17
16
|
*/
|
|
18
|
-
|
|
17
|
+
interface Options extends MongoEntityService.Options {
|
|
18
|
+
defaultLimit?: number;
|
|
19
|
+
}
|
|
20
|
+
interface CreateOptions extends MongoEntityService.CreateOptions {
|
|
21
|
+
}
|
|
22
|
+
interface CountOptions<T> extends MongoEntityService.CountOptions<T> {
|
|
23
|
+
}
|
|
24
|
+
interface DeleteOptions<T> extends MongoEntityService.DeleteOptions<T> {
|
|
25
|
+
}
|
|
26
|
+
interface DeleteManyOptions<T> extends MongoEntityService.DeleteManyOptions<T> {
|
|
27
|
+
}
|
|
28
|
+
interface DistinctOptions<T> extends MongoEntityService.DistinctOptions<T> {
|
|
29
|
+
}
|
|
30
|
+
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
31
|
+
}
|
|
32
|
+
interface ExistsOneOptions<T> extends MongoService.ExistsOneOptions<T> {
|
|
33
|
+
}
|
|
34
|
+
interface FindOneOptions<T> extends MongoEntityService.FindOneOptions<T> {
|
|
35
|
+
}
|
|
36
|
+
interface FindManyOptions<T> extends MongoEntityService.FindManyOptions<T> {
|
|
37
|
+
}
|
|
38
|
+
interface UpdateOptions<T> extends MongoEntityService.UpdateOptions<T> {
|
|
39
|
+
}
|
|
40
|
+
interface UpdateManyOptions<T> extends MongoEntityService.UpdateManyOptions<T> {
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* @class MongoCollectionService
|
|
45
|
+
* @template T - The type of the documents in the collection.
|
|
46
|
+
*/
|
|
47
|
+
export declare class MongoCollectionService<T extends mongodb.Document> extends MongoEntityService<T> {
|
|
19
48
|
/**
|
|
20
49
|
* Represents the default limit value for a certain operation.
|
|
21
50
|
*
|
|
22
51
|
* @type {number}
|
|
23
52
|
*/
|
|
24
53
|
defaultLimit: number;
|
|
25
|
-
/**
|
|
26
|
-
* Represents a common filter function for a MongoCollectionService.
|
|
27
|
-
*
|
|
28
|
-
* @type {FilterInput | Function}
|
|
29
|
-
*/
|
|
30
|
-
$documentFilter?: FilterInput | ((args: {
|
|
31
|
-
crud: MongoService.CrudOp;
|
|
32
|
-
method: string;
|
|
33
|
-
byId: boolean;
|
|
34
|
-
documentId?: AnyId;
|
|
35
|
-
input?: Record<string, any>;
|
|
36
|
-
options?: Record<string, any>;
|
|
37
|
-
}, _this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
38
|
-
/**
|
|
39
|
-
* Interceptor function for handling callback execution with provided arguments.
|
|
40
|
-
*
|
|
41
|
-
* @param callback - The callback function to be intercepted.
|
|
42
|
-
* @param args - The arguments object containing the following properties:
|
|
43
|
-
* @param crud - The CRUD operation type.
|
|
44
|
-
* @param method - The method name.
|
|
45
|
-
* @param byId - True if current operation affects byId records (updateMany, deleteMany etc)
|
|
46
|
-
* @param documentId - The document ID (optional).
|
|
47
|
-
* @param input - The input object (optional).
|
|
48
|
-
* @param options - Additional options (optional).
|
|
49
|
-
* @param _this - The reference to the current object.
|
|
50
|
-
* @returns - The promise that resolves to the result of the callback execution.
|
|
51
|
-
*/
|
|
52
|
-
$interceptor?: (callback: () => any, args: {
|
|
53
|
-
crud: MongoService.CrudOp;
|
|
54
|
-
method: string;
|
|
55
|
-
byId: boolean;
|
|
56
|
-
documentId?: AnyId;
|
|
57
|
-
input?: Record<string, any>;
|
|
58
|
-
options?: Record<string, any>;
|
|
59
|
-
}, _this: any) => Promise<any>;
|
|
60
54
|
/**
|
|
61
55
|
* Constructs a new instance
|
|
62
56
|
*
|
|
@@ -69,22 +63,22 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
69
63
|
* Asserts the existence of a resource with the given ID.
|
|
70
64
|
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
71
65
|
*
|
|
72
|
-
* @param {AnyId} id - The ID of the resource to assert.
|
|
73
|
-
* @param {MongoCollectionService.ExistsOptions} [options] - Optional options for checking the existence.
|
|
66
|
+
* @param {MongoAdapter.AnyId} id - The ID of the resource to assert.
|
|
67
|
+
* @param {MongoCollectionService.ExistsOptions<T>} [options] - Optional options for checking the existence.
|
|
74
68
|
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
|
|
75
69
|
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
76
70
|
*/
|
|
77
|
-
assert(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<void>;
|
|
71
|
+
assert(id: MongoAdapter.AnyId, options?: MongoCollectionService.ExistsOptions<T>): Promise<void>;
|
|
78
72
|
/**
|
|
79
73
|
* Creates a new document in the MongoDB collection.
|
|
74
|
+
* Interceptors will be called before performing db operation
|
|
80
75
|
*
|
|
81
|
-
* @param {
|
|
76
|
+
* @param {PartialDTO<T>} input - The input data for creating the document.
|
|
82
77
|
* @param {MongoCollectionService.CreateOptions} [options] - The options for creating the document.
|
|
83
78
|
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
84
79
|
* @throws {Error} if an unknown error occurs while creating the document.
|
|
85
80
|
*/
|
|
86
81
|
create(input: PartialDTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
|
|
87
|
-
protected _create(input: PartialDTO<T>, options?: MongoCollectionService.CreateOptions): Promise<PartialDTO<T>>;
|
|
88
82
|
/**
|
|
89
83
|
* Returns the count of documents in the collection based on the provided options.
|
|
90
84
|
*
|
|
@@ -92,16 +86,14 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
92
86
|
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
93
87
|
*/
|
|
94
88
|
count(options?: MongoCollectionService.CountOptions<T>): Promise<number>;
|
|
95
|
-
protected _count(options?: MongoCollectionService.CountOptions<T>): Promise<number>;
|
|
96
89
|
/**
|
|
97
90
|
* Deletes a document from the collection.
|
|
98
91
|
*
|
|
99
|
-
* @param {AnyId} id - The ID of the document to delete.
|
|
92
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document to delete.
|
|
100
93
|
* @param {MongoCollectionService.DeleteOptions<T>} [options] - Optional delete options.
|
|
101
94
|
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
102
95
|
*/
|
|
103
|
-
delete(id: AnyId, options?: MongoCollectionService.DeleteOptions<T>): Promise<number>;
|
|
104
|
-
protected _delete(id: AnyId, options?: MongoCollectionService.DeleteOptions<T>): Promise<number>;
|
|
96
|
+
delete(id: MongoAdapter.AnyId, options?: MongoCollectionService.DeleteOptions<T>): Promise<number>;
|
|
105
97
|
/**
|
|
106
98
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
107
99
|
*
|
|
@@ -109,17 +101,21 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
109
101
|
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
110
102
|
*/
|
|
111
103
|
deleteMany(options?: MongoCollectionService.DeleteManyOptions<T>): Promise<number>;
|
|
112
|
-
|
|
104
|
+
/**
|
|
105
|
+
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
106
|
+
* @param {string} field
|
|
107
|
+
* @param {MongoCollectionService.DistinctOptions<T>} [options]
|
|
108
|
+
* @protected
|
|
109
|
+
*/
|
|
113
110
|
distinct(field: string, options?: MongoCollectionService.DistinctOptions<T>): Promise<any[]>;
|
|
114
|
-
protected _distinct(field: string, options?: MongoCollectionService.DistinctOptions<T>): Promise<any[]>;
|
|
115
111
|
/**
|
|
116
112
|
* Checks if an object with the given id exists.
|
|
117
113
|
*
|
|
118
|
-
* @param {AnyId} id - The id of the object to check.
|
|
119
|
-
* @param {MongoCollectionService.ExistsOptions} [options] - The options for the query (optional).
|
|
114
|
+
* @param {MongoAdapter.AnyId} id - The id of the object to check.
|
|
115
|
+
* @param {MongoCollectionService.ExistsOptions<T>} [options] - The options for the query (optional).
|
|
120
116
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
121
117
|
*/
|
|
122
|
-
exists(id: AnyId, options?: MongoCollectionService.ExistsOptions): Promise<boolean>;
|
|
118
|
+
exists(id: MongoAdapter.AnyId, options?: MongoCollectionService.ExistsOptions<T>): Promise<boolean>;
|
|
123
119
|
/**
|
|
124
120
|
* Checks if an object with the given arguments exists.
|
|
125
121
|
*
|
|
@@ -130,237 +126,78 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
130
126
|
/**
|
|
131
127
|
* Finds a document by its ID.
|
|
132
128
|
*
|
|
133
|
-
* @param {AnyId} id - The ID of the document.
|
|
134
|
-
* @param {MongoCollectionService.FindOneOptions} [options] - The options for the find query.
|
|
129
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document.
|
|
130
|
+
* @param {MongoCollectionService.FindOneOptions<T>} [options] - The options for the find query.
|
|
135
131
|
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
136
132
|
*/
|
|
137
|
-
findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
138
|
-
protected _findById(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
133
|
+
findById(id: MongoAdapter.AnyId, options?: MongoCollectionService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
139
134
|
/**
|
|
140
135
|
* Finds a document in the collection that matches the specified options.
|
|
141
136
|
*
|
|
142
|
-
* @param {MongoCollectionService.FindOneOptions} options - The options for the query.
|
|
137
|
+
* @param {MongoCollectionService.FindOneOptions<T>} [options] - The options for the query.
|
|
143
138
|
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
144
139
|
*/
|
|
145
|
-
findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
146
|
-
protected _findOne(options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
140
|
+
findOne(options?: MongoCollectionService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
147
141
|
/**
|
|
148
142
|
* Finds multiple documents in the MongoDB collection.
|
|
149
143
|
*
|
|
150
|
-
* @param options - The options for the find operation.
|
|
151
|
-
* - pick: string[] - An array of fields to include in the returned documents.
|
|
152
|
-
* - include: string[] - An array of fields to include in the returned documents.
|
|
153
|
-
* - omit: string[] - An array of fields to exclude from the returned documents.
|
|
154
|
-
* - sort: Record<string, number> - The sorting criteria.
|
|
155
|
-
* - skip: number - The number of documents to skip.
|
|
156
|
-
* - limit: number - The maximum number of documents to return.
|
|
157
|
-
* - filter: FilterQuery<T> - The filter conditions to apply to the find operation.
|
|
158
|
-
* - count: boolean - If set to true, returns the total count of matching documents.
|
|
159
|
-
*
|
|
144
|
+
* @param {MongoCollectionService.FindManyOptions<T>} options - The options for the find operation.
|
|
160
145
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
161
146
|
*/
|
|
162
147
|
findMany(options?: MongoCollectionService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
163
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Finds multiple documents in the collection and returns both records (max limit)
|
|
150
|
+
* and total count that matched the given criteria
|
|
151
|
+
*
|
|
152
|
+
* @param {MongoCollectionService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
153
|
+
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
154
|
+
*/
|
|
155
|
+
findManyWithCount(options?: MongoCollectionService.FindManyOptions<T>): Promise<{
|
|
156
|
+
count: number;
|
|
157
|
+
items: PartialDTO<T>[];
|
|
158
|
+
}>;
|
|
164
159
|
/**
|
|
165
160
|
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
166
161
|
*
|
|
167
|
-
* @param {
|
|
168
|
-
* @param {MongoCollectionService.FindOneOptions} [options] - Optional options for the findOne operation.
|
|
162
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
|
|
163
|
+
* @param {MongoCollectionService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
169
164
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
170
165
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
171
166
|
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
172
167
|
*/
|
|
173
|
-
get(id: AnyId, options?: MongoCollectionService.FindOneOptions): Promise<PartialDTO<T>>;
|
|
168
|
+
get(id: MongoAdapter.AnyId, options?: MongoCollectionService.FindOneOptions<T>): Promise<PartialDTO<T>>;
|
|
174
169
|
/**
|
|
175
170
|
* Updates a document with the given id in the collection.
|
|
176
171
|
*
|
|
177
|
-
* @param {
|
|
178
|
-
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
172
|
+
* @param {MongoAdapter.AnyId} id - The id of the document to update.
|
|
173
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
179
174
|
* @param {MongoCollectionService.UpdateOptions<T>} [options] - The options for the update operation.
|
|
180
175
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
181
176
|
* undefined if the document was not found.
|
|
182
177
|
*/
|
|
183
|
-
update(id: AnyId, input: PatchDTO<T> |
|
|
184
|
-
protected _update(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
178
|
+
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
185
179
|
/**
|
|
186
180
|
* Updates a document in the collection with the specified ID.
|
|
187
181
|
*
|
|
188
|
-
* @param {
|
|
189
|
-
* @param {PatchDTO<T>} input - The partial input data to update the document with.
|
|
190
|
-
* @param {MongoCollectionService.UpdateOptions<T>} options - The options for updating the document.
|
|
182
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
183
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
184
|
+
* @param {MongoCollectionService.UpdateOptions<T>} [options] - The options for updating the document.
|
|
191
185
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
192
186
|
*/
|
|
193
|
-
updateOnly(id: AnyId, input: PatchDTO<T> |
|
|
194
|
-
protected _updateOnly(id: AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
|
|
187
|
+
updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoCollectionService.UpdateOptions<T>): Promise<number>;
|
|
195
188
|
/**
|
|
196
189
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
197
190
|
*
|
|
198
|
-
* @param {PatchDTO<T>} input - The partial input to update the documents with.
|
|
191
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
199
192
|
* @param {MongoCollectionService.UpdateManyOptions<T>} options - The options for updating the documents.
|
|
200
193
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
201
194
|
*/
|
|
202
|
-
updateMany(input: PatchDTO<T> |
|
|
203
|
-
protected _updateMany(input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
|
|
195
|
+
updateMany(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoCollectionService.UpdateManyOptions<T>): Promise<number>;
|
|
204
196
|
/**
|
|
205
197
|
* Generates an ID.
|
|
206
198
|
*
|
|
207
199
|
* @protected
|
|
208
|
-
* @returns {AnyId} The generated ID.
|
|
200
|
+
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
209
201
|
*/
|
|
210
|
-
protected _generateId(): AnyId;
|
|
211
|
-
/**
|
|
212
|
-
* Retrieves the common filter used for querying documents.
|
|
213
|
-
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
214
|
-
*
|
|
215
|
-
* @protected
|
|
216
|
-
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
217
|
-
* that resolves to the common filter, or undefined if not available.
|
|
218
|
-
*/
|
|
219
|
-
protected _getDocumentFilter(args: {
|
|
220
|
-
crud: MongoService.CrudOp;
|
|
221
|
-
method: string;
|
|
222
|
-
byId: boolean;
|
|
223
|
-
documentId?: AnyId;
|
|
224
|
-
input?: Object;
|
|
225
|
-
options?: Record<string, any>;
|
|
226
|
-
}): FilterInput | Promise<FilterInput> | undefined;
|
|
227
|
-
protected _intercept(callback: (...args: any[]) => any, args: {
|
|
228
|
-
crud: MongoService.CrudOp;
|
|
229
|
-
method: string;
|
|
230
|
-
byId: boolean;
|
|
231
|
-
documentId?: AnyId;
|
|
232
|
-
input?: Object;
|
|
233
|
-
options?: Record<string, any>;
|
|
234
|
-
}): Promise<any>;
|
|
235
|
-
}
|
|
236
|
-
/**
|
|
237
|
-
*
|
|
238
|
-
* @namespace MongoCollectionService
|
|
239
|
-
*/
|
|
240
|
-
export declare namespace MongoCollectionService {
|
|
241
|
-
/**
|
|
242
|
-
* The constructor options of MongoCollectionService.
|
|
243
|
-
*
|
|
244
|
-
* @interface Options
|
|
245
|
-
* @extends MongoService.Options
|
|
246
|
-
*/
|
|
247
|
-
interface Options extends MongoService.Options {
|
|
248
|
-
collectionKey?: MongoCollectionService<any>['collectionKey'];
|
|
249
|
-
defaultLimit?: MongoCollectionService<any>['defaultLimit'];
|
|
250
|
-
documentFilter?: MongoCollectionService<any>['$documentFilter'];
|
|
251
|
-
interceptor?: MongoCollectionService<any>['$interceptor'];
|
|
252
|
-
}
|
|
253
|
-
/**
|
|
254
|
-
* Represents options for "create" operation
|
|
255
|
-
*
|
|
256
|
-
* @interface
|
|
257
|
-
*/
|
|
258
|
-
interface CreateOptions extends mongodb.InsertOneOptions {
|
|
259
|
-
pick?: string[];
|
|
260
|
-
omit?: string[];
|
|
261
|
-
include?: string[];
|
|
262
|
-
}
|
|
263
|
-
/**
|
|
264
|
-
* Represents options for "count" operation
|
|
265
|
-
*
|
|
266
|
-
* @interface
|
|
267
|
-
* @template T - The type of the document.
|
|
268
|
-
*/
|
|
269
|
-
interface CountOptions<T> extends mongodb.CountOptions {
|
|
270
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
271
|
-
}
|
|
272
|
-
/**
|
|
273
|
-
* Represents options for "delete" operation
|
|
274
|
-
*
|
|
275
|
-
* @interface
|
|
276
|
-
* @template T - The type of the document.
|
|
277
|
-
*/
|
|
278
|
-
interface DeleteOptions<T> extends mongodb.DeleteOptions {
|
|
279
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Represents options for "deleteMany" operation
|
|
283
|
-
*
|
|
284
|
-
* @interface
|
|
285
|
-
* @template T - The type of the document.
|
|
286
|
-
*/
|
|
287
|
-
interface DeleteManyOptions<T> extends mongodb.DeleteOptions {
|
|
288
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Represents options for "distinct" operation
|
|
292
|
-
*
|
|
293
|
-
* @interface
|
|
294
|
-
* @template T - The type of the document.
|
|
295
|
-
*/
|
|
296
|
-
interface DistinctOptions<T> extends mongodb.DistinctOptions {
|
|
297
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
298
|
-
}
|
|
299
|
-
/**
|
|
300
|
-
* Represents options for "findOne" operation
|
|
301
|
-
*
|
|
302
|
-
* @interface
|
|
303
|
-
* @template T - The type of the document.
|
|
304
|
-
*/
|
|
305
|
-
interface FindOneOptions<T = any> extends StrictOmit<FindManyOptions<T>, 'count' | 'limit'> {
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Represents options for "findMany" operation
|
|
309
|
-
*
|
|
310
|
-
* @interface
|
|
311
|
-
* @template T - The type of the document.
|
|
312
|
-
*/
|
|
313
|
-
interface FindManyOptions<T> extends mongodb.AggregateOptions {
|
|
314
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
315
|
-
pick?: string[];
|
|
316
|
-
omit?: string[];
|
|
317
|
-
include?: string[];
|
|
318
|
-
sort?: string[];
|
|
319
|
-
limit?: number;
|
|
320
|
-
skip?: number;
|
|
321
|
-
count?: boolean;
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Represents options for "update" operation
|
|
325
|
-
*
|
|
326
|
-
* @interface
|
|
327
|
-
* @template T - The type of the document.
|
|
328
|
-
*/
|
|
329
|
-
interface UpdateOptions<T> extends StrictOmit<mongodb.FindOneAndUpdateOptions, 'projection' | 'returnDocument' | 'includeResultMetadata'> {
|
|
330
|
-
pick?: string[];
|
|
331
|
-
omit?: string[];
|
|
332
|
-
include?: string[];
|
|
333
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Represents options for "updateMany" operation
|
|
337
|
-
*
|
|
338
|
-
* @interface
|
|
339
|
-
* @template T - The type of the document.
|
|
340
|
-
*/
|
|
341
|
-
interface UpdateManyOptions<T> extends StrictOmit<mongodb.UpdateOptions, 'upsert'> {
|
|
342
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
343
|
-
}
|
|
344
|
-
/**
|
|
345
|
-
* Represents options for "exists" operation
|
|
346
|
-
*
|
|
347
|
-
* @interface
|
|
348
|
-
*/
|
|
349
|
-
interface ExistsOptions extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
|
|
350
|
-
}
|
|
351
|
-
/**
|
|
352
|
-
* Represents options for checking the document exists
|
|
353
|
-
*
|
|
354
|
-
* @interface
|
|
355
|
-
*/
|
|
356
|
-
interface ExistsOneOptions<T> extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
|
|
357
|
-
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
358
|
-
}
|
|
359
|
-
interface OperationInfo {
|
|
360
|
-
crud: MongoService.CrudOp;
|
|
361
|
-
method: string;
|
|
362
|
-
documentId?: AnyId;
|
|
363
|
-
input?: Record<string, any>;
|
|
364
|
-
options?: Record<string, any>;
|
|
365
|
-
}
|
|
202
|
+
protected _generateId(): MongoAdapter.AnyId;
|
|
366
203
|
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import mongodb from 'mongodb';
|
|
2
|
+
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
|
+
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
|
+
import { MongoService } from './mongo-service.js';
|
|
5
|
+
/**
|
|
6
|
+
*
|
|
7
|
+
* @namespace MongoEntityService
|
|
8
|
+
*/
|
|
9
|
+
export declare namespace MongoEntityService {
|
|
10
|
+
/**
|
|
11
|
+
* The constructor options of MongoEntityService.
|
|
12
|
+
*
|
|
13
|
+
* @interface Options
|
|
14
|
+
* @extends MongoService.Options
|
|
15
|
+
*/
|
|
16
|
+
interface Options extends MongoService.Options {
|
|
17
|
+
}
|
|
18
|
+
interface CommandInfo extends MongoService.CommandInfo {
|
|
19
|
+
}
|
|
20
|
+
interface CreateOptions extends MongoService.CreateOptions {
|
|
21
|
+
}
|
|
22
|
+
interface CountOptions<T> extends MongoService.CountOptions<T> {
|
|
23
|
+
}
|
|
24
|
+
interface DeleteOptions<T> extends MongoService.DeleteOptions<T> {
|
|
25
|
+
}
|
|
26
|
+
interface DeleteManyOptions<T> extends MongoService.DeleteManyOptions<T> {
|
|
27
|
+
}
|
|
28
|
+
interface DistinctOptions<T> extends MongoService.DistinctOptions<T> {
|
|
29
|
+
}
|
|
30
|
+
interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
|
|
31
|
+
}
|
|
32
|
+
interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
|
|
33
|
+
}
|
|
34
|
+
interface UpdateOptions<T> extends MongoService.UpdateOptions<T> {
|
|
35
|
+
}
|
|
36
|
+
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @class MongoEntityService
|
|
41
|
+
* @template T - The type of the documents in the collection.
|
|
42
|
+
*/
|
|
43
|
+
export declare class MongoEntityService<T extends mongodb.Document> extends MongoService<T> {
|
|
44
|
+
/**
|
|
45
|
+
* Constructs a new instance
|
|
46
|
+
*
|
|
47
|
+
* @param {Type | string} dataType - The data type of the array elements.
|
|
48
|
+
* @param {MongoEntityService.Options} [options] - The options for the array service.
|
|
49
|
+
* @constructor
|
|
50
|
+
*/
|
|
51
|
+
constructor(dataType: Type | string, options?: MongoEntityService.Options);
|
|
52
|
+
/**
|
|
53
|
+
* Creates a new document in the MongoDB collection.
|
|
54
|
+
*
|
|
55
|
+
* @param {PartialDTO<T>} input
|
|
56
|
+
* @param {MongoEntityService.CreateOptions} options
|
|
57
|
+
* @protected
|
|
58
|
+
*/
|
|
59
|
+
protected _create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<PartialDTO<T>>;
|
|
60
|
+
/**
|
|
61
|
+
* Returns the count of documents in the collection based on the provided options.
|
|
62
|
+
*
|
|
63
|
+
* @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
|
|
64
|
+
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
65
|
+
*/
|
|
66
|
+
protected _count(options?: MongoEntityService.CountOptions<T>): Promise<number>;
|
|
67
|
+
/**
|
|
68
|
+
* Deletes a document from the collection.
|
|
69
|
+
*
|
|
70
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document to delete.
|
|
71
|
+
* @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
|
|
72
|
+
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
73
|
+
*/
|
|
74
|
+
protected _delete(id: MongoAdapter.AnyId, options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
75
|
+
/**
|
|
76
|
+
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
77
|
+
*
|
|
78
|
+
* @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
|
|
79
|
+
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
80
|
+
*/
|
|
81
|
+
protected _deleteMany(options?: MongoEntityService.DeleteManyOptions<T>): Promise<number>;
|
|
82
|
+
/**
|
|
83
|
+
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
84
|
+
* @param {string} field
|
|
85
|
+
* @param {MongoEntityService.DistinctOptions<T>} options
|
|
86
|
+
* @protected
|
|
87
|
+
*/
|
|
88
|
+
protected _distinct(field: string, options?: MongoEntityService.DistinctOptions<T>): Promise<any[]>;
|
|
89
|
+
/**
|
|
90
|
+
* Finds a document by its ID.
|
|
91
|
+
*
|
|
92
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document.
|
|
93
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
94
|
+
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
95
|
+
*/
|
|
96
|
+
protected _findById(id: MongoAdapter.AnyId, options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
97
|
+
/**
|
|
98
|
+
* Finds a document in the collection that matches the specified options.
|
|
99
|
+
*
|
|
100
|
+
* @param {MongoEntityService.FindOneOptions} [options] - The options for the query.
|
|
101
|
+
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
102
|
+
*/
|
|
103
|
+
protected _findOne(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
104
|
+
/**
|
|
105
|
+
* Finds multiple documents in the MongoDB collection.
|
|
106
|
+
*
|
|
107
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
108
|
+
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
109
|
+
*/
|
|
110
|
+
protected _findMany(options?: MongoEntityService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
111
|
+
/**
|
|
112
|
+
* Finds multiple documents in the collection and returns both records (max limit)
|
|
113
|
+
* and total count that matched the given criteria
|
|
114
|
+
*
|
|
115
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
116
|
+
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
117
|
+
*/
|
|
118
|
+
protected _findManyWithCount(options?: MongoEntityService.FindManyOptions<T>): Promise<{
|
|
119
|
+
count: number;
|
|
120
|
+
items: PartialDTO<T>[];
|
|
121
|
+
}>;
|
|
122
|
+
/**
|
|
123
|
+
* Updates a document with the given id in the collection.
|
|
124
|
+
*
|
|
125
|
+
* @param {AnyId} id - The id of the document to update.
|
|
126
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
127
|
+
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for the update operation.
|
|
128
|
+
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
129
|
+
* undefined if the document was not found.
|
|
130
|
+
*/
|
|
131
|
+
protected _update(id: MongoAdapter.AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoEntityService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
132
|
+
/**
|
|
133
|
+
* Updates a document in the collection with the specified ID.
|
|
134
|
+
*
|
|
135
|
+
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
136
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
137
|
+
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for updating the document.
|
|
138
|
+
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
139
|
+
*/
|
|
140
|
+
protected _updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoEntityService.UpdateOptions<T>): Promise<number>;
|
|
141
|
+
/**
|
|
142
|
+
* Updates multiple documents in the collection based on the specified input and options.
|
|
143
|
+
*
|
|
144
|
+
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
145
|
+
* @param {MongoEntityService.UpdateManyOptions<T>} [options] - The options for updating the documents.
|
|
146
|
+
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
147
|
+
*/
|
|
148
|
+
protected _updateMany(input: PatchDTO<T> | mongodb.UpdateFilter<T>, options?: MongoEntityService.UpdateManyOptions<T>): Promise<number>;
|
|
149
|
+
}
|