@opra/mongodb 1.5.0 → 1.5.1

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.
@@ -6,7 +6,7 @@ const FIELD_NAME_PATTERN = /^([-><*:])?(.+)$/;
6
6
  class MongoPatchGenerator {
7
7
  generatePatch(dataType, doc, options) {
8
8
  const ctx = {};
9
- this._processComplexType(ctx, dataType, options?.currentPath || '', doc);
9
+ this._processComplexType(ctx, dataType, options?.currentPath || '', doc, options?.scope);
10
10
  const update = {};
11
11
  if (ctx.$pull)
12
12
  update.$pull = ctx.$pull;
@@ -21,12 +21,12 @@ class MongoPatchGenerator {
21
21
  arrayFilters: ctx.arrayFilters,
22
22
  };
23
23
  }
24
- _processComplexType(ctx, dataType, path, input) {
24
+ _processComplexType(ctx, dataType, path, input, scope) {
25
25
  if (input._$push) {
26
- this._processPush(ctx, dataType, path, input._$push);
26
+ this._processPush(ctx, dataType, path, input._$push, scope);
27
27
  }
28
28
  if (input._$pull) {
29
- this._processPull(ctx, dataType, path, input._$pull);
29
+ this._processPull(ctx, dataType, path, input._$pull, scope);
30
30
  }
31
31
  const keys = Object.keys(input);
32
32
  const pathDot = path + (path ? '.' : '');
@@ -42,8 +42,10 @@ class MongoPatchGenerator {
42
42
  if (!m)
43
43
  continue;
44
44
  key = m[2];
45
+ if (key === '_$push' || key === '_$pull')
46
+ continue;
45
47
  value = input[key];
46
- field = dataType.fields.get(key);
48
+ field = dataType.getField(key, scope);
47
49
  if (!field) {
48
50
  if (dataType.additionalFields) {
49
51
  if (value === null) {
@@ -54,7 +56,7 @@ class MongoPatchGenerator {
54
56
  ctx.$set = ctx.$set || {};
55
57
  if (dataType.additionalFields instanceof common_1.ComplexType) {
56
58
  /** Process nested object */
57
- this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value);
59
+ this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope);
58
60
  continue;
59
61
  }
60
62
  ctx.$set[pathDot + key] = value;
@@ -92,7 +94,7 @@ class MongoPatchGenerator {
92
94
  [`${arrayFilterName}.${keyField}`]: keyValue,
93
95
  });
94
96
  /** Process each object in array */
95
- this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v);
97
+ this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope);
96
98
  }
97
99
  continue;
98
100
  }
@@ -100,14 +102,14 @@ class MongoPatchGenerator {
100
102
  if (!(typeof value === 'object'))
101
103
  continue;
102
104
  /** Process nested object */
103
- this._processComplexType(ctx, field.type, pathDot + field.name, value);
105
+ this._processComplexType(ctx, field.type, pathDot + field.name, value, scope);
104
106
  continue;
105
107
  }
106
108
  ctx.$set = ctx.$set || {};
107
109
  ctx.$set[pathDot + field.name] = value;
108
110
  }
109
111
  }
