@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.
@@ -70,14 +70,12 @@ var MongoAdapter;
70
70
  };
71
71
  }
72
72
  case 'findMany': {
73
- const out = {
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);
@@ -10,39 +10,39 @@ class MongoCollection {
10
10
  this.defaultLimit = options?.defaultLimit || 100;
11
11
  }
12
12
  async create(ctx) {
13
- const prepared = this._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request));
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
- _onPrepare(ctx, prepared) {
56
- return (this.onPrepare && this.onPrepare(ctx, prepared)) || prepared;
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
- class MongoEntityService {
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
- if (this.context === context && this.db === db && this.session === session)
169
- return this;
170
- const instance = { context };
171
- // Should reset session if db changed
172
- if (db) {
173
- instance.db = db;
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) {
@@ -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 = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
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 = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
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 = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
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 = mongo_adapter_js_1.MongoAdapter.transformRequest(ctx.request);
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([
@@ -66,14 +66,12 @@ export var MongoAdapter;
66
66
  };
67
67
  }
68
68
  case 'findMany': {
69
- const out = {
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);
@@ -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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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._onPrepare(ctx, MongoAdapter.transformRequest(ctx.request));
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
- _onPrepare(ctx, prepared) {
53
- return (this.onPrepare && this.onPrepare(ctx, prepared)) || prepared;
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
- export class MongoEntityService {
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
- if (this.context === context && this.db === db && this.session === session)
166
- return this;
167
- const instance = { context };
168
- // Should reset session if db changed
169
- if (db) {
170
- instance.db = db;
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) {
@@ -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 = MongoAdapter.transformRequest(ctx.request);
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 = MongoAdapter.transformRequest(ctx.request);
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 = MongoAdapter.transformRequest(ctx.request);
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 = MongoAdapter.transformRequest(ctx.request);
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.2",
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.1",
37
- "@opra/core": "^0.26.1",
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
- abstract getService(ctx: RequestContext): MongoEntityService<T> | Promise<MongoEntityService<T>>;
22
- onPrepare?(ctx: RequestContext, prepared: any): any;
23
- protected _onPrepare(ctx: RequestContext, prepared: any): any;
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
- defaultLimit: number;
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
- abstract getService(ctx: RequestContext): MongoEntityService<T> | Promise<MongoEntityService<T>>;
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
  }