@opra/mongodb 0.32.3 → 0.32.4
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 +7 -3
- package/cjs/mongo-array-service.js +215 -57
- package/cjs/mongo-collection-service.js +133 -29
- package/cjs/mongo-service.js +182 -106
- package/cjs/mongo-singleton-service.js +114 -17
- package/esm/adapter-utils/prepare-filter.js +7 -3
- package/esm/mongo-array-service.js +215 -57
- package/esm/mongo-collection-service.js +133 -29
- package/esm/mongo-service.js +182 -106
- package/esm/mongo-singleton-service.js +114 -17
- package/package.json +4 -6
- package/types/adapter-utils/prepare-filter.d.ts +8 -5
- package/types/mongo-array-service.d.ts +106 -33
- package/types/mongo-collection-service.d.ts +78 -24
- package/types/mongo-service.d.ts +157 -62
- package/types/mongo-singleton-service.d.ts +82 -16
|
@@ -9,10 +9,19 @@ import { MongoService } from './mongo-service.js';
|
|
|
9
9
|
* @template T - The type of document stored in the collection
|
|
10
10
|
*/
|
|
11
11
|
export class MongoSingletonService extends MongoService {
|
|
12
|
+
/**
|
|
13
|
+
* Constructs a new instance
|
|
14
|
+
*
|
|
15
|
+
* @param {Type | string} dataType - The data type of the array elements.
|
|
16
|
+
* @param {MongoSingletonService.Options} [options] - The options for the array service.
|
|
17
|
+
* @constructor
|
|
18
|
+
*/
|
|
12
19
|
constructor(dataType, options) {
|
|
13
20
|
super(dataType, options);
|
|
14
|
-
this.collectionKey = options?.collectionKey || '_id';
|
|
15
|
-
this._id = options?._id || new ObjectId('655608925cad472b75fc6485');
|
|
21
|
+
this.collectionKey = this.collectionKey || options?.collectionKey || '_id';
|
|
22
|
+
this._id = this._id || options?._id || new ObjectId('655608925cad472b75fc6485');
|
|
23
|
+
this.$documentFilter = this.$documentFilter || options?.documentFilter;
|
|
24
|
+
this.$interceptor = this.$interceptor || options?.interceptor;
|
|
16
25
|
}
|
|
17
26
|
/**
|
|
18
27
|
* Asserts the existence of a resource based on the given options.
|
|
@@ -20,19 +29,26 @@ export class MongoSingletonService extends MongoService {
|
|
|
20
29
|
* @returns {Promise<void>} A Promise that resolves when the resource exists.
|
|
21
30
|
* @throws {ResourceNotFoundError} If the resource does not exist.
|
|
22
31
|
*/
|
|
23
|
-
async assert() {
|
|
24
|
-
if (!(await this.exists()))
|
|
25
|
-
throw new ResourceNotFoundError(this.
|
|
32
|
+
async assert(options) {
|
|
33
|
+
if (!(await this.exists(options)))
|
|
34
|
+
throw new ResourceNotFoundError(this.getResourceName());
|
|
26
35
|
}
|
|
27
36
|
/**
|
|
28
37
|
* Creates the document in the database.
|
|
29
38
|
*
|
|
30
|
-
* @param {
|
|
39
|
+
* @param {DTO<T>} input - The partial input to create the document with.
|
|
31
40
|
* @param {MongoSingletonService.CreateOptions} [options] - The options for creating the document.
|
|
32
|
-
* @return {Promise<
|
|
41
|
+
* @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
|
|
33
42
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
34
43
|
*/
|
|
35
44
|
async create(input, options) {
|
|
45
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
46
|
+
return this.$interceptor(() => this.create(input, { ...options, __interceptor__: true }), {
|
|
47
|
+
crud: 'create',
|
|
48
|
+
method: 'create',
|
|
49
|
+
input,
|
|
50
|
+
options,
|
|
51
|
+
}, this);
|
|
36
52
|
const encode = this.getEncoder('create');
|
|
37
53
|
const doc = encode(input);
|
|
38
54
|
doc._id = this._id;
|
|
@@ -58,7 +74,17 @@ export class MongoSingletonService extends MongoService {
|
|
|
58
74
|
* @returns {Promise<number>} The number of records deleted
|
|
59
75
|
*/
|
|
60
76
|
async delete(options) {
|
|
61
|
-
|
|
77
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
78
|
+
return this.$interceptor(() => this.delete({ ...options, __interceptor__: true }), {
|
|
79
|
+
crud: 'delete',
|
|
80
|
+
method: 'delete',
|
|
81
|
+
options,
|
|
82
|
+
}, this);
|
|
83
|
+
const filter = MongoAdapter.prepareFilter([
|
|
84
|
+
{ _id: this._id },
|
|
85
|
+
await this._getDocumentFilter(),
|
|
86
|
+
options?.filter
|
|
87
|
+
]);
|
|
62
88
|
const r = await this.__deleteOne(filter, options);
|
|
63
89
|
return r.deletedCount;
|
|
64
90
|
}
|
|
@@ -67,17 +93,33 @@ export class MongoSingletonService extends MongoService {
|
|
|
67
93
|
*
|
|
68
94
|
* @return {Promise<boolean>} - A promise that resolves to a boolean value indicating if the document exists.
|
|
69
95
|
*/
|
|
70
|
-
async exists() {
|
|
71
|
-
|
|
96
|
+
async exists(options) {
|
|
97
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
98
|
+
return this.$interceptor(() => this.exists({ ...options, __interceptor__: true }), {
|
|
99
|
+
crud: 'read',
|
|
100
|
+
method: 'exists',
|
|
101
|
+
options,
|
|
102
|
+
}, this);
|
|
103
|
+
return !!(await this.findOne({ ...options, pick: ['_id'] }));
|
|
72
104
|
}
|
|
73
105
|
/**
|
|
74
106
|
* Fetches the document if it exists. Returns undefined if not found.
|
|
75
107
|
*
|
|
76
108
|
* @param {MongoSingletonService.FindOptions<T>} [options] - The options for finding the document.
|
|
77
|
-
* @returns {Promise<
|
|
109
|
+
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
78
110
|
*/
|
|
79
111
|
async findOne(options) {
|
|
80
|
-
|
|
112
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
113
|
+
return this.$interceptor(() => this.findOne({ ...options, __interceptor__: true }), {
|
|
114
|
+
crud: 'read',
|
|
115
|
+
method: 'find',
|
|
116
|
+
options,
|
|
117
|
+
}, this);
|
|
118
|
+
const filter = MongoAdapter.prepareFilter([
|
|
119
|
+
{ _id: this._id },
|
|
120
|
+
await this._getDocumentFilter(),
|
|
121
|
+
options?.filter
|
|
122
|
+
]);
|
|
81
123
|
const mongoOptions = {
|
|
82
124
|
...options,
|
|
83
125
|
projection: MongoAdapter.prepareProjection(this.getDataType(), options),
|
|
@@ -85,30 +127,72 @@ export class MongoSingletonService extends MongoService {
|
|
|
85
127
|
skip: undefined,
|
|
86
128
|
limit: undefined
|
|
87
129
|
};
|
|
88
|
-
|
|
130
|
+
const decoder = this.getDecoder();
|
|
131
|
+
const out = await this.__findOne(filter, mongoOptions);
|
|
132
|
+
return out ? decoder(out) : undefined;
|
|
89
133
|
}
|
|
90
134
|
/**
|
|
91
135
|
* Fetches the document from the Mongo collection service. Throws error if not found.
|
|
92
136
|
*
|
|
93
137
|
* @param {MongoCollectionService.FindOneOptions} options - The options to customize the query.
|
|
94
|
-
* @return {Promise<
|
|
138
|
+
* @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
|
|
95
139
|
* @throws {ResourceNotFoundError} - If the document is not found in the collection.
|
|
96
140
|
*/
|
|
97
141
|
async get(options) {
|
|
98
142
|
const out = await this.findOne(options);
|
|
99
143
|
if (!out)
|
|
100
|
-
throw new ResourceNotFoundError(this.
|
|
144
|
+
throw new ResourceNotFoundError(this.getResourceName());
|
|
101
145
|
return out;
|
|
102
146
|
}
|
|
103
147
|
/**
|
|
104
148
|
* Updates a document in the MongoDB collection.
|
|
105
149
|
*
|
|
106
|
-
* @param {
|
|
150
|
+
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
107
151
|
* @param {MongoSingletonService.UpdateOptions<T>} [options] - The update options.
|
|
108
152
|
*
|
|
109
|
-
* @return {Promise<
|
|
153
|
+
* @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
|
|
154
|
+
*/
|
|
155
|
+
async updateOnly(input, options) {
|
|
156
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
157
|
+
return this.$interceptor(() => this.updateOnly(input, { ...options, __interceptor__: true }), {
|
|
158
|
+
crud: 'update',
|
|
159
|
+
method: 'update',
|
|
160
|
+
input,
|
|
161
|
+
options,
|
|
162
|
+
}, this);
|
|
163
|
+
const encode = this.getEncoder('update');
|
|
164
|
+
const doc = encode(input);
|
|
165
|
+
const patch = MongoAdapter.preparePatch(doc);
|
|
166
|
+
const mongoOptions = {
|
|
167
|
+
...options,
|
|
168
|
+
includeResultMetadata: false,
|
|
169
|
+
upsert: undefined,
|
|
170
|
+
projection: MongoAdapter.prepareProjection(this.getDataType(), options),
|
|
171
|
+
};
|
|
172
|
+
const filter = MongoAdapter.prepareFilter([
|
|
173
|
+
{ _id: this._id },
|
|
174
|
+
await this._getDocumentFilter(),
|
|
175
|
+
options?.filter
|
|
176
|
+
]);
|
|
177
|
+
const r = await this.__updateOne(filter, patch, mongoOptions);
|
|
178
|
+
return r.modifiedCount;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Updates a document in the MongoDB collection.
|
|
182
|
+
*
|
|
183
|
+
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
184
|
+
* @param {MongoSingletonService.UpdateOptions<T>} [options] - The update options.
|
|
185
|
+
*
|
|
186
|
+
* @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
|
|
110
187
|
*/
|
|
111
188
|
async update(input, options) {
|
|
189
|
+
if (this.$interceptor && !options?.__interceptor__)
|
|
190
|
+
return this.$interceptor(() => this.update(input, { ...options, __interceptor__: true }), {
|
|
191
|
+
crud: 'update',
|
|
192
|
+
method: 'update',
|
|
193
|
+
input,
|
|
194
|
+
options,
|
|
195
|
+
}, this);
|
|
112
196
|
const encode = this.getEncoder('update');
|
|
113
197
|
const doc = encode(input);
|
|
114
198
|
const patch = MongoAdapter.preparePatch(doc);
|
|
@@ -120,10 +204,23 @@ export class MongoSingletonService extends MongoService {
|
|
|
120
204
|
};
|
|
121
205
|
const filter = MongoAdapter.prepareFilter([
|
|
122
206
|
{ _id: this._id },
|
|
207
|
+
await this._getDocumentFilter(),
|
|
123
208
|
options?.filter
|
|
124
209
|
]);
|
|
125
210
|
const decoder = this.getDecoder();
|
|
126
211
|
const out = await this.__findOneAndUpdate(filter, patch, mongoOptions);
|
|
127
212
|
return out ? decoder(out) : undefined;
|
|
128
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Retrieves the common filter used for querying the document.
|
|
216
|
+
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
217
|
+
*
|
|
218
|
+
* @protected
|
|
219
|
+
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
220
|
+
* that resolves to the common filter, or undefined if not available.
|
|
221
|
+
*/
|
|
222
|
+
_getDocumentFilter() {
|
|
223
|
+
return typeof this.$documentFilter === 'function' ?
|
|
224
|
+
this.$documentFilter(this) : this.$documentFilter;
|
|
225
|
+
}
|
|
129
226
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "0.32.
|
|
3
|
+
"version": "0.32.4",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,14 +26,12 @@
|
|
|
26
26
|
},
|
|
27
27
|
"devDependencies": {
|
|
28
28
|
"@faker-js/faker": "^8.3.1",
|
|
29
|
-
"@types/lodash.isnil": "^4.0.9",
|
|
30
|
-
"@types/lodash.omitby": "^4.6.9",
|
|
31
29
|
"mongodb": "^6.3.0",
|
|
32
|
-
"ts-gems": "^2.
|
|
30
|
+
"ts-gems": "^2.8.0"
|
|
33
31
|
},
|
|
34
32
|
"peerDependencies": {
|
|
35
|
-
"@opra/common": "^0.32.
|
|
36
|
-
"@opra/core": "^0.32.
|
|
33
|
+
"@opra/common": "^0.32.4",
|
|
34
|
+
"@opra/core": "^0.32.4",
|
|
37
35
|
"mongodb": ">=6.x.x"
|
|
38
36
|
},
|
|
39
37
|
"type": "module",
|
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
2
|
import { OpraFilter } from '@opra/common';
|
|
3
|
-
type FilterInput = (OpraFilter.Expression | mongodb.Filter<any> | string | undefined);
|
|
3
|
+
export type FilterInput = (OpraFilter.Expression | mongodb.Filter<any> | string | undefined);
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @param filters
|
|
5
|
+
* Prepare the MongoDB filter based on the provided filters and options.
|
|
6
|
+
*
|
|
7
|
+
* @param {FilterInput|FilterInput[]} filters - The filter(s) to be applied.
|
|
8
|
+
* @param {Object} [options] - Additional options.
|
|
9
|
+
* @param {string} [options.fieldPrefix] - The prefix to be added to field names.
|
|
10
|
+
*
|
|
11
|
+
* @returns {mongodb.Filter<any>} - The prepared MongoDB filter.
|
|
8
12
|
*/
|
|
9
13
|
export default function prepareFilter(filters: FilterInput | FilterInput[], options?: {
|
|
10
14
|
fieldPrefix?: string;
|
|
11
15
|
}): mongodb.Filter<any>;
|
|
12
|
-
export {};
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
2
|
import { StrictOmit, Type } from 'ts-gems';
|
|
3
3
|
import * as OpraCommon from '@opra/common';
|
|
4
|
-
import { ComplexType,
|
|
5
|
-
import {
|
|
6
|
-
import { MongoCollectionService } from './mongo-collection-service.js';
|
|
4
|
+
import { ComplexType, DTO, PartialDTO, PatchDTO } from '@opra/common';
|
|
5
|
+
import { FilterInput } from './adapter-utils/prepare-filter.js';
|
|
7
6
|
import { MongoService } from './mongo-service.js';
|
|
8
7
|
import { AnyId } from './types.js';
|
|
9
8
|
/**
|
|
@@ -35,34 +34,70 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
35
34
|
* @type {number}
|
|
36
35
|
*/
|
|
37
36
|
defaultLimit: number;
|
|
38
|
-
constructor(dataType: Type | string, fieldName: string, options?: MongoArrayService.Options);
|
|
39
37
|
/**
|
|
40
|
-
*
|
|
38
|
+
* Represents a common document filter function for a MongoCollectionService.
|
|
41
39
|
*
|
|
42
|
-
* @
|
|
43
|
-
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
40
|
+
* @type {FilterInput
|
|
44
41
|
*/
|
|
45
|
-
|
|
42
|
+
$documentFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
43
|
+
/**
|
|
44
|
+
* Represents a common array filter function for a MongoCollectionService.
|
|
45
|
+
*
|
|
46
|
+
* @type {FilterInput | MongoCollectionService.CommonFilterFunction}
|
|
47
|
+
*/
|
|
48
|
+
$arrayFilter?: FilterInput | ((_this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
49
|
+
/**
|
|
50
|
+
* Interceptor function for handling callback execution with provided arguments.
|
|
51
|
+
*
|
|
52
|
+
* @param {Function} callback - The callback function to be intercepted.
|
|
53
|
+
* @param {Object} args - The arguments object containing the following properties:
|
|
54
|
+
* @param {string} crud - The CRUD operation type.
|
|
55
|
+
* @param {string} method - The method name.
|
|
56
|
+
* @param {AnyId} documentId - The document ID (optional).
|
|
57
|
+
* @param {AnyId} itemId - ID for an item. (optional).
|
|
58
|
+
* @param {Object} input - The input object (optional).
|
|
59
|
+
* @param {Object} options - Additional options (optional).
|
|
60
|
+
* @param {Object} _this - The reference to the current object.
|
|
61
|
+
* @returns {Promise<any>} - The promise that resolves to the result of the callback execution.
|
|
62
|
+
*/
|
|
63
|
+
$interceptor?: (callback: (...args: any[]) => any, args: {
|
|
64
|
+
crud: MongoService.CrudOp;
|
|
65
|
+
method: string;
|
|
66
|
+
documentId?: AnyId;
|
|
67
|
+
itemId?: AnyId;
|
|
68
|
+
input?: Object;
|
|
69
|
+
options?: Record<string, any>;
|
|
70
|
+
}, _this: any) => Promise<any>;
|
|
71
|
+
/**
|
|
72
|
+
* Constructs a new instance
|
|
73
|
+
*
|
|
74
|
+
* @param {Type | string} dataType - The data type of the array elements.
|
|
75
|
+
* @param {string} fieldName - The name of the field in the document representing the array.
|
|
76
|
+
* @param {MongoArrayService.Options} [options] - The options for the array service.
|
|
77
|
+
* @constructor
|
|
78
|
+
*/
|
|
79
|
+
constructor(dataType: Type | string, fieldName: string, options?: MongoArrayService.Options);
|
|
46
80
|
/**
|
|
47
81
|
* Asserts whether a resource with the specified parentId and id exists.
|
|
48
82
|
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
49
83
|
*
|
|
50
84
|
* @param {AnyId} documentId - The ID of the parent document.
|
|
51
85
|
* @param {AnyId} id - The ID of the resource.
|
|
86
|
+
* @param {MongoArrayService.ExistsOptions} [options] - Optional parameters for checking resource existence.
|
|
52
87
|
* @return {Promise<void>} - A promise that resolves with no value upon success.
|
|
53
88
|
* @throws {ResourceNotFoundError} - If the resource does not exist.
|
|
54
89
|
*/
|
|
55
|
-
assert(documentId: AnyId, id: AnyId): Promise<void>;
|
|
90
|
+
assert(documentId: AnyId, id: AnyId, options?: MongoArrayService.ExistsOptions): Promise<void>;
|
|
56
91
|
/**
|
|
57
92
|
* Adds a single item into the array field.
|
|
58
93
|
*
|
|
59
94
|
* @param {AnyId} documentId - The ID of the parent document.
|
|
60
95
|
* @param {T} input - The item to be added to the array field.
|
|
61
96
|
* @param {MongoArrayService.CreateOptions} [options] - Optional options for the create operation.
|
|
62
|
-
* @return {Promise<
|
|
97
|
+
* @return {Promise<PartialDTO<T>>} - A promise that resolves with the partial output of the created item.
|
|
63
98
|
* @throws {ResourceNotFoundError} - If the parent document is not found.
|
|
64
99
|
*/
|
|
65
|
-
create(documentId: AnyId, input: T
|
|
100
|
+
create(documentId: AnyId, input: DTO<T>, options?: MongoArrayService.CreateOptions): Promise<PartialDTO<T>>;
|
|
66
101
|
/**
|
|
67
102
|
* Counts the number of documents in the collection that match the specified parentId and options.
|
|
68
103
|
*
|
|
@@ -94,90 +129,116 @@ export declare class MongoArrayService<T extends mongodb.Document> extends Mongo
|
|
|
94
129
|
*
|
|
95
130
|
* @param {AnyId} documentId - The ID of the parent document.
|
|
96
131
|
* @param {AnyId} id - The id of the record.
|
|
132
|
+
* @param {MongoArrayService.ExistsOptions} [options] - The options for the exists method.
|
|
97
133
|
* @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the record exists or not.
|
|
98
134
|
*/
|
|
99
|
-
exists(documentId: AnyId, id: AnyId): Promise<boolean>;
|
|
135
|
+
exists(documentId: AnyId, id: AnyId, options?: MongoArrayService.ExistsOptions): Promise<boolean>;
|
|
100
136
|
/**
|
|
101
137
|
* Finds an element in array field by its parent ID and ID.
|
|
102
138
|
*
|
|
103
139
|
* @param {AnyId} documentId - The ID of the document.
|
|
104
140
|
* @param {AnyId} id - The ID of the document.
|
|
105
141
|
* @param {MongoArrayService.FindOneOptions} [options] - The optional options for the operation.
|
|
106
|
-
* @returns {Promise<
|
|
142
|
+
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
107
143
|
*/
|
|
108
|
-
findById(documentId: AnyId, id: AnyId, options?: MongoArrayService.FindOneOptions): Promise<
|
|
144
|
+
findById(documentId: AnyId, id: AnyId, options?: MongoArrayService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
109
145
|
/**
|
|
110
146
|
* Finds the first array element that matches the given parentId.
|
|
111
147
|
*
|
|
112
148
|
* @param {AnyId} documentId - The ID of the document.
|
|
113
149
|
* @param {MongoArrayService.FindOneOptions} [options] - Optional options to customize the query.
|
|
114
|
-
* @returns {Promise<
|
|
150
|
+
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
|
|
115
151
|
*/
|
|
116
|
-
findOne(documentId: AnyId, options?: MongoArrayService.FindOneOptions): Promise<
|
|
152
|
+
findOne(documentId: AnyId, options?: MongoArrayService.FindOneOptions): Promise<PartialDTO<T> | undefined>;
|
|
117
153
|
/**
|
|
118
154
|
* Finds multiple elements in an array field.
|
|
119
155
|
*
|
|
120
156
|
* @param {AnyId} documentId - The ID of the parent document.
|
|
121
157
|
* @param {MongoArrayService.FindManyOptions<T>} [options] - The options for finding the documents.
|
|
122
|
-
* @returns {Promise<
|
|
158
|
+
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
123
159
|
*/
|
|
124
|
-
findMany(documentId: AnyId, options?: MongoArrayService.FindManyOptions<T>): Promise<
|
|
160
|
+
findMany(documentId: AnyId, options?: MongoArrayService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
125
161
|
/**
|
|
126
162
|
* Retrieves a specific item from the array of a document.
|
|
127
163
|
*
|
|
128
164
|
* @param {AnyId} documentId - The ID of the document.
|
|
129
165
|
* @param {AnyId} id - The ID of the item.
|
|
130
166
|
* @param {MongoArrayService.FindOneOptions<T>} [options] - The options for finding the item.
|
|
131
|
-
* @returns {Promise<
|
|
167
|
+
* @returns {Promise<PartialDTO<T>>} - The item found.
|
|
132
168
|
* @throws {ResourceNotFoundError} - If the item is not found.
|
|
133
169
|
*/
|
|
134
|
-
get(documentId: AnyId, id: AnyId, options?: MongoArrayService.FindOneOptions<T>): Promise<
|
|
170
|
+
get(documentId: AnyId, id: AnyId, options?: MongoArrayService.FindOneOptions<T>): Promise<PartialDTO<T>>;
|
|
135
171
|
/**
|
|
136
172
|
* Updates an array element with new data and returns the updated element
|
|
137
173
|
*
|
|
138
174
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
139
175
|
* @param {AnyId} id - The ID of the item to update within the document.
|
|
140
|
-
* @param {
|
|
176
|
+
* @param {PatchDTO<T>} input - The new data to update the item with.
|
|
141
177
|
* @param {MongoArrayService.UpdateOptions<T>} [options] - Additional update options.
|
|
142
|
-
* @returns {Promise<
|
|
178
|
+
* @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
|
|
143
179
|
* @throws {Error} If an error occurs while updating the item.
|
|
144
180
|
*/
|
|
145
|
-
update(documentId: AnyId, id: AnyId, input:
|
|
181
|
+
update(documentId: AnyId, id: AnyId, input: PatchDTO<T>, options?: MongoArrayService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
146
182
|
/**
|
|
147
183
|
* Update an array element with new data. Returns 1 if document updated 0 otherwise.
|
|
148
184
|
*
|
|
149
185
|
* @param {AnyId} documentId - The ID of the parent document.
|
|
150
186
|
* @param {AnyId} id - The ID of the document to update.
|
|
151
|
-
* @param {
|
|
187
|
+
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
152
188
|
* @param {MongoArrayService.UpdateOptions<T>} [options] - Optional update options.
|
|
153
189
|
* @returns {Promise<number>} - A promise that resolves to the number of elements updated.
|
|
154
190
|
*/
|
|
155
|
-
updateOnly(documentId: AnyId, id: AnyId,
|
|
191
|
+
updateOnly(documentId: AnyId, id: AnyId, input: PatchDTO<T>, options?: MongoArrayService.UpdateOptions<T>): Promise<number>;
|
|
156
192
|
/**
|
|
157
193
|
* Updates multiple array elements in document
|
|
158
194
|
*
|
|
159
195
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
160
|
-
* @param {
|
|
196
|
+
* @param {PatchDTO<T>} input - The updated data for the document(s).
|
|
161
197
|
* @param {MongoArrayService.UpdateManyOptions<T>} [options] - Additional options for the update operation.
|
|
162
198
|
* @returns {Promise<number>} - A promise that resolves to the number of documents updated.
|
|
163
199
|
*/
|
|
164
|
-
updateMany(documentId: AnyId, input:
|
|
200
|
+
updateMany(documentId: AnyId, input: PatchDTO<T>, options?: MongoArrayService.UpdateManyOptions<T>): Promise<number>;
|
|
165
201
|
/**
|
|
166
202
|
* Updates multiple elements and returns the count of elements that were updated.
|
|
167
203
|
*
|
|
168
204
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
169
|
-
* @param {
|
|
205
|
+
* @param {PatchDTO<T>} input - The partial document to update with.
|
|
170
206
|
* @param {MongoArrayService.UpdateManyOptions<T>} [options] - The options for updating multiple documents.
|
|
171
207
|
* @return {Promise<number>} A promise that resolves to the number of elements updated.
|
|
172
208
|
*/
|
|
173
|
-
updateManyReturnCount(documentId: AnyId,
|
|
209
|
+
updateManyReturnCount(documentId: AnyId, input: PatchDTO<T>, options?: MongoArrayService.UpdateManyOptions<T>): Promise<number>;
|
|
174
210
|
/**
|
|
175
|
-
*
|
|
211
|
+
* Retrieves the data type of the array field
|
|
212
|
+
*
|
|
213
|
+
* @returns {ComplexType} The complex data type of the field.
|
|
214
|
+
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
215
|
+
*/
|
|
216
|
+
getDataType(): ComplexType;
|
|
217
|
+
/**
|
|
218
|
+
* Generates an ID.
|
|
176
219
|
*
|
|
177
220
|
* @protected
|
|
178
|
-
* @returns {AnyId}
|
|
221
|
+
* @returns {AnyId} The generated ID.
|
|
179
222
|
*/
|
|
180
223
|
protected _generateId(): AnyId;
|
|
224
|
+
/**
|
|
225
|
+
* Retrieves the common filter used for querying documents.
|
|
226
|
+
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
227
|
+
*
|
|
228
|
+
* @protected
|
|
229
|
+
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
230
|
+
* that resolves to the common filter, or undefined if not available.
|
|
231
|
+
*/
|
|
232
|
+
protected _getDocumentFilter(): FilterInput | Promise<FilterInput> | undefined;
|
|
233
|
+
/**
|
|
234
|
+
* Retrieves the common filter used for querying array elements.
|
|
235
|
+
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
236
|
+
*
|
|
237
|
+
* @protected
|
|
238
|
+
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
239
|
+
* that resolves to the common filter, or undefined if not available.
|
|
240
|
+
*/
|
|
241
|
+
protected _getArrayFilter(): FilterInput | Promise<FilterInput> | undefined;
|
|
181
242
|
}
|
|
182
243
|
/**
|
|
183
244
|
*
|
|
@@ -187,8 +248,13 @@ export declare namespace MongoArrayService {
|
|
|
187
248
|
/**
|
|
188
249
|
* The constructor options of MongoArrayService.
|
|
189
250
|
*/
|
|
190
|
-
interface Options extends
|
|
191
|
-
|
|
251
|
+
interface Options extends MongoService.Options {
|
|
252
|
+
collectionKey?: MongoArrayService<any>['collectionKey'];
|
|
253
|
+
defaultLimit?: MongoArrayService<any>['defaultLimit'];
|
|
254
|
+
arrayKey?: MongoArrayService<any>['arrayKey'];
|
|
255
|
+
documentFilter?: MongoArrayService<any>['$documentFilter'];
|
|
256
|
+
arrayFilter?: MongoArrayService<any>['$arrayFilter'];
|
|
257
|
+
interceptor?: MongoArrayService<any>['$interceptor'];
|
|
192
258
|
}
|
|
193
259
|
/**
|
|
194
260
|
* Represents options for creating objects.
|
|
@@ -272,4 +338,11 @@ export declare namespace MongoArrayService {
|
|
|
272
338
|
interface UpdateManyOptions<T> extends mongodb.UpdateOptions {
|
|
273
339
|
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
274
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Represents options for checking the document exists
|
|
343
|
+
*
|
|
344
|
+
* @interface
|
|
345
|
+
*/
|
|
346
|
+
interface ExistsOptions extends Omit<mongodb.CommandOperationOptions, 'writeConcern'> {
|
|
347
|
+
}
|
|
275
348
|
}
|