@opra/mongodb 1.5.2 → 1.5.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.
@@ -22,11 +22,14 @@ class MongoPatchGenerator {
22
22
  };
23
23
  }
24
24
  _processComplexType(ctx, dataType, path, input, scope) {
25
+ let result = false;
25
26
  if (input._$push) {
26
- this._processPush(ctx, dataType, path, input._$push, scope);
27
+ result =
28
+ result || this._processPush(ctx, dataType, path, input._$push, scope);
27
29
  }
28
30
  if (input._$pull) {
29
- this._processPull(ctx, dataType, path, input._$pull, scope);
31
+ result =
32
+ result || this._processPull(ctx, dataType, path, input._$pull, scope);
30
33
  }
31
34
  const keys = Object.keys(input);
32
35
  const pathDot = path + (path ? '.' : '');
@@ -45,29 +48,36 @@ class MongoPatchGenerator {
45
48
  if (key === '_$push' || key === '_$pull')
46
49
  continue;
47
50
  value = input[key];
48
- field = dataType.getField(key, scope);
51
+ field = dataType.findField(key, scope);
52
+ if (field && !field.inScope(scope))
53
+ continue;
49
54
  if (!field) {
50
55
  if (dataType.additionalFields) {
51
56
  if (value === null) {
52
57
  ctx.$unset = ctx.$unset || {};
53
58
  ctx.$unset[pathDot + key] = 1;
59
+ result = true;
54
60
  }
55
61
  else {
56
62
  ctx.$set = ctx.$set || {};
57
63
  if (dataType.additionalFields instanceof common_1.ComplexType) {
58
64
  /** Process nested object */
59
- this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope);
65
+ if (this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope)) {
66
+ result = true;
67
+ }
60
68
  continue;
61
69
  }
62
70
  ctx.$set[pathDot + key] = value;
71
+ result = true;
63
72
  }
64
73
  }
65
74
  continue;
66
75
  }
67
- // if (field.readonly) continue;
76
+ // if (field.readonly) continue; todo
68
77
  if (value === null) {
69
78
  ctx.$unset = ctx.$unset || {};
70
79
  ctx.$unset[pathDot + field.name] = 1;
80
+ result = true;
71
81
  continue;
72
82
  }
73
83
  if (field.type instanceof common_1.ComplexType) {
@@ -88,13 +98,15 @@ class MongoPatchGenerator {
88
98
  v = { ...v };
89
99
  /** Remove key field from object */
90
100
  delete v[keyField];
91
- /** Add array filter */
92
- ctx.arrayFilters = ctx.arrayFilters || [];
93
- ctx.arrayFilters.push({
94
- [`${arrayFilterName}.${keyField}`]: keyValue,
95
- });
96
101
  /** Process each object in array */
97
- this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope);
102
+ if (this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope)) {
103
+ result = true;
104
+ /** Add array filter */
105
+ ctx.arrayFilters = ctx.arrayFilters || [];
106
+ ctx.arrayFilters.unshift({
107
+ [`${arrayFilterName}.${keyField}`]: keyValue,
108
+ });
109
+ }
98
110
  }
99
111
  continue;
100
112
  }
@@ -102,14 +114,19 @@ class MongoPatchGenerator {
102
114
  if (!(typeof value === 'object'))
103
115
  continue;
104
116
  /** Process nested object */
105
- this._processComplexType(ctx, field.type, pathDot + field.name, value, scope);
117
+ if (this._processComplexType(ctx, field.type, pathDot + field.name, value, scope)) {
118
+ result = true;
119
+ }
106
120
  continue;
107
121
  }
108
122
  ctx.$set = ctx.$set || {};
109
123
  ctx.$set[pathDot + field.name] = value;
124
+ result = true;
110
125
  }
126
+ return result;
111
127
  }
112
128
  _processPush(ctx, dataType, path, input, scope) {
129
+ let result = false;
113
130
  let field;
114
131
  let key;
115
132
  let value;
@@ -132,12 +149,14 @@ class MongoPatchGenerator {
132
149
  }
133
150
  });
