@opra/mongodb 1.0.0-alpha.9 → 1.0.0-beta.2
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 +1 -1
- package/cjs/adapter-utils/prepare-key-values.js +1 -1
- package/cjs/adapter-utils/prepare-patch.js +1 -1
- package/cjs/adapter-utils/prepare-projection.js +6 -7
- package/cjs/adapter-utils/prepare-sort.js +1 -1
- package/cjs/index.js +1 -0
- package/cjs/mongo-adapter.js +4 -4
- package/cjs/mongo-collection-service.js +123 -141
- package/cjs/mongo-entity-service.js +259 -131
- package/cjs/mongo-nested-service.js +416 -211
- package/cjs/mongo-service.js +79 -239
- package/cjs/mongo-singleton-service.js +101 -63
- package/esm/adapter-utils/prepare-projection.js +4 -4
- package/esm/index.js +1 -0
- package/esm/mongo-adapter.js +4 -4
- package/esm/mongo-collection-service.js +122 -141
- package/esm/mongo-entity-service.js +259 -131
- package/esm/mongo-nested-service.js +416 -211
- package/esm/mongo-service.js +79 -239
- package/esm/mongo-singleton-service.js +100 -63
- package/esm/package.json +3 -0
- package/package.json +27 -33
- package/types/adapter-utils/prepare-filter.d.ts +2 -2
- package/types/adapter-utils/prepare-projection.d.ts +1 -1
- package/types/index.d.cts +6 -0
- package/types/index.d.ts +1 -0
- package/types/mongo-adapter.d.ts +1 -1
- package/types/mongo-collection-service.d.ts +44 -64
- package/types/mongo-entity-service.d.ts +91 -53
- package/types/mongo-nested-service.d.ts +107 -43
- package/types/mongo-service.d.ts +55 -153
- package/types/mongo-singleton-service.d.ts +33 -30
package/package.json
CHANGED
|
@@ -1,49 +1,43 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-beta.2",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
7
|
-
"repository": {
|
|
8
|
-
"type": "git",
|
|
9
|
-
"url": "https://github.com/panates/opra.git",
|
|
10
|
-
"directory": "packages/mongodb"
|
|
11
|
-
},
|
|
12
|
-
"scripts": {
|
|
13
|
-
"compile": "tsc",
|
|
14
|
-
"prebuild": "npm run lint && npm run clean",
|
|
15
|
-
"build": "npm run build:cjs && npm run build:esm",
|
|
16
|
-
"build:cjs": "tsc -b tsconfig-build-cjs.json",
|
|
17
|
-
"build:esm": "tsc -b tsconfig-build-esm.json",
|
|
18
|
-
"postbuild": "cp README.md package.json ../../LICENSE ../../build/mongodb && cp ../../package.cjs.json ../../build/mongodb/cjs/package.json",
|
|
19
|
-
"lint": "eslint . --max-warnings=0",
|
|
20
|
-
"lint:fix": "eslint . --max-warnings=0 --fix",
|
|
21
|
-
"format": "prettier . --write --log-level=warn",
|
|
22
|
-
"test": "jest --passWithNoTests",
|
|
23
|
-
"cover": "jest --passWithNoTests --collect-coverage",
|
|
24
|
-
"clean": "npm run clean:src && npm run clean:dist && npm run clean:cover",
|
|
25
|
-
"clean:src": "ts-cleanup -s src --all && ts-cleanup -s test --all",
|
|
26
|
-
"clean:dist": "rimraf ../../build/mongodb",
|
|
27
|
-
"clean:cover": "rimraf ../../coverage/mongodb"
|
|
28
|
-
},
|
|
29
7
|
"dependencies": {
|
|
30
8
|
"lodash.omit": "^4.5.0",
|
|
31
|
-
"putil-isplainobject": "^1.1.5"
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
"@faker-js/faker": "^8.4.1",
|
|
35
|
-
"mongodb": "^6.8.0",
|
|
36
|
-
"ts-gems": "^3.4.0"
|
|
9
|
+
"putil-isplainobject": "^1.1.5",
|
|
10
|
+
"tslib": "^2.7.0",
|
|
11
|
+
"valgen": "^5.9.0"
|
|
37
12
|
},
|
|
38
13
|
"peerDependencies": {
|
|
39
|
-
"@opra/common": "^1.0.0-
|
|
40
|
-
"@opra/core": "^1.0.0-
|
|
14
|
+
"@opra/common": "^1.0.0-beta.2",
|
|
15
|
+
"@opra/core": "^1.0.0-beta.2",
|
|
41
16
|
"mongodb": ">= 6.0.0"
|
|
42
17
|
},
|
|
43
18
|
"type": "module",
|
|
44
|
-
"
|
|
19
|
+
"exports": {
|
|
20
|
+
".": {
|
|
21
|
+
"import": {
|
|
22
|
+
"types": "./types/index.d.ts",
|
|
23
|
+
"default": "./esm/index.js"
|
|
24
|
+
},
|
|
25
|
+
"require": {
|
|
26
|
+
"types": "./types/index.d.cts",
|
|
27
|
+
"default": "./cjs/index.js"
|
|
28
|
+
},
|
|
29
|
+
"default": "./esm/index.js"
|
|
30
|
+
},
|
|
31
|
+
"./package.json": "./package.json"
|
|
32
|
+
},
|
|
45
33
|
"main": "./cjs/index.js",
|
|
34
|
+
"module": "./esm/index.js",
|
|
46
35
|
"types": "./types/index.d.ts",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "https://github.com/panates/opra.git",
|
|
39
|
+
"directory": "packages/mongodb"
|
|
40
|
+
},
|
|
47
41
|
"engines": {
|
|
48
42
|
"node": ">=16.0",
|
|
49
43
|
"npm": ">=7.0.0"
|
|
@@ -61,4 +55,4 @@
|
|
|
61
55
|
"mongodb",
|
|
62
56
|
"adapter"
|
|
63
57
|
]
|
|
64
|
-
}
|
|
58
|
+
}
|
|
@@ -9,6 +9,6 @@ import type { MongoAdapter } from '../mongo-adapter.js';
|
|
|
9
9
|
*
|
|
10
10
|
* @returns {mongodb.Filter<any>} - The prepared MongoDB filter.
|
|
11
11
|
*/
|
|
12
|
-
export default function prepareFilter(filters: MongoAdapter.FilterInput | MongoAdapter.FilterInput[], options?: {
|
|
12
|
+
export default function prepareFilter<T = any>(filters: MongoAdapter.FilterInput<T> | MongoAdapter.FilterInput<T>[], options?: {
|
|
13
13
|
fieldPrefix?: string;
|
|
14
|
-
}): mongodb.Filter<
|
|
14
|
+
}): mongodb.Filter<T>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ComplexType, FieldsProjection } from '@opra/common';
|
|
2
|
-
import mongodb, { Document } from 'mongodb';
|
|
2
|
+
import mongodb, { type Document } from 'mongodb';
|
|
3
3
|
export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document): mongodb.Document | undefined;
|
|
4
4
|
export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection): void;
|
package/types/index.d.ts
CHANGED
package/types/mongo-adapter.d.ts
CHANGED
|
@@ -8,7 +8,7 @@ import _prepareProjection from './adapter-utils/prepare-projection.js';
|
|
|
8
8
|
import _prepareSort from './adapter-utils/prepare-sort.js';
|
|
9
9
|
export declare namespace MongoAdapter {
|
|
10
10
|
type AnyId = string | number | ObjectId;
|
|
11
|
-
type FilterInput = OpraFilter.Expression | mongodb.Filter<
|
|
11
|
+
type FilterInput<T = any> = OpraFilter.Expression | mongodb.Filter<T> | string | undefined;
|
|
12
12
|
type WithTransactionCallback = (session: ClientSession) => any;
|
|
13
13
|
const prepareFilter: typeof _prepareFilter;
|
|
14
14
|
const prepareKeyValues: typeof _prepareKeyValues;
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import mongodb, { UpdateFilter } from 'mongodb';
|
|
2
|
-
import { PartialDTO, PatchDTO, Type } from 'ts-gems';
|
|
1
|
+
import mongodb, { type UpdateFilter } from 'mongodb';
|
|
2
|
+
import type { PartialDTO, PatchDTO, RequiredSome, 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,140 +41,143 @@ 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: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
59
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
82
60
|
/**
|
|
83
61
|
* Returns the count of documents in the collection based on the provided options.
|
|
84
62
|
*
|
|
85
|
-
* @param {
|
|
63
|
+
* @param {MongoEntityService.CountOptions<T>} options - The options for the count operation.
|
|
86
64
|
* @return {Promise<number>} - A promise that resolves to the count of documents in the collection.
|
|
87
65
|
*/
|
|
88
|
-
count(options?:
|
|
66
|
+
count(options?: MongoEntityService.CountOptions<T>): Promise<number>;
|
|
89
67
|
/**
|
|
90
68
|
* Deletes a document from the collection.
|
|
91
69
|
*
|
|
92
70
|
* @param {MongoAdapter.AnyId} id - The ID of the document to delete.
|
|
93
|
-
* @param {
|
|
71
|
+
* @param {MongoEntityService.DeleteOptions<T>} [options] - Optional delete options.
|
|
94
72
|
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
95
73
|
*/
|
|
96
|
-
delete(id: MongoAdapter.AnyId, options?:
|
|
74
|
+
delete(id: MongoAdapter.AnyId, options?: MongoEntityService.DeleteOptions<T>): Promise<number>;
|
|
97
75
|
/**
|
|
98
76
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
99
77
|
*
|
|
100
|
-
* @param {
|
|
78
|
+
* @param {MongoEntityService.DeleteManyOptions<T>} options - The options for the delete operation.
|
|
101
79
|
* @return {Promise<number>} - A promise that resolves to the number of documents deleted.
|
|
102
80
|
*/
|
|
103
|
-
deleteMany(options?:
|
|
81
|
+
deleteMany(options?: MongoEntityService.DeleteManyOptions<T>): Promise<number>;
|
|
104
82
|
/**
|
|
105
83
|
* The distinct command returns a list of distinct values for the given key across a collection.
|
|
106
84
|
* @param {string} field
|
|
107
|
-
* @param {
|
|
85
|
+
* @param {MongoEntityService.DistinctOptions<T>} [options]
|
|
108
86
|
* @protected
|
|
109
87
|
*/
|
|
110
|
-
distinct(field: string, options?:
|
|
88
|
+
distinct(field: string, options?: MongoEntityService.DistinctOptions<T>): Promise<any[]>;
|
|
111
89
|
/**
|
|
112
90
|
* Checks if an object with the given id exists.
|
|
113
91
|
*
|
|
114
92
|
* @param {MongoAdapter.AnyId} id - The id of the object to check.
|
|
115
|
-
* @param {
|
|
93
|
+
* @param {MongoEntityService.ExistsOptions<T>} [options] - The options for the query (optional).
|
|
116
94
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
117
95
|
*/
|
|
118
|
-
exists(id: MongoAdapter.AnyId, options?:
|
|
96
|
+
exists(id: MongoAdapter.AnyId, options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
119
97
|
/**
|
|
120
98
|
* Checks if an object with the given arguments exists.
|
|
121
99
|
*
|
|
122
|
-
* @param {
|
|
100
|
+
* @param {MongoEntityService.ExistsOptions} [options] - The options for the query (optional).
|
|
123
101
|
* @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the object exists or not.
|
|
124
102
|
*/
|
|
125
|
-
existsOne(options?:
|
|
103
|
+
existsOne(options?: MongoEntityService.ExistsOptions<T>): Promise<boolean>;
|
|
126
104
|
/**
|
|
127
105
|
* Finds a document by its ID.
|
|
128
106
|
*
|
|
129
107
|
* @param {MongoAdapter.AnyId} id - The ID of the document.
|
|
130
|
-
* @param {
|
|
108
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the find query.
|
|
131
109
|
* @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found.
|
|
132
110
|
*/
|
|
133
|
-
findById(id: MongoAdapter.AnyId, options
|
|
111
|
+
findById(id: MongoAdapter.AnyId, options: RequiredSome<MongoEntityService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
112
|
+
findById(id: MongoAdapter.AnyId, options?: MongoEntityService.FindOneOptions<T>): Promise<T | undefined>;
|
|
134
113
|
/**
|
|
135
114
|
* Finds a document in the collection that matches the specified options.
|
|
136
115
|
*
|
|
137
|
-
* @param {
|
|
116
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - The options for the query.
|
|
138
117
|
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
139
118
|
*/
|
|
140
|
-
findOne(options
|
|
119
|
+
findOne(options: RequiredSome<MongoEntityService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
120
|
+
findOne(options?: MongoEntityService.FindOneOptions<T>): Promise<T | undefined>;
|
|
141
121
|
/**
|
|
142
122
|
* Finds multiple documents in the MongoDB collection.
|
|
143
123
|
*
|
|
144
|
-
* @param {
|
|
124
|
+
* @param {MongoEntityService.FindManyOptions<T>} options - The options for the find operation.
|
|
145
125
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
146
126
|
*/
|
|
147
|
-
findMany(options
|
|
127
|
+
findMany(options: RequiredSome<MongoEntityService.FindManyOptions<T>, 'projection'>): Promise<PartialDTO<T>[]>;
|
|
128
|
+
findMany(options?: MongoEntityService.FindManyOptions<T>): Promise<T[]>;
|
|
148
129
|
/**
|
|
149
130
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
150
131
|
* and total count that matched the given criteria
|
|
151
132
|
*
|
|
152
|
-
* @param {
|
|
133
|
+
* @param {MongoEntityService.FindManyOptions<T>} [options] - The options for the find operation.
|
|
153
134
|
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
154
135
|
*/
|
|
155
|
-
findManyWithCount(options
|
|
136
|
+
findManyWithCount(options: RequiredSome<MongoEntityService.FindManyOptions<T>, 'projection'>): Promise<{
|
|
156
137
|
count: number;
|
|
157
138
|
items: PartialDTO<T>[];
|
|
158
139
|
}>;
|
|
140
|
+
findManyWithCount(options?: MongoEntityService.FindManyOptions<T>): Promise<{
|
|
141
|
+
count: number;
|
|
142
|
+
items: T[];
|
|
143
|
+
}>;
|
|
159
144
|
/**
|
|
160
145
|
* Retrieves a document from the collection by its ID. Throws error if not found.
|
|
161
146
|
*
|
|
162
147
|
* @param {MongoAdapter.AnyId} id - The ID of the document to retrieve.
|
|
163
|
-
* @param {
|
|
148
|
+
* @param {MongoEntityService.FindOneOptions<T>} [options] - Optional options for the findOne operation.
|
|
164
149
|
* @returns {Promise<PartialDTO<T>>} - A promise that resolves to the retrieved document,
|
|
165
150
|
* or rejects with a ResourceNotFoundError if the document does not exist.
|
|
166
151
|
* @throws {ResourceNotAvailableError} - If the document with the specified ID does not exist.
|
|
167
152
|
*/
|
|
168
|
-
get(id: MongoAdapter.AnyId, options
|
|
153
|
+
get(id: MongoAdapter.AnyId, options: RequiredSome<MongoEntityService.FindOneOptions<T>, 'projection'>): Promise<PartialDTO<T>>;
|
|
154
|
+
get(id: MongoAdapter.AnyId, options?: MongoEntityService.FindOneOptions<T>): Promise<T>;
|
|
169
155
|
/**
|
|
170
156
|
* Updates a document with the given id in the collection.
|
|
171
157
|
*
|
|
172
158
|
* @param {MongoAdapter.AnyId} id - The id of the document to update.
|
|
173
159
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input object containing the fields to update.
|
|
174
|
-
* @param {
|
|
160
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for the update operation.
|
|
175
161
|
* @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or
|
|
176
162
|
* undefined if the document was not found.
|
|
177
163
|
*/
|
|
178
|
-
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options
|
|
164
|
+
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options: RequiredSome<MongoEntityService.UpdateOneOptions<T>, 'projection'>): Promise<PartialDTO<T> | undefined>;
|
|
165
|
+
update(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<T | undefined>;
|
|
179
166
|
/**
|
|
180
167
|
* Updates a document in the collection with the specified ID.
|
|
181
168
|
*
|
|
182
169
|
* @param {MongoAdapter.AnyId} id - The ID of the document to update.
|
|
183
170
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input data to update the document with.
|
|
184
|
-
* @param {
|
|
171
|
+
* @param {MongoEntityService.UpdateOneOptions<T>} [options] - The options for updating the document.
|
|
185
172
|
* @returns {Promise<number>} - A promise that resolves to the number of documents modified.
|
|
186
173
|
*/
|
|
187
|
-
updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?:
|
|
174
|
+
updateOnly(id: MongoAdapter.AnyId, input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateOneOptions<T>): Promise<number>;
|
|
188
175
|
/**
|
|
189
176
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
190
177
|
*
|
|
191
178
|
* @param {PatchDTO<T>|UpdateFilter<T>} input - The partial input to update the documents with.
|
|
192
|
-
* @param {
|
|
179
|
+
* @param {MongoEntityService.UpdateManyOptions<T>} options - The options for updating the documents.
|
|
193
180
|
* @return {Promise<number>} - A promise that resolves to the number of documents matched and modified.
|
|
194
181
|
*/
|
|
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;
|
|
182
|
+
updateMany(input: PatchDTO<T> | UpdateFilter<T>, options?: MongoEntityService.UpdateManyOptions<T>): Promise<number>;
|
|
203
183
|
}
|
|
@@ -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 type { PartialDTO, PatchDTO, StrictOmit, Type } from 'ts-gems';
|
|
4
3
|
import { MongoService } from './mongo-service.js';
|
|
5
4
|
/**
|
|
6
5
|
*
|
|
@@ -27,18 +26,62 @@ 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<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
40
|
+
crud: 'create';
|
|
41
|
+
input: PartialDTO<T>;
|
|
42
|
+
options?: CreateOptions;
|
|
43
|
+
}
|
|
44
|
+
interface CountCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
45
|
+
crud: 'read';
|
|
46
|
+
options?: CountOptions<T>;
|
|
47
|
+
}
|
|
48
|
+
interface DeleteCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
49
|
+
crud: 'delete';
|
|
50
|
+
options?: DeleteOptions<T>;
|
|
51
|
+
}
|
|
52
|
+
interface DistinctCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
53
|
+
crud: 'read';
|
|
54
|
+
field: string;
|
|
55
|
+
options?: DistinctOptions<T>;
|
|
56
|
+
}
|
|
57
|
+
interface ExistsCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
58
|
+
crud: 'read';
|
|
59
|
+
options?: ExistsOptions<T>;
|
|
60
|
+
}
|
|
61
|
+
interface FindOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
62
|
+
crud: 'read';
|
|
63
|
+
options?: FindOneOptions<T>;
|
|
64
|
+
}
|
|
65
|
+
interface FindManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId' | 'input'> {
|
|
66
|
+
crud: 'read';
|
|
67
|
+
options?: FindManyOptions<T>;
|
|
68
|
+
}
|
|
69
|
+
interface UpdateOneCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
70
|
+
crud: 'update';
|
|
71
|
+
input?: PatchDTO<T>;
|
|
72
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
73
|
+
options?: UpdateOneOptions<T>;
|
|
74
|
+
}
|
|
75
|
+
interface UpdateManyCommand<T> extends StrictOmit<CommandInfo, 'nestedId'> {
|
|
76
|
+
crud: 'update';
|
|
77
|
+
input?: PatchDTO<T>;
|
|
78
|
+
inputRaw?: mongodb.UpdateFilter<T>;
|
|
79
|
+
options?: UpdateManyOptions<T>;
|
|
80
|
+
}
|
|
38
81
|
}
|
|
39
82
|
/**
|
|
40
83
|
* @class MongoEntityService
|
|
41
|
-
* @template T - The type of the documents in the collection
|
|
84
|
+
* @template T - The type of the documents in the collection
|
|
42
85
|
*/
|
|
43
86
|
export declare class MongoEntityService<T extends mongodb.Document> extends MongoService<T> {
|
|
44
87
|
/**
|
|
@@ -50,100 +93,95 @@ export declare class MongoEntityService<T extends mongodb.Document> extends Mong
|
|
|
50
93
|
*/
|
|
51
94
|
constructor(dataType: Type | string, options?: MongoEntityService.Options);
|
|
52
95
|
/**
|
|
53
|
-
* Creates a new document in the MongoDB collection
|
|
96
|
+
* Creates a new document in the MongoDB collection
|
|
54
97
|
*
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {MongoEntityService.CreateOptions} options
|
|
98
|
+
* @param {MongoEntityService.CreateCommand} command
|
|
57
99
|
* @protected
|
|
58
100
|
*/
|
|
59
|
-
protected _create(
|
|
101
|
+
protected _create(command: MongoEntityService.CreateCommand<T>): Promise<T>;
|
|
60
102
|
/**
|
|
61
103
|
* Returns the count of documents in the collection based on the provided options.
|
|
62
104
|
*
|
|
63
|
-
* @param {MongoEntityService.
|
|
64
|
-
* @
|
|
105
|
+
* @param {MongoEntityService.CountCommand<T>} command
|
|
106
|
+
* @protected
|
|
65
107
|
*/
|
|
66
|
-
protected _count(
|
|
108
|
+
protected _count(command: MongoEntityService.CountCommand<T>): Promise<number>;
|
|
67
109
|
/**
|
|
68
|
-
* Deletes a document from the collection
|
|
110
|
+
* Deletes a document from the collection
|
|
69
111
|
*
|
|
70
|
-
* @param {
|
|
71
|
-
* @
|
|
72
|
-
* @return {Promise<number>} - A Promise that resolves to the number of documents deleted.
|
|
112
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
113
|
+
* @protected
|
|
73
114
|
*/
|
|
74
|
-
protected _delete(
|
|
115
|
+
protected _delete(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
75
116
|
/**
|
|
76
117
|
* Deletes multiple documents from the collection that meet the specified filter criteria.
|
|
77
118
|
*
|
|
78
|
-
* @param {MongoEntityService.
|
|
79
|
-
* @
|
|
119
|
+
* @param {MongoEntityService.DeleteCommand<T>} command
|
|
120
|
+
* @protected
|
|
80
121
|
*/
|
|
81
|
-
protected _deleteMany(
|
|
122
|
+
protected _deleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<number>;
|
|
82
123
|
/**
|
|
83
|
-
* The distinct command returns a list of distinct values for the given key across a collection
|
|
84
|
-
*
|
|
85
|
-
* @param {MongoEntityService.
|
|
124
|
+
* The distinct command returns a list of distinct values for the given key across a collection
|
|
125
|
+
*
|
|
126
|
+
* @param {MongoEntityService.DistinctCommand<T>} command
|
|
86
127
|
* @protected
|
|
87
128
|
*/
|
|
88
|
-
protected _distinct(
|
|
129
|
+
protected _distinct(command: MongoEntityService.DistinctCommand<T>): Promise<any[]>;
|
|
89
130
|
/**
|
|
90
131
|
* Finds a document by its ID.
|
|
91
132
|
*
|
|
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.
|
|
133
|
+
* @param { MongoEntityService.FindOneCommand<T>} command
|
|
95
134
|
*/
|
|
96
|
-
protected _findById(
|
|
135
|
+
protected _findById(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
97
136
|
/**
|
|
98
137
|
* Finds a document in the collection that matches the specified options.
|
|
99
138
|
*
|
|
100
|
-
* @param {MongoEntityService.
|
|
101
|
-
* @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found.
|
|
139
|
+
* @param {MongoEntityService.FindOneCommand<T>} command
|
|
102
140
|
*/
|
|
103
|
-
protected _findOne(
|
|
141
|
+
protected _findOne(command: MongoEntityService.FindOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
104
142
|
/**
|
|
105
|
-
* Finds multiple documents in the MongoDB collection
|
|
143
|
+
* Finds multiple documents in the MongoDB collection
|
|
106
144
|
*
|
|
107
|
-
* @param {MongoEntityService.
|
|
108
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
145
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
109
146
|
*/
|
|
110
|
-
protected _findMany(
|
|
147
|
+
protected _findMany(command: MongoEntityService.FindManyCommand<T>): Promise<PartialDTO<T>[]>;
|
|
111
148
|
/**
|
|
112
149
|
* Finds multiple documents in the collection and returns both records (max limit)
|
|
113
150
|
* and total count that matched the given criteria
|
|
114
151
|
*
|
|
115
|
-
* @param {MongoEntityService.
|
|
116
|
-
* @return A Promise that resolves to an array of partial outputs of type T.
|
|
152
|
+
* @param {MongoEntityService.FindManyCommand<T>} command
|
|
117
153
|
*/
|
|
118
|
-
protected _findManyWithCount(
|
|
154
|
+
protected _findManyWithCount(command: MongoEntityService.FindManyCommand<T>): Promise<{
|
|
119
155
|
count: number;
|
|
120
156
|
items: PartialDTO<T>[];
|
|
121
157
|
}>;
|
|
122
158
|
/**
|
|
123
|
-
* Updates a document with the given id in the collection
|
|
159
|
+
* Updates a document with the given id in the collection
|
|
124
160
|
*
|
|
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.
|
|
161
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
130
162
|
*/
|
|
131
|
-
protected _update(
|
|
163
|
+
protected _update(command: MongoEntityService.UpdateOneCommand<T>): Promise<PartialDTO<T> | undefined>;
|
|
132
164
|
/**
|
|
133
165
|
* Updates a document in the collection with the specified ID.
|
|
134
166
|
*
|
|
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.
|
|
167
|
+
* @param {MongoEntityService.UpdateOneCommand<T>} command
|
|
139
168
|
*/
|
|
140
|
-
protected _updateOnly(
|
|
169
|
+
protected _updateOnly(command: MongoEntityService.UpdateOneCommand<T>): Promise<number>;
|
|
141
170
|
/**
|
|
142
171
|
* Updates multiple documents in the collection based on the specified input and options.
|
|
143
172
|
*
|
|
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.
|
|
173
|
+
* @param {MongoEntityService.UpdateManyCommand<T>} command
|
|
147
174
|
*/
|
|
148
|
-
protected _updateMany(
|
|
175
|
+
protected _updateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<number>;
|
|
176
|
+
protected _executeCommand(command: MongoEntityService.CommandInfo, commandFn: () => any): Promise<any>;
|
|
177
|
+
protected _beforeCreate(command: MongoEntityService.CreateCommand<T>): Promise<void>;
|
|
178
|
+
protected _beforeUpdate(command: MongoEntityService.UpdateOneCommand<T>): Promise<void>;
|
|
179
|
+
protected _beforeUpdateMany(command: MongoEntityService.UpdateManyCommand<T>): Promise<void>;
|
|
180
|
+
protected _beforeDelete(command: MongoEntityService.DeleteCommand<T>): Promise<void>;
|
|
181
|
+
protected _beforeDeleteMany(command: MongoEntityService.DeleteCommand<T>): Promise<void>;
|
|
182
|
+
protected _afterCreate(command: MongoEntityService.CreateCommand<T>, result: PartialDTO<T>): Promise<void>;
|
|
183
|
+
protected _afterUpdate(command: MongoEntityService.UpdateOneCommand<T>, result?: PartialDTO<T>): Promise<void>;
|
|
184
|
+
protected _afterUpdateMany(command: MongoEntityService.UpdateManyCommand<T>, affected: number): Promise<void>;
|
|
185
|
+
protected _afterDelete(command: MongoEntityService.DeleteCommand<T>, affected: number): Promise<void>;
|
|
186
|
+
protected _afterDeleteMany(command: MongoEntityService.DeleteCommand<T>, affected: number): Promise<void>;
|
|
149
187
|
}
|