@opra/mongodb 1.0.0-alpha.3 → 1.0.0-alpha.30
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 +3 -1
- package/cjs/adapter-utils/prepare-key-values.js +5 -4
- package/cjs/adapter-utils/prepare-patch.js +3 -2
- package/cjs/adapter-utils/prepare-projection.js +2 -3
- package/cjs/adapter-utils/prepare-sort.js +1 -1
- package/cjs/index.js +1 -1
- package/cjs/mongo-adapter.js +13 -8
- package/cjs/mongo-collection-service.js +113 -95
- package/cjs/mongo-entity-service.js +116 -93
- package/cjs/mongo-nested-service.js +262 -144
- package/cjs/mongo-service.js +62 -50
- package/cjs/mongo-singleton-service.js +63 -43
- package/esm/adapter-utils/prepare-filter.js +2 -0
- package/esm/adapter-utils/prepare-key-values.js +4 -3
- package/esm/adapter-utils/prepare-patch.js +2 -1
- package/esm/index.js +1 -1
- package/esm/mongo-adapter.js +13 -8
- package/esm/mongo-collection-service.js +113 -95
- package/esm/mongo-entity-service.js +116 -93
- package/esm/mongo-nested-service.js +262 -144
- package/esm/mongo-service.js +62 -50
- package/esm/mongo-singleton-service.js +63 -43
- package/package.json +13 -6
- package/types/adapter-utils/prepare-projection.d.ts +1 -1
- package/types/index.d.ts +1 -1
- package/types/mongo-adapter.d.ts +1 -1
- 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 +63 -28
- package/types/mongo-service.d.ts +43 -40
- package/types/mongo-singleton-service.d.ts +19 -29
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { ObjectId } from 'mongodb';
|
|
2
1
|
import { ResourceNotAvailableError } from '@opra/common';
|
|
3
2
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
3
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
@@ -16,14 +15,14 @@ export class MongoCollectionService extends MongoEntityService {
|
|
|
16
15
|
*/
|
|
17
16
|
constructor(dataType, options) {
|
|
18
17
|
super(dataType, options);
|
|
19
|
-
this.defaultLimit =
|
|
18
|
+
this.defaultLimit = options?.defaultLimit || 10;
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
22
21
|
* Asserts the existence of a resource with the given ID.
|
|
23
22
|
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
24
23
|
*
|
|
25
24
|
* @param {MongoAdapter.AnyId} id - The ID of the resource to assert.
|
|
26
|
-
* @param {
|
|
25
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options] - Optional options for checking the existence.
|
|
27
26
|
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
|
|
28
27
|
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
29
28
|
*/
|
|
@@ -36,112 +35,126 @@ export class MongoCollectionService extends MongoEntityService {
|
|
|
36
35
|
* Interceptors will be called before performing db operation
|
|
37
36
|
*
|
|
38
37
|
* @param {PartialDTO<T>} input - The input data for creating the document.
|
|
39
|
-
* @param {
|
|
38
|
+
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
40
39
|
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
41
40
|
* @throws {Error} if an unknown error occurs while creating the document.
|
|
42
41
|
*/
|
|
43
42
|
async create(input, options) {
|
|
44
|
-
const
|
|
45
|
-
if (id != null)
|
|
46
|
-
input._id = id;
|
|
47
|
-
const info = {
|
|
43
|
+
const command = {
|
|
48
44
|
crud: 'create',
|
|
49
45
|
method: 'create',
|
|
50
46
|
byId: false,
|
|
51
|
-
documentId: id,
|
|
52
47
|
input,
|
|
53
48
|
options,
|
|
54
49
|
};
|
|
55
|
-
return this.
|
|
50
|
+
return this._executeCommand(command, () => this._create(command));
|
|
56
51
|
}
|
|
57
52
|
/**
|
|
58
53
|
* Returns the count of documents in the collection based on the provided options.
|
|
59
54
|
*
|
|
60
|
-
* @param {
|
|
55
|
+
* @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
|
|
61
56
|
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
62
57
|
*/
|
|
63
58
|
async count(options) {
|
|
64
|
-
const
|
|
59
|
+
const command = {
|
|
65
60
|
crud: 'read',
|
|
66
61
|
method: 'count',
|
|
67
62
|
byId: false,
|
|
68
63
|
options,
|
|
69
64
|
};
|
|
70
|
-
return this.
|
|
71
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
return this._executeCommand(command, async () => {
|
|
66
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
67
|
+
command.options = { ...command.options, filter };
|
|
68
|
+
return this._count(command);
|
|
69
|
+
});
|
|
74
70
|
}
|
|
75
71
|
/**
|
|
76
72
|
* Deletes a document from the collection.
|
|
77
73
|
*
|
|
78
74
|
* @param {MongoAdapter.AnyId} id - The ID of the document to delete.
|
|
79
|
-
* @param {
|
|
75
|
+
* @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
|
|
80
76
|
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
81
77
|
*/
|
|
82
78
|
async delete(id, options) {
|
|
83
|
-
const
|
|
79
|
+
const command = {
|
|
84
80
|
crud: 'delete',
|
|
85
81
|
method: 'delete',
|
|
86
82
|
byId: true,
|
|
87
83
|
documentId: id,
|
|
88
84
|
options,
|
|
89
85
|
};
|
|
90
|
-
return this.
|
|
91
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
92
|
-
|
|
93
|
-
|
|
86
|
+
return this._executeCommand(command, async () => {
|
|
87
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
88
|
+
command.options = { ...command.options, filter };
|
|
89
|
+
return this._delete(command);
|
|
90
|
+
});
|
|
94
91
|
}
|
|
95
92
|
/**
|
|
96
93
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
97
94
|
*
|
|
98
|
-
* @param {
|
|
95
|
+
* @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
|
|
99
96
|
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
100
97
|
*/
|
|
101
98
|
async deleteMany(options) {
|
|
102
|
-
const
|
|
99
|
+
const command = {
|
|
103
100
|
crud: 'delete',
|
|
104
101
|
method: 'deleteMany',
|
|
105
102
|
byId: false,
|
|
106
103
|
options,
|
|
107
104
|
};
|
|
108
|
-
return this.
|
|
109
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
return this._executeCommand(command, async () => {
|
|
106
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
107
|
+
command.options = { ...command.options, filter };
|
|
108
|
+
return this._deleteMany(command);
|
|
109
|
+
});
|
|
112
110
|
}
|
|
113
111
|
/**
|
|
114
112
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
115
113
|
* @param {string} field
|
|
116
|
-
* @param {
|
|
114
|
+
* @param {MongoEntityService.DistinctOptions<T>} [options]
|
|
117
115
|
* @protected
|
|
118
116
|
*/
|
|
119
117
|
async distinct(field, options) {
|
|
120
|
-
const
|
|
118
|
+
const command = {
|
|
121
119
|
crud: 'read',
|
|
122
120
|
method: 'distinct',
|
|
123
|
-
byId:
|
|
121
|
+
byId: false,
|
|
122
|
+
field,
|
|
124
123
|
options,
|
|
125
124
|
};
|
|
126
|
-
return this.
|
|
127
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
128
|
-
|
|
129
|
-
|
|
125
|
+
return this._executeCommand(command, async () => {
|
|
126
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
127
|
+
command.options = { ...command.options, filter };
|
|
128
|
+
return this._distinct(command);
|
|
129
|
+
});
|
|
130
130
|
}
|
|
131
131
|
/**
|
|
132
132
|
* Checks if an object with the given id exists.
|
|
133
133
|
*
|
|
134
134
|
* @param {MongoAdapter.AnyId} id - The id of the object to check.
|
|
135
|
-
* @param {
|
|
135
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options] - The options for the query (optional).
|
|
136
136
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
137
137
|
*/
|
|
138
138
|
async exists(id, options) {
|
|
139
|
-
|
|
139
|
+
const command = {
|
|
140
|
+
crud: 'read',
|
|
141
|
+
method: 'exists',
|
|
142
|
+
byId: true,
|
|
143
|
+
documentId: id,
|
|
144
|
+
options,
|
|
145
|
+
};
|
|
146
|
+
return this._executeCommand(command, async () => {
|
|
147
|
+
const documentFilter = await this._getDocumentFilter(command);
|
|
148
|
+
const filter = MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
|
|
149
|
+
const findCommand = command;
|
|
150
|
+
findCommand.options = { ...command.options, filter, projection: ['_id'] };
|
|
151
|
+
return !!(await this._findById(findCommand));
|
|
152
|
+
});
|
|
140
153
|
}
|
|
141
154
|
/**
|
|
142
155
|
* Checks if an object with the given arguments exists.
|
|
143
156
|
*
|
|
144
|
-
* @param {
|
|
157
|
+
* @param {MongoEntityService.ExistsOneOptions} [options] - The options for the query (optional).
|
|
145
158
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
146
159
|
*/
|
|
147
160
|
async existsOne(options) {
|
|
@@ -151,83 +164,88 @@ export class MongoCollectionService extends MongoEntityService {
|
|
|
151
164
|
* Finds a document by its ID.
|
|
152
165
|
*
|
|
153
166
|
* @param {MongoAdapter.AnyId} id - The ID of the document.
|
|
154
|
-
* @param {
|
|
167
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
155
168
|
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
156
169
|
*/
|
|
157
170
|
async findById(id, options) {
|
|
158
|
-
const
|
|
171
|
+
const command = {
|
|
159
172
|
crud: 'read',
|
|
160
173
|
method: 'findById',
|
|
161
174
|
byId: true,
|
|
162
175
|
documentId: id,
|
|
163
176
|
options,
|
|
164
177
|
};
|
|
165
|
-
return this.
|
|
166
|
-
const documentFilter = await this._getDocumentFilter(
|
|
167
|
-
const filter = MongoAdapter.prepareFilter([documentFilter, options?.filter]);
|
|
168
|
-
|
|
169
|
-
|
|
178
|
+
return this._executeCommand(command, async () => {
|
|
179
|
+
const documentFilter = await this._getDocumentFilter(command);
|
|
180
|
+
const filter = MongoAdapter.prepareFilter([documentFilter, command.options?.filter]);
|
|
181
|
+
command.options = { ...command.options, filter };
|
|
182
|
+
return this._findById(command);
|
|
183
|
+
});
|
|
170
184
|
}
|
|
171
185
|
/**
|
|
172
186
|
* Finds a document in the collection that matches the specified options.
|
|
173
187
|
*
|
|
174
|
-
* @param {
|
|
188
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the query.
|
|
175
189
|
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
176
190
|
*/
|
|
177
191
|
async findOne(options) {
|
|
178
|
-
const
|
|
192
|
+
const command = {
|
|
179
193
|
crud: 'read',
|
|
180
194
|
method: 'findOne',
|
|
181
195
|
byId: false,
|
|
182
196
|
options,
|
|
183
197
|
};
|
|
184
|
-
return this.
|
|
185
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
186
|
-
|
|
187
|
-
|
|
198
|
+
return this._executeCommand(command, async () => {
|
|
199
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
200
|
+
command.options = { ...command.options, filter };
|
|
201
|
+
return this._findOne(command);
|
|
202
|
+
});
|
|
188
203
|
}
|
|
189
204
|
/**
|
|
190
205
|
* Finds multiple documents in the MongoDB collection.
|
|
191
206
|
*
|
|
192
|
-
* @param {
|
|
207
|
+
* @param {MongoEntityService.FindManyOptions<T>} options - The options for the find operation.
|
|
193
208
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
194
209
|
*/
|
|
195
210
|
async findMany(options) {
|
|
196
|
-
const
|
|
211
|
+
const command = {
|
|
197
212
|
crud: 'read',
|
|
198
213
|
method: 'findMany',
|
|
199
214
|
byId: false,
|
|
200
215
|
options,
|
|
201
216
|
};
|
|
202
|
-
return this.
|
|
203
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
204
|
-
|
|
205
|
-
|
|
217
|
+
return this._executeCommand(command, async () => {
|
|
218
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
219
|
+
const limit = command.options?.limit || this.defaultLimit;
|
|
220
|
+
command.options = { ...command.options, filter, limit };
|
|
221
|
+
return this._findMany(command);
|
|
222
|
+
});
|
|
206
223
|
}
|
|
207
224
|
/**
|
|
208
225
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
209
226
|
* and total count that matched the given criteria
|
|
210
227
|
*
|
|
211
|
-
* @param {
|
|
228
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
212
229
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
213
230
|
*/
|
|
214
231
|
async findManyWithCount(options) {
|
|
215
|
-
const
|
|
232
|
+
const command = {
|
|
216
233
|
crud: 'read',
|
|
217
234
|
method: 'findManyWithCount',
|
|
218
235
|
byId: false,
|
|
219
236
|
options,
|
|
220
237
|
};
|
|
221
|
-
return this.
|
|
222
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
223
|
-
|
|
224
|
-
|
|
238
|
+
return this._executeCommand(command, async () => {
|
|
239
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
240
|
+
command.options = { ...command.options, filter, limit: command.options?.limit || this.defaultLimit };
|
|
241
|
+
return this._findManyWithCount(command);
|
|
242
|
+
});
|
|
225
243
|
}
|
|
226
244
|
/**
|
|
227
245
|
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
228
246
|
*
|
|
229
247
|
* @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
|
|
230
|
-
* @param {
|
|
248
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
231
249
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
232
250
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
233
251
|
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
@@ -243,73 +261,73 @@ export class MongoCollectionService extends MongoEntityService {
|
|
|
243
261
|
*
|
|
244
262
|
* @param {MongoAdapter.AnyId} id - The id of the document to update.
|
|
245
263
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
246
|
-
* @param {
|
|
264
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for the update operation.
|
|
247
265
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
248
266
|
* undefined if the document was not found.
|
|
249
267
|
*/
|
|
250
268
|
async update(id, input, options) {
|
|
251
|
-
const
|
|
269
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
270
|
+
const command = {
|
|
252
271
|
crud: 'update',
|
|
253
272
|
method: 'update',
|
|
254
273
|
documentId: id,
|
|
255
274
|
byId: true,
|
|
256
|
-
input,
|
|
275
|
+
input: isUpdateFilter ? undefined : input,
|
|
276
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
257
277
|
options,
|
|
258
278
|
};
|
|
259
|
-
return this.
|
|
260
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
261
|
-
|
|
262
|
-
|
|
279
|
+
return this._executeCommand(command, async () => {
|
|
280
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
281
|
+
command.options = { ...command.options, filter };
|
|
282
|
+
return this._update(command);
|
|
283
|
+
});
|
|
263
284
|
}
|
|
264
285
|
/**
|
|
265
286
|
* Updates a document in the collection with the specified ID.
|
|
266
287
|
*
|
|
267
288
|
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
268
289
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
269
|
-
* @param {
|
|
290
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
|
|
270
291
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
271
292
|
*/
|
|
272
293
|
async updateOnly(id, input, options) {
|
|
273
|
-
const
|
|
294
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
295
|
+
const command = {
|
|
274
296
|
crud: 'update',
|
|
275
|
-
method: '
|
|
297
|
+
method: 'updateOnly',
|
|
276
298
|
documentId: id,
|
|
277
299
|
byId: true,
|
|
278
|
-
input,
|
|
300
|
+
input: isUpdateFilter ? undefined : input,
|
|
301
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
279
302
|
options,
|
|
280
303
|
};
|
|
281
|
-
return this.
|
|
282
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
283
|
-
|
|
284
|
-
|
|
304
|
+
return this._executeCommand(command, async () => {
|
|
305
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
306
|
+
command.options = { ...command.options, filter };
|
|
307
|
+
return this._updateOnly(command);
|
|
308
|
+
});
|
|
285
309
|
}
|
|
286
310
|
/**
|
|
287
311
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
288
312
|
*
|
|
289
313
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
290
|
-
* @param {
|
|
314
|
+
* @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
|
|
291
315
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
292
316
|
*/
|
|
293
317
|
async updateMany(input, options) {
|
|
294
|
-
const
|
|
318
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
319
|
+
const command = {
|
|
295
320
|
crud: 'update',
|
|
296
321
|
method: 'updateMany',
|
|
297
322
|
byId: false,
|
|
298
|
-
input,
|
|
323
|
+
input: isUpdateFilter ? undefined : input,
|
|
324
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
299
325
|
options,
|
|
300
326
|
};
|
|
301
|
-
return this.
|
|
302
|
-
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
/**
|
|
307
|
-
* Generates an ID.
|
|
308
|
-
*
|
|
309
|
-
* @protected
|
|
310
|
-
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
311
|
-
*/
|
|
312
|
-
_generateId() {
|
|
313
|
-
return typeof this.$idGenerator === 'function' ? this.$idGenerator(this) : new ObjectId();
|
|
327
|
+
return this._executeCommand(command, async () => {
|
|
328
|
+
const filter = MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
329
|
+
command.options = { ...command.options, filter };
|
|
330
|
+
return this._updateMany(command);
|
|
331
|
+
});
|
|
314
332
|
}
|
|
315
333
|
}
|