134
151
  ctx.$push[pathDot + key] = { $each: value };
152
+ result = true;
135
153
  }
136
154
  else {
137
155
  if (!value[keyField]) {
138
156
  throw new TypeError(`You must provide a key value of ${field.type.name} for $push operation.`);
139
157
  }
140
158
  ctx.$push[pathDot + key] = value;
159
+ result = true;
141
160
  }
142
161
  }
143
162
  continue;
@@ -145,9 +164,12 @@ class MongoPatchGenerator {
145
164
  ctx.$push[pathDot + key] = Array.isArray(value)
146
165
  ? { $each: value }
147
166
  : value;
167
+ result = true;
148
168
  }
169
+ return result;
149
170
  }
150
171
  _processPull(ctx, dataType, path, input, scope) {
172
+ let result = false;
151
173
  let field;
152
174
  let key;
153
175
  let value;
@@ -169,13 +191,16 @@ class MongoPatchGenerator {
169
191
  [keyField]: Array.isArray(value) ? { $in: value } : value,
170
192
  },
171
193
  };
194
+ result = true;
172
195
  }
173
196
  else {
174
197
  ctx.$pull[pathDot + key] = Array.isArray(value)
175
198
  ? { $in: value }
176
199
  : value;
200
+ result = true;
177
201
  }
178
202
  }
203
+ return result;
179
204
  }
180
205
  }
181
206
  exports.MongoPatchGenerator = MongoPatchGenerator;
