@opra/mongodb 0.26.2 → 0.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/cjs/mongo-adapter.js +1 -3
- package/cjs/mongo-collection.js +11 -10
- package/cjs/mongo-entity-service.js +9 -12
- 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 +9 -12
- package/esm/mongo-singleton.js +8 -7
- package/package.json +4 -4
- package/types/mongo-collection.d.ts +11 -10
- package/types/mongo-entity-service.d.ts +3 -3
- package/types/mongo-singleton.d.ts +10 -8
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;
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MongoEntityService = void 0;
|
|
4
|
-
|
|
4
|
+
const core_1 = require("@opra/core");
|
|
5
|
+
class MongoEntityService extends core_1.ApiService {
|
|
5
6
|
constructor(arg0, arg1) {
|
|
7
|
+
super();
|
|
6
8
|
const options = typeof arg0 === 'object' ? arg0 : arg1;
|
|
7
9
|
if (typeof arg0 === 'string')
|
|
8
10
|
this._collectionName = arg0;
|
|
@@ -165,17 +167,12 @@ class MongoEntityService {
|
|
|
165
167
|
}
|
|
166
168
|
}
|
|
167
169
|
with(context, db, session) {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
instance.session = session;
|
|
175
|
-
}
|
|
176
|
-
if (session)
|
|
177
|
-
instance.session = session;
|
|
178
|
-
Object.setPrototypeOf(instance, this);
|
|
170
|
+
return this.forContext(context, db, session);
|
|
171
|
+
}
|
|
172
|
+
forContext(context, db, session) {
|
|
173
|
+
const instance = super.forContext(context);
|
|
174
|
+
instance.db = db || this.db;
|
|
175
|
+
instance.session = session || this.session;
|
|
179
176
|
return instance;
|
|
180
177
|
}
|
|
181
178
|
async _onError(error) {
|
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([
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
import { ApiService } from '@opra/core';
|
|
2
|
+
export class MongoEntityService extends ApiService {
|
|
2
3
|
constructor(arg0, arg1) {
|
|
4
|
+
super();
|
|
3
5
|
const options = typeof arg0 === 'object' ? arg0 : arg1;
|
|
4
6
|
if (typeof arg0 === 'string')
|
|
5
7
|
this._collectionName = arg0;
|
|
@@ -162,17 +164,12 @@ export class MongoEntityService {
|
|
|
162
164
|
}
|
|
163
165
|
}
|
|
164
166
|
with(context, db, session) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
instance.session = session;
|
|
172
|
-
}
|
|
173
|
-
if (session)
|
|
174
|
-
instance.session = session;
|
|
175
|
-
Object.setPrototypeOf(instance, this);
|
|
167
|
+
return this.forContext(context, db, session);
|
|
168
|
+
}
|
|
169
|
+
forContext(context, db, session) {
|
|
170
|
+
const instance = super.forContext(context);
|
|
171
|
+
instance.db = db || this.db;
|
|
172
|
+
instance.session = session || this.session;
|
|
176
173
|
return instance;
|
|
177
174
|
}
|
|
178
175
|
async _onError(error) {
|
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.4",
|
|
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.4",
|
|
37
|
+
"@opra/core": "^0.26.4",
|
|
38
38
|
"mongodb": ">=6.x.x"
|
|
39
39
|
},
|
|
40
40
|
"type": "module",
|
|
@@ -58,4 +58,4 @@
|
|
|
58
58
|
"mongodb",
|
|
59
59
|
"adapter"
|
|
60
60
|
]
|
|
61
|
-
}
|
|
61
|
+
}
|
|
@@ -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 {
|
|
@@ -11,14 +12,14 @@ export declare namespace MongoCollection {
|
|
|
11
12
|
export declare abstract class MongoCollection<T extends mongodb.Document> implements ICollection<T> {
|
|
12
13
|
defaultLimit?: number;
|
|
13
14
|
constructor(options?: MongoCollection.Options);
|
|
14
|
-
create(ctx: Collection.Create.Context): Promise<PartialOutput<T>>;
|
|
15
|
-
delete(ctx: RequestContext): Promise<number>;
|
|
16
|
-
deleteMany(ctx: RequestContext): Promise<number>;
|
|
17
|
-
get(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
18
|
-
update(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
19
|
-
updateMany(ctx: RequestContext): Promise<number>;
|
|
20
|
-
findMany(ctx: RequestContext): Promise<PartialOutput<T>[]>;
|
|
21
|
-
|
|
22
|
-
onPrepare?(ctx: RequestContext, prepared:
|
|
23
|
-
protected
|
|
15
|
+
create?(ctx: Collection.Create.Context): Promise<PartialOutput<T>>;
|
|
16
|
+
delete?(ctx: RequestContext): Promise<number>;
|
|
17
|
+
deleteMany?(ctx: RequestContext): Promise<number>;
|
|
18
|
+
get?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
19
|
+
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
20
|
+
updateMany?(ctx: RequestContext): Promise<number>;
|
|
21
|
+
findMany?(ctx: RequestContext): Promise<PartialOutput<T>[]>;
|
|
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
|
}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import mongodb, { UpdateFilter } from 'mongodb';
|
|
2
2
|
import { StrictOmit } from 'ts-gems';
|
|
3
|
-
import { PartialOutput, RequestContext } from '@opra/core';
|
|
3
|
+
import { ApiService, PartialOutput, RequestContext } from '@opra/core';
|
|
4
4
|
export declare namespace MongoEntityService {
|
|
5
5
|
interface Options {
|
|
6
6
|
db?: mongodb.Db;
|
|
7
7
|
defaultLimit?: number;
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
|
-
export declare class MongoEntityService<T extends mongodb.Document> {
|
|
10
|
+
export declare class MongoEntityService<T extends mongodb.Document> extends ApiService {
|
|
11
11
|
protected _collectionName: string;
|
|
12
|
-
context: RequestContext;
|
|
13
12
|
defaultLimit: number;
|
|
14
13
|
db?: mongodb.Db;
|
|
15
14
|
session?: mongodb.ClientSession;
|
|
@@ -24,6 +23,7 @@ export declare class MongoEntityService<T extends mongodb.Document> {
|
|
|
24
23
|
updateOne(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: mongodb.UpdateOptions): Promise<PartialOutput<T> | undefined>;
|
|
25
24
|
updateMany(filter: mongodb.Filter<T>, doc: UpdateFilter<T> | Partial<T>, options?: StrictOmit<mongodb.UpdateOptions, 'upsert'>): Promise<number>;
|
|
26
25
|
with(context: RequestContext, db?: mongodb.Db, session?: mongodb.ClientSession): MongoEntityService<T>;
|
|
26
|
+
forContext(context: RequestContext, db?: mongodb.Db, session?: mongodb.ClientSession): typeof this;
|
|
27
27
|
protected _onError(error: unknown): Promise<void>;
|
|
28
28
|
protected getDatabase(): mongodb.Db | Promise<mongodb.Db>;
|
|
29
29
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
export declare abstract class MongoSingleton<T extends mongodb.Document> implements ISingleton<T> {
|
|
8
|
+
create?(ctx: RequestContext): Promise<PartialOutput<T>>;
|
|
9
|
+
delete?(ctx: RequestContext): Promise<number>;
|
|
10
|
+
get?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
11
|
+
update?(ctx: RequestContext): Promise<Maybe<PartialOutput<T>>>;
|
|
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
|
}
|