@opra/mongodb 0.33.13 → 1.0.0-alpha.10

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.
Files changed (37) hide show
  1. package/cjs/adapter-utils/prepare-filter.js +22 -28
  2. package/cjs/adapter-utils/prepare-key-values.js +4 -3
  3. package/cjs/adapter-utils/prepare-patch.js +2 -1
  4. package/cjs/adapter-utils/prepare-projection.js +40 -39
  5. package/cjs/index.js +1 -2
  6. package/cjs/mongo-adapter.js +71 -1
  7. package/cjs/mongo-collection-service.js +73 -314
  8. package/cjs/mongo-entity-service.js +335 -0
  9. package/cjs/{mongo-array-service.js → mongo-nested-service.js} +252 -244
  10. package/cjs/mongo-service.js +146 -202
  11. package/cjs/mongo-singleton-service.js +29 -125
  12. package/esm/adapter-utils/prepare-filter.js +22 -28
  13. package/esm/adapter-utils/prepare-key-values.js +4 -3
  14. package/esm/adapter-utils/prepare-patch.js +2 -1
  15. package/esm/adapter-utils/prepare-projection.js +39 -38
  16. package/esm/index.js +1 -2
  17. package/esm/mongo-adapter.js +71 -1
  18. package/esm/mongo-collection-service.js +73 -313
  19. package/esm/mongo-entity-service.js +330 -0
  20. package/esm/mongo-nested-service.js +571 -0
  21. package/esm/mongo-service.js +147 -203
  22. package/esm/mongo-singleton-service.js +29 -125
  23. package/package.json +16 -10
  24. package/types/adapter-utils/prepare-filter.d.ts +2 -3
  25. package/types/adapter-utils/prepare-projection.d.ts +4 -13
  26. package/types/index.d.ts +1 -2
  27. package/types/mongo-adapter.d.ts +14 -1
  28. package/types/mongo-collection-service.d.ts +88 -251
  29. package/types/mongo-entity-service.d.ts +149 -0
  30. package/types/mongo-nested-service.d.ts +258 -0
  31. package/types/mongo-service.d.ts +218 -91
  32. package/types/mongo-singleton-service.d.ts +39 -148
  33. package/cjs/types.js +0 -2
  34. package/esm/mongo-array-service.js +0 -563
  35. package/esm/types.js +0 -1
  36. package/types/mongo-array-service.d.ts +0 -409
  37. package/types/types.d.ts +0 -3
