@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
package/types/mongo-service.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import mongodb, { Document, TransactionOptions } from 'mongodb';
|
|
2
|
-
import { PartialDTO, StrictOmit, Type } from 'ts-gems';
|
|
3
|
-
import { IsObject } from 'valgen';
|
|
4
1
|
import * as OpraCommon from '@opra/common';
|
|
5
2
|
import { ComplexType } from '@opra/common';
|
|
6
3
|
import { ServiceBase } from '@opra/core';
|
|
4
|
+
import mongodb, { Document, TransactionOptions } from 'mongodb';
|
|
5
|
+
import { PartialDTO, StrictOmit, Type } from 'ts-gems';
|
|
6
|
+
import { IsObject } from 'valgen';
|
|
7
7
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
8
8
|
/**
|
|
9
9
|
* The namespace for the MongoService.
|
|
@@ -14,12 +14,12 @@ export declare namespace MongoService {
|
|
|
14
14
|
interface Options {
|
|
15
15
|
db?: MongoService<any>['db'];
|
|
16
16
|
session?: MongoService<any>['session'];
|
|
17
|
-
collectionName?: MongoService<any>['
|
|
18
|
-
resourceName?: MongoService<any>['
|
|
19
|
-
documentFilter?: MongoService<any>['
|
|
20
|
-
interceptor?: MongoService<any>['
|
|
21
|
-
idGenerator?: MongoService<any>['
|
|
22
|
-
onError?: MongoService<any>['
|
|
17
|
+
collectionName?: MongoService<any>['collectionName'];
|
|
18
|
+
resourceName?: MongoService<any>['resourceName'];
|
|
19
|
+
documentFilter?: MongoService<any>['documentFilter'];
|
|
20
|
+
interceptor?: MongoService<any>['interceptor'];
|
|
21
|
+
idGenerator?: MongoService<any>['idGenerator'];
|
|
22
|
+
onError?: MongoService<any>['onError'];
|
|
23
23
|
}
|
|
24
24
|
type CrudOp = 'create' | 'read' | 'update' | 'delete';
|
|
25
25
|
interface CommandInfo {
|
|
@@ -28,8 +28,8 @@ export declare namespace MongoService {
|
|
|
28
28
|
byId: boolean;
|
|
29
29
|
documentId?: MongoAdapter.AnyId;
|
|
30
30
|
nestedId?: MongoAdapter.AnyId;
|
|
31
|
-
input?:
|
|
32
|
-
options?:
|
|
31
|
+
input?: any;
|
|
32
|
+
options?: any;
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Represents options for "create" operation
|
|
@@ -37,7 +37,7 @@ export declare namespace MongoService {
|
|
|
37
37
|
* @interface
|
|
38
38
|
*/
|
|
39
39
|
interface CreateOptions extends mongodb.InsertOneOptions {
|
|
40
|
-
|
|
40
|
+
projection?: string[];
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Represents options for "count" operation
|
|
@@ -118,7 +118,7 @@ export declare namespace MongoService {
|
|
|
118
118
|
* @interface
|
|
119
119
|
* @template T - The type of the document.
|
|
120
120
|
*/
|
|
121
|
-
interface
|
|
121
|
+
interface UpdateOneOptions<T> extends StrictOmit<mongodb.FindOneAndUpdateOptions, 'projection' | 'returnDocument' | 'includeResultMetadata'> {
|
|
122
122
|
projection?: string | string[] | Document;
|
|
123
123
|
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
124
124
|
}
|
|
@@ -132,24 +132,36 @@ export declare namespace MongoService {
|
|
|
132
132
|
filter?: mongodb.Filter<T> | OpraCommon.OpraFilter.Ast | string;
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
|
+
export interface MongoService {
|
|
136
|
+
/**
|
|
137
|
+
* Interceptor function for handling callback execution with provided arguments.
|
|
138
|
+
* @type Function
|
|
139
|
+
* @param next - The callback function to be intercepted.
|
|
140
|
+
* @param {MongoService.CommandInfo} command - The arguments object containing the following properties:
|
|
141
|
+
* @param _this - The reference to the current object.
|
|
142
|
+
* @returns - The promise that resolves to the result of the callback execution.
|
|
143
|
+
*/
|
|
144
|
+
interceptor?(next: () => any, command: MongoService.CommandInfo, _this: any): Promise<any>;
|
|
145
|
+
}
|
|
135
146
|
/**
|
|
136
147
|
* Class representing a MongoDB service for interacting with a collection.
|
|
137
148
|
* @extends ServiceBase
|
|
138
149
|
* @template T - The type of the documents in the collection.
|
|
139
150
|
*/
|
|
140
151
|
export declare class MongoService<T extends mongodb.Document = mongodb.Document> extends ServiceBase {
|
|
141
|
-
protected
|
|
142
|
-
protected
|
|
143
|
-
protected
|
|
152
|
+
protected _dataType_: Type | string;
|
|
153
|
+
protected _dataType: ComplexType;
|
|
154
|
+
protected _inputCodecs: Record<string, IsObject.Validator<T>>;
|
|
155
|
+
protected _outputCodecs: Record<string, IsObject.Validator<T>>;
|
|
144
156
|
/**
|
|
145
157
|
* Represents the name of a collection in MongoDB
|
|
146
158
|
*/
|
|
147
|
-
|
|
159
|
+
collectionName?: string | ((_this: any) => string);
|
|
148
160
|
/**
|
|
149
161
|
* Represents the name of a resource.
|
|
150
162
|
* @type {string}
|
|
151
163
|
*/
|
|
152
|
-
|
|
164
|
+
resourceName?: string | ((_this: any) => string);
|
|
153
165
|
/**
|
|
154
166
|
* Represents a MongoDB database object.
|
|
155
167
|
*/
|
|
@@ -162,29 +174,20 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
162
174
|
* Generates a new id for new inserting Document.
|
|
163
175
|
*
|
|
164
176
|
*/
|
|
165
|
-
|
|
177
|
+
idGenerator?: (command: MongoService.CommandInfo, _this: any) => MongoAdapter.AnyId;
|
|
166
178
|
/**
|
|
167
179
|
* Callback function for handling errors.
|
|
168
180
|
*
|
|
169
181
|
* @param {unknown} error - The error object.
|
|
170
182
|
* @param _this - The context object.
|
|
171
183
|
*/
|
|
172
|
-
|
|
184
|
+
onError?: (error: unknown, _this: any) => void | Promise<void>;
|
|
173
185
|
/**
|
|
174
|
-
* Represents a common filter function for a
|
|
186
|
+
* Represents a common filter function for a MongoService.
|
|
175
187
|
*
|
|
176
188
|
* @type {FilterInput | Function}
|
|
177
189
|
*/
|
|
178
|
-
|
|
179
|
-
/**
|
|
180
|
-
* Interceptor function for handling callback execution with provided arguments.
|
|
181
|
-
*
|
|
182
|
-
* @param callback - The callback function to be intercepted.
|
|
183
|
-
* @param {MongoService.CommandInfo} info - The arguments object containing the following properties:
|
|
184
|
-
* @param _this - The reference to the current object.
|
|
185
|
-
* @returns - The promise that resolves to the result of the callback execution.
|
|
186
|
-
*/
|
|
187
|
-
$interceptor?: (callback: () => any, info: MongoService.CommandInfo, _this: any) => Promise<any>;
|
|
190
|
+
documentFilter?: MongoAdapter.FilterInput | ((args: MongoService.CommandInfo, _this: this) => MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined);
|
|
188
191
|
/**
|
|
189
192
|
* Constructs a new instance
|
|
190
193
|
*
|
|
@@ -206,25 +209,25 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
206
209
|
*
|
|
207
210
|
* @protected
|
|
208
211
|
* @returns {string} The resource name.
|
|
209
|
-
* @throws {Error} If the
|
|
212
|
+
* @throws {Error} If the resource name is not defined.
|
|
210
213
|
*/
|
|
211
214
|
getResourceName(): string;
|
|
212
215
|
/**
|
|
213
|
-
* Retrieves the data type
|
|
216
|
+
* Retrieves the OPRA data type
|
|
214
217
|
*
|
|
215
218
|
* @throws {NotAcceptableError} If the data type is not a ComplexType.
|
|
216
219
|
*/
|
|
217
|
-
|
|
220
|
+
get dataType(): ComplexType;
|
|
218
221
|
/**
|
|
219
|
-
* Retrieves the
|
|
222
|
+
* Retrieves the codec for the specified operation.
|
|
220
223
|
*
|
|
221
224
|
* @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
|
|
222
225
|
*/
|
|
223
|
-
|
|
226
|
+
getInputCodec(operation: string): IsObject.Validator<T>;
|
|
224
227
|
/**
|
|
225
|
-
* Retrieves the
|
|
228
|
+
* Retrieves the codec.
|
|
226
229
|
*/
|
|
227
|
-
|
|
230
|
+
getOutputCodec(operation: string): IsObject.Validator<T>;
|
|
228
231
|
/**
|
|
229
232
|
* Executes the provided function within a transaction.
|
|
230
233
|
*
|
|
@@ -354,7 +357,7 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
354
357
|
* @protected
|
|
355
358
|
* @returns {MongoAdapter.AnyId} The generated ID.
|
|
356
359
|
*/
|
|
357
|
-
protected _generateId(): MongoAdapter.AnyId;
|
|
360
|
+
protected _generateId(command: MongoService.CommandInfo): MongoAdapter.AnyId;
|
|
358
361
|
/**
|
|
359
362
|
* Retrieves the common filter used for querying documents.
|
|
360
363
|
* This method is mostly used for security issues like securing multi-tenant applications.
|
|
@@ -363,6 +366,6 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
363
366
|
* @returns {FilterInput | Promise<FilterInput> | undefined} The common filter or a Promise
|
|
364
367
|
* that resolves to the common filter, or undefined if not available.
|
|
365
368
|
*/
|
|
366
|
-
protected _getDocumentFilter(
|
|
367
|
-
protected
|
|
369
|
+
protected _getDocumentFilter(command: MongoService.CommandInfo): MongoAdapter.FilterInput | Promise<MongoAdapter.FilterInput> | undefined;
|
|
370
|
+
protected _executeCommand(command: MongoService.CommandInfo, commandFn: () => any): Promise<any>;
|
|
368
371
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import mongodb from 'mongodb';
|
|
1
|
+
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 MongoSingletonService
|
|
@@ -17,16 +16,6 @@ export declare namespace MongoSingletonService {
|
|
|
17
16
|
interface Options extends MongoEntityService.Options {
|
|
18
17
|
_id?: MongoAdapter.AnyId;
|
|
19
18
|
}
|
|
20
|
-
interface CreateOptions extends MongoEntityService.CreateOptions {
|
|
21
|
-
}
|
|
22
|
-
interface DeleteOptions<T> extends MongoEntityService.DeleteOptions<T> {
|
|
23
|
-
}
|
|
24
|
-
interface ExistsOptions<T> extends MongoService.ExistsOptions<T> {
|
|
25
|
-
}
|
|
26
|
-
interface FindOneOptions<T> extends MongoEntityService.FindOneOptions<T> {
|
|
27
|
-
}
|
|
28
|
-
interface UpdateOptions<T> extends MongoEntityService.UpdateOptions<T> {
|
|
29
|
-
}
|
|
30
19
|
}
|
|
31
20
|
/**
|
|
32
21
|
* A class that provides access to a MongoDB collection, with support for singleton document operations.
|
|
@@ -51,64 +40,65 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
|
|
|
51
40
|
/**
|
|
52
41
|
* Asserts the existence of a resource based on the given options.
|
|
53
42
|
*
|
|
54
|
-
* @param {
|
|
43
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options]
|
|
55
44
|
* @returns {Promise<void>} A Promise that resolves when the resource exists.
|
|
56
45
|
* @throws {ResourceNotAvailableError} If the resource does not exist.
|
|
57
46
|
*/
|
|
58
|
-
assert(options?:
|
|
47
|
+
assert(options?: MongoEntityService.ExistsOptions<T>): Promise<void>;
|
|
59
48
|
/**
|
|
60
49
|
* Creates the document in the database.
|
|
61
50
|
*
|
|
62
51
|
* @param {PartialDTO<T>} input - The partial input to create the document with.
|
|
63
|
-
* @param {
|
|
52
|
+
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
64
53
|
* @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
|
|
65
54
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
66
55
|
*/
|
|
67
|
-
create(input: PartialDTO<T>, options?:
|
|
56
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<PartialDTO<T>>;
|
|
68
57
|
/**
|
|
69
58
|
* Deletes a record from the database
|
|
70
59
|
*
|
|
71
|
-
* @param {
|
|
60
|
+
* @param {MongoEntityService.DeleteOptions<T>} options - The options for deleting the record
|
|
72
61
|
* @returns {Promise<number>} The number of records deleted
|
|
73
62
|
*/
|
|
74
|
-
delete(options?:
|
|
63
|
+
delete(options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
75
64
|
/**
|
|
76
65
|
* Checks if the document exists in the database.
|
|
77
66
|
*
|
|
67
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for finding the document.
|
|
78
68
|
* @return {Promise<boolean>} - A promise that resolves to a boolean value indicating if the document exists.
|
|
79
69
|
*/
|
|
80
|
-
exists(options?:
|
|
70
|
+
exists(options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
81
71
|
/**
|
|
82
72
|
* Fetches the document if it exists. Returns undefined if not found.
|
|
83
73
|
*
|
|
84
|
-
* @param {
|
|
74
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for finding the document.
|
|
85
75
|
* @returns {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the found document or undefined if not found.
|
|
86
76
|
*/
|
|
87
|
-
find(options?:
|
|
77
|
+
find(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
88
78
|
/**
|
|
89
79
|
* Fetches the document from the Mongo collection service. Throws error if not found.
|
|
90
80
|
*
|
|
91
|
-
* @param {
|
|
81
|
+
* @param {MongoEntityService.FindOneOptions<T>} options - The options to customize the query.
|
|
92
82
|
* @return {Promise<PartialDTO<T>>} - A promise that resolves to the fetched document.
|
|
93
83
|
* @throws {ResourceNotAvailableError} - If the document is not found in the collection.
|
|
94
84
|
*/
|
|
95
|
-
get(options?:
|
|
85
|
+
get(options?: MongoEntityService.FindOneOptions<T>): Promise<PartialDTO<T>>;
|
|
96
86
|
/**
|
|
97
87
|
* Updates a document in the MongoDB collection.
|
|
98
88
|
*
|
|
99
89
|
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
100
|
-
* @param {
|
|
90
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The update options.
|
|
101
91
|
*
|
|
102
|
-
* @return {Promise<
|
|
92
|
+
* @return {Promise<PartialDTO<T> | undefined>} - A promise that resolves to the updated document or undefined if not found.
|
|
103
93
|
*/
|
|
104
|
-
|
|
94
|
+
update(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<PartialDTO<T> | undefined>;
|
|
105
95
|
/**
|
|
106
96
|
* Updates a document in the MongoDB collection.
|
|
107
97
|
*
|
|
108
98
|
* @param {PatchDTO<T>} input - The partial input to update the document.
|
|
109
|
-
* @param {
|
|
99
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The update options.
|
|
110
100
|
*
|
|
111
|
-
* @return {Promise<
|
|
101
|
+
* @return {Promise<number>} - A promise that resolves to the updated document or undefined if not found.
|
|
112
102
|
*/
|
|
113
|
-
|
|
103
|
+
updateOnly(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<number>;
|
|
114
104
|
}
|