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