@opra/mongodb 1.3.0 → 1.3.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.
@@ -14,12 +14,17 @@ class MongoPatchGenerator {
14
14
  update.$unset = ctx.$unset;
15
15
  if (ctx.$set)
16
16
  update.$set = ctx.$set;
17
+ if (ctx.$push)
18
+ update.$push = ctx.$push;
17
19
  return {
18
20
  update,
19
21
  arrayFilters: ctx.arrayFilters,
20
22
  };
21
23
  }
22
24
  _processComplexType(ctx, dataType, path, input) {
25
+ if (input.$add) {
26
+ this._processAdd(ctx, dataType, path, input.$add);
27
+ }
23
28
  if (input.$remove) {
24
29
  this._processRemove(ctx, dataType, path, input.$remove);
25
30
  }
@@ -96,6 +101,42 @@ class MongoPatchGenerator {
96
101
  ctx.$set[pathDot + field.name] = value;
97
102
  }
98
103
  }
104
+ _processAdd(ctx, dataType, path, input) {
105
+ let field;
106
+ let key;
107
+ let value;
108
+ const pathDot = path + (path ? '.' : '');
109
+ const keys = Object.keys(input);
110
+ let keyField;
111
+ for (key of keys) {
112
+ value = input[key];
113
+ field = dataType.fields.get(key);
114
+ if (!(field && field.isArray))
115
+ continue;
116
+ ctx.$push = ctx.$push || {};
117
+ if (field.type instanceof common_1.ComplexType) {
118
+ keyField = field.keyField || field.type.keyField;
119
+ if (keyField) {
120
+ if (Array.isArray(value)) {
121
+ value.forEach(v => {
122
+ if (!v[keyField]) {
123
+ throw new TypeError(`You must provide a key value of ${field.type.name} for $add operation.`);
124
+ }
125
+ });
126
+ ctx.$push[pathDot + key] = { $each: value };
127
+ }
128
+ else {
129
+ if (!value[keyField]) {
130
+ throw new TypeError(`You must provide a key value of ${field.type.name} for $add operation.`);
131
+ }
132
+ ctx.$push[pathDot + key] = value;
133
+ }
134
+ }
135
+ continue;
136
+ }
137
+ ctx.$push[pathDot + key] = Array.isArray(value) ? { $each: value } : value;
138
+ }
139
+ }
99
140
  _processRemove(ctx, dataType, path, input) {
100
141
  let field;
101
142
  let key;
@@ -11,12 +11,17 @@ export class MongoPatchGenerator {
11
11
  update.$unset = ctx.$unset;
12
12
  if (ctx.$set)
13
13
  update.$set = ctx.$set;
14
+ if (ctx.$push)
15
+ update.$push = ctx.$push;
14
16
  return {
15
17
  update,
16
18
  arrayFilters: ctx.arrayFilters,
17
19
  };
18
20
  }
19
21
  _processComplexType(ctx, dataType, path, input) {
22
+ if (input.$add) {
23
+ this._processAdd(ctx, dataType, path, input.$add);
24
+ }
20
25
  if (input.$remove) {
21
26
  this._processRemove(ctx, dataType, path, input.$remove);
22
27
  }
@@ -93,6 +98,42 @@ export class MongoPatchGenerator {
93
98
  ctx.$set[pathDot + field.name] = value;
94
99
  }
95
100
  }
101
+ _processAdd(ctx, dataType, path, input) {
102
+ let field;
103
+ let key;
104
+ let value;
105
+ const pathDot = path + (path ? '.' : '');
106
+ const keys = Object.keys(input);
107
+ let keyField;
108
+ for (key of keys) {
109
+ value = input[key];
110
+ field = dataType.fields.get(key);
111
+ if (!(field && field.isArray))
112
+ continue;
113
+ ctx.$push = ctx.$push || {};
114
+ if (field.type instanceof ComplexType) {
115
+ keyField = field.keyField || field.type.keyField;
116
+ if (keyField) {
117
+ if (Array.isArray(value)) {
118
+ value.forEach(v => {
119
+ if (!v[keyField]) {
120
+ throw new TypeError(`You must provide a key value of ${field.type.name} for $add operation.`);
121
+ }
122
+ });
123
+ ctx.$push[pathDot + key] = { $each: value };
124
+ }
125
+ else {
126
+ if (!value[keyField]) {
127
+ throw new TypeError(`You must provide a key value of ${field.type.name} for $add operation.`);
128
+ }
129
+ ctx.$push[pathDot + key] = value;
130
+ }
131
+ }
132
+ continue;
133
+ }
134
+ ctx.$push[pathDot + key] = Array.isArray(value) ? { $each: value } : value;
135
+ }
136
+ }
96
137
  _processRemove(ctx, dataType, path, input) {
97
138
  let field;
98
139
  let key;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opra/mongodb",
3
- "version": "1.3.0",
3
+ "version": "1.3.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.3.0",
14
- "@opra/core": "^1.3.0",
15
- "@opra/http": "^1.3.0",
13
+ "@opra/common": "^1.3.1",
14
+ "@opra/core": "^1.3.1",
15
+ "@opra/http": "^1.3.1",
16
16
  "mongodb": ">= 6.0.0"
17
17
  },
18
18
  "type": "module",
@@ -4,6 +4,7 @@ import type { PatchDTO } from 'ts-gems';
4
4
  interface Context {
5
5
  $set?: Record<string, any>;
6
6
  $unset?: Record<string, any>;
7
+ $push?: Record<string, any>;
7
8
  $pull?: Record<string, any>;
8
9
  arrayFilters?: Record<string, any>;
9
10
  }
@@ -13,6 +14,7 @@ export declare class MongoPatchGenerator {
13
14
  arrayFilters?: Record<string, any>;
14
15
  };
15
16
  protected _processComplexType(ctx: Context, dataType: ComplexType, path: string, input: any): void;
17
+ protected _processAdd(ctx: Context, dataType: ComplexType, path: string, input: any): void;
16
18
  protected _processRemove(ctx: Context, dataType: ComplexType, path: string, input: any): void;
17
19
  }
18
20
  export declare namespace MongoPatchGenerator {