@opra/mongodb 0.31.5 → 0.31.7

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.
@@ -1,19 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- function preparePatch(doc) {
3
+ function preparePatch(doc, options) {
4
4
  const trg = {};
5
- _preparePatch(doc, trg);
5
+ _preparePatch(doc, trg, '', options);
6
6
  return trg;
7
7
  }
8
8
  exports.default = preparePatch;
9
- function _preparePatch(src, trg = {}, path = '') {
10
- let fieldName;
9
+ function _preparePatch(src, trg = {}, path, options) {
10
+ let f;
11
+ let key;
12
+ let field;
13
+ const fieldPrefix = options?.fieldPrefix;
11
14
  for (const [k, v] of Object.entries(src)) {
12
- fieldName = k.startsWith('*') ? k.substring(1) : k;
13
- const key = path ? path + '.' + fieldName : fieldName;
15
+ f = k.startsWith('*') ? k.substring(1) : k;
16
+ key = path ? path + '.' + f : f;
17
+ field = (fieldPrefix ? fieldPrefix : '') + key;
14
18
  if (v == null) {
15
19
  trg.$unset = trg.$unset || {};
16
- trg.$unset[key] = '';
20
+ trg.$unset[field] = '';
17
21
  continue;
18
22
  }
19
23
  if (v && typeof v === 'object') {
@@ -24,7 +28,7 @@ function _preparePatch(src, trg = {}, path = '') {
24
28
  }
25
29
  }
26
30
  trg.$set = trg.$set || {};
27
- trg.$set[key] = v;
31
+ trg.$set[field] = v;
28
32
  }
29
33
  return trg;
30
34
  }
@@ -105,20 +105,21 @@ class MongoCollectionService extends mongo_service_js_1.MongoService {
105
105
  // Prevent updating _id field
106
106
  delete doc._id;
107
107
  const patch = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
108
+ patch.$set = patch.$set || {};
108
109
  const mongoOptions = {
109
110
  ...options,
110
111
  upsert: undefined
111
112
  };
112
113
  const r = await this._rawUpdateOne(filter, patch, mongoOptions);
113
- if (r.modifiedCount)
114
- return await this.get(filter, options);
115
114
  if (r.upsertedCount)
116
115
  return await this.get(r.upsertedId, options);
116
+ return await this.get(id, options);
117
117
  }
118
118
  async updateMany(doc, options) {
119
119
  // Prevent updating _id field
120
120
  delete doc._id;
121
121
  const patch = mongo_adapter_js_1.MongoAdapter.preparePatch(doc);
122
+ patch.$set = patch.$set || {};
122
123
  const mongoOptions = {
123
124
  ...(0, lodash_omit_1.default)(options, 'filter'),
124
125
  upsert: undefined
@@ -7,8 +7,8 @@ class MongoService extends core_1.ApiService {
7
7
  constructor(dataType, options) {
8
8
  super();
9
9
  this._dataType = dataType;
10
- this.collectionName = options?.collectionName;
11
10
  this.db = options?.db;
11
+ this.collectionName = options?.collectionName || options?.resourceName;
12
12
  if (!this.collectionName) {
13
13
  if (typeof dataType === 'string')
14
14
  this.collectionName = dataType;
@@ -20,12 +20,8 @@ class MongoService extends core_1.ApiService {
20
20
  }
21
21
  this.resourceName = options?.resourceName || this.collectionName;
22
22
  }
23
- forContext(context, options) {
24
- return super.forContext(context, {
25
- newInstance: options?.newInstance ||
26
- (options?.db && this.db !== options?.db) ||
27
- (options?.session && this.session !== options?.session)
28
- });
23
+ forContext(arg0, attributes) {
24
+ return super.forContext(arg0, attributes);
29
25
  }
30
26
  async _rawInsertOne(doc, options) {
31
27
  const db = await this.getDatabase();
@@ -171,5 +167,10 @@ class MongoService extends core_1.ApiService {
171
167
  return this.collectionName;
172
168
  throw new Error('collectionName is not defined');
173
169
  }
170
+ _cacheMatch(service, context, attributes) {
171
+ return super._cacheMatch(service, context, attributes) &&
172
+ (!attributes?.db || service.db === attributes.db) &&
173
+ (!attributes?.session || this.session === attributes?.session);
174
+ }
174
175
  }
175
176
  exports.MongoService = MongoService;
@@ -1,16 +1,20 @@
1
- export default function preparePatch(doc) {
1
+ export default function preparePatch(doc, options) {
2
2
  const trg = {};
3
- _preparePatch(doc, trg);
3
+ _preparePatch(doc, trg, '', options);
4
4
  return trg;
5
5
  }
6
- function _preparePatch(src, trg = {}, path = '') {
7
- let fieldName;
6
+ function _preparePatch(src, trg = {}, path, options) {
7
+ let f;
8
+ let key;
9
+ let field;
10
+ const fieldPrefix = options?.fieldPrefix;
8
11
  for (const [k, v] of Object.entries(src)) {
9
- fieldName = k.startsWith('*') ? k.substring(1) : k;
10
- const key = path ? path + '.' + fieldName : fieldName;
12
+ f = k.startsWith('*') ? k.substring(1) : k;
13
+ key = path ? path + '.' + f : f;
14
+ field = (fieldPrefix ? fieldPrefix : '') + key;
11
15
  if (v == null) {
12
16
  trg.$unset = trg.$unset || {};
13
- trg.$unset[key] = '';
17
+ trg.$unset[field] = '';
14
18
  continue;
15
19
  }
16
20
  if (v && typeof v === 'object') {
@@ -21,7 +25,7 @@ function _preparePatch(src, trg = {}, path = '') {
21
25
  }
22
26
  }
23
27
  trg.$set = trg.$set || {};
24
- trg.$set[key] = v;
28
+ trg.$set[field] = v;
25
29
  }
26
30
  return trg;
27
31
  }
@@ -101,20 +101,21 @@ export class MongoCollectionService extends MongoService {
101
101
  // Prevent updating _id field
102
102
  delete doc._id;
103
103
  const patch = MongoAdapter.preparePatch(doc);
104
+ patch.$set = patch.$set || {};
104
105
  const mongoOptions = {
105
106
  ...options,
106
107
  upsert: undefined
107
108
  };
108
109
  const r = await this._rawUpdateOne(filter, patch, mongoOptions);
109
- if (r.modifiedCount)
110
- return await this.get(filter, options);
111
110
  if (r.upsertedCount)
112
111
  return await this.get(r.upsertedId, options);
112
+ return await this.get(id, options);
113
113
  }
114
114
  async updateMany(doc, options) {
115
115
  // Prevent updating _id field
116
116
  delete doc._id;
117
117
  const patch = MongoAdapter.preparePatch(doc);
118
+ patch.$set = patch.$set || {};
118
119
  const mongoOptions = {
119
120
  ...omit(options, 'filter'),
120
121
  upsert: undefined
@@ -4,8 +4,8 @@ export class MongoService extends ApiService {
4
4
  constructor(dataType, options) {
5
5
  super();
6
6
  this._dataType = dataType;
7
- this.collectionName = options?.collectionName;
8
7
  this.db = options?.db;
8
+ this.collectionName = options?.collectionName || options?.resourceName;
9
9
  if (!this.collectionName) {
10
10
  if (typeof dataType === 'string')
11
11
  this.collectionName = dataType;
@@ -17,12 +17,8 @@ export class MongoService extends ApiService {
17
17
  }
18
18
  this.resourceName = options?.resourceName || this.collectionName;
19
19
  }
20
- forContext(context, options) {
21
- return super.forContext(context, {
22
- newInstance: options?.newInstance ||
23
- (options?.db && this.db !== options?.db) ||
24
- (options?.session && this.session !== options?.session)
25
- });
20
+ forContext(arg0, attributes) {
21
+ return super.forContext(arg0, attributes);
26
22
  }
27
23
  async _rawInsertOne(doc, options) {
28
24
  const db = await this.getDatabase();
@@ -168,4 +164,9 @@ export class MongoService extends ApiService {
168
164
  return this.collectionName;
169
165
  throw new Error('collectionName is not defined');
170
166
  }
167
+ _cacheMatch(service, context, attributes) {
168
+ return super._cacheMatch(service, context, attributes) &&
169
+ (!attributes?.db || service.db === attributes.db) &&
170
+ (!attributes?.session || this.session === attributes?.session);
171
+ }
171
172
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "0.31.5",
3
+ "version": "0.31.7",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -24,17 +24,16 @@
24
24
  "clean:dist": "rimraf ../../build/mongodb",
25
25
  "clean:cover": "rimraf ../../coverage/mongodb"
26
26
  },
27
- "dependencies": {},
28
27
  "devDependencies": {
29
28
  "@faker-js/faker": "^8.3.1",
30
29
  "@types/lodash.isnil": "^4.0.9",
31
30
  "@types/lodash.omitby": "^4.6.9",
32
- "mongodb": "^6.2.0",
31
+ "mongodb": "^6.3.0",
33
32
  "ts-gems": "^2.5.0"
34
33
  },
35
34
  "peerDependencies": {
36
- "@opra/common": "^0.31.5",
37
- "@opra/core": "^0.31.5",
35
+ "@opra/common": "^0.31.7",
36
+ "@opra/core": "^0.31.7",
38
37
  "mongodb": ">=6.x.x"
39
38
  },
40
39
  "type": "module",
@@ -1 +1,3 @@
1
- export default function preparePatch(doc: any): any;
1
+ export default function preparePatch(doc: any, options?: {
2
+ fieldPrefix?: string;
3
+ }): any;
@@ -16,8 +16,8 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
16
16
  session?: mongodb.ClientSession;
17
17
  resourceName?: string;
18
18
  constructor(dataType: Type | string, options?: MongoService.Options);
19
- forContext(context: RequestContext, options?: {
20
- newInstance?: boolean;
19
+ forContext(source: ApiService): this;
20
+ forContext(context: RequestContext, attributes?: {
21
21
  db?: mongodb.Db;
22
22
  session?: mongodb.ClientSession;
23
23
  }): this;
@@ -34,6 +34,7 @@ export declare class MongoService<T extends mongodb.Document> extends ApiService
34
34
  protected getDataType(): ComplexType;
35
35
  protected getCollection(db: mongodb.Db): Promise<mongodb.Collection<T>>;
36
36
  protected getCollectionName(): string;
37
+ protected _cacheMatch(service: MongoService<any>, context: RequestContext, attributes?: any): boolean;
37
38
  protected onError?(error: unknown): void | Promise<void>;
38
39
  protected transformData?(row: PartialOutput<T>): PartialOutput<T>;
39
40
  }