110
- _processPush(ctx, dataType, path, input) {
112
+ _processPush(ctx, dataType, path, input, scope) {
111
113
  let field;
112
114
  let key;
113
115
  let value;
@@ -116,7 +118,7 @@ class MongoPatchGenerator {
116
118
  let keyField;
117
119
  for (key of keys) {
118
120
  value = input[key];
119
- field = dataType.fields.get(key);
121
+ field = dataType.getField(key, scope);
120
122
  if (!(field && field.isArray))
121
123
  continue;
122
124
  ctx.$push = ctx.$push || {};
@@ -145,7 +147,7 @@ class MongoPatchGenerator {
145
147
  : value;
146
148
  }
147
149
  }
148
- _processPull(ctx, dataType, path, input) {
150
+ _processPull(ctx, dataType, path, input, scope) {
149
151
  let field;
150
152
  let key;
151
153
  let value;
@@ -154,7 +156,7 @@ class MongoPatchGenerator {
154
156
  let keyField;
155
157
  for (key of keys) {
156
158
  value = input[key];
157
- field = dataType.fields.get(key);
159
+ field = dataType.getField(key, scope);
158
160
  if (!(field && field.isArray))
159
161
  continue;
160
162
  ctx.$pull = ctx.$pull || {};
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.default = prepareProjection;
4
4
  exports.prepare = prepare;
5
5
  const common_1 = require("@opra/common");
6
- function prepareProjection(dataType, projection, scopes) {
6
+ function prepareProjection(dataType, projection, scope) {
7
7
  if (projection === '*')
8
8
  return undefined;
9
9
  if (projection &&
@@ -14,10 +14,10 @@ function prepareProjection(dataType, projection, scopes) {
14
14
  const projection_ = typeof projection === 'string' || Array.isArray(projection)
15
15
  ? (0, common_1.parseFieldsProjection)(projection)
16
16
  : projection;
17
- prepare(dataType, out, projection_, scopes);
17
+ prepare(dataType, out, projection_, scope);
18
18
  return Object.keys(out).length ? out : undefined;
19
19
  }
20
- function prepare(dataType, target, projection, scopes) {
20
+ function prepare(dataType, target, projection, scope) {
21
21
  const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
22
22
  const projectionKeys = projection && Object.keys(projection);
23
23
  const projectionKeysSet = new Set(projectionKeys);
@@ -30,7 +30,7 @@ function prepare(dataType, target, projection, scopes) {
30
30
  k = fieldName.toLowerCase();
31
31
  projectionKeysSet.delete(k);
32
32
  /** Ignore if field is not in scope */
33
- if (!field.inScope(scopes))
33
+ if (scope && !field.inScope(scope))
34
34
  continue;
35
35
  const p = projection?.[k];
36
36
  if (
@@ -406,7 +406,9 @@ class MongoEntityService extends mongo_service_js_1.MongoService {
406
406
  }
407
407
  _generatePatch(command, doc) {
408
408
  const patchGenerator = new mongo_patch_generator_js_1.MongoPatchGenerator();
409
- const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc);
409
+ const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc, {
410
+ scope: this._dataTypeScope,
411
+ });
410
412
  command.options = command.options || {};
411
413
  if (arrayFilters) {
412
414
  command.options.arrayFilters = command.options.arrayFilters || [];
@@ -35,7 +35,7 @@ class MongoNestedService extends mongo_service_js_1.MongoService {
35
35
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
36
36
  */
37
37
  get dataType() {
38
- const t = super.dataType.getField(this.fieldName).type;
38
+ const t = super.dataType.getField(this.fieldName, this.scope).type;
39
39
  if (!(t instanceof common_1.ComplexType))
40
40
  throw new common_1.NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
41
41
  return t;
@@ -91,14 +91,11 @@ class MongoService extends core_1.ServiceBase {
91
91
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
92
92
  */
93
93
  get dataType() {
94
- if (this._dataType && this._dataTypeScope !== this.scopes)
94
+ if (this._dataType && this._dataTypeScope !== this.scope)
95
95
  this._dataType = undefined;
96
96
  if (!this._dataType)
97
97
  this._dataType = this.context.documentNode.getComplexType(this._dataType_);
98
- this._dataTypeScope = this.scopes;
99
- this._dataTypeScopes = this.scopes
100
- ? this.scopes?.split(/\s*,\s*/)
101
- : undefined;
98
+ this._dataTypeScope = this.scope;
102
99
  return this._dataType;
103
100
  }
104
101
  /**
@@ -249,7 +246,7 @@ class MongoService extends core_1.ServiceBase {
249
246
  if (validator)
250
247
  return validator;
251
248
  const options = { projection: '*' };
252
- options.scope = this._dataTypeScopes;
249
+ options.scope = this._dataTypeScope;
253
250
  if (operation === 'update') {
254
251
  options.partial = 'deep';
255
252
  options.allowPatchOperators = true;
@@ -269,7 +266,7 @@ class MongoService extends core_1.ServiceBase {
269
266
  const options = {
270
267
  projection: '*',
271
268
  partial: 'deep',
272
- scope: this._dataTypeScopes,
269
+ scope: this._dataTypeScope,
273
270
  };
274
271
  const dataType = this.dataType;
275
272
  validator = dataType.generateCodec('decode', options);
@@ -3,7 +3,7 @@ const FIELD_NAME_PATTERN = /^([-><*:])?(.+)$/;
3
3
  export class MongoPatchGenerator {
4
4
  generatePatch(dataType, doc, options) {
5
5
  const ctx = {};
6
- this._processComplexType(ctx, dataType, options?.currentPath || '', doc);
6
+ this._processComplexType(ctx, dataType, options?.currentPath || '', doc, options?.scope);
7
7
  const update = {};
8
8
  if (ctx.$pull)
9
9
  update.$pull = ctx.$pull;
@@ -18,12 +18,12 @@ export class MongoPatchGenerator {
18
18
  arrayFilters: ctx.arrayFilters,
19
19
  };
20
20
  }
21
- _processComplexType(ctx, dataType, path, input) {
21
+ _processComplexType(ctx, dataType, path, input, scope) {
22
22
  if (input._$push) {
23
- this._processPush(ctx, dataType, path, input._$push);
23
+ this._processPush(ctx, dataType, path, input._$push, scope);
24
24
  }
25
25
  if (input._$pull) {
26
- this._processPull(ctx, dataType, path, input._$pull);
26
+ this._processPull(ctx, dataType, path, input._$pull, scope);
27
27
  }
28
28
  const keys = Object.keys(input);
29
29
  const pathDot = path + (path ? '.' : '');
@@ -39,8 +39,10 @@ export class MongoPatchGenerator {
39
39
  if (!m)
40
40
  continue;
41
41
  key = m[2];
42
+ if (key === '_$push' || key === '_$pull')
43
+ continue;
42
44
  value = input[key];
43
- field = dataType.fields.get(key);
45
+ field = dataType.getField(key, scope);
44
46
  if (!field) {
45
47
  if (dataType.additionalFields) {
46
48
  if (value === null) {
@@ -51,7 +53,7 @@ export class MongoPatchGenerator {
51
53
  ctx.$set = ctx.$set || {};
52
54
  if (dataType.additionalFields instanceof ComplexType) {
53
55
  /** Process nested object */
54
- this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value);
56
+ this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope);
55
57
  continue;
56
58
  }
57
59
  ctx.$set[pathDot + key] = value;
@@ -89,7 +91,7 @@ export class MongoPatchGenerator {
89
91
  [`${arrayFilterName}.${keyField}`]: keyValue,
90
92
  });
91
93
  /** Process each object in array */
92
- this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v);
94
+ this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope);
93
95
  }
94
96
  continue;
95
97
  }
@@ -97,14 +99,14 @@ export class MongoPatchGenerator {
97
99
  if (!(typeof value === 'object'))
98
100
  continue;
99
101
  /** Process nested object */
100
- this._processComplexType(ctx, field.type, pathDot + field.name, value);
102
+ this._processComplexType(ctx, field.type, pathDot + field.name, value, scope);
101
103
  continue;
102
104
  }
103
105
  ctx.$set = ctx.$set || {};
104
106
  ctx.$set[pathDot + field.name] = value;
105
107
  }
106
108
  }
107
- _processPush(ctx, dataType, path, input) {
109
+ _processPush(ctx, dataType, path, input, scope) {
108
110
  let field;
109
111
  let key;
110
112
  let value;
@@ -113,7 +115,7 @@ export class MongoPatchGenerator {
113
115
  let keyField;
114
116
  for (key of keys) {
115
117
  value = input[key];
116
- field = dataType.fields.get(key);
118
+ field = dataType.getField(key, scope);
117
119
  if (!(field && field.isArray))
118
120
  continue;
119
121
  ctx.$push = ctx.$push || {};
@@ -142,7 +144,7 @@ export class MongoPatchGenerator {
142
144
  : value;
143
145
  }
144
146
  }
145
- _processPull(ctx, dataType, path, input) {
147
+ _processPull(ctx, dataType, path, input, scope) {
146
148
  let field;
147
149
  let key;
148
150
  let value;
@@ -151,7 +153,7 @@ export class MongoPatchGenerator {
151
153
  let keyField;
152
154
  for (key of keys) {
153
155
  value = input[key];
154
- field = dataType.fields.get(key);
156
+ field = dataType.getField(key, scope);
155
157
  if (!(field && field.isArray))
156
158
  continue;
157
159
  ctx.$pull = ctx.$pull || {};
@@ -1,5 +1,5 @@
1
1
  import { ComplexType, parseFieldsProjection, } from '@opra/common';
2
- export default function prepareProjection(dataType, projection, scopes) {
2
+ export default function prepareProjection(dataType, projection, scope) {
3
3
  if (projection === '*')
4
4
  return undefined;
5
5
  if (projection &&
@@ -10,10 +10,10 @@ export default function prepareProjection(dataType, projection, scopes) {
10
10
  const projection_ = typeof projection === 'string' || Array.isArray(projection)
11
11
  ? parseFieldsProjection(projection)
12
12
  : projection;
13
- prepare(dataType, out, projection_, scopes);
13
+ prepare(dataType, out, projection_, scope);
14
14
  return Object.keys(out).length ? out : undefined;
15
15
  }
16
- export function prepare(dataType, target, projection, scopes) {
16
+ export function prepare(dataType, target, projection, scope) {
17
17
  const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
18
18
  const projectionKeys = projection && Object.keys(projection);
19
19
  const projectionKeysSet = new Set(projectionKeys);
@@ -26,7 +26,7 @@ export function prepare(dataType, target, projection, scopes) {
26
26
  k = fieldName.toLowerCase();
27
27
  projectionKeysSet.delete(k);
28
28
  /** Ignore if field is not in scope */
29
- if (!field.inScope(scopes))
29
+ if (scope && !field.inScope(scope))
30
30
  continue;
31
31
  const p = projection?.[k];
32
32
  if (
@@ -403,7 +403,9 @@ export class MongoEntityService extends MongoService {
403
403
  }
404
404
  _generatePatch(command, doc) {
405
405
  const patchGenerator = new MongoPatchGenerator();
406
- const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc);
406
+ const { update, arrayFilters } = patchGenerator.generatePatch(this.dataType, doc, {
407
+ scope: this._dataTypeScope,
408
+ });
407
409
  command.options = command.options || {};
408
410
  if (arrayFilters) {
409
411
  command.options.arrayFilters = command.options.arrayFilters || [];
@@ -32,7 +32,7 @@ export class MongoNestedService extends MongoService {
32
32
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
33
33
  */
34
34
  get dataType() {
35
- const t = super.dataType.getField(this.fieldName).type;
35
+ const t = super.dataType.getField(this.fieldName, this.scope).type;
36
36
  if (!(t instanceof ComplexType))
37
37
  throw new NotAcceptableError(`Data type "${t.name}" is not a ComplexType`);
38
38
  return t;
@@ -88,14 +88,11 @@ export class MongoService extends ServiceBase {
88
88
  * @throws {NotAcceptableError} If the data type is not a ComplexType.
89
89
  */
90
90
  get dataType() {
91
- if (this._dataType && this._dataTypeScope !== this.scopes)
91
+ if (this._dataType && this._dataTypeScope !== this.scope)
92
92
  this._dataType = undefined;
93
93
  if (!this._dataType)
94
94
  this._dataType = this.context.documentNode.getComplexType(this._dataType_);
95
- this._dataTypeScope = this.scopes;
96
- this._dataTypeScopes = this.scopes
97
- ? this.scopes?.split(/\s*,\s*/)
98
- : undefined;
95
+ this._dataTypeScope = this.scope;
99
96
  return this._dataType;
100
97
  }
101
98
  /**
@@ -246,7 +243,7 @@ export class MongoService extends ServiceBase {
246
243
  if (validator)
247
244
  return validator;
248
245
  const options = { projection: '*' };
249
- options.scope = this._dataTypeScopes;
246
+ options.scope = this._dataTypeScope;
250
247
  if (operation === 'update') {
251
248
  options.partial = 'deep';
252
249
  options.allowPatchOperators = true;
@@ -266,7 +263,7 @@ export class MongoService extends ServiceBase {
266
263
  const options = {
267
264
  projection: '*',
268
265
  partial: 'deep',
269
- scope: this._dataTypeScopes,
266
+ scope: this._dataTypeScope,
270
267
  };
271
268
  const dataType = this.dataType;
272
269
  validator = dataType.generateCodec('decode', options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.5.0",
3
+ "version": "1.5.1",
4
4
  "description": "Opra MongoDB adapter package",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -10,9 +10,9 @@
10
10
  "valgen": "^5.12.0"
11
11
  },
12
12
  "peerDependencies": {
13
- "@opra/common": "^1.5.0",
14
- "@opra/core": "^1.5.0",
15
- "@opra/http": "^1.5.0",
13
+ "@opra/common": "^1.5.1",
14
+ "@opra/core": "^1.5.1",
15
+ "@opra/http": "^1.5.1",
16
16
  "mongodb": ">= 6.0.0"
17
17
  },
18
18
  "type": "module",
@@ -13,13 +13,14 @@ export declare class MongoPatchGenerator {
13
13
  update: UpdateFilter<T>;
14
14
  arrayFilters?: Record<string, any>[];
15
15
  };
16
- protected _processComplexType(ctx: Context, dataType: ComplexType, path: string, input: any): void;
17
- protected _processPush(ctx: Context, dataType: ComplexType, path: string, input: any): void;
18
- protected _processPull(ctx: Context, dataType: ComplexType, path: string, input: any): void;
16
+ protected _processComplexType(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): void;
17
+ protected _processPush(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): void;
18
+ protected _processPull(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): void;
19
19
  }
20
20
  export declare namespace MongoPatchGenerator {
21
21
  interface Options {
22
22
  currentPath?: string;
23
+ scope?: string;
23
24
  }
24
25
  }
25
26
  export {};
@@ -1,4 +1,4 @@
1
1
  import { ComplexType, FieldsProjection } from '@opra/common';
2
2
  import mongodb, { type Document } from 'mongodb';
3
- export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document | '*', scopes?: string[]): mongodb.Document | undefined;
4
- export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection, scopes?: string[]): void;
3
+ export default function prepareProjection(dataType: ComplexType, projection?: string | string[] | Document | '*', scope?: string): mongodb.Document | undefined;
4
+ export declare function prepare(dataType: ComplexType, target: mongodb.Document, projection?: FieldsProjection, scope?: string): void;
@@ -153,7 +153,6 @@ export interface MongoService {
153
153
  */
154
154
  export declare class MongoService<T extends mongodb.Document = mongodb.Document> extends ServiceBase {
155
155
  protected _dataTypeScope?: string;
156
- protected _dataTypeScopes?: string[];
157
156
  protected _dataType_: Type | string;
158
157
  protected _dataType?: ComplexType;
159
158
  protected _inputCodecs: Record<string, IsObject.Validator<T>>;
@@ -161,7 +160,7 @@ export declare class MongoService<T extends mongodb.Document = mongodb.Document>
161
160
  /**
162
161
  * Defines comma delimited scopes for api document
163
162
  */
164
- scopes?: string;
163
+ scope?: string;
165
164
  /**
166
165
  * Represents the name of a collection in MongoDB
167
166
  */