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