@@ -25,13 +25,10 @@ function prepare(dataType, target, projection, scope) {
25
25
  let field;
26
26
  let k;
27
27
  /** Add fields from data type */
28
- for (field of dataType.fields.values()) {
28
+ for (field of dataType.fields(scope)) {
29
29
  fieldName = field.name;
30
30
  k = fieldName.toLowerCase();
31
31
  projectionKeysSet.delete(k);
32
- /** Ignore if field is not in scope */
33
- if (scope && !field.inScope(scope))
34
- continue;
35
32
  const p = projection?.[k];
36
33
  if (
37
34
  /** Ignore if field is omitted */
@@ -242,7 +242,8 @@ class MongoService extends core_1.ServiceBase {
242
242
  * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
243
243
  */
244
244
  _getInputCodec(operation) {
245
- let validator = this._inputCodecs[operation];
245
+ const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
246
+ let validator = this._inputCodecs[cacheKey];
246
247
  if (validator)
247
248
  return validator;
248
249
  const options = {
@@ -255,14 +256,15 @@ class MongoService extends core_1.ServiceBase {
255
256
  }
256
257
  const dataType = this.dataType;
257
258
  validator = dataType.generateCodec('decode', options);
258
- this._inputCodecs[operation] = validator;
259
+ this._inputCodecs[cacheKey] = validator;
259
260
  return validator;
260
261
  }
261
262
  /**
262
263
  * Retrieves the codec.
263
264
  */
264
265
  _getOutputCodec(operation) {
265
- let validator = this._outputCodecs[operation];
266
+ const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
267
+ let validator = this._outputCodecs[cacheKey];
266
268
  if (validator)
267
269
  return validator;
268
270
  const options = {
@@ -272,7 +274,7 @@ class MongoService extends core_1.ServiceBase {
272
274
  };
273
275
  const dataType = this.dataType;
274
276
  validator = dataType.generateCodec('decode', options);
275
- this._outputCodecs[operation] = validator;
277
+ this._outputCodecs[cacheKey] = validator;
276
278
  return validator;
277
279
  }
278
280
  }
@@ -19,11 +19,14 @@ export class MongoPatchGenerator {
19
19
  };
20
20
  }
21
21
  _processComplexType(ctx, dataType, path, input, scope) {
22
+ let result = false;
22
23
  if (input._$push) {
23
- this._processPush(ctx, dataType, path, input._$push, scope);
24
+ result =
25
+ result || this._processPush(ctx, dataType, path, input._$push, scope);
24
26
  }
25
27
  if (input._$pull) {
26
- this._processPull(ctx, dataType, path, input._$pull, scope);
28
+ result =
29
+ result || this._processPull(ctx, dataType, path, input._$pull, scope);
27
30
  }
28
31
  const keys = Object.keys(input);
29
32
  const pathDot = path + (path ? '.' : '');
@@ -42,29 +45,36 @@ export class MongoPatchGenerator {
42
45
  if (key === '_$push' || key === '_$pull')
43
46
  continue;
44
47
  value = input[key];
45
- field = dataType.getField(key, scope);
48
+ field = dataType.findField(key, scope);
49
+ if (field && !field.inScope(scope))
50
+ continue;
46
51
  if (!field) {
47
52
  if (dataType.additionalFields) {
48
53
  if (value === null) {
49
54
  ctx.$unset = ctx.$unset || {};
50
55
  ctx.$unset[pathDot + key] = 1;
56
+ result = true;
51
57
  }
52
58
  else {
53
59
  ctx.$set = ctx.$set || {};
54
60
  if (dataType.additionalFields instanceof ComplexType) {
55
61
  /** Process nested object */
56
- this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope);
62
+ if (this._processComplexType(ctx, dataType.additionalFields, pathDot + key, value, scope)) {
63
+ result = true;
64
+ }
57
65
  continue;
58
66
  }
59
67
  ctx.$set[pathDot + key] = value;
68
+ result = true;
60
69
  }
61
70
  }
62
71
  continue;
63
72
  }
64
- // if (field.readonly) continue;
73
+ // if (field.readonly) continue; todo
65
74
  if (value === null) {
66
75
  ctx.$unset = ctx.$unset || {};
67
76
  ctx.$unset[pathDot + field.name] = 1;
77
+ result = true;
68
78
  continue;
69
79
  }
70
80
  if (field.type instanceof ComplexType) {
@@ -85,13 +95,15 @@ export class MongoPatchGenerator {
85
95
  v = { ...v };
86
96
  /** Remove key field from object */
87
97
  delete v[keyField];
88
- /** Add array filter */
89
- ctx.arrayFilters = ctx.arrayFilters || [];
90
- ctx.arrayFilters.push({
91
- [`${arrayFilterName}.${keyField}`]: keyValue,
92
- });
93
98
  /** Process each object in array */
94
- this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope);
99
+ if (this._processComplexType(ctx, field.type, pathDot + field.name + `.$[${arrayFilterName}]`, v, scope)) {
100
+ result = true;
101
+ /** Add array filter */
102
+ ctx.arrayFilters = ctx.arrayFilters || [];
103
+ ctx.arrayFilters.unshift({
104
+ [`${arrayFilterName}.${keyField}`]: keyValue,
105
+ });
106
+ }
95
107
  }
96
108
  continue;
97
109
  }
@@ -99,14 +111,19 @@ export class MongoPatchGenerator {
99
111
  if (!(typeof value === 'object'))
100
112
  continue;
101
113
  /** Process nested object */
102
- this._processComplexType(ctx, field.type, pathDot + field.name, value, scope);
114
+ if (this._processComplexType(ctx, field.type, pathDot + field.name, value, scope)) {
115
+ result = true;
116
+ }
103
117
  continue;
104
118
  }
105
119
  ctx.$set = ctx.$set || {};
106
120
  ctx.$set[pathDot + field.name] = value;
121
+ result = true;
107
122
  }
123
+ return result;
108
124
  }
109
125
  _processPush(ctx, dataType, path, input, scope) {
126
+ let result = false;
110
127
  let field;
111
128
  let key;
112
129
  let value;
@@ -129,12 +146,14 @@ export class MongoPatchGenerator {
129
146
  }
130
147
  });
131
148
  ctx.$push[pathDot + key] = { $each: value };
149
+ result = true;
132
150
  }
133
151
  else {
134
152
  if (!value[keyField]) {
135
153
  throw new TypeError(`You must provide a key value of ${field.type.name} for $push operation.`);
136
154
  }
137
155
  ctx.$push[pathDot + key] = value;
156
+ result = true;
138
157
  }
139
158
  }
