@opra/mongodb 1.0.0-alpha.2 → 1.0.0-alpha.21
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
|
@@ -2,7 +2,6 @@ import mongodb, { UpdateFilter } from 'mongodb';
|
|
|
2
2
|
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
3
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
4
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
5
|
-
import { MongoService } from './mongo-service.js';
|
|
6
5
|
/**
|
|
7
6
|
*
|
|
8
7
|
* @namespace MongoCollectionService
|
|
@@ -17,28 +16,6 @@ export declare namespace MongoCollectionService {
|
|
|
17
16
|
interface Options extends MongoEntityService.Options {
|
|
18
17
|
defaultLimit?: number;
|
|
19
18
|
}
|
|
20
|
-
interface CreateOptions extends MongoEntityService.CreateOptions {
|
|
21
|
-
}
|
|
22
|
-
interface CountOptions<T> extends MongoEntityService.CountOptions<T> {
|
|
23
|
-
}
|
|
24
|
-
interface DeleteOptions<T> extends MongoEntityService.DeleteOptions<T> {
|
|
25
|
-
}
|
|
26
|
-
interface DeleteManyOptions<T> extends MongoEntityService.DeleteManyOptions<T> {
|
|
27
|
-
}
|
|
28
|
-
interface DistinctOptions<T> extends MongoEntityService.DistinctOptions<T> {
|
|
29
|
-
}
|
|
30
|
-
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
31
|
-
}
|
|
32
|
-
interface ExistsOneOptions<T> extends MongoService.ExistsOneOptions<T> {
|
|
33
|
-
}
|
|
34
|
-
interface FindOneOptions<T> extends MongoEntityService.FindOneOptions<T> {
|
|
35
|
-
}
|
|
36
|
-
interface FindManyOptions<T> extends MongoEntityService.FindManyOptions<T> {
|
|
37
|
-
}
|
|
38
|
-
interface UpdateOptions<T> extends MongoEntityService.UpdateOptions<T> {
|
|
39
|
-
}
|
|
40
|
-
interface UpdateManyOptions<T> extends MongoEntityService.UpdateManyOptions<T> {
|
|
41
|
-
}
|
|
42
19
|
}
|
|
43
20
|
/**
|
|
44
21
|
* @class MongoCollectionService
|
|
@@ -64,95 +41,95 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
64
41
|
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
65
42
|
*
|
|
66
43
|
* @param {MongoAdapter.AnyId} id - The ID of the resource to assert.
|
|
67
|
-
* @param {
|
|
44
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options] - Optional options for checking the existence.
|
|
68
45
|
* @returns {Promise<void>} - A Promise that resolves when the resource exists.
|
|
69
46
|
* @throws {ResourceNotAvailableError} - If the resource does not exist.
|
|
70
47
|
*/
|
|
71
|
-
assert(id: MongoAdapter.AnyId, options?:
|
|
48
|
+
assert(id: MongoAdapter.AnyId, options?: MongoEntityService.ExistsOptions<T>): Promise<void>;
|
|
72
49
|
/**
|
|
73
50
|
* Creates a new document in the MongoDB collection.
|
|
74
51
|
* Interceptors will be called before performing db operation
|
|
75
52
|
*
|
|
76
53
|
* @param {PartialDTO<T>} input - The input data for creating the document.
|
|
77
|
-
* @param {
|
|
54
|
+
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
78
55
|
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
79
56
|
* @throws {Error} if an unknown error occurs while creating the document.
|
|
80
57
|
*/
|
|
81
|
-
create(input: PartialDTO<T>, options?:
|
|
58
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<PartialDTO<T>>;
|
|
82
59
|
/**
|
|
83
60
|
* Returns the count of documents in the collection based on the provided options.
|
|
84
61
|
*
|
|
85
|
-
* @param {
|
|
62
|
+
* @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
|
|
86
63
|
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
87
64
|
*/
|
|
88
|
-
count(options?:
|
|
65
|
+
count(options?: MongoEntityService.CountOptions<T>): Promise<number>;
|
|
89
66
|
/**
|
|
90
67
|
* Deletes a document from the collection.
|
|
91
68
|
*
|
|
92
69
|
* @param {MongoAdapter.AnyId} id - The ID of the document to delete.
|
|
93
|
-
* @param {
|
|
70
|
+
* @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
|
|
94
71
|
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
95
72
|
*/
|
|
96
|
-
delete(id: MongoAdapter.AnyId, options?:
|
|
73
|
+
delete(id: MongoAdapter.AnyId, options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
97
74
|
/**
|
|
98
75
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
99
76
|
*
|
|
100
|
-
* @param {
|
|
77
|
+
* @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
|
|
101
78
|
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
102
79
|
*/
|
|
103
|
-
deleteMany(options?:
|
|
80
|
+
deleteMany(options?: MongoEntityService.DeleteManyOptions<T>): Promise<number>;
|
|
104
81
|
/**
|
|
105
82
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
106
83
|
* @param {string} field
|
|
107
|
-
* @param {
|
|
84
|
+
* @param {MongoEntityService.DistinctOptions<T>} [options]
|
|
108
85
|
* @protected
|
|
109
86
|
*/
|
|
110
|
-
distinct(field: string, options?:
|
|
87
|
+
distinct(field: string, options?: MongoEntityService.DistinctOptions<T>): Promise<any[]>;
|
|
111
88
|
/**
|
|
112
89
|
* Checks if an object with the given id exists.
|
|
113
90
|
*
|
|
114
91
|
* @param {MongoAdapter.AnyId} id - The id of the object to check.
|
|
115
|
-
* @param {
|
|
92
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options] - The options for the query (optional).
|
|
116
93
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
117
94
|
*/
|
|
118
|
-
exists(id: MongoAdapter.AnyId, options?:
|
|
95
|
+
exists(id: MongoAdapter.AnyId, options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
119
96
|
/**
|
|
120
97
|
* Checks if an object with the given arguments exists.
|
|
121
98
|
*
|
|
122
|
-
* @param {
|
|
99
|
+
* @param {MongoEntityService.ExistsOneOptions} [options] - The options for the query (optional).
|
|
123
100
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
124
101
|
*/
|
|
125
|
-
existsOne(options?:
|
|
102
|
+
existsOne(options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
126
103
|
/**
|
|
127
104
|
* Finds a document by its ID.
|
|
128
105
|
*
|
|
129
106
|
* @param {MongoAdapter.AnyId} id - The ID of the document.
|
|
130
|
-
* @param {
|
|
107
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
131
108
|
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
132
109
|
*/
|
|
133
|
-
findById(id: MongoAdapter.AnyId, options?:
|
|
110
|
+
findById(id: MongoAdapter.AnyId, options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
134
111
|
/**
|
|
135
112
|
* Finds a document in the collection that matches the specified options.
|
|
136
113
|
*
|
|
137
|
-
* @param {
|
|
114
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the query.
|
|
138
115
|
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
139
116
|
*/
|
|
140
|
-
findOne(options?:
|
|
117
|
+
findOne(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
141
118
|
/**
|
|
142
119
|
* Finds multiple documents in the MongoDB collection.
|
|
143
120
|
*
|
|
144
|
-
* @param {
|
|
121
|
+
* @param {MongoEntityService.FindManyOptions<T>} options - The options for the find operation.
|
|
145
122
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
146
123
|
*/
|
|
147
|
-
findMany(options?:
|
|
124
|
+
findMany(options?: MongoEntityService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
148
125
|
/**
|
|
149
126
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
150
127
|
* and total count that matched the given criteria
|
|
151
128
|
*
|
|
152
|
-
* @param {
|
|
129
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
153
130
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
154
131
|
*/
|
|
155
|
-
findManyWithCount(options?:
|
|
132
|
+
findManyWithCount(options?: MongoEntityService.FindManyOptions<T>): Promise<{
|
|
156
133
|
count: number;
|
|
157
134
|
items: PartialDTO<T>[];
|
|
158
135
|
}>;
|
|
@@ -160,44 +137,37 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
160
137
|
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
161
138
|
*
|
|
162
139
|
* @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
|
|
163
|
-
* @param {
|
|
140
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
164
141
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
165
142
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
166
143
|
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
167
144
|
*/
|
|
168
|
-
get(id: MongoAdapter.AnyId, options?:
|
|
145
|
+
get(id: MongoAdapter.AnyId, options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T>>;
|
|
169
146
|
/**
|
|
170
147
|
* Updates a document with the given id in the collection.
|
|
171
148
|
*
|
|
172
149
|
* @param {MongoAdapter.AnyId} id - The id of the document to update.
|
|
173
150
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
174
|
-
* @param {
|
|
151
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for the update operation.
|
|
175
152
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
176
153
|
* undefined if the document was not found.
|
|
177
154
|
*/
|
|
178
|
-
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?:
|
|
155
|
+
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
179
156
|
/**
|
|
180
157
|
* Updates a document in the collection with the specified ID.
|
|
181
158
|
*
|
|
182
159
|
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
183
160
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
184
|
-
* @param {
|
|
161
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
|
|
185
162
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
186
163
|
*/
|
|
187
|
-
updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?:
|
|
164
|
+
updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<number>;
|
|
188
165
|
/**
|
|
189
166
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
190
167
|
*
|
|
191
168
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
192
|
-
* @param {
|
|
169
|
+
* @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
|
|
193
170
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
194
171
|
*/
|
|
195
|
-
updateMany(input: PatchDTO<T> | UpdateFilter<T>, options?:
|
|
196
|
-
/**
|
|
197
|
-
* Generates an ID.
|
|
198
|
-
*
|
|
199
|
-
* @protected
|
|
200
|
-
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
201
|
-
*/
|
|
202
|
-
protected _generateId(): MongoAdapter.AnyId;
|
|
172
|
+
updateMany(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateManyOptions<T>): Promise<number>;
|
|
203
173
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
|
-
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
|
-
import { MongoAdapter } from './mongo-adapter.js';
|
|
2
|
+
import { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
|
|
4
3
|
import { MongoService } from './mongo-service.js';
|
|
5
4
|
/**
|
|
6
5
|
*
|
|
@@ -27,14 +26,57 @@ export declare namespace MongoEntityService {
|
|
|
27
26
|
}
|
|
28
27
|
interface DistinctOptions<T> extends MongoService.DistinctOptions<T> {
|
|
29
28
|
}
|
|
29
|
+
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
30
|
+
}
|
|
30
31
|
interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
|
|
31
32
|
}
|
|
32
33
|
interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
|
|
33
34
|
}
|
|
34
|
-
interface
|
|
35
|
+
interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
|
|
35
36
|
}
|
|
36
37
|
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
37
38
|
}
|
|
39
|
+
interface CreateCommand extends StrictOmit<RequiredSome<CommandInfo, 'input'>, 'documentId' | 'nestedId'> {
|
|
40
|
+
crud: 'create';
|
|
41
|
+
options?: CreateOptions;
|
|
42
|
+
}
|
|
43
|
+
interface CountCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
44
|
+
crud: 'read';
|
|
45
|
+
options?: CountOptions<T>;
|
|
46
|
+
}
|
|
47
|
+
interface DeleteCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
48
|
+
crud: 'delete';
|
|
49
|
+
options?: DeleteOptions<T>;
|
|
50
|
+
}
|
|
51
|
+
interface DistinctCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
52
|
+
crud: 'read';
|
|
53
|
+
field: string;
|
|
54
|
+
options?: DistinctOptions<T>;
|
|
55
|
+
}
|
|
56
|
+
interface ExistsCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
57
|
+
crud: 'read';
|
|
58
|
+
options?: ExistsOptions<T>;
|
|
59
|
+
}
|
|
60
|
+
interface FindOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
61
|
+
crud: 'read';
|
|
62
|
+
options?: FindOneOptions<T>;
|
|
63
|
+
}
|
|
64
|
+
interface FindManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
65
|
+
crud: 'read';
|
|
66
|
+
options?: FindManyOptions<T>;
|
|
67
|
+
}
|
|
68
|
+
interface UpdateOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
69
|
+
crud: 'update';
|
|
70
|
+
input?: PatchDTO<T>;
|
|
71
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
72
|
+
options?: UpdateOneOptions<T>;
|
|
73
|
+
}
|
|
74
|
+
interface UpdateManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
75
|
+
crud: 'update';
|
|
76
|
+
input?: PatchDTO<T>;
|
|
77
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
78
|
+
options?: UpdateManyOptions<T>;
|
|
79
|
+
}
|
|
38
80
|
}
|
|
39
81
|
/**
|
|
40
82
|
* @class MongoEntityService
|
|
@@ -52,98 +94,82 @@ export declare class MongoEntityService<T extends mongodb.Document> extends Mong
|
|
|
52
94
|
/**
|
|
53
95
|
* Creates a new document in the MongoDB collection.
|
|
54
96
|
*
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {MongoEntityService.CreateOptions} options
|
|
97
|
+
* @param {MongoEntityService.CreateCommand} command
|
|
57
98
|
* @protected
|
|
58
99
|
*/
|
|
59
|
-
protected _create(
|
|
100
|
+
protected _create(command: MongoEntityService.CreateCommand): Promise<PartialDTO<T>>;
|
|
60
101
|
/**
|
|
61
102
|
* Returns the count of documents in the collection based on the provided options.
|
|
62
103
|
*
|
|
63
|
-
* @param {MongoEntityService.
|
|
64
|
-
* @
|
|
104
|
+
* @param {MongoEntityService.CountCommand<T>} command
|
|
105
|
+
* @protected
|
|
65
106
|
*/
|
|
66
|
-
protected _count(
|
|
107
|
+
protected _count(command: MongoEntityService.CountCommand<T>): Promise<number>;
|
|
67
108
|
/**
|
|
68
109
|
* Deletes a document from the collection.
|
|
69
110
|
*
|
|
70
|
-
* @param {
|
|
71
|
-
* @
|
|
72
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
111
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
112
|
+
* @protected
|
|
73
113
|
*/
|
|
74
|
-
protected _delete(
|
|
114
|
+
protected _delete(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
75
115
|
/**
|
|
76
116
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
77
117
|
*
|
|
78
|
-
* @param {MongoEntityService.
|
|
79
|
-
* @
|
|
118
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
119
|
+
* @protected
|
|
80
120
|
*/
|
|
81
|
-
protected _deleteMany(
|
|
121
|
+
protected _deleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
82
122
|
/**
|
|
83
123
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
84
|
-
*
|
|
85
|
-
* @param {MongoEntityService.
|
|
124
|
+
*
|
|
125
|
+
* @param {MongoEntityService.DistinctCommand<T>} command
|
|
86
126
|
* @protected
|
|
87
127
|
*/
|
|
88
|
-
protected _distinct(
|
|
128
|
+
protected _distinct(command: MongoEntityService.DistinctCommand<T>): Promise<any[]>;
|
|
89
129
|
/**
|
|
90
130
|
* Finds a document by its ID.
|
|
91
131
|
*
|
|
92
|
-
* @param {
|
|
93
|
-
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
94
|
-
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
132
|
+
* @param { MongoEntityService.FindOneCommand<T>} command
|
|
95
133
|
*/
|
|
96
|
-
protected _findById(
|
|
134
|
+
protected _findById(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
97
135
|
/**
|
|
98
136
|
* Finds a document in the collection that matches the specified options.
|
|
99
137
|
*
|
|
100
|
-
* @param {MongoEntityService.
|
|
101
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
138
|
+
* @param {MongoEntityService.FindOneCommand<T>} command
|
|
102
139
|
*/
|
|
103
|
-
protected _findOne(
|
|
140
|
+
protected _findOne(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
104
141
|
/**
|
|
105
142
|
* Finds multiple documents in the MongoDB collection.
|
|
106
143
|
*
|
|
107
|
-
* @param {MongoEntityService.
|
|
108
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
144
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
109
145
|
*/
|
|
110
|
-
protected _findMany(
|
|
146
|
+
protected _findMany(command: MongoEntityService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
|
|
111
147
|
/**
|
|
112
148
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
113
149
|
* and total count that matched the given criteria
|
|
114
150
|
*
|
|
115
|
-
* @param {MongoEntityService.
|
|
116
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
151
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
117
152
|
*/
|
|
118
|
-
protected _findManyWithCount(
|
|
153
|
+
protected _findManyWithCount(command: MongoEntityService.FindManyCommand<T>): Promise<{
|
|
119
154
|
count: number;
|
|
120
155
|
items: PartialDTO<T>[];
|
|
121
156
|
}>;
|
|
122
157
|
/**
|
|
123
158
|
* Updates a document with the given id in the collection.
|
|
124
159
|
*
|
|
125
|
-
* @param {
|
|
126
|
-
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
127
|
-
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for the update operation.
|
|
128
|
-
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
129
|
-
* undefined if the document was not found.
|
|
160
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
130
161
|
*/
|
|
131
|
-
protected _update(
|
|
162
|
+
protected _update(command: MongoEntityService.UpdateOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
132
163
|
/**
|
|
133
164
|
* Updates a document in the collection with the specified ID.
|
|
134
165
|
*
|
|
135
|
-
* @param {
|
|
136
|
-
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
137
|
-
* @param {MongoEntityService.UpdateOptions<T>} [options] - The options for updating the document.
|
|
138
|
-
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
166
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
139
167
|
*/
|
|
140
|
-
protected _updateOnly(
|
|
168
|
+
protected _updateOnly(command: MongoEntityService.UpdateOneCommand<T>): Promise<number>;
|
|
141
169
|
/**
|
|
142
170
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
143
171
|
*
|
|
144
|
-
* @param {
|
|
145
|
-
* @param {MongoEntityService.UpdateManyOptions<T>} [options] - The options for updating the documents.
|
|
146
|
-
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
172
|
+
* @param {MongoEntityService.UpdateManyCommand<T>} command
|
|
147
173
|
*/
|
|
148
|
-
protected _updateMany(
|
|
174
|
+
protected _updateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<number>;
|
|
149
175
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import mongodb from 'mongodb';
|
|
2
|
-
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
3
1
|
import { ComplexType } from '@opra/common';
|
|
2
|
+
import mongodb from 'mongodb';
|
|
3
|
+
import { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems';
|
|
4
4
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
5
|
-
import { MongoCollectionService } from './mongo-collection-service.js';
|
|
6
5
|
import { MongoService } from './mongo-service.js';
|
|
7
6
|
import FilterInput = MongoAdapter.FilterInput;
|
|
8
7
|
/**
|
|
@@ -16,7 +15,9 @@ export declare namespace MongoNestedService {
|
|
|
16
15
|
interface Options extends MongoService.Options {
|
|
17
16
|
defaultLimit?: number;
|
|
18
17
|
nestedKey?: string;
|
|
19
|
-
|
|
18
|
+
nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
19
|
+
}
|
|
20
|
+
interface CommandInfo extends MongoService.CommandInfo {
|
|
20
21
|
}
|
|
21
22
|
interface CreateOptions extends MongoService.CreateOptions {
|
|
22
23
|
}
|
|
@@ -40,13 +41,47 @@ export declare namespace MongoNestedService {
|
|
|
40
41
|
documentFilter?: FilterInput;
|
|
41
42
|
nestedFilter?: FilterInput;
|
|
42
43
|
}
|
|
43
|
-
interface
|
|
44
|
+
interface UpdateOneOptions<T> extends MongoService.UpdateOneOptions<T> {
|
|
44
45
|
documentFilter?: FilterInput;
|
|
45
46
|
}
|
|
46
47
|
interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
|
|
47
48
|
documentFilter?: FilterInput;
|
|
48
49
|
count?: boolean;
|
|
49
50
|
}
|
|
51
|
+
interface CreateCommand extends RequiredSome<CommandInfo, 'documentId' | 'input'> {
|
|
52
|
+
crud: 'create';
|
|
53
|
+
options?: CreateOptions;
|
|
54
|
+
}
|
|
55
|
+
interface CountCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'nestedId' | 'input'> {
|
|
56
|
+
crud: 'read';
|
|
57
|
+
options?: CountOptions<T>;
|
|
58
|
+
}
|
|
59
|
+
interface DeleteCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
60
|
+
crud: 'delete';
|
|
61
|
+
options?: DeleteOptions<T>;
|
|
62
|
+
}
|
|
63
|
+
interface ExistsCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
64
|
+
crud: 'read';
|
|
65
|
+
options?: ExistsOptions<T>;
|
|
66
|
+
}
|
|
67
|
+
interface FindOneCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
68
|
+
crud: 'read';
|
|
69
|
+
options?: FindOneOptions<T>;
|
|
70
|
+
}
|
|
71
|
+
interface FindManyCommand<T> extends StrictOmit<RequiredSome<CommandInfo, 'documentId'>, 'input'> {
|
|
72
|
+
crud: 'read';
|
|
73
|
+
options?: FindManyOptions<T>;
|
|
74
|
+
}
|
|
75
|
+
interface UpdateOneCommand<T> extends RequiredSome<CommandInfo, 'documentId'> {
|
|
76
|
+
crud: 'update';
|
|
77
|
+
input: PatchDTO<T> | mongodb.UpdateFilter<T>;
|
|
78
|
+
options?: MongoNestedService.UpdateOneOptions<T>;
|
|
79
|
+
}
|
|
80
|
+
interface UpdateManyCommand<T> extends RequiredSome<CommandInfo, 'documentId'> {
|
|
81
|
+
crud: 'update';
|
|
82
|
+
input: PatchDTO<T> | mongodb.UpdateFilter<T>;
|
|
83
|
+
options?: MongoNestedService.UpdateManyOptions<T>;
|
|
84
|
+
}
|
|
50
85
|
}
|
|
51
86
|
/**
|
|
52
87
|
* A class that provides methods to perform operations on an array field in a MongoDB collection.
|
|
@@ -77,7 +112,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
77
112
|
*
|
|
78
113
|
* @type {FilterInput | Function}
|
|
79
114
|
*/
|
|
80
|
-
|
|
115
|
+
nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
|
|
81
116
|
/**
|
|
82
117
|
* Constructs a new instance
|
|
83
118
|
*
|
|
@@ -87,6 +122,13 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
87
122
|
* @constructor
|
|
88
123
|
*/
|
|
89
124
|
constructor(dataType: Type | string, fieldName: string, options?: MongoNestedService.Options);
|
|
125
|
+
/**
|
|
126
|
+
* Retrieves the data type of the array field
|
|
127
|
+
*
|
|
128
|
+
* @returns {ComplexType} The complex data type of the field.
|
|
129
|
+
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
130
|
+
*/
|
|
131
|
+
get dataType(): ComplexType;
|
|
90
132
|
/**
|
|
91
133
|
* Asserts whether a resource with the specified parentId and id exists.
|
|
92
134
|
* Throws a ResourceNotFoundError if the resource does not exist.
|
|
@@ -108,7 +150,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
108
150
|
* @throws {ResourceNotAvailableError} - If the parent document is not found.
|
|
109
151
|
*/
|
|
110
152
|
create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<PartialDTO<T>>;
|
|
111
|
-
protected _create(
|
|
153
|
+
protected _create(command: MongoNestedService.CreateCommand): Promise<PartialDTO<T>>;
|
|
112
154
|
/**
|
|
113
155
|
* Counts the number of documents in the collection that match the specified parentId and options.
|
|
114
156
|
*
|
|
@@ -117,7 +159,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
117
159
|
* @returns {Promise<number>} - A promise that resolves to the count of documents.
|
|
118
160
|
*/
|
|
119
161
|
count(documentId: MongoAdapter.AnyId, options?: MongoNestedService.CountOptions<T>): Promise<number>;
|
|
120
|
-
protected _count(
|
|
162
|
+
protected _count(command: MongoNestedService.CountCommand<T>): Promise<number>;
|
|
121
163
|
/**
|
|
122
164
|
* Deletes an element from an array within a document in the MongoDB collection.
|
|
123
165
|
*
|
|
@@ -127,7 +169,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
127
169
|
* @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
|
|
128
170
|
*/
|
|
129
171
|
delete(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteOptions<T>): Promise<number>;
|
|
130
|
-
protected _delete(
|
|
172
|
+
protected _delete(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
|
|
131
173
|
/**
|
|
132
174
|
* Deletes multiple items from a collection based on the parent ID and optional filter.
|
|
133
175
|
*
|
|
@@ -136,7 +178,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
136
178
|
* @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
|
|
137
179
|
*/
|
|
138
180
|
deleteMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteManyOptions<T>): Promise<number>;
|
|
139
|
-
protected _deleteMany(
|
|
181
|
+
protected _deleteMany(command: MongoNestedService.DeleteCommand<T>): Promise<number>;
|
|
140
182
|
/**
|
|
141
183
|
* Checks if an array element with the given parentId and id exists.
|
|
142
184
|
*
|
|
@@ -153,7 +195,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
153
195
|
* @param {MongoNestedService.ExistsOneOptions} [options] - The options for the query (optional).
|
|
154
196
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
155
197
|
*/
|
|
156
|
-
existsOne(documentId: MongoAdapter.AnyId, options?:
|
|
198
|
+
existsOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOneOptions<T>): Promise<boolean>;
|
|
157
199
|
/**
|
|
158
200
|
* Finds an element in array field by its parent ID and ID.
|
|
159
201
|
*
|
|
@@ -163,7 +205,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
163
205
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
164
206
|
*/
|
|
165
207
|
findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
166
|
-
protected _findById(
|
|
208
|
+
protected _findById(command: MongoNestedService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
167
209
|
/**
|
|
168
210
|
* Finds the first array element that matches the given parentId.
|
|
169
211
|
*
|
|
@@ -172,7 +214,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
172
214
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
|
|
173
215
|
*/
|
|
174
216
|
findOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
175
|
-
protected _findOne(
|
|
217
|
+
protected _findOne(command: MongoNestedService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
176
218
|
/**
|
|
177
219
|
* Finds multiple elements in an array field.
|
|
178
220
|
*
|
|
@@ -181,7 +223,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
181
223
|
* @returns {Promise<PartialDTO<T>[]>} - The found documents.
|
|
182
224
|
*/
|
|
183
225
|
findMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
|
|
184
|
-
protected _findMany(
|
|
226
|
+
protected _findMany(command: MongoNestedService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
|
|
185
227
|
/**
|
|
186
228
|
* Finds multiple elements in an array field.
|
|
187
229
|
*
|
|
@@ -193,7 +235,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
193
235
|
count: number;
|
|
194
236
|
items: PartialDTO<T>[];
|
|
195
237
|
}>;
|
|
196
|
-
protected _findManyWithCount(
|
|
238
|
+
protected _findManyWithCount(command: MongoNestedService.FindManyCommand<T>): Promise<{
|
|
197
239
|
count: number;
|
|
198
240
|
items: PartialDTO<T>[];
|
|
199
241
|
}>;
|
|
@@ -213,22 +255,22 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
213
255
|
* @param {AnyId} documentId - The ID of the document to update.
|
|
214
256
|
* @param {AnyId} nestedId - The ID of the item to update within the document.
|
|
215
257
|
* @param {PatchDTO<T>} input - The new data to update the item with.
|
|
216
|
-
* @param {MongoNestedService.
|
|
258
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Additional update options.
|
|
217
259
|
* @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
|
|
218
260
|
* @throws {Error} If an error occurs while updating the item.
|
|
219
261
|
*/
|
|
220
|
-
update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.
|
|
262
|
+
update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
221
263
|
/**
|
|
222
264
|
* Update an array element with new data. Returns 1 if document updated 0 otherwise.
|
|
223
265
|
*
|
|
224
266
|
* @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
|
|
225
267
|
* @param {MongoAdapter.AnyId} nestedId - The ID of the document to update.
|
|
226
268
|
* @param {PatchDTO<T>} input - The partial input object containing the fields to update.
|
|
227
|
-
* @param {MongoNestedService.
|
|
269
|
+
* @param {MongoNestedService.UpdateOneOptions<T>} [options] - Optional update options.
|
|
228
270
|
* @returns {Promise<number>} - A promise that resolves to the number of elements updated.
|
|
229
271
|
*/
|
|
230
|
-
updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.
|
|
231
|
-
protected _updateOnly(
|
|
272
|
+
updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOneOptions<T>): Promise<number>;
|
|
273
|
+
protected _updateOnly(command: MongoNestedService.UpdateOneCommand<T>): Promise<number>;
|
|
232
274
|
/**
|
|
233
275
|
* Updates multiple array elements in document
|
|
234
276
|
*
|
|
@@ -238,14 +280,7 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
238
280
|
* @returns {Promise<number>} - A promise that resolves to the number of documents updated.
|
|
239
281
|
*/
|
|
240
282
|
updateMany(documentId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateManyOptions<T>): Promise<number>;
|
|
241
|
-
protected _updateMany(
|
|
242
|
-
/**
|
|
243
|
-
* Retrieves the data type of the array field
|
|
244
|
-
*
|
|
245
|
-
* @returns {ComplexType} The complex data type of the field.
|
|
246
|
-
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
247
|
-
*/
|
|
248
|
-
getDataType(): ComplexType;
|
|
283
|
+
protected _updateMany(command: MongoNestedService.UpdateManyCommand<T>): Promise<number>;
|
|
249
284
|
/**
|
|
250
285
|
* Retrieves the common filter used for querying array elements.
|
|
251
286
|
* This method is mostly used for security issues like securing multi-tenant applications.
|