@@ -0,0 +1,258 @@
1
+ import { ComplexType } from '@opra/common';
2
+ import mongodb from 'mongodb';
3
+ import { PartialDTO, PatchDTO, Type } from 'ts-gems';
4
+ import { MongoAdapter } from './mongo-adapter.js';
5
+ import { MongoCollectionService } from './mongo-collection-service.js';
6
+ import { MongoService } from './mongo-service.js';
7
+ import FilterInput = MongoAdapter.FilterInput;
8
+ /**
9
+ *
10
+ * @namespace MongoNestedService
11
+ */
12
+ export declare namespace MongoNestedService {
13
+ /**
14
+ * The constructor options of MongoArrayService.
15
+ */
16
+ interface Options extends MongoService.Options {
17
+ defaultLimit?: number;
18
+ nestedKey?: string;
19
+ $nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
20
+ }
21
+ interface CreateOptions extends MongoService.CreateOptions {
22
+ }
23
+ interface CountOptions<T> extends MongoService.CountOptions<T> {
24
+ documentFilter?: FilterInput;
25
+ }
26
+ interface DeleteOptions<T> extends MongoService.DeleteOptions<T> {
27
+ documentFilter?: FilterInput;
28
+ }
29
+ interface DeleteManyOptions<T> extends MongoService.DeleteManyOptions<T> {
30
+ documentFilter?: FilterInput;
31
+ }
32
+ interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
33
+ }
34
+ interface ExistsOneOptions<T> extends MongoService.ExistsOneOptions<T> {
35
+ }
36
+ interface FindOneOptions<T> extends MongoService.FindOneOptions<T> {
37
+ documentFilter?: FilterInput;
38
+ }
39
+ interface FindManyOptions<T> extends MongoService.FindManyOptions<T> {
40
+ documentFilter?: FilterInput;
41
+ nestedFilter?: FilterInput;
42
+ }
43
+ interface UpdateOptions<T> extends MongoService.UpdateOptions<T> {
44
+ documentFilter?: FilterInput;
45
+ }
46
+ interface UpdateManyOptions<T> extends MongoService.UpdateManyOptions<T> {
47
+ documentFilter?: FilterInput;
48
+ count?: boolean;
49
+ }
50
+ }
51
+ /**
52
+ * A class that provides methods to perform operations on an array field in a MongoDB collection.
53
+ * @class MongoNestedService
54
+ * @template T The type of the array item.
55
+ */
56
+ export declare class MongoNestedService<T extends mongodb.Document> extends MongoService<T> {
57
+ /**
58
+ * Represents the name of the array field in parent document
59
+ *
60
+ * @type {string}
61
+ */
62
+ fieldName: string;
63
+ /**
64
+ * Represents the value of a nested array key field
65
+ *
66
+ * @type {string}
67
+ */
68
+ nestedKey: string;
69
+ /**
70
+ * Represents the default limit value for a certain operation.
71
+ *
72
+ * @type {number}
73
+ */
74
+ defaultLimit: number;
75
+ /**
76
+ * Represents a common array filter function
77
+ *
78
+ * @type {FilterInput | Function}
79
+ */
80
+ $nestedFilter?: FilterInput | ((args: MongoService.CommandInfo, _this: this) => FilterInput | Promise<FilterInput> | undefined);
81
+ /**
82
+ * Constructs a new instance
83
+ *
84
+ * @param {Type | string} dataType - The data type of the array elements.
85
+ * @param {string} fieldName - The name of the field in the document representing the array.
86
+ * @param {MongoNestedService.Options} [options] - The options for the array service.
87
+ * @constructor
88
+ */
89
+ constructor(dataType: Type | string, fieldName: string, options?: MongoNestedService.Options);
90
+ /**
91
+ * Retrieves the data type of the array field
92
+ *
93
+ * @returns {ComplexType} The complex data type of the field.
94
+ * @throws {NotAcceptableError} If the data type is not a ComplexType.
95
+ */
96
+ get dataType(): ComplexType;
97
+ /**
98
+ * Asserts whether a resource with the specified parentId and id exists.
99
+ * Throws a ResourceNotFoundError if the resource does not exist.
100
+ *
101
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
102
+ * @param {MongoAdapter.AnyId} id - The ID of the resource.
103
+ * @param {MongoNestedService.ExistsOptions<T>} [options] - Optional parameters for checking resource existence.
104
+ * @return {Promise<void>} - A promise that resolves with no value upon success.
105
+ * @throws {ResourceNotAvailableError} - If the resource does not exist.
106
+ */
107
+ assert(documentId: MongoAdapter.AnyId, id: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOptions<T>): Promise<void>;
108
+ /**
109
+ * Adds a single item into the array field.
110
+ *
111
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
112
+ * @param {T} input - The item to be added to the array field.
113
+ * @param {MongoNestedService.CreateOptions} [options] - Optional options for the create operation.
114
+ * @return {Promise<PartialDTO<T>>} - A promise that resolves with the partial output of the created item.
115
+ * @throws {ResourceNotAvailableError} - If the parent document is not found.
116
+ */
117
+ create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<PartialDTO<T>>;
118
+ protected _create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<PartialDTO<T>>;
119
+ /**
120
+ * Counts the number of documents in the collection that match the specified parentId and options.
121
+ *
122
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
123
+ * @param {MongoNestedService.CountOptions<T>} [options] - Optional parameters for counting.
124
+ * @returns {Promise<number>} - A promise that resolves to the count of documents.
125
+ */
126
+ count(documentId: MongoAdapter.AnyId, options?: MongoNestedService.CountOptions<T>): Promise<number>;
127
+ protected _count(documentId: MongoAdapter.AnyId, options?: MongoNestedService.CountOptions<T>): Promise<number>;
128
+ /**
129
+ * Deletes an element from an array within a document in the MongoDB collection.
130
+ *
131
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
132
+ * @param {MongoAdapter.AnyId} nestedId - The ID of the element to delete from the nested array.
133
+ * @param {MongoNestedService.DeleteOptions<T>} [options] - Additional options for the delete operation.
134
+ * @return {Promise<number>} - A Promise that resolves to the number of elements deleted (1 if successful, 0 if not).
135
+ */
136
+ delete(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteOptions<T>): Promise<number>;
137
+ protected _delete(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteOptions<T>): Promise<number>;
138
+ /**
139
+ * Deletes multiple items from a collection based on the parent ID and optional filter.
140
+ *
141
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
142
+ * @param {MongoNestedService.DeleteManyOptions<T>} [options] - Optional options to specify a filter.
143
+ * @returns {Promise<number>} - A Promise that resolves to the number of items deleted.
144
+ */
145
+ deleteMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteManyOptions<T>): Promise<number>;
146
+ protected _deleteMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.DeleteManyOptions<T>): Promise<number>;
147
+ /**
148
+ * Checks if an array element with the given parentId and id exists.
149
+ *
150
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
151
+ * @param {MongoAdapter.AnyId} nestedId - The id of the record.
152
+ * @param {MongoNestedService.ExistsOptions<T>} [options] - The options for the exists method.
153
+ * @returns {Promise<boolean>} - A promise that resolves to a boolean indicating if the record exists or not.
154
+ */
155
+ exists(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.ExistsOptions<T>): Promise<boolean>;
156
+ /**
157
+ * Checks if an object with the given arguments exists.
158
+ *
159
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
160
+ * @param {MongoNestedService.ExistsOneOptions} [options] - The options for the query (optional).
161
+ * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
162
+ */
163
+ existsOne(documentId: MongoAdapter.AnyId, options?: MongoCollectionService.ExistsOneOptions<T>): Promise<boolean>;
164
+ /**
165
+ * Finds an element in array field by its parent ID and ID.
166
+ *
167
+ * @param {MongoAdapter.AnyId} documentId - The ID of the document.
168
+ * @param {MongoAdapter.AnyId} nestedId - The ID of the document.
169
+ * @param {MongoNestedService.FindOneOptions<T>} [options] - The optional options for the operation.
170
+ * @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
171
+ */
172
+ findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
173
+ protected _findById(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
174
+ /**
175
+ * Finds the first array element that matches the given parentId.
176
+ *
177
+ * @param {MongoAdapter.AnyId} documentId - The ID of the document.
178
+ * @param {MongoNestedService.FindOneOptions<T>} [options] - Optional options to customize the query.
179
+ * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the first matching document, or `undefined` if no match is found.
180
+ */
181
+ findOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
182
+ protected _findOne(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
183
+ /**
184
+ * Finds multiple elements in an array field.
185
+ *
186
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
187
+ * @param {MongoNestedService.FindManyOptions<T>} [options] - The options for finding the documents.
188
+ * @returns {Promise<PartialDTO<T>[]>} - The found documents.
189
+ */
190
+ findMany(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
191
+ protected _findMany(documentId: MongoAdapter.AnyId, options: MongoNestedService.FindManyOptions<T>): Promise<PartialDTO<T>[]>;
192
+ /**
193
+ * Finds multiple elements in an array field.
194
+ *
195
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
196
+ * @param {MongoNestedService.FindManyOptions<T>} [options] - The options for finding the documents.
197
+ * @returns {Promise<PartialDTO<T>[]>} - The found documents.
198
+ */
199
+ findManyWithCount(documentId: MongoAdapter.AnyId, options?: MongoNestedService.FindManyOptions<T>): Promise<{
200
+ count: number;
201
+ items: PartialDTO<T>[];
202
+ }>;
203
+ protected _findManyWithCount(documentId: MongoAdapter.AnyId, options: MongoNestedService.FindManyOptions<T>): Promise<{
204
+ count: number;
205
+ items: PartialDTO<T>[];
206
+ }>;
207
+ /**
208
+ * Retrieves a specific item from the array of a document.
209
+ *
210
+ * @param {MongoAdapter.AnyId} documentId - The ID of the document.
211
+ * @param {MongoAdapter.AnyId} nestedId - The ID of the item.
212
+ * @param {MongoNestedService.FindOneOptions<T>} [options] - The options for finding the item.
213
+ * @returns {Promise<PartialDTO<T>>} - The item found.
214
+ * @throws {ResourceNotAvailableError} - If the item is not found.
215
+ */
216
+ get(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, options?: MongoNestedService.FindOneOptions<T>): Promise<PartialDTO<T>>;
217
+ /**
218
+ * Updates an array element with new data and returns the updated element
219
+ *
220
+ * @param {AnyId} documentId - The ID of the document to update.
221
+ * @param {AnyId} nestedId - The ID of the item to update within the document.
222
+ * @param {PatchDTO<T>} input - The new data to update the item with.
223
+ * @param {MongoNestedService.UpdateOptions<T>} [options] - Additional update options.
224
+ * @returns {Promise<PartialDTO<T> | undefined>} The updated item or undefined if it does not exist.
225
+ * @throws {Error} If an error occurs while updating the item.
226
+ */
227
+ update(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOptions<T>): Promise<PartialDTO<T> | undefined>;
228
+ /**
229
+ * Update an array element with new data. Returns 1 if document updated 0 otherwise.
230
+ *
231
+ * @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
232
+ * @param {MongoAdapter.AnyId} nestedId - The ID of the document to update.
233
+ * @param {PatchDTO<T>} input - The partial input object containing the fields to update.
234
+ * @param {MongoNestedService.UpdateOptions<T>} [options] - Optional update options.
235
+ * @returns {Promise<number>} - A promise that resolves to the number of elements updated.
236
+ */
237
+ updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOptions<T>): Promise<number>;
238
+ protected _updateOnly(documentId: MongoAdapter.AnyId, nestedId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateOptions<T>): Promise<number>;
239
+ /**
240
+ * Updates multiple array elements in document
241
+ *
242
+ * @param {MongoAdapter.AnyId} documentId - The ID of the document to update.
243
+ * @param {PatchDTO<T>} input - The updated data for the document(s).
244
+ * @param {MongoNestedService.UpdateManyOptions<T>} [options] - Additional options for the update operation.
245
+ * @returns {Promise<number>} - A promise that resolves to the number of documents updated.
246
+ */
247
+ updateMany(documentId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateManyOptions<T>): Promise<number>;
248
+ protected _updateMany(documentId: MongoAdapter.AnyId, input: PatchDTO<T>, options?: MongoNestedService.UpdateManyOptions<T>): Promise<number>;
249
+ /**
250
+ * Retrieves the common filter used for querying array elements.
251
+ * This method is mostly used for security issues like securing multi-tenant applications.
252
+ *
253
+ * @protected
254
+ * @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
255
+ * that resolves to the common filter, or undefined if not available.
256
+ */
257
+ protected _getNestedFilter(args: MongoService.CommandInfo): FilterInput | Promise<FilterInput> | undefined;
258
+ }