@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,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
|
@@ -16,8 +16,8 @@ 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
|
|
20
|
-
const
|
|
19
|
+
if (operation?.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
|
|
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
|
};
|
|
@@ -42,16 +43,17 @@ var MongoAdapter;
|
|
|
42
43
|
case 'Entity.FindMany': {
|
|
43
44
|
const options = {
|
|
44
45
|
filter: context.queryParams.filter,
|
|
45
|
-
projection: context.queryParams.projection,
|
|
46
|
+
projection: context.queryParams.projection || operation.compositionOptions.defaultProjection,
|
|
46
47
|
count: context.queryParams.count,
|
|
47
|
-
limit: context.queryParams.limit,
|
|
48
|
+
limit: context.queryParams.limit || operation.compositionOptions.defaultLimit,
|
|
48
49
|
skip: context.queryParams.skip,
|
|
49
|
-
sort: context.queryParams.sort,
|
|
50
|
+
sort: context.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
50
51
|
};
|
|
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");
|
|
@@ -19,14 +18,14 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
|
|
|
19
18
|
*/
|
|
20
19
|
constructor(dataType, options) {
|
|
21
20
|
super(dataType, options);
|
|
22
|
-
this.defaultLimit =
|
|
21
|
+
this.defaultLimit = options?.defaultLimit || 10;
|
|
23
22
|
}
|
|
24
23
|
/**
|
|
25
24
|
* Asserts the existence of a resource with the given ID.
|
|
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,88 @@ 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
|
+
const limit = command.options?.limit || this.defaultLimit;
|
|
223
|
+
command.options = { ...command.options, filter, limit };
|
|
224
|
+
return this._findMany(command);
|
|
225
|
+
});
|
|
209
226
|
}
|
|
210
227
|
/**
|
|
211
228
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
212
229
|
* and total count that matched the given criteria
|
|
213
230
|
*
|
|
214
|
-
* @param {
|
|
231
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
215
232
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
216
233
|
*/
|
|
217
234
|
async findManyWithCount(options) {
|
|
218
|
-
const
|
|
235
|
+
const command = {
|
|
219
236
|
crud: 'read',
|
|
220
237
|
method: 'findManyWithCount',
|
|
221
238
|
byId: false,
|
|
222
239
|
options,
|
|
223
240
|
};
|
|
224
|
-
return this.
|
|
225
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
226
|
-
|
|
227
|
-
|
|
241
|
+
return this._executeCommand(command, async () => {
|
|
242
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
243
|
+
command.options = { ...command.options, filter, limit: command.options?.limit || this.defaultLimit };
|
|
244
|
+
return this._findManyWithCount(command);
|
|
245
|
+
});
|
|
228
246
|
}
|
|
229
247
|
/**
|
|
230
248
|
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
231
249
|
*
|
|
232
250
|
* @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
|
|
233
|
-
* @param {
|
|
251
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
234
252
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
235
253
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
236
254
|
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
@@ -246,74 +264,74 @@ class MongoCollectionService extends mongo_entity_service_js_1.MongoEntityServic
|
|
|
246
264
|
*
|
|
247
265
|
* @param {MongoAdapter.AnyId} id - The id of the document to update.
|
|
248
266
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
249
|
-
* @param {
|
|
267
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for the update operation.
|
|
250
268
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
251
269
|
* undefined if the document was not found.
|
|
252
270
|
*/
|
|
253
271
|
async update(id, input, options) {
|
|
254
|
-
const
|
|
272
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
273
|
+
const command = {
|
|
255
274
|
crud: 'update',
|
|
256
275
|
method: 'update',
|
|
257
276
|
documentId: id,
|
|
258
277
|
byId: true,
|
|
259
|
-
input,
|
|
278
|
+
input: isUpdateFilter ? undefined : input,
|
|
279
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
260
280
|
options,
|
|
261
281
|
};
|
|
262
|
-
return this.
|
|
263
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
264
|
-
|
|
265
|
-
|
|
282
|
+
return this._executeCommand(command, async () => {
|
|
283
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
284
|
+
command.options = { ...command.options, filter };
|
|
285
|
+
return this._update(command);
|
|
286
|
+
});
|
|
266
287
|
}
|
|
267
288
|
/**
|
|
268
289
|
* Updates a document in the collection with the specified ID.
|
|
269
290
|
*
|
|
270
291
|
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
271
292
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
272
|
-
* @param {
|
|
293
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
|
|
273
294
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
274
295
|
*/
|
|
275
296
|
async updateOnly(id, input, options) {
|
|
276
|
-
const
|
|
297
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
298
|
+
const command = {
|
|
277
299
|
crud: 'update',
|
|
278
|
-
method: '
|
|
300
|
+
method: 'updateOnly',
|
|
279
301
|
documentId: id,
|
|
280
302
|
byId: true,
|
|
281
|
-
input,
|
|
303
|
+
input: isUpdateFilter ? undefined : input,
|
|
304
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
282
305
|
options,
|
|
283
306
|
};
|
|
284
|
-
return this.
|
|
285
|
-
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(
|
|
286
|
-
|
|
287
|
-
|
|
307
|
+
return this._executeCommand(command, async () => {
|
|
308
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
309
|
+
command.options = { ...command.options, filter };
|
|
310
|
+
return this._updateOnly(command);
|
|
311
|
+
});
|
|
288
312
|
}
|
|
289
313
|
/**
|
|
290
314
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
291
315
|
*
|
|
292
316
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
293
|
-
* @param {
|
|
317
|
+
* @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
|
|
294
318
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
295
319
|
*/
|
|
296
320
|
async updateMany(input, options) {
|
|
297
|
-
const
|
|
321
|
+
const isUpdateFilter = Array.isArray(input) || !!Object.keys(input).find(x => x.startsWith('$'));
|
|
322
|
+
const command = {
|
|
298
323
|
crud: 'update',
|
|
299
324
|
method: 'updateMany',
|
|
300
325
|
byId: false,
|
|
301
|
-
input,
|
|
326
|
+
input: isUpdateFilter ? undefined : input,
|
|
327
|
+
inputRaw: isUpdateFilter ? input : undefined,
|
|
302
328
|
options,
|
|
303
329
|
};
|
|
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();
|
|
330
|
+
return this._executeCommand(command, async () => {
|
|
331
|
+
const filter = mongo_adapter_js_1.MongoAdapter.prepareFilter([await this._getDocumentFilter(command), command.options?.filter]);
|
|
332
|
+
command.options = { ...command.options, filter };
|
|
333
|
+
return this._updateMany(command);
|
|
334
|
+
});
|
|
317
335
|
}
|
|
318
336
|
}
|
|
319
337
|
exports.MongoCollectionService = MongoCollectionService;
|