@opra/mongodb 1.26.2 → 1.26.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/README.md +24 -1
- package/adapter/mongo-adapter.d.ts +45 -0
- package/adapter/mongo-adapter.js +23 -0
- package/adapter/mongo-patch-generator.d.ts +12 -0
- package/adapter/mongo-patch-generator.js +22 -10
- package/adapter/prepare-filter.d.ts +4 -6
- package/adapter/prepare-filter.js +4 -6
- package/adapter/prepare-key-values.d.ts +9 -0
- package/adapter/prepare-key-values.js +9 -0
- package/adapter/prepare-projection.d.ts +8 -0
- package/adapter/prepare-projection.js +13 -5
- package/adapter/prepare-sort.d.ts +6 -0
- package/adapter/prepare-sort.js +6 -0
- package/package.json +4 -4
- package/services/mongo-collection-service.d.ts +133 -84
- package/services/mongo-collection-service.js +36 -38
- package/services/mongo-entity-service.d.ts +71 -23
- package/services/mongo-entity-service.js +39 -40
- package/services/mongo-nested-service.d.ts +136 -87
- package/services/mongo-nested-service.js +62 -70
- package/services/mongo-service.d.ts +60 -34
- package/services/mongo-service.js +10 -15
- package/services/mongo-singleton-service.d.ts +49 -46
- package/services/mongo-singleton-service.js +25 -28
- package/types.d.ts +9 -3
|
@@ -5,47 +5,43 @@ import { MongoAdapter } from '../adapter/mongo-adapter.js';
|
|
|
5
5
|
import type { MongoPatchDTO } from '../types.js';
|
|
6
6
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
9
|
-
* @namespace MongoSingletonService
|
|
8
|
+
* Options for MongoSingletonService.
|
|
10
9
|
*/
|
|
11
10
|
export declare namespace MongoSingletonService {
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* @interface Options
|
|
16
|
-
* @extends MongoService.Options
|
|
12
|
+
* Configuration options for MongoSingletonService.
|
|
17
13
|
*/
|
|
18
14
|
interface Options extends MongoEntityService.Options {
|
|
15
|
+
/**
|
|
16
|
+
* The unique identifier for the singleton record.
|
|
17
|
+
*/
|
|
19
18
|
_id?: MongoAdapter.AnyId;
|
|
20
19
|
}
|
|
21
20
|
}
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
* @
|
|
26
|
-
* @template T - The type of document stored in the collection
|
|
22
|
+
* Service for managing a single entity record backed by a MongoDB data source.
|
|
23
|
+
*
|
|
24
|
+
* @template T - The entity type managed by this service.
|
|
27
25
|
*/
|
|
28
26
|
export declare class MongoSingletonService<T extends mongodb.Document> extends MongoEntityService<T> {
|
|
29
27
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @type {MongoAdapter.AnyId} _id
|
|
28
|
+
* The unique identifier for the singleton record.
|
|
32
29
|
*/
|
|
33
30
|
_id: MongoAdapter.AnyId;
|
|
34
31
|
/**
|
|
35
|
-
* Constructs a new instance
|
|
32
|
+
* Constructs a new instance.
|
|
36
33
|
*
|
|
37
|
-
* @param
|
|
38
|
-
* @param
|
|
39
|
-
* @constructor
|
|
34
|
+
* @param dataType - The entity class or its registered name.
|
|
35
|
+
* @param options - Options for the singleton service.
|
|
40
36
|
*/
|
|
41
37
|
constructor(dataType: Type | string, options?: MongoSingletonService.Options);
|
|
42
38
|
/**
|
|
43
39
|
* Create a copy of this instance with given properties and context applied.
|
|
44
40
|
*
|
|
45
|
-
* @param
|
|
46
|
-
* @param
|
|
47
|
-
* @param
|
|
48
|
-
* @
|
|
41
|
+
* @param context - The execution context or service base to associate with this instance.
|
|
42
|
+
* @param [overwriteProperties] - An optional object containing properties to overwrite in the current instance.
|
|
43
|
+
* @param [overwriteContext] - An optional partial context to apply and potentially overwrite parts of the provided execution context.
|
|
44
|
+
* @returns The current instance with the specified properties and context applied.
|
|
49
45
|
* @template P
|
|
50
46
|
* @template C
|
|
51
47
|
*/
|
|
@@ -53,78 +49,85 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
|
|
|
53
49
|
/**
|
|
54
50
|
* Asserts the existence of a resource based on the given options.
|
|
55
51
|
*
|
|
56
|
-
* @param
|
|
57
|
-
* @returns
|
|
58
|
-
* @throws {ResourceNotAvailableError} If the resource does not exist.
|
|
52
|
+
* @param options - Optional options for checking the existence.
|
|
53
|
+
* @returns A Promise that resolves when the resource exists.
|
|
54
|
+
* @throws {@link ResourceNotAvailableError} If the resource does not exist.
|
|
59
55
|
*/
|
|
60
56
|
assert(options?: MongoEntityService.ExistsOptions<T>): Promise<void>;
|
|
61
57
|
/**
|
|
62
58
|
* Creates the document in the database.
|
|
63
59
|
*
|
|
64
|
-
* @param
|
|
65
|
-
* @param
|
|
66
|
-
* @
|
|
67
|
-
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
60
|
+
* @param input - The partial input to create the document with.
|
|
61
|
+
* @param options - The options for creating the document.
|
|
62
|
+
* @returns A promise that resolves to the partial output of the created document.
|
|
63
|
+
* @throws {@link Error} Throws an error if an unknown error occurs while creating the document.
|
|
68
64
|
*/
|
|
69
65
|
create(input: PartialDTO<T>, options: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
70
66
|
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
71
67
|
/**
|
|
72
68
|
* Creates the document in the database.
|
|
73
69
|
*
|
|
74
|
-
* @param
|
|
75
|
-
* @param
|
|
76
|
-
* @returns
|
|
77
|
-
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
70
|
+
* @param input - The partial input to create the document with.
|
|
71
|
+
* @param options - The options for creating the document.
|
|
72
|
+
* @returns A promise that resolves to the created document.
|
|
73
|
+
* @throws {@link Error} Throws an error if an unknown error occurs while creating the document.
|
|
78
74
|
*/
|
|
79
75
|
createOnly(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
80
76
|
/**
|
|
81
77
|
* Deletes a record from the database
|
|
82
78
|
*
|
|
83
|
-
* @param
|
|
84
|
-
* @returns
|
|
79
|
+
* @param options - The options for deleting the record
|
|
80
|
+
* @returns The number of records deleted
|
|
85
81
|
*/
|
|
86
82
|
delete(options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
87
83
|
/**
|
|
88
84
|
* Checks if the document exists in the database.
|
|
89
85
|
*
|
|
90
|
-
* @param
|
|
91
|
-
* @
|
|
86
|
+
* @param [options] - The options for finding the document.
|
|
87
|
+
* @returns A promise that resolves to a boolean value indicating if the document exists.
|
|
92
88
|
*/
|
|
93
89
|
exists(options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
94
90
|
/**
|
|
95
91
|
* Fetches the document if it exists. Returns undefined if not found.
|
|
96
92
|
*
|
|
97
|
-
* @param
|
|
98
|
-
* @returns
|
|
93
|
+
* @param [options] - The options for finding the document.
|
|
94
|
+
* @returns A promise that resolves to the found document or undefined if not found.
|
|
99
95
|
*/
|
|
100
96
|
find(options: RequiredSome<MongoEntityService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
101
97
|
find(options?: MongoEntityService.FindOneOptions<T>): Promise<T | undefined>;
|
|
102
98
|
/**
|
|
103
99
|
* Fetches the document from the Mongo collection service. Throws error if not found.
|
|
104
100
|
*
|
|
105
|
-
* @param
|
|
106
|
-
* @
|
|
107
|
-
* @throws {ResourceNotAvailableError}
|
|
101
|
+
* @param options - The options to customize the query.
|
|
102
|
+
* @returns A promise that resolves to the fetched document.
|
|
103
|
+
* @throws {@link ResourceNotAvailableError} If the document is not found in the collection.
|
|
108
104
|
*/
|
|
109
105
|
get(options: RequiredSome<MongoEntityService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>;
|
|
106
|
+
/**
|
|
107
|
+
* Fetches the document from the Mongo collection service. Throws error if not found.
|
|
108
|
+
*
|
|
109
|
+
* @param options - The options to customize the query.
|
|
110
|
+
* @returns A promise that resolves to the fetched document.
|
|
111
|
+
* @throws {@link ResourceNotAvailableError} If the document is not found in the collection.
|
|
112
|
+
*/
|
|
110
113
|
get(options?: MongoEntityService.FindOneOptions<T>): Promise<T>;
|
|
111
114
|
/**
|
|
112
115
|
* Updates a document in the MongoDB collection
|
|
113
116
|
*
|
|
114
|
-
* @param
|
|
115
|
-
* @param
|
|
117
|
+
* @param input - The partial input to update the document.
|
|
118
|
+
* @param [options] - The update options.
|
|
116
119
|
*
|
|
117
|
-
* @
|
|
120
|
+
* @returns A promise that resolves to the updated document or undefined if not found.
|
|
118
121
|
*/
|
|
119
122
|
update(input: MongoPatchDTO<T> | UpdateFilter<T>, options: RequiredSome<MongoEntityService.UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
120
123
|
update(input: MongoPatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<T | undefined>;
|
|
121
124
|
/**
|
|
122
125
|
* Updates a document in the MongoDB collection
|
|
123
126
|
*
|
|
124
|
-
* @param
|
|
125
|
-
* @param
|
|
127
|
+
* @param input - The partial input to update the document.
|
|
128
|
+
* @param [options] - The update options.
|
|
126
129
|
*
|
|
127
|
-
* @
|
|
130
|
+
* @returns A promise that resolves to the updated document or undefined if not found.
|
|
128
131
|
*/
|
|
129
132
|
updateOnly(input: MongoPatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<number>;
|
|
130
133
|
}
|
|
@@ -4,23 +4,20 @@ import { ObjectId } from 'mongodb';
|
|
|
4
4
|
import { MongoAdapter } from '../adapter/mongo-adapter.js';
|
|
5
5
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
10
|
-
* @template T - The type of document stored in the collection
|
|
7
|
+
* Service for managing a single entity record backed by a MongoDB data source.
|
|
8
|
+
*
|
|
9
|
+
* @template T - The entity type managed by this service.
|
|
11
10
|
*/
|
|
12
11
|
export class MongoSingletonService extends MongoEntityService {
|
|
13
12
|
/**
|
|
14
|
-
*
|
|
15
|
-
* @type {MongoAdapter.AnyId} _id
|
|
13
|
+
* The unique identifier for the singleton record.
|
|
16
14
|
*/
|
|
17
15
|
_id;
|
|
18
16
|
/**
|
|
19
|
-
* Constructs a new instance
|
|
17
|
+
* Constructs a new instance.
|
|
20
18
|
*
|
|
21
|
-
* @param
|
|
22
|
-
* @param
|
|
23
|
-
* @constructor
|
|
19
|
+
* @param dataType - The entity class or its registered name.
|
|
20
|
+
* @param options - Options for the singleton service.
|
|
24
21
|
*/
|
|
25
22
|
constructor(dataType, options) {
|
|
26
23
|
super(dataType, options);
|
|
@@ -29,10 +26,10 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
29
26
|
/**
|
|
30
27
|
* Create a copy of this instance with given properties and context applied.
|
|
31
28
|
*
|
|
32
|
-
* @param
|
|
33
|
-
* @param
|
|
34
|
-
* @param
|
|
35
|
-
* @
|
|
29
|
+
* @param context - The execution context or service base to associate with this instance.
|
|
30
|
+
* @param [overwriteProperties] - An optional object containing properties to overwrite in the current instance.
|
|
31
|
+
* @param [overwriteContext] - An optional partial context to apply and potentially overwrite parts of the provided execution context.
|
|
32
|
+
* @returns The current instance with the specified properties and context applied.
|
|
36
33
|
* @template P
|
|
37
34
|
* @template C
|
|
38
35
|
*/
|
|
@@ -42,9 +39,9 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
42
39
|
/**
|
|
43
40
|
* Asserts the existence of a resource based on the given options.
|
|
44
41
|
*
|
|
45
|
-
* @param
|
|
46
|
-
* @returns
|
|
47
|
-
* @throws {ResourceNotAvailableError} If the resource does not exist.
|
|
42
|
+
* @param options - Optional options for checking the existence.
|
|
43
|
+
* @returns A Promise that resolves when the resource exists.
|
|
44
|
+
* @throws {@link ResourceNotAvailableError} If the resource does not exist.
|
|
48
45
|
*/
|
|
49
46
|
async assert(options) {
|
|
50
47
|
if (!(await this.exists(options)))
|
|
@@ -78,10 +75,10 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
78
75
|
/**
|
|
79
76
|
* Creates the document in the database.
|
|
80
77
|
*
|
|
81
|
-
* @param
|
|
82
|
-
* @param
|
|
83
|
-
* @returns
|
|
84
|
-
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
78
|
+
* @param input - The partial input to create the document with.
|
|
79
|
+
* @param options - The options for creating the document.
|
|
80
|
+
* @returns A promise that resolves to the created document.
|
|
81
|
+
* @throws {@link Error} Throws an error if an unknown error occurs while creating the document.
|
|
85
82
|
*/
|
|
86
83
|
async createOnly(input, options) {
|
|
87
84
|
const command = {
|
|
@@ -97,8 +94,8 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
97
94
|
/**
|
|
98
95
|
* Deletes a record from the database
|
|
99
96
|
*
|
|
100
|
-
* @param
|
|
101
|
-
* @returns
|
|
97
|
+
* @param options - The options for deleting the record
|
|
98
|
+
* @returns The number of records deleted
|
|
102
99
|
*/
|
|
103
100
|
async delete(options) {
|
|
104
101
|
const command = {
|
|
@@ -120,8 +117,8 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
120
117
|
/**
|
|
121
118
|
* Checks if the document exists in the database.
|
|
122
119
|
*
|
|
123
|
-
* @param
|
|
124
|
-
* @
|
|
120
|
+
* @param [options] - The options for finding the document.
|
|
121
|
+
* @returns A promise that resolves to a boolean value indicating if the document exists.
|
|
125
122
|
*/
|
|
126
123
|
async exists(options) {
|
|
127
124
|
const command = {
|
|
@@ -201,10 +198,10 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
201
198
|
/**
|
|
202
199
|
* Updates a document in the MongoDB collection
|
|
203
200
|
*
|
|
204
|
-
* @param
|
|
205
|
-
* @param
|
|
201
|
+
* @param input - The partial input to update the document.
|
|
202
|
+
* @param [options] - The update options.
|
|
206
203
|
*
|
|
207
|
-
* @
|
|
204
|
+
* @returns A promise that resolves to the updated document or undefined if not found.
|
|
208
205
|
*/
|
|
209
206
|
async updateOnly(input, options) {
|
|
210
207
|
const command = {
|
package/types.d.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import type { DTO, IfNoDeepValue } from 'ts-gems';
|
|
2
|
+
/**
|
|
3
|
+
* Represents a MongoDB-specific Data Transfer Object for patch operations.
|
|
4
|
+
* It extends the standard patch DTO with MongoDB-specific operators like _$push and _$pull.
|
|
5
|
+
*/
|
|
2
6
|
export type MongoPatchDTO<T> = _MongoPatchDTO<DTO<T>> & PatchOperators<T>;
|
|
3
7
|
/**
|
|
4
|
-
*
|
|
5
|
-
* @template T - The type of the data being transferred.
|
|
8
|
+
* Internal type for recursive MongoDB patch DTO.
|
|
6
9
|
*/
|
|
7
10
|
type _MongoPatchDTO<T> = {
|
|
8
11
|
[K in keyof T]?: Exclude<T[K], undefined> extends (infer U)[] ? _MongoPatchDTO<U>[] | null : IfNoDeepValue<NonNullable<T[K]>> extends true ? T[K] | null : // Deep process objects
|
|
9
12
|
(_MongoPatchDTO<NonNullable<T[K]>> & PatchOperators<T>) | null;
|
|
10
13
|
};
|
|
11
14
|
/**
|
|
12
|
-
*
|
|
15
|
+
* Utility type to pick all array properties from T.
|
|
13
16
|
*/
|
|
14
17
|
type PickArrays<T> = {
|
|
15
18
|
[K in keyof T as NonNullable<T[K]> extends any[] ? K : never]: T[K];
|
|
16
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* MongoDB-specific patch operators.
|
|
22
|
+
*/
|
|
17
23
|
type PatchOperators<T> = {
|
|
18
24
|
_$push?: PickArrays<Partial<T>>;
|
|
19
25
|
_$pull?: {
|