140
159
  continue;
@@ -142,9 +161,12 @@ export class MongoPatchGenerator {
142
161
  ctx.$push[pathDot + key] = Array.isArray(value)
143
162
  ? { $each: value }
144
163
  : value;
164
+ result = true;
145
165
  }
166
+ return result;
146
167
  }
147
168
  _processPull(ctx, dataType, path, input, scope) {
169
+ let result = false;
148
170
  let field;
149
171
  let key;
150
172
  let value;
@@ -166,12 +188,15 @@ export class MongoPatchGenerator {
166
188
  [keyField]: Array.isArray(value) ? { $in: value } : value,
167
189
  },
168
190
  };
191
+ result = true;
169
192
  }
170
193
  else {
171
194
  ctx.$pull[pathDot + key] = Array.isArray(value)
172
195
  ? { $in: value }
173
196
  : value;
197
+ result = true;
174
198
  }
175
199
  }
200
+ return result;
176
201
  }
177
202
  }
@@ -21,13 +21,10 @@ export function prepare(dataType, target, projection, scope) {
21
21
  let field;
22
22
  let k;
23
23
  /** Add fields from data type */
24
- for (field of dataType.fields.values()) {
24
+ for (field of dataType.fields(scope)) {
25
25
  fieldName = field.name;
26
26
  k = fieldName.toLowerCase();
27
27
  projectionKeysSet.delete(k);
28
- /** Ignore if field is not in scope */
29
- if (scope && !field.inScope(scope))
30
- continue;
31
28
  const p = projection?.[k];
32
29
  if (
33
30
  /** Ignore if field is omitted */
@@ -239,7 +239,8 @@ export class MongoService extends ServiceBase {
239
239
  * @param operation - The operation to retrieve the encoder for. Valid values are 'create' and 'update'.
240
240
  */
241
241
  _getInputCodec(operation) {
242
- let validator = this._inputCodecs[operation];
242
+ const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
243
+ let validator = this._inputCodecs[cacheKey];
243
244
  if (validator)
244
245
  return validator;
245
246
  const options = {
@@ -252,14 +253,15 @@ export class MongoService extends ServiceBase {
252
253
  }
253
254
  const dataType = this.dataType;
254
255
  validator = dataType.generateCodec('decode', options);
255
- this._inputCodecs[operation] = validator;
256
+ this._inputCodecs[cacheKey] = validator;
256
257
  return validator;
257
258
  }
258
259
  /**
259
260
  * Retrieves the codec.
260
261
  */
261
262
  _getOutputCodec(operation) {
262
- let validator = this._outputCodecs[operation];
263
+ const cacheKey = operation + (this._dataTypeScope ? ':' + this._dataTypeScope : '');
264
+ let validator = this._outputCodecs[cacheKey];
263
265
  if (validator)
264
266
  return validator;
265
267
  const options = {
@@ -269,7 +271,7 @@ export class MongoService extends ServiceBase {
269
271
  };
270
272
  const dataType = this.dataType;
271
273
  validator = dataType.generateCodec('decode', options);
272
- this._outputCodecs[operation] = validator;
274
+ this._outputCodecs[cacheKey] = validator;
273
275
  return validator;
274
276
  }
275
277
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.5.2",
3
+ "version": "1.5.4",
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.2",
14
- "@opra/core": "^1.5.2",
15
- "@opra/http": "^1.5.2",
13
+ "@opra/common": "^1.5.4",
14
+ "@opra/core": "^1.5.4",
15
+ "@opra/http": "^1.5.4",
16
16
  "mongodb": ">= 6.0.0"
17
17
  },
18
18
  "type": "module",
@@ -13,9 +13,9 @@ 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, 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;
16
+ protected _processComplexType(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): boolean;
17
+ protected _processPush(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): boolean;
18
+ protected _processPull(ctx: Context, dataType: ComplexType, path: string, input: any, scope?: string): boolean;
19
19
  }
20
20
  export declare namespace MongoPatchGenerator {
21
21
  interface Options {