@opra/mongodb 0.26.1 → 0.26.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 +1 -3
- package/cjs/mongo-collection.js +11 -10
- package/cjs/mongo-entity-service.js +3 -0
- package/cjs/mongo-singleton.js +8 -7
- package/esm/mongo-adapter.js +1 -3
- package/esm/mongo-collection.js +11 -10
- package/esm/mongo-entity-service.js +3 -0
- package/esm/mongo-singleton.js +8 -7
- package/package.json +3 -3
- package/types/mongo-collection.d.ts +4 -3
- package/types/mongo-entity-service.d.ts +1 -0
- package/types/mongo-singleton.d.ts +6 -4
package/cjs/mongo-adapter.js
CHANGED
|
@@ -70,14 +70,12 @@ var MongoAdapter;
|
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
case 'findMany': {
|
|
73
|
-
|
|
73
|
+
return {
|
|
74
74
|
method: 'find',
|
|
75
75
|
filter,
|
|
76
76
|
options,
|
|
77
77
|
args: [filter, options]
|
|
78
78
|
};
|
|
79
|
-
out.count = params?.count;
|
|
80
|
-
return out;
|
|
81
79
|
}
|
|
82
80
|
case 'update': {
|
|
83
81
|
const data = MongoAdapter.transformPatch(request.data);
|
package/cjs/mongo-collection.js
CHANGED
|
@@ -10,39 +10,39 @@ class MongoCollection {
|
|
|
10
10
|
this.defaultLimit = options?.defaultLimit || 100;
|
|
11
11
|
}
|
|
12
12
|
async create(ctx) {
|
|
13
|
-
const prepared = this.
|
|
13
|
+
const prepared = await this._prepare(ctx);
|
|
14
14
|
const service = await this.getService(ctx);
|
|
15
15
|
return service.insertOne(prepared.data, prepared.options);
|
|
16
16
|
}
|
|
17
17
|
async delete(ctx) {
|
|
18
|
-
const prepared = this.
|
|
18
|
+
const prepared = await this._prepare(ctx);
|
|
19
19
|
const service = await this.getService(ctx);
|
|
20
20
|
return service.deleteOne(prepared.filter, prepared.options);
|
|
21
21
|
}
|
|
22
22
|
async deleteMany(ctx) {
|
|
23
|
-
const prepared = this.
|
|
23
|
+
const prepared = await this._prepare(ctx);
|
|
24
24
|
const service = await this.getService(ctx);
|
|
25
25
|
return service.deleteMany(prepared.filter, prepared.options);
|
|
26
26
|
}
|
|
27
27
|
async get(ctx) {
|
|
28
|
-
const prepared = this.
|
|
28
|
+
const prepared = await this._prepare(ctx);
|
|
29
29
|
const service = await this.getService(ctx);
|
|
30
30
|
return service.findOne(prepared.filter, prepared.options);
|
|
31
31
|
}
|
|
32
32
|
async update(ctx) {
|
|
33
|
-
const prepared = this.
|
|
33
|
+
const prepared = await this._prepare(ctx);
|
|
34
34
|
const service = await this.getService(ctx);
|
|
35
35
|
return service.updateOne(prepared.filter, prepared.data, prepared.options);
|
|
36
36
|
}
|
|
37
37
|
async updateMany(ctx) {
|
|
38
|
-
const prepared = this.
|
|
38
|
+
const prepared = await this._prepare(ctx);
|
|
39
39
|
const service = await this.getService(ctx);
|
|
40
40
|
return service.updateMany(prepared.filter, prepared.data, prepared.options);
|
|
41
41
|
}
|
|
42
42
|
async findMany(ctx) {
|
|
43
|
-
const prepared = this.
|
|
43
|
+
const prepared = await this._prepare(ctx);
|
|
44
44
|
const service = await this.getService(ctx);
|
|
45
|
-
if (prepared.count) {
|
|
45
|
+
if (prepared.options.count) {
|
|
46
46
|
const [items, count] = await Promise.all([
|
|
47
47
|
service.find(prepared.filter, prepared.options),
|
|
48
48
|
service.count(prepared.filter, prepared.options)
|
|
@@ -52,8 +52,9 @@ class MongoCollection {
|
|
|
52
52
|
}
|
|
53
53
|
return service.find(prepared.filter, prepared.options);
|
|
54
54
|
}
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
async _prepare(ctx) {
|
|
56
|
+
const prepared = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
|
|
57
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
57
58
|
}
|
|
58
59
|
}
|
|
59
60
|
exports.MongoCollection = MongoCollection;
|
|
@@ -165,6 +165,9 @@ class MongoEntityService {
|
|
|
165
165
|
}
|
|
166
166
|
}
|
|
167
167
|
with(context, db, session) {
|
|
168
|
+
return this.forContext(context, db, session);
|
|
169
|
+
}
|
|
170
|
+
forContext(context, db, session) {
|
|
168
171
|
if (this.context === context && this.db === db && this.session === session)
|
|
169
172
|
return this;
|
|
170
173
|
const instance = { context };
|
package/cjs/mongo-singleton.js
CHANGED
|
@@ -5,30 +5,31 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const common_1 = require("@opra/common");
|
|
6
6
|
const mongo_adapter_js_1 = require("./mongo-adapter.js");
|
|
7
7
|
class MongoSingleton {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.defaultLimit = 100;
|
|
10
|
-
}
|
|
11
8
|
async create(ctx) {
|
|
12
|
-
const prepared =
|
|
9
|
+
const prepared = await this._prepare(ctx);
|
|
13
10
|
const service = await this.getService(ctx);
|
|
14
11
|
await service.deleteMany();
|
|
15
12
|
return service.insertOne(prepared.data, prepared.options);
|
|
16
13
|
}
|
|
17
14
|
async delete(ctx) {
|
|
18
|
-
const prepared =
|
|
15
|
+
const prepared = await this._prepare(ctx);
|
|
19
16
|
const service = await this.getService(ctx);
|
|
20
17
|
return service.deleteOne(prepared.filter, prepared.options);
|
|
21
18
|
}
|
|
22
19
|
async get(ctx) {
|
|
23
|
-
const prepared =
|
|
20
|
+
const prepared = await this._prepare(ctx);
|
|
24
21
|
const service = await this.getService(ctx);
|
|
25
22
|
return service.findOne(prepared.filter, prepared.options);
|
|
26
23
|
}
|
|
27
24
|
async update(ctx) {
|
|
28
|
-
const prepared =
|
|
25
|
+
const prepared = await this._prepare(ctx);
|
|
29
26
|
const service = await this.getService(ctx);
|
|
30
27
|
return service.updateOne(prepared.filter, prepared.data, prepared.options);
|
|
31
28
|
}
|
|
29
|
+
async _prepare(ctx) {
|
|
30
|
+
const prepared = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
|
|
31
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
32
|
+
}
|
|
32
33
|
}
|
|
33
34
|
exports.MongoSingleton = MongoSingleton;
|
|
34
35
|
tslib_1.__decorate([
|
package/esm/mongo-adapter.js
CHANGED
|
@@ -66,14 +66,12 @@ export var MongoAdapter;
|
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
case 'findMany': {
|
|
69
|
-
|
|
69
|
+
return {
|
|
70
70
|
method: 'find',
|
|
71
71
|
filter,
|
|
72
72
|
options,
|
|
73
73
|
args: [filter, options]
|
|
74
74
|
};
|
|
75
|
-
out.count = params?.count;
|
|
76
|
-
return out;
|
|
77
75
|
}
|
|
78
76
|
case 'update': {
|
|
79
77
|
const data = MongoAdapter.transformPatch(request.data);
|
package/esm/mongo-collection.js
CHANGED
|
@@ -7,39 +7,39 @@ export class MongoCollection {
|
|
|
7
7
|
this.defaultLimit = options?.defaultLimit || 100;
|
|
8
8
|
}
|
|
9
9
|
async create(ctx) {
|
|
10
|
-
const prepared = this.
|
|
10
|
+
const prepared = await this._prepare(ctx);
|
|
11
11
|
const service = await this.getService(ctx);
|
|
12
12
|
return service.insertOne(prepared.data, prepared.options);
|
|
13
13
|
}
|
|
14
14
|
async delete(ctx) {
|
|
15
|
-
const prepared = this.
|
|
15
|
+
const prepared = await this._prepare(ctx);
|
|
16
16
|
const service = await this.getService(ctx);
|
|
17
17
|
return service.deleteOne(prepared.filter, prepared.options);
|
|
18
18
|
}
|
|
19
19
|
async deleteMany(ctx) {
|
|
20
|
-
const prepared = this.
|
|
20
|
+
const prepared = await this._prepare(ctx);
|
|
21
21
|
const service = await this.getService(ctx);
|
|
22
22
|
return service.deleteMany(prepared.filter, prepared.options);
|
|
23
23
|
}
|
|
24
24
|
async get(ctx) {
|
|
25
|
-
const prepared = this.
|
|
25
|
+
const prepared = await this._prepare(ctx);
|
|
26
26
|
const service = await this.getService(ctx);
|
|
27
27
|
return service.findOne(prepared.filter, prepared.options);
|
|
28
28
|
}
|
|
29
29
|
async update(ctx) {
|
|
30
|
-
const prepared = this.
|
|
30
|
+
const prepared = await this._prepare(ctx);
|
|
31
31
|
const service = await this.getService(ctx);
|
|
32
32
|
return service.updateOne(prepared.filter, prepared.data, prepared.options);
|
|
33
33
|
}
|
|
34
34
|
async updateMany(ctx) {
|
|
35
|
-
const prepared = this.
|
|
35
|
+
const prepared = await this._prepare(ctx);
|
|
36
36
|
const service = await this.getService(ctx);
|
|
37
37
|
return service.updateMany(prepared.filter, prepared.data, prepared.options);
|
|
38
38
|
}
|
|
39
39
|
async findMany(ctx) {
|
|
40
|
-
const prepared = this.
|
|
40
|
+
const prepared = await this._prepare(ctx);
|
|
41
41
|
const service = await this.getService(ctx);
|
|
42
|
-
if (prepared.count) {
|
|
42
|
+
if (prepared.options.count) {
|
|
43
43
|
const [items, count] = await Promise.all([
|
|
44
44
|
service.find(prepared.filter, prepared.options),
|
|
45
45
|
service.count(prepared.filter, prepared.options)
|
|
@@ -49,8 +49,9 @@ export class MongoCollection {
|
|
|
49
49
|
}
|
|
50
50
|
return service.find(prepared.filter, prepared.options);
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
async _prepare(ctx) {
|
|
53
|
+
const prepared = MongoAdapter.transformRequest(ctx.request);
|
|
54
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
54
55
|
}
|
|
55
56
|
}
|
|
56
57
|
__decorate([
|
|
@@ -162,6 +162,9 @@ export class MongoEntityService {
|
|
|
162
162
|
}
|
|
163
163
|
}
|
|
164
164
|
with(context, db, session) {
|
|
165
|
+
return this.forContext(context, db, session);
|
|
166
|
+
}
|
|
167
|
+
forContext(context, db, session) {
|
|
165
168
|
if (this.context === context && this.db === db && this.session === session)
|
|
166
169
|
return this;
|
|
167
170
|
const instance = { context };
|
package/esm/mongo-singleton.js
CHANGED
|
@@ -2,30 +2,31 @@ import { __decorate } from "tslib";
|
|
|
2
2
|
import { Singleton } from '@opra/common';
|
|
3
3
|
import { MongoAdapter } from './mongo-adapter.js';
|
|
4
4
|
export class MongoSingleton {
|
|
5
|
-
constructor() {
|
|
6
|
-
this.defaultLimit = 100;
|
|
7
|
-
}
|
|
8
5
|
async create(ctx) {
|
|
9
|
-
const prepared =
|
|
6
|
+
const prepared = await this._prepare(ctx);
|
|
10
7
|
const service = await this.getService(ctx);
|
|
11
8
|
await service.deleteMany();
|
|
12
9
|
return service.insertOne(prepared.data, prepared.options);
|
|
13
10
|
}
|
|
14
11
|
async delete(ctx) {
|
|
15
|
-
const prepared =
|
|
12
|
+
const prepared = await this._prepare(ctx);
|
|
16
13
|
const service = await this.getService(ctx);
|
|
17
14
|
return service.deleteOne(prepared.filter, prepared.options);
|
|
18
15
|
}
|
|
19
16
|
async get(ctx) {
|
|
20
|
-
const prepared =
|
|
17
|
+
const prepared = await this._prepare(ctx);
|
|
21
18
|
const service = await this.getService(ctx);
|
|
22
19
|
return service.findOne(prepared.filter, prepared.options);
|
|
23
20
|
}
|
|
24
21
|
async update(ctx) {
|
|
25
|
-
const prepared =
|
|
22
|
+
const prepared = await this._prepare(ctx);
|
|
26
23
|
const service = await this.getService(ctx);
|
|
27
24
|
return service.updateOne(prepared.filter, prepared.data, prepared.options);
|
|
28
25
|
}
|
|
26
|
+
async _prepare(ctx) {
|
|
27
|
+
const prepared = MongoAdapter.transformRequest(ctx.request);
|
|
28
|
+
return (this.onPrepare && await this.onPrepare(ctx, prepared)) || prepared;
|
|
29
|
+
}
|
|
29
30
|
}
|
|
30
31
|
__decorate([
|
|
31
32
|
Singleton.Create()
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opra/mongodb",
|
|
3
|
-
"version": "0.26.
|
|
3
|
+
"version": "0.26.3",
|
|
4
4
|
"description": "Opra MongoDB adapter package",
|
|
5
5
|
"author": "Panates",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"ts-gems": "^2.5.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@opra/common": "^0.26.
|
|
37
|
-
"@opra/core": "^0.26.
|
|
36
|
+
"@opra/common": "^0.26.3",
|
|
37
|
+
"@opra/core": "^0.26.3",
|
|
38
38
|
"mongodb": ">=6.x.x"
|
|
39
39
|
},
|
|
40
40
|
"type": "module",
|
|
@@ -2,6 +2,7 @@ import mongodb from 'mongodb';
|
|
|
2
2
|
import { Maybe } from 'ts-gems';
|
|
3
3
|
import { Collection, ICollection, PartialOutput } from '@opra/common';
|
|
4
4
|
import { RequestContext } from '@opra/core';
|
|
5
|
+
import { MongoAdapter } from './mongo-adapter.js';
|
|
5
6
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
6
7
|
export declare namespace MongoCollection {
|
|
7
8
|
interface Options {
|
|
@@ -18,7 +19,7 @@ export declare abstract class MongoCollection<T extends mongodb.Document> implem
|
|
|
18
19
|
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
19
20
|
updateMany?(ctx: RequestContext): Promise<number>;
|
|
20
21
|
findMany?(ctx: RequestContext): Promise<PartialOutput<T>[]>;
|
|
21
|
-
|
|
22
|
-
onPrepare?(ctx: RequestContext, prepared:
|
|
23
|
-
protected
|
|
22
|
+
protected _prepare(ctx: RequestContext): Promise<MongoAdapter.TransformedRequest>;
|
|
23
|
+
protected onPrepare?(ctx: RequestContext, prepared: MongoAdapter.TransformedRequest): MongoAdapter.TransformedRequest | Promise<MongoAdapter.TransformedRequest>;
|
|
24
|
+
protected abstract getService(ctx: RequestContext): MongoEntityService<T> | Promise<MongoEntityService<T>>;
|
|
24
25
|
}
|
|
@@ -24,6 +24,7 @@ export declare class MongoEntityService<T extends mongodb.Document> {
|
|
|
24
24
|
updateOne(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: mongodb.UpdateOptions): Promise<PartialOutput<T> | undefined>;
|
|
25
25
|
updateMany(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<number>;
|
|
26
26
|
with(context: RequestContext, db?: mongodb.Db, session?: mongodb.ClientSession): MongoEntityService<T>;
|
|
27
|
+
forContext(context: RequestContext, db?: mongodb.Db, session?: mongodb.ClientSession): MongoEntityService<T>;
|
|
27
28
|
protected _onError(error: unknown): Promise<void>;
|
|
28
29
|
protected getDatabase(): mongodb.Db | Promise<mongodb.Db>;
|
|
29
30
|
protected getCollection(db: mongodb.Db): Promise<mongodb.Collection<T>>;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import mongodb from 'mongodb';
|
|
2
2
|
import { Maybe } from 'ts-gems';
|
|
3
|
-
import { PartialOutput } from '@opra/common';
|
|
3
|
+
import { ISingleton, PartialOutput } from '@opra/common';
|
|
4
4
|
import { RequestContext } from '@opra/core';
|
|
5
|
+
import { MongoAdapter } from './mongo-adapter.js';
|
|
5
6
|
import { MongoEntityService } from './mongo-entity-service.js';
|
|
6
|
-
export declare abstract class MongoSingleton<T extends mongodb.Document> {
|
|
7
|
-
defaultLimit: number;
|
|
7
|
+
export declare abstract class MongoSingleton<T extends mongodb.Document> implements ISingleton<T> {
|
|
8
8
|
create?(ctx: RequestContext): Promise<PartialOutput<T>>;
|
|
9
9
|
delete?(ctx: RequestContext): Promise<number>;
|
|
10
10
|
get?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
11
11
|
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
12
|
-
|
|
12
|
+
protected _prepare(ctx: RequestContext): Promise<MongoAdapter.TransformedRequest>;
|
|
13
|
+
protected onPrepare?(ctx: RequestContext, prepared: MongoAdapter.TransformedRequest): MongoAdapter.TransformedRequest | Promise<MongoAdapter.TransformedRequest>;
|
|
14
|
+
protected abstract getService(ctx: RequestContext): MongoEntityService<T> | Promise<MongoEntityService<T>>;
|
|
13
15
|
}
|