@opra/mongodb 1.0.0-beta.1 → 1.0.0-beta.3
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/mongo-adapter.js +25 -21
- package/cjs/mongo-singleton-service.js +1 -1
- package/esm/mongo-adapter.js +25 -21
- package/esm/mongo-singleton-service.js +1 -1
- package/package.json +5 -4
- package/types/mongo-adapter.d.ts +2 -2
- package/types/mongo-collection-service.d.ts +3 -3
- package/types/mongo-entity-service.d.ts +2 -2
- package/types/mongo-nested-service.d.ts +3 -3
- package/types/mongo-service.d.ts +2 -2
- package/types/mongo-singleton-service.d.ts +6 -6
package/cjs/mongo-adapter.js
CHANGED
|
@@ -15,65 +15,69 @@ var MongoAdapter;
|
|
|
15
15
|
MongoAdapter.prepareProjection = prepare_projection_js_1.default;
|
|
16
16
|
MongoAdapter.prepareSort = prepare_sort_js_1.default;
|
|
17
17
|
async function parseRequest(context) {
|
|
18
|
-
|
|
18
|
+
if (context.protocol !== 'http') {
|
|
19
|
+
throw new TypeError('MongoAdapter can parse only HttpContext');
|
|
20
|
+
}
|
|
21
|
+
const ctx = context;
|
|
22
|
+
const { operation } = ctx;
|
|
19
23
|
if (operation?.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
|
|
20
24
|
const controller = operation.owner;
|
|
21
25
|
switch (operation.composition) {
|
|
22
26
|
case 'Entity.Create': {
|
|
23
|
-
const data = await
|
|
27
|
+
const data = await ctx.getBody();
|
|
24
28
|
const options = {
|
|
25
|
-
projection:
|
|
29
|
+
projection: ctx.queryParams.projection,
|
|
26
30
|
};
|
|
27
31
|
return { method: 'create', data, options };
|
|
28
32
|
}
|
|
29
33
|
case 'Entity.Delete': {
|
|
30
34
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
31
|
-
const key = keyParam &&
|
|
35
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
32
36
|
const options = {
|
|
33
|
-
filter:
|
|
37
|
+
filter: ctx.queryParams.filter,
|
|
34
38
|
};
|
|
35
39
|
return { method: 'delete', key, options };
|
|
36
40
|
}
|
|
37
41
|
case 'Entity.DeleteMany': {
|
|
38
42
|
const options = {
|
|
39
|
-
filter:
|
|
43
|
+
filter: ctx.queryParams.filter,
|
|
40
44
|
};
|
|
41
45
|
return { method: 'deleteMany', options };
|
|
42
46
|
}
|
|
43
47
|
case 'Entity.FindMany': {
|
|
44
48
|
const options = {
|
|
45
|
-
filter:
|
|
46
|
-
projection:
|
|
47
|
-
count:
|
|
48
|
-
limit:
|
|
49
|
-
skip:
|
|
50
|
-
sort:
|
|
49
|
+
filter: ctx.queryParams.filter,
|
|
50
|
+
projection: ctx.queryParams.projection || operation.compositionOptions.defaultProjection,
|
|
51
|
+
count: ctx.queryParams.count,
|
|
52
|
+
limit: ctx.queryParams.limit || operation.compositionOptions.defaultLimit,
|
|
53
|
+
skip: ctx.queryParams.skip,
|
|
54
|
+
sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
51
55
|
};
|
|
52
56
|
return { method: 'findMany', options };
|
|
53
57
|
}
|
|
54
58
|
case 'Entity.Get': {
|
|
55
59
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
56
|
-
const key = keyParam &&
|
|
60
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
57
61
|
const options = {
|
|
58
|
-
projection:
|
|
59
|
-
filter:
|
|
62
|
+
projection: ctx.queryParams.projection,
|
|
63
|
+
filter: ctx.queryParams.filter,
|
|
60
64
|
};
|
|
61
65
|
return { method: 'get', key, options };
|
|
62
66
|
}
|
|
63
67
|
case 'Entity.Update': {
|
|
64
|
-
const data = await
|
|
68
|
+
const data = await ctx.getBody();
|
|
65
69
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
66
|
-
const key = keyParam &&
|
|
70
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
67
71
|
const options = {
|
|
68
|
-
projection:
|
|
69
|
-
filter:
|
|
72
|
+
projection: ctx.queryParams.projection,
|
|
73
|
+
filter: ctx.queryParams.filter,
|
|
70
74
|
};
|
|
71
75
|
return { method: 'update', key, data, options };
|
|
72
76
|
}
|
|
73
77
|
case 'Entity.UpdateMany': {
|
|
74
|
-
const data = await
|
|
78
|
+
const data = await ctx.getBody();
|
|
75
79
|
const options = {
|
|
76
|
-
filter:
|
|
80
|
+
filter: ctx.queryParams.filter,
|
|
77
81
|
};
|
|
78
82
|
return { method: 'updateMany', data, options };
|
|
79
83
|
}
|
|
@@ -66,7 +66,7 @@ class MongoSingletonService extends mongo_entity_service_js_1.MongoEntityService
|
|
|
66
66
|
*
|
|
67
67
|
* @param {DTO<T>} input - The partial input to create the document with.
|
|
68
68
|
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
69
|
-
* @returns {Promise<
|
|
69
|
+
* @returns {Promise<T>} A promise that resolves create operation result
|
|
70
70
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
71
71
|
*/
|
|
72
72
|
async createOnly(input, options) {
|
package/esm/mongo-adapter.js
CHANGED
|
@@ -11,65 +11,69 @@ export var MongoAdapter;
|
|
|
11
11
|
MongoAdapter.prepareProjection = _prepareProjection;
|
|
12
12
|
MongoAdapter.prepareSort = _prepareSort;
|
|
13
13
|
async function parseRequest(context) {
|
|
14
|
-
|
|
14
|
+
if (context.protocol !== 'http') {
|
|
15
|
+
throw new TypeError('MongoAdapter can parse only HttpContext');
|
|
16
|
+
}
|
|
17
|
+
const ctx = context;
|
|
18
|
+
const { operation } = ctx;
|
|
15
19
|
if (operation?.composition?.startsWith('Entity.') && operation.compositionOptions?.type) {
|
|
16
20
|
const controller = operation.owner;
|
|
17
21
|
switch (operation.composition) {
|
|
18
22
|
case 'Entity.Create': {
|
|
19
|
-
const data = await
|
|
23
|
+
const data = await ctx.getBody();
|
|
20
24
|
const options = {
|
|
21
|
-
projection:
|
|
25
|
+
projection: ctx.queryParams.projection,
|
|
22
26
|
};
|
|
23
27
|
return { method: 'create', data, options };
|
|
24
28
|
}
|
|
25
29
|
case 'Entity.Delete': {
|
|
26
30
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
27
|
-
const key = keyParam &&
|
|
31
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
28
32
|
const options = {
|
|
29
|
-
filter:
|
|
33
|
+
filter: ctx.queryParams.filter,
|
|
30
34
|
};
|
|
31
35
|
return { method: 'delete', key, options };
|
|
32
36
|
}
|
|
33
37
|
case 'Entity.DeleteMany': {
|
|
34
38
|
const options = {
|
|
35
|
-
filter:
|
|
39
|
+
filter: ctx.queryParams.filter,
|
|
36
40
|
};
|
|
37
41
|
return { method: 'deleteMany', options };
|
|
38
42
|
}
|
|
39
43
|
case 'Entity.FindMany': {
|
|
40
44
|
const options = {
|
|
41
|
-
filter:
|
|
42
|
-
projection:
|
|
43
|
-
count:
|
|
44
|
-
limit:
|
|
45
|
-
skip:
|
|
46
|
-
sort:
|
|
45
|
+
filter: ctx.queryParams.filter,
|
|
46
|
+
projection: ctx.queryParams.projection || operation.compositionOptions.defaultProjection,
|
|
47
|
+
count: ctx.queryParams.count,
|
|
48
|
+
limit: ctx.queryParams.limit || operation.compositionOptions.defaultLimit,
|
|
49
|
+
skip: ctx.queryParams.skip,
|
|
50
|
+
sort: ctx.queryParams.sort || operation.compositionOptions.defaultSort,
|
|
47
51
|
};
|
|
48
52
|
return { method: 'findMany', options };
|
|
49
53
|
}
|
|
50
54
|
case 'Entity.Get': {
|
|
51
55
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
52
|
-
const key = keyParam &&
|
|
56
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
53
57
|
const options = {
|
|
54
|
-
projection:
|
|
55
|
-
filter:
|
|
58
|
+
projection: ctx.queryParams.projection,
|
|
59
|
+
filter: ctx.queryParams.filter,
|
|
56
60
|
};
|
|
57
61
|
return { method: 'get', key, options };
|
|
58
62
|
}
|
|
59
63
|
case 'Entity.Update': {
|
|
60
|
-
const data = await
|
|
64
|
+
const data = await ctx.getBody();
|
|
61
65
|
const keyParam = operation.parameters.find(p => p.keyParam) || controller.parameters.find(p => p.keyParam);
|
|
62
|
-
const key = keyParam &&
|
|
66
|
+
const key = keyParam && ctx.pathParams[String(keyParam.name)];
|
|
63
67
|
const options = {
|
|
64
|
-
projection:
|
|
65
|
-
filter:
|
|
68
|
+
projection: ctx.queryParams.projection,
|
|
69
|
+
filter: ctx.queryParams.filter,
|
|
66
70
|
};
|
|
67
71
|
return { method: 'update', key, data, options };
|
|
68
72
|
}
|
|
69
73
|
case 'Entity.UpdateMany': {
|
|
70
|
-
const data = await
|
|
74
|
+
const data = await ctx.getBody();
|
|
71
75
|
const options = {
|
|
72
|
-
filter:
|
|
76
|
+
filter: ctx.queryParams.filter,
|
|
73
77
|
};
|
|
74
78
|
return { method: 'updateMany', data, options };
|
|
75
79
|
}
|
|
@@ -62,7 +62,7 @@ export class MongoSingletonService extends MongoEntityService {
|
|
|
62
62
|
*
|
|
63
63
|
* @param {DTO<T>} input - The partial input to create the document with.
|
|
64
64
|
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
65
|
-
* @returns {Promise<
|
|
65
|
+
* @returns {Promise<T>} A promise that resolves create operation result
|
|
66
66
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
67
67
|
*/
|
|
68
68
|
async createOnly(input, options) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.3",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -8,11 +8,12 @@
|
|
|
8
8
|
"lodash.omit": "^4.5.0",
|
|
9
9
|
"putil-isplainobject": "^1.1.5",
|
|
10
10
|
"tslib": "^2.7.0",
|
|
11
|
-
"valgen": "^5.
|
|
11
|
+
"valgen": "^5.10.0"
|
|
12
12
|
},
|
|
13
13
|
"peerDependencies": {
|
|
14
|
-
"@opra/common": "^1.0.0-beta.
|
|
15
|
-
"@opra/core": "^1.0.0-beta.
|
|
14
|
+
"@opra/common": "^1.0.0-beta.3",
|
|
15
|
+
"@opra/core": "^1.0.0-beta.3",
|
|
16
|
+
"@opra/http": "^1.0.0-beta.3",
|
|
16
17
|
"mongodb": ">= 6.0.0"
|
|
17
18
|
},
|
|
18
19
|
"type": "module",
|
package/types/mongo-adapter.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { OpraFilter } from '@opra/common';
|
|
2
|
-
import {
|
|
2
|
+
import type { ExecutionContext } from '@opra/core';
|
|
3
3
|
import mongodb, { ClientSession, ObjectId } from 'mongodb';
|
|
4
4
|
import _prepareFilter from './adapter-utils/prepare-filter.js';
|
|
5
5
|
import _prepareKeyValues from './adapter-utils/prepare-key-values.js';
|
|
@@ -21,5 +21,5 @@ export declare namespace MongoAdapter {
|
|
|
21
21
|
data?: any;
|
|
22
22
|
options: any;
|
|
23
23
|
}
|
|
24
|
-
function parseRequest(context:
|
|
24
|
+
function parseRequest(context: ExecutionContext): Promise<TransformedRequest>;
|
|
25
25
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongodb, { type UpdateFilter } from 'mongodb';
|
|
2
|
-
import type {
|
|
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
5
|
/**
|
|
@@ -55,8 +55,8 @@ export declare class MongoCollectionService<T extends mongodb.Document> extends
|
|
|
55
55
|
* @returns {Promise<PartialDTO<T>>} A promise that resolves to the created document.
|
|
56
56
|
* @throws {Error} if an unknown error occurs while creating the document.
|
|
57
57
|
*/
|
|
58
|
-
create(input:
|
|
59
|
-
create(input:
|
|
58
|
+
create(input: PartialDTO<T>, options: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
59
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
60
60
|
/**
|
|
61
61
|
* Returns the count of documents in the collection based on the provided options.
|
|
62
62
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
|
-
import type {
|
|
2
|
+
import type { PartialDTO, PatchDTO, StrictOmit, Type } from 'ts-gems';
|
|
3
3
|
import { MongoService } from './mongo-service.js';
|
|
4
4
|
/**
|
|
5
5
|
*
|
|
@@ -38,7 +38,7 @@ export declare namespace MongoEntityService {
|
|
|
38
38
|
}
|
|
39
39
|
interface CreateCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
40
40
|
crud: 'create';
|
|
41
|
-
input:
|
|
41
|
+
input: PartialDTO<T>;
|
|
42
42
|
options?: CreateOptions;
|
|
43
43
|
}
|
|
44
44
|
interface CountCommand<T> extends StrictOmit<CommandInfo, 'documentId' | 'nestedId' | 'input'> {
|
|
@@ -142,13 +142,13 @@ export declare class MongoNestedService<T extends mongodb.Document> extends Mong
|
|
|
142
142
|
* Adds a single item into the array field.
|
|
143
143
|
*
|
|
144
144
|
* @param {MongoAdapter.AnyId} documentId - The ID of the parent document.
|
|
145
|
-
* @param {
|
|
145
|
+
* @param {PartialDTO<T>} input - The item to be added to the array field.
|
|
146
146
|
* @param {MongoNestedService.CreateOptions<T>} [options] - Optional options for the create operation.
|
|
147
147
|
* @return {Promise<PartialDTO<T>>} - A promise that resolves with the partial output of the created item.
|
|
148
148
|
* @throws {ResourceNotAvailableError} - If the parent document is not found.
|
|
149
149
|
*/
|
|
150
|
-
create(documentId: MongoAdapter.AnyId, input:
|
|
151
|
-
create(documentId: MongoAdapter.AnyId, input:
|
|
150
|
+
create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options: RequiredSome<MongoNestedService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
151
|
+
create(documentId: MongoAdapter.AnyId, input: PartialDTO<T>, options?: MongoNestedService.CreateOptions): Promise<T>;
|
|
152
152
|
/**
|
|
153
153
|
* Adds a single item into the array field.
|
|
154
154
|
*
|
package/types/mongo-service.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ComplexType } from '@opra/common';
|
|
2
|
-
import {
|
|
2
|
+
import { ExecutionContext, ServiceBase } from '@opra/core';
|
|
3
3
|
import mongodb, { type Document, type TransactionOptions } from 'mongodb';
|
|
4
4
|
import type { Nullish, StrictOmit, Type } from 'ts-gems';
|
|
5
5
|
import type { IsObject } from 'valgen';
|
|
@@ -188,7 +188,7 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
|
|
|
188
188
|
* @constructor
|
|
189
189
|
*/
|
|
190
190
|
constructor(dataType: Type | string, options?: MongoService.Options);
|
|
191
|
-
for<C extends
|
|
191
|
+
for<C extends ExecutionContext, P extends Partial<this>>(context: C, overwriteProperties?: Nullish<P>, overwriteContext?: Partial<C>): this & Required<P>;
|
|
192
192
|
/**
|
|
193
193
|
* Retrieves the collection name.
|
|
194
194
|
*
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongodb, { type UpdateFilter } from 'mongodb';
|
|
2
|
-
import type {
|
|
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
5
|
/**
|
|
@@ -48,22 +48,22 @@ export declare class MongoSingletonService<T extends mongodb.Document> extends M
|
|
|
48
48
|
/**
|
|
49
49
|
* Creates the document in the database.
|
|
50
50
|
*
|
|
51
|
-
* @param {
|
|
51
|
+
* @param {PartialDTO<T>} input - The partial input to create the document with.
|
|
52
52
|
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
53
53
|
* @return {Promise<PartialDTO<T>>} A promise that resolves to the partial output of the created document.
|
|
54
54
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
55
55
|
*/
|
|
56
|
-
create(input:
|
|
57
|
-
create(input:
|
|
56
|
+
create(input: PartialDTO<T>, options: RequiredSome<MongoEntityService.CreateOptions, 'projection'>): Promise<PartialDTO<T>>;
|
|
57
|
+
create(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
58
58
|
/**
|
|
59
59
|
* Creates the document in the database.
|
|
60
60
|
*
|
|
61
61
|
* @param {DTO<T>} input - The partial input to create the document with.
|
|
62
62
|
* @param {MongoEntityService.CreateOptions} [options] - The options for creating the document.
|
|
63
|
-
* @returns {Promise<
|
|
63
|
+
* @returns {Promise<T>} A promise that resolves create operation result
|
|
64
64
|
* @throws {Error} Throws an error if an unknown error occurs while creating the document.
|
|
65
65
|
*/
|
|
66
|
-
createOnly(input:
|
|
66
|
+
createOnly(input: PartialDTO<T>, options?: MongoEntityService.CreateOptions): Promise<T>;
|
|
67
67
|
/**
|
|
68
68
|
* Deletes a record from the database
|
|
69
69
|
*
|