@opra/mongodb 1.0.0-alpha.17 → 1.0.0-alpha.19
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 +1 -1
- package/cjs/adapter-utils/prepare-key-values.js +1 -1
- package/cjs/adapter-utils/prepare-patch.js +1 -1
- package/cjs/adapter-utils/prepare-projection.js +2 -3
- package/cjs/adapter-utils/prepare-sort.js +1 -1
- package/cjs/mongo-collection-service.js +111 -94
- package/cjs/mongo-entity-service.js +86 -69
- package/cjs/mongo-nested-service.js +236 -120
- package/cjs/mongo-service.js +31 -22
- package/cjs/mongo-singleton-service.js +61 -41
- package/esm/mongo-collection-service.js +111 -94
- package/esm/mongo-entity-service.js +86 -69
- package/esm/mongo-nested-service.js +236 -120
- package/esm/mongo-service.js +31 -22
- package/esm/mongo-singleton-service.js +61 -41
- package/package.json +5 -4
- package/types/mongo-collection-service.d.ts +32 -62
- package/types/mongo-entity-service.d.ts +73 -47
- package/types/mongo-nested-service.d.ts +55 -20
- package/types/mongo-service.d.ts +28 -26
- package/types/mongo-singleton-service.d.ts +19 -29
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
|
-
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
|
-
import { MongoAdapter } from './mongo-adapter.js';
|
|
2
|
+
import { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
|
|
4
3
|
import { MongoService } from './mongo-service.js';
|
|
5
4
|
/**
|
|
6
5
|
*
|
|
@@ -27,14 +26,57 @@ export declare namespace MongoEntityService {
|
|
|
27
26
|
}
|
|
28
27
|
interface DistinctOptions<T> extends MongoService.DistinctOptions<T> {
|
|
29
28
|
}
|
|
29
|
+
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
30
|
+
}
|
|
30
31
|
interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
|
|
31
32
|
}
|
|
32
33
|
interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
|
|
33
34
|
}
|
|
34
|
-
interface
|
|
35
|
+
interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
|
|
35
36
|
}
|
|
36
37
|
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
37
38
|
}
|
|
39
|
+
interface CreateCommand extends StrictOmit<RequiredSome<CommandInfo, 'input'>, 'documentId' | 'nestedId'> {
|
|
40
|
+
crud: 'create';
|
|
41
|
+
options?: CreateOptions;
|
|
42
|
+
}
|
|
43
|
+
interface CountCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
44
|
+
crud: 'read';
|
|
45
|
+
options?: CountOptions<T>;
|
|
46
|
+
}
|
|
47
|
+
interface DeleteCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
48
|
+
crud: 'delete';
|
|
49
|
+
options?: DeleteOptions<T>;
|
|
50
|
+
}
|
|
51
|
+
interface DistinctCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
52
|
+
crud: 'read';
|
|
53
|
+
field: string;
|
|
54
|
+
options?: DistinctOptions<T>;
|
|
55
|
+
}
|
|
56
|
+
interface ExistsCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
57
|
+
crud: 'read';
|
|
58
|
+
options?: ExistsOptions<T>;
|
|
59
|
+
}
|
|
60
|
+
interface FindOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
61
|
+
crud: 'read';
|
|
62
|
+
options?: FindOneOptions<T>;
|
|
63
|
+
}
|
|
64
|
+
interface FindManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
65
|
+
crud: 'read';
|
|
66
|
+
options?: FindManyOptions<T>;
|
|
67
|
+
}
|
|
68
|
+
interface UpdateOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
69
|
+
crud: 'update';
|
|
70
|
+
input?: PatchDTO<T>;
|
|
71
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
72
|
+
options?: UpdateOneOptions<T>;
|
|
73
|
+
}
|
|
74
|
+
interface UpdateManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
75
|
+
crud: 'update';
|
|
76
|
+
input?: PatchDTO<T>;
|
|
77
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
78
|
+
options?: UpdateManyOptions<T>;
|
|
79
|
+
}
|
|
38
80
|
}
|
|
39
81
|
/**
|
|
40
82
|
* @class MongoEntityService
|
|
@@ -52,98 +94,82 @@ export declare class MongoEntityService<T extends mongodb.Document> extends Mong
|
|
|
52
94
|
/**
|
|
53
95
|
* Creates a new document in the MongoDB collection.
|
|
54
96
|
*
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {MongoEntityService.CreateOptions} options
|
|
97
|
+
* @param {MongoEntityService.CreateCommand} command
|
|
57
98
|
* @protected
|
|
58
99
|
*/
|
|
59
|
-
protected _create(
|
|
100
|
+
protected _create(command: MongoEntityService.CreateCommand): Promise<PartialDTO<T>>;
|
|
60
101
|
/**
|
|
61
102
|
* Returns the count of documents in the collection based on the provided options.
|
|
62
103
|
*
|
|
63
|
-
* @param {MongoEntityService.
|
|
64
|
-
* @
|
|
104
|
+
* @param {MongoEntityService.CountCommand<T>} command
|
|
105
|
+
* @protected
|
|
65
106
|
*/
|
|
66
|
-
protected _count(
|
|
107
|
+
protected _count(command: MongoEntityService.CountCommand<T>): Promise<number>;
|
|
67
108
|
/**
|
|
68
109
|
* Deletes a document from the collection.
|
|
69
110
|
*
|
|
70
|
-
* @param {
|
|
71
|
-
* @
|
|
72
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
111
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
112
|
+
* @protected
|
|
73
113
|
*/
|
|
74
|
-
protected _delete(
|
|
114
|
+
protected _delete(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
75
115
|
/**
|
|
76
116
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
77
117
|
*
|
|
78
|
-
* @param {MongoEntityService.
|
|
79
|
-
* @
|
|
118
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
119
|
+
* @protected
|
|
80
120
|
*/
|
|
81
|
-
protected _deleteMany(
|
|
121
|
+
protected _deleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
82
122
|
/**
|
|
83
123
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
84
|
-
*
|
|
85
|
-
* @param {MongoEntityService.
|
|
124
|
+
*
|
|
125
|
+
* @param {MongoEntityService.DistinctCommand<T>} command
|
|
86
126
|
* @protected
|
|
87
127
|
*/
|
|
88
|
-
protected _distinct(
|
|
128
|
+
protected _distinct(command: MongoEntityService.DistinctCommand<T>): Promise<any[]>;
|
|
89
129
|
/**
|
|
90
130
|
* Finds a document by its ID.
|
|
91
131
|
*
|
|
92
|
-
* @param {
|
|
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.
|
|
132
|
+
* @param { MongoEntityService.FindOneCommand<T>} command
|
|
95
133
|
*/
|
|
96
|
-
protected _findById(
|
|
134
|
+
protected _findById(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
97
135
|
/**
|
|
98
136
|
* Finds a document in the collection that matches the specified options.
|
|
99
137
|
*
|
|
100
|
-
* @param {MongoEntityService.
|
|
101
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
138
|
+
* @param {MongoEntityService.FindOneCommand<T>} command
|
|
102
139
|
*/
|
|
103
|
-
protected _findOne(
|
|
140
|
+
protected _findOne(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
104
141
|
/**
|
|
105
142
|
* Finds multiple documents in the MongoDB collection.
|
|
106
143
|
*
|
|
107
|
-
* @param {MongoEntityService.
|
|
108
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
144
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
109
145
|
*/
|
|
110
|
-
protected _findMany(
|
|
146
|
+
protected _findMany(command: MongoEntityService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
|
|
111
147
|
/**
|
|
112
148
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
113
149
|
* and total count that matched the given criteria
|
|
114
150
|
*
|
|
115
|
-
* @param {MongoEntityService.
|
|
116
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
151
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
117
152
|
*/
|
|
118
|
-
protected _findManyWithCount(
|
|
153
|
+
protected _findManyWithCount(command: MongoEntityService.FindManyCommand<T>): Promise<{
|
|
119
154
|
count: number;
|
|
120
155
|
items: PartialDTO<T>[];
|
|
121
156
|
}>;
|
|
122
157
|
/**
|
|
123
158
|
* Updates a document with the given id in the collection.
|
|
124
159
|
*
|
|
125
|
-
* @param {
|
|
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.
|
|
160
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
130
161
|
*/
|
|
131
|
-
protected _update(
|
|
162
|
+
protected _update(command: MongoEntityService.UpdateOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
132
163
|
/**
|
|
133
164
|
* Updates a document in the collection with the specified ID.
|
|
134
165
|
*
|
|
135
|
-
* @param {
|
|
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.
|
|
166
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
139
167
|
*/
|
|
140
|
-
protected _updateOnly(
|
|
168
|
+
protected _updateOnly(command: MongoEntityService.UpdateOneCommand<T>): Promise<number>;
|
|
141
169
|
/**
|
|
142
170
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
143
171
|
*
|
|
144
|
-
* @param {
|
|
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.
|
|
172
|
+
* @param {MongoEntityService.UpdateManyCommand<T>} command
|
|
147
173
|
*/
|
|
148
|
-
protected _updateMany(
|
|
174
|
+
protected _updateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<number>;
|
|
149
175
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { ComplexType } from '@opra/common';
|
|
2
2
|
import mongodb from 'mongodb';
|
|
3
|
-
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
|
+
import { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
|
|
4
4
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
5
|
-
import { MongoCollectionService } from './mongo-collection-service.js';
|
|
6
5
|
import { MongoService } from './mongo-service.js';
|
|
7
6
|
import FilterInput = MongoAdapter.FilterInput;
|
|
8
7
|
/**
|
|
@@ -16,7 +15,9 @@ export declare namespace MongoNestedService {
|
|
|
16
15
|
interface Options extends MongoService.Options {
|
|
17
16
|
defaultLimit?: number;
|
|
18
17
|
nestedKey?: string;
|
|
19
|
-
|
|
18
|
+
nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
19
|
+
}
|
|
20
|
+
interface CommandInfo extends MongoService.CommandInfo {
|
|
20
21
|
}
|
|
21
22
|
interface CreateOptions extends MongoService.CreateOptions {
|
|
22
23
|
}
|
|
@@ -40,13 +41,47 @@ export declare namespace MongoNestedService {
|
|
|
40
41
|
documentFilter?: FilterInput;
|
|
41
42
|
nestedFilter?: FilterInput;
|
|
42
43
|
}
|
|
43
|
-
interface
|
|
44
|
+
interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
|
|
44
45
|
documentFilter?: FilterInput;
|
|
45
46
|
}
|
|
46
47
|
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
47
48
|
documentFilter?: FilterInput;
|
|
48
49
|
count?: boolean;
|
|
49
50
|
}
|
|
51
|
+
interface CreateCommand extends RequiredSome<CommandInfo, 'documentId' | 'input'> {
|
|
52
|
+
crud: 'create';
|
|
53
|
+
options?: CreateOptions;
|
|
54
|
+
}
|
|
55
|
+
interface CountCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'nestedId' | 'input'> {
|
|
56
|
+
crud: 'read';
|
|
57
|
+
options?: CountOptions<T>;
|
|
58
|
+
}
|
|
59
|
+
interface DeleteCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
60
|
+
crud: 'delete';
|
|
61
|
+
options?: DeleteOptions<T>;
|
|
62
|
+
}
|
|
63
|
+
interface ExistsCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
64
|
+
crud: 'read';
|
|
65
|
+
options?: ExistsOptions<T>;
|
|
66
|
+
}
|
|
67
|
+
interface FindOneCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
68
|
+
crud: 'read';
|
|
69
|
+
options?: FindOneOptions<T>;
|
|
70
|
+
}
|
|
71
|
+
interface FindManyCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
72
|
+
crud: 'read';
|
|
73
|
+
options?: FindManyOptions<T>;
|
|
74
|
+
}
|
|
75
|
+
interface UpdateOneCommand<T> extends RequiredSome<CommandInfo, 'documentId'> {
|
|
76
|
+
crud: 'update';
|
|
77
|
+
input: PatchDTO<T> | mongodb.UpdateFilter<T>;
|
|
78
|
+
options?: MongoNestedService.UpdateOneOptions<T>;
|
|
79
|
+
}
|
|
80
|
+
interface UpdateManyCommand<T> extends RequiredSome<CommandInfo, 'documentId'> {
|
|
81
|
+
crud: 'update';
|
|
82
|
+
input: PatchDTO<T> | mongodb.UpdateFilter<T>;
|
|
83
|
+
options?: MongoNestedService.UpdateManyOptions<T>;
|
|
84
|
+
}
|
|
50
85
|
}
|
|
51
86
|
/**
|
|
52
87
|
* A class that provides methods to perform operations on an array field in a MongoDB collection.
|
|
@@ -77,7 +112,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
77
112
|
*
|
|
78
113
|
* @type {FilterInput | Function}
|
|
79
114
|
*/
|
|
80
|
-
|
|
115
|
+
nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
81
116
|
/**
|
|
82
117
|
* Constructs a new instance
|
|
83
118
|
*
|
|
@@ -115,7 +150,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
115
150
|
* @throws {ResourceNotAvailableError} - If the parent document is not found.
|
|
116
151
|
*/
|
|
117
152
|
create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<PartialDTO<T>>;
|
|
118
|
-
protected _create(
|
|
153
|
+
protected _create(command: MongoNestedService.CreateCommand): Promise<PartialDTO<T>>;
|
|
119
154
|
/**
|
|
120
155
|
* Counts the number of documents in the collection that match the specified parentId and options.
|
|
121
156
|
*
|
|
@@ -124,7 +159,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
124
159
|
* @returns {Promise<number>} - A promise that resolves to the count of documents.
|
|
125
160
|
*/
|
|
126
161
|
count(documentId: MongoAdapter.AnyId, options?: MongoNestedService.CountOptions<T>): Promise<number>;
|
|
127
|
-
protected _count(
|
|
162
|
+
protected _count(command: MongoNestedService.CountCommand<T>): Promise<number>;
|
|
128
163
|
/**
|
|
129
164
|
* Deletes an element from an array within a document in the MongoDB collection.
|
|
130
165
|
*
|
|
@@ -134,7 +169,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
134
169
|
* @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
|
|
135
170
|
*/
|
|
136
171
|
delete(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteOptions<T>): Promise<number>;
|
|
137
|
-
protected _delete(
|
|
172
|
+
protected _delete(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
|
|
138
173
|
/**
|
|
139
174
|
* Deletes multiple items from a collection based on the parent ID and optional filter.
|
|
140
175
|
*
|
|
@@ -143,7 +178,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
143
178
|
* @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
|
|
144
179
|
*/
|
|
145
180
|
deleteMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteManyOptions<T>): Promise<number>;
|
|
146
|
-
protected _deleteMany(
|
|
181
|
+
protected _deleteMany(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
|
|
147
182
|
/**
|
|
148
183
|
* Checks if an array element with the given parentId and id exists.
|
|
149
184
|
*
|
|
@@ -160,7 +195,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
160
195
|
* @param {MongoNestedService.ExistsOneOptions} [options] - The options for the query (optional).
|
|
161
196
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
162
197
|
*/
|
|
163
|
-
existsOne(documentId: MongoAdapter.AnyId, options?:
|
|
198
|
+
existsOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOneOptions<T>): Promise<boolean>;
|
|
164
199
|
/**
|
|
165
200
|
* Finds an element in array field by its parent ID and ID.
|
|
166
201
|
*
|
|
@@ -170,7 +205,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
170
205
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
171
206
|
*/
|
|
172
207
|
findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
173
|
-
protected _findById(
|
|
208
|
+
protected _findById(command: MongoNestedService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
174
209
|
/**
|
|
175
210
|
* Finds the first array element that matches the given parentId.
|
|
176
211
|
*
|
|
@@ -179,7 +214,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
179
214
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
|
|
180
215
|
*/
|
|
181
216
|
findOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
182
|
-
protected _findOne(
|
|
217
|
+
protected _findOne(command: MongoNestedService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
183
218
|
/**
|
|
184
219
|
* Finds multiple elements in an array field.
|
|
185
220
|
*
|
|
@@ -188,7 +223,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
188
223
|
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
189
224
|
*/
|
|
190
225
|
findMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
191
|
-
protected _findMany(
|
|
226
|
+
protected _findMany(command: MongoNestedService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
|
|
192
227
|
/**
|
|
193
228
|
* Finds multiple elements in an array field.
|
|
194
229
|
*
|
|
@@ -200,7 +235,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
200
235
|
count: number;
|
|
201
236
|
items: PartialDTO<T>[];
|
|
202
237
|
}>;
|
|
203
|
-
protected _findManyWithCount(
|
|
238
|
+
protected _findManyWithCount(command: MongoNestedService.FindManyCommand<T>): Promise<{
|
|
204
239
|
count: number;
|
|
205
240
|
items: PartialDTO<T>[];
|
|
206
241
|
}>;
|
|
@@ -220,22 +255,22 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
220
255
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
221
256
|
* @param {AnyId} nestedId - The ID of the item to update within the document.
|
|
222
257
|
* @param {PatchDTO<T>} input - The new data to update the item with.
|
|
223
|
-
* @param {MongoNestedService.
|
|
258
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Additional update options.
|
|
224
259
|
* @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
|
|
225
260
|
* @throws {Error} If an error occurs while updating the item.
|
|
226
261
|
*/
|
|
227
|
-
update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.
|
|
262
|
+
update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
228
263
|
/**
|
|
229
264
|
* Update an array element with new data. Returns 1 if document updated 0 otherwise.
|
|
230
265
|
*
|
|
231
266
|
* @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
|
|
232
267
|
* @param {MongoAdapter.AnyId} nestedId - The ID of the document to update.
|
|
233
268
|
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
234
|
-
* @param {MongoNestedService.
|
|
269
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Optional update options.
|
|
235
270
|
* @returns {Promise<number>} - A promise that resolves to the number of elements updated.
|
|
236
271
|
*/
|
|
237
|
-
updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.
|
|
238
|
-
protected _updateOnly(
|
|
272
|
+
updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<number>;
|
|
273
|
+
protected _updateOnly(command: MongoNestedService.UpdateOneCommand<T>): Promise<number>;
|
|
239
274
|
/**
|
|
240
275
|
* Updates multiple array elements in document
|
|
241
276
|
*
|
|
@@ -245,7 +280,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
245
280
|
* @returns {Promise<number>} - A promise that resolves to the number of documents updated.
|
|
246
281
|
*/
|
|
247
282
|
updateMany(documentId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateManyOptions<T>): Promise<number>;
|
|
248
|
-
protected _updateMany(
|
|
283
|
+
protected _updateMany(command: MongoNestedService.UpdateManyCommand<T>): Promise<number>;
|
|
249
284
|
/**
|
|
250
285
|
* Retrieves the common filter used for querying array elements.
|
|
251
286
|
* This method is mostly used for security issues like securing multi-tenant applications.
|
package/types/mongo-service.d.ts
CHANGED
|
@@ -14,12 +14,12 @@ export declare namespace MongoService {
|
|
|
14
14
|
interface Options {
|
|
15
15
|
db?: MongoService<any>['db'];
|
|
16
16
|
session?: MongoService<any>['session'];
|
|
17
|
-
collectionName?: MongoService<any>['
|
|
18
|
-
resourceName?: MongoService<any>['
|
|
19
|
-
documentFilter?: MongoService<any>['
|
|
20
|
-
interceptor?: MongoService<any>['
|
|
21
|
-
idGenerator?: MongoService<any>['
|
|
22
|
-
onError?: MongoService<any>['
|
|
17
|
+
collectionName?: MongoService<any>['collectionName'];
|
|
18
|
+
resourceName?: MongoService<any>['resourceName'];
|
|
19
|
+
documentFilter?: MongoService<any>['documentFilter'];
|
|
20
|
+
interceptor?: MongoService<any>['interceptor'];
|
|
21
|
+
idGenerator?: MongoService<any>['idGenerator'];
|
|
22
|
+
onError?: MongoService<any>['onError'];
|
|
23
23
|
}
|
|
24
24
|
type CrudOp = 'create' | 'read' | 'update' | 'delete';
|
|
25
25
|
interface CommandInfo {
|
|
@@ -28,8 +28,8 @@ export declare namespace MongoService {
|
|
|
28
28
|
byId: boolean;
|
|
29
29
|
documentId?: MongoAdapter.AnyId;
|
|
30
30
|
nestedId?: MongoAdapter.AnyId;
|
|
31
|
-
input?:
|
|
32
|
-
options?:
|
|
31
|
+
input?: any;
|
|
32
|
+
options?: any;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Represents options for "create" operation
|
|
@@ -118,7 +118,7 @@ export declare namespace MongoService {
|
|
|
118
118
|
* @interface
|
|
119
119
|
* @template T - The type of the document.
|
|
120
120
|
*/
|
|
121
|
-
interface
|
|
121
|
+
interface UpdateOneOptions<T> extends StrictOmit<mongodb.FindOneAndUpdateOptions, 'projection' | 'returnDocument' | 'includeResultMetadata'> {
|
|
122
122
|
projection?: string | string[] | Document;
|
|
123
123
|
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
124
124
|
}
|
|
@@ -132,6 +132,17 @@ export declare namespace MongoService {
|
|
|
132
132
|
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
+
export interface MongoService {
|
|
136
|
+
/**
|
|
137
|
+
* Interceptor function for handling callback execution with provided arguments.
|
|
138
|
+
* @type Function
|
|
139
|
+
* @param next - The callback function to be intercepted.
|
|
140
|
+
* @param {MongoService.CommandInfo} info - The arguments object containing the following properties:
|
|
141
|
+
* @param _this - The reference to the current object.
|
|
142
|
+
* @returns - The promise that resolves to the result of the callback execution.
|
|
143
|
+
*/
|
|
144
|
+
interceptor?(next: () => any, info: MongoService.CommandInfo, _this: any): Promise<any>;
|
|
145
|
+
}
|
|
135
146
|
/**
|
|
136
147
|
* Class representing a MongoDB service for interacting with a collection.
|
|
137
148
|
* @extends ServiceBase
|
|
@@ -145,12 +156,12 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
145
156
|
/**
|
|
146
157
|
* Represents the name of a collection in MongoDB
|
|
147
158
|
*/
|
|
148
|
-
|
|
159
|
+
collectionName?: string | ((_this: any) => string);
|
|
149
160
|
/**
|
|
150
161
|
* Represents the name of a resource.
|
|
151
162
|
* @type {string}
|
|
152
163
|
*/
|
|
153
|
-
|
|
164
|
+
resourceName?: string | ((_this: any) => string);
|
|
154
165
|
/**
|
|
155
166
|
* Represents a MongoDB database object.
|
|
156
167
|
*/
|
|
@@ -163,29 +174,20 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
163
174
|
* Generates a new id for new inserting Document.
|
|
164
175
|
*
|
|
165
176
|
*/
|
|
166
|
-
|
|
177
|
+
idGenerator?: (command: MongoService.CommandInfo, _this: any) => MongoAdapter.AnyId;
|
|
167
178
|
/**
|
|
168
179
|
* Callback function for handling errors.
|
|
169
180
|
*
|
|
170
181
|
* @param {unknown} error - The error object.
|
|
171
182
|
* @param _this - The context object.
|
|
172
183
|
*/
|
|
173
|
-
|
|
184
|
+
onError?: (error: unknown, _this: any) => void | Promise<void>;
|
|
174
185
|
/**
|
|
175
186
|
* Represents a common filter function for a MongoService.
|
|
176
187
|
*
|
|
177
188
|
* @type {FilterInput | Function}
|
|
178
189
|
*/
|
|
179
|
-
|
|
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>;
|
|
190
|
+
documentFilter?: MongoAdapter.FilterInput | ((args: MongoService.CommandInfo, _this: this) => MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined);
|
|
189
191
|
/**
|
|
190
192
|
* Constructs a new instance
|
|
191
193
|
*
|
|
@@ -355,7 +357,7 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
355
357
|
* @protected
|
|
356
358
|
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
357
359
|
*/
|
|
358
|
-
protected _generateId(): MongoAdapter.AnyId;
|
|
360
|
+
protected _generateId(command: MongoService.CommandInfo): MongoAdapter.AnyId;
|
|
359
361
|
/**
|
|
360
362
|
* Retrieves the common filter used for querying documents.
|
|
361
363
|
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
@@ -364,6 +366,6 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
364
366
|
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
365
367
|
* that resolves to the common filter, or undefined if not available.
|
|
366
368
|
*/
|
|
367
|
-
protected _getDocumentFilter(
|
|
368
|
-
protected
|
|
369
|
+
protected _getDocumentFilter(command: MongoService.CommandInfo): MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined;
|
|
370
|
+
protected _executeCommand(command: MongoService.CommandInfo, commandFn: () => any): Promise<any>;
|
|
369
371
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import mongodb from 'mongodb';
|
|
1
|
+
import mongodb, { UpdateFilter } from 'mongodb';
|
|
2
2
|
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
3
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
4
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
5
|
-
import { MongoService } from './mongo-service.js';
|
|
6
5
|
/**
|
|
7
6
|
*
|
|
8
7
|
* @namespace MongoSingletonService
|
|
@@ -17,16 +16,6 @@ export declare namespace MongoSingletonService {
|
|
|
17
16
|
interface Options extends MongoEntityService.Options {
|
|
18
17
|
_id?: MongoAdapter.AnyId;
|
|
19
18
|
}
|
|
20
|
-
interface CreateOptions extends MongoEntityService.CreateOptions {
|
|
21
|
-
}
|
|
22
|
-
interface DeleteOptions<T> extends MongoEntityService.DeleteOptions<T> {
|
|
23
|
-
}
|
|
24
|
-
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
25
|
-
}
|
|
26
|
-
interface FindOneOptions<T> extends MongoEntityService.FindOneOptions<T> {
|
|
27
|
-
}
|
|
28
|
-
interface UpdateOptions<T> extends MongoEntityService.UpdateOptions<T> {
|
|
29
|
-
}
|
|
30
19
|
}
|
|
31
20
|
/**
|
|
32
21
|
* A class that provides access to a MongoDB collection, with support for singleton document operations.
|
|
@@ -51,64 +40,65 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
|
|
|
51
40
|
/**
|
|
52
41
|
* Asserts the existence of a resource based on the given options.
|
|
53
42
|
*
|
|
54
|
-
* @param {
|
|
43
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options]
|
|
55
44
|
* @returns {Promise<void>} A Promise that resolves when the resource exists.
|
|
56
45
|
* @throws {ResourceNotAvailableError} If the resource does not exist.
|
|
57
46
|
*/
|
|
58
|
-
assert(options?:
|
|
47
|
+
assert(options?: MongoEntityService.ExistsOptions<T>): Promise<void>;
|
|
59
48
|
/**
|
|
60
49
|
* Creates the document in the database.
|
|
61
50
|
*
|
|
62
51
|
* @param {PartialDTO<T>} input - The partial input to create the document with.
|
|
63
|
-
* @param {
|
|
52
|
+
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
64
53
|
* @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
|
|
65
54
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
66
55
|
*/
|
|
67
|
-
create(input: PartialDTO<T>, options?:
|
|
56
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<PartialDTO<T>>;
|
|
68
57
|
/**
|
|
69
58
|
* Deletes a record from the database
|
|
70
59
|
*
|
|
71
|
-
* @param {
|
|
60
|
+
* @param {MongoEntityService.DeleteOptions<T>} options - The options for deleting the record
|
|
72
61
|
* @returns {Promise<number>} The number of records deleted
|
|
73
62
|
*/
|
|
74
|
-
delete(options?:
|
|
63
|
+
delete(options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
75
64
|
/**
|
|
76
65
|
* Checks if the document exists in the database.
|
|
77
66
|
*
|
|
67
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for finding the document.
|
|
78
68
|
* @return {Promise<boolean>} - A promise that resolves to a boolean value indicating if the document exists.
|
|
79
69
|
*/
|
|
80
|
-
exists(options?:
|
|
70
|
+
exists(options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
81
71
|
/**
|
|
82
72
|
* Fetches the document if it exists. Returns undefined if not found.
|
|
83
73
|
*
|
|
84
|
-
* @param {
|
|
74
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for finding the document.
|
|
85
75
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
86
76
|
*/
|
|
87
|
-
find(options?:
|
|
77
|
+
find(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
88
78
|
/**
|
|
89
79
|
* Fetches the document from the Mongo collection service. Throws error if not found.
|
|
90
80
|
*
|
|
91
|
-
* @param {
|
|
81
|
+
* @param {MongoEntityService.FindOneOptions<T>} options - The options to customize the query.
|
|
92
82
|
* @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
|
|
93
83
|
* @throws {ResourceNotAvailableError} - If the document is not found in the collection.
|
|
94
84
|
*/
|
|
95
|
-
get(options?:
|
|
85
|
+
get(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T>>;
|
|
96
86
|
/**
|
|
97
87
|
* Updates a document in the MongoDB collection.
|
|
98
88
|
*
|
|
99
89
|
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
100
|
-
* @param {
|
|
90
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The update options.
|
|
101
91
|
*
|
|
102
|
-
* @return {Promise<
|
|
92
|
+
* @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
|
|
103
93
|
*/
|
|
104
|
-
|
|
94
|
+
update(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
105
95
|
/**
|
|
106
96
|
* Updates a document in the MongoDB collection.
|
|
107
97
|
*
|
|
108
98
|
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
109
|
-
* @param {
|
|
99
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The update options.
|
|
110
100
|
*
|
|
111
|
-
* @return {Promise<
|
|
101
|
+
* @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
|
|
112
102
|
*/
|
|
113
|
-
|
|
103
|
+
updateOnly(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<number>;
|
|
114
104
|
}
|