electrodb 2.10.0 → 2.10.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.
package/src/update.js CHANGED
@@ -1,108 +1,112 @@
1
- const { UpdateOperations } = require('./updateOperations');
2
- const {AttributeOperationProxy, ExpressionState} = require("./operations");
3
- const {ItemOperations, BuilderTypes} = require("./types");
1
+ const { UpdateOperations } = require("./updateOperations");
2
+ const { AttributeOperationProxy, ExpressionState } = require("./operations");
3
+ const { ItemOperations, BuilderTypes } = require("./types");
4
4
 
5
5
  class UpdateExpression extends ExpressionState {
6
- constructor(props = {}) {
7
- super({...props});
8
- this.operations = {
9
- set: new Set(),
10
- remove: new Set(),
11
- add: new Set(),
12
- subtract: new Set(),
13
- delete: new Set(),
14
- };
15
- this.composites = {};
16
- this.seen = new Map();
17
- this.type = BuilderTypes.update;
18
- }
19
- addComposite(attrName, value) {
20
- if (value !== undefined) {
21
- if (this.composites[attrName] === undefined || this.composites[attrName] === value) {
22
- this.composites[attrName] = value;
23
- return true;
24
- }
25
- }
26
- return false;
6
+ constructor(props = {}) {
7
+ super({ ...props });
8
+ this.operations = {
9
+ set: new Set(),
10
+ remove: new Set(),
11
+ add: new Set(),
12
+ subtract: new Set(),
13
+ delete: new Set(),
14
+ };
15
+ this.composites = {};
16
+ this.seen = new Map();
17
+ this.type = BuilderTypes.update;
18
+ }
19
+ addComposite(attrName, value) {
20
+ if (value !== undefined) {
21
+ if (
22
+ this.composites[attrName] === undefined ||
23
+ this.composites[attrName] === value
24
+ ) {
25
+ this.composites[attrName] = value;
26
+ return true;
27
+ }
27
28
  }
29
+ return false;
30
+ }
28
31
 
29
- add(type, expression) {
30
- this.operations[type].add(expression);
31
- }
32
+ add(type, expression) {
33
+ this.operations[type].add(expression);
34
+ }
32
35
 
33
- unadd(type, expression) {
34
- this.operations[type].delete(expression);
35
- }
36
+ unadd(type, expression) {
37
+ this.operations[type].delete(expression);
38
+ }
36
39
 
37
- set(name, value, operation = ItemOperations.set, attribute) {
38
- let operationToApply = operation;
39
- if (operation === ItemOperations.ifNotExists) {
40
- operationToApply = ItemOperations.set;
41
- }
42
- const seen = this.seen.get(name);
43
- let n;
44
- let v;
45
- if (seen) {
46
- n = seen.name;
47
- v = seen.value;
48
- this.unadd(operationToApply, seen.expression);
49
-
50
- } else {
51
- n = this.setName({}, name, name);
52
- v = this.setValue(name, value);
53
- }
54
- let expression = `${n.prop} = ${v}`;
55
- if (operation === ItemOperations.ifNotExists) {
56
- expression = `${n.prop} = if_not_exists(${n.prop}, ${v})`;
57
- }
58
- this.seen.set(name, {
59
- name: n,
60
- value: v,
61
- expression,
62
- });
63
- this.add(operationToApply, expression);
40
+ set(name, value, operation = ItemOperations.set, attribute) {
41
+ let operationToApply = operation;
42
+ if (operation === ItemOperations.ifNotExists) {
43
+ operationToApply = ItemOperations.set;
64
44
  }
65
-
66
- remove(name) {
67
- const n = this.setName({}, name, name);
68
- this.add(ItemOperations.remove, `${n.prop}`);
45
+ const seen = this.seen.get(name);
46
+ let n;
47
+ let v;
48
+ if (seen) {
49
+ n = seen.name;
50
+ v = seen.value;
51
+ this.unadd(operationToApply, seen.expression);
52
+ } else {
53
+ n = this.setName({}, name, name);
54
+ v = this.setValue(name, value);
69
55
  }
56
+ let expression = `${n.prop} = ${v}`;
57
+ if (operation === ItemOperations.ifNotExists) {
58
+ expression = `${n.prop} = if_not_exists(${n.prop}, ${v})`;
59
+ }
60
+ this.seen.set(name, {
61
+ name: n,
62
+ value: v,
63
+ expression,
64
+ });
65
+ this.add(operationToApply, expression);
66
+ }
67
+
68
+ remove(name) {
69
+ const n = this.setName({}, name, name);
70
+ this.add(ItemOperations.remove, `${n.prop}`);
71
+ }
70
72
 
71
- build() {
72
- let expressions = [];
73
- for (const type of Object.keys(this.operations)) {
74
- const operations = this.operations[type];
75
- if (operations.size > 0) {
76
- expressions.push(`${type.toUpperCase()} ${Array.from(operations).join(", ")}`);
77
- }
78
- }
79
- return expressions.join(" ");
73
+ build() {
74
+ let expressions = [];
75
+ for (const type of Object.keys(this.operations)) {
76
+ const operations = this.operations[type];
77
+ if (operations.size > 0) {
78
+ expressions.push(
79
+ `${type.toUpperCase()} ${Array.from(operations).join(", ")}`,
80
+ );
81
+ }
80
82
  }
83
+ return expressions.join(" ");
84
+ }
81
85
  }
82
86
 
83
87
  class UpdateEntity {
84
- constructor(attributes = {}, operations = {}) {
85
- this.attributes = {...attributes};
86
- this.operations = {...operations};
87
- }
88
+ constructor(attributes = {}, operations = {}) {
89
+ this.attributes = { ...attributes };
90
+ this.operations = { ...operations };
91
+ }
88
92
 
89
- buildCallbackHandler(entity, state) {
90
- const proxy = new AttributeOperationProxy({
91
- builder: state.query.update,
92
- attributes: this.attributes,
93
- operations: this.operations,
94
- });
93
+ buildCallbackHandler(entity, state) {
94
+ const proxy = new AttributeOperationProxy({
95
+ builder: state.query.update,
96
+ attributes: this.attributes,
97
+ operations: this.operations,
98
+ });
95
99
 
96
- return (cb, ...params) => {
97
- if (typeof cb !== "function") {
98
- throw new Error('Update Callback must be of type "function"');
99
- }
100
- proxy.invokeCallback(cb, ...params);
101
- }
102
- }
100
+ return (cb, ...params) => {
101
+ if (typeof cb !== "function") {
102
+ throw new Error('Update Callback must be of type "function"');
103
+ }
104
+ proxy.invokeCallback(cb, ...params);
105
+ };
106
+ }
103
107
  }
104
108
 
105
109
  module.exports = {
106
- UpdateEntity,
107
- UpdateExpression
108
- }
110
+ UpdateEntity,
111
+ UpdateExpression,
112
+ };
@@ -1,178 +1,197 @@
1
- const {AttributeTypes, ItemOperations} = require("./types");
1
+ const { AttributeTypes, ItemOperations } = require("./types");
2
2
 
3
3
  const deleteOperations = {
4
- canNest: false,
5
- template: function del(options, attr, path, value) {
6
- let operation = "";
7
- let expression = "";
8
- switch(attr.type) {
9
- case AttributeTypes.any:
10
- case AttributeTypes.set:
11
- operation = ItemOperations.delete;
12
- expression = `${path} ${value}`;
13
- break;
14
- default:
15
- throw new Error(`Invalid Update Attribute Operation: "DELETE" Operation can only be performed on attributes with type "set" or "any".`);
16
- }
17
- return { operation, expression };
18
- },
4
+ canNest: false,
5
+ template: function del(options, attr, path, value) {
6
+ let operation = "";
7
+ let expression = "";
8
+ switch (attr.type) {
9
+ case AttributeTypes.any:
10
+ case AttributeTypes.set:
11
+ operation = ItemOperations.delete;
12
+ expression = `${path} ${value}`;
13
+ break;
14
+ default:
15
+ throw new Error(
16
+ `Invalid Update Attribute Operation: "DELETE" Operation can only be performed on attributes with type "set" or "any".`,
17
+ );
18
+ }
19
+ return { operation, expression };
20
+ },
19
21
  };
20
22
 
21
23
  const UpdateOperations = {
22
- ifNotExists: {
23
- template: function if_not_exists(options, attr, path, value) {
24
- const operation = ItemOperations.set;
25
- const expression = `${path} = if_not_exists(${path}, ${value})`;
26
- return {operation, expression};
27
- }
24
+ ifNotExists: {
25
+ template: function if_not_exists(options, attr, path, value) {
26
+ const operation = ItemOperations.set;
27
+ const expression = `${path} = if_not_exists(${path}, ${value})`;
28
+ return { operation, expression };
28
29
  },
29
- name: {
30
- canNest: true,
31
- template: function name(options, attr, path) {
32
- return path;
33
- }
30
+ },
31
+ name: {
32
+ canNest: true,
33
+ template: function name(options, attr, path) {
34
+ return path;
34
35
  },
35
- value: {
36
- canNest: true,
37
- template: function value(options, attr, path, value) {
38
- return value;
39
- }
36
+ },
37
+ value: {
38
+ canNest: true,
39
+ template: function value(options, attr, path, value) {
40
+ return value;
40
41
  },
41
- append: {
42
- canNest: false,
43
- template: function append(options, attr, path, value) {
44
- let operation = "";
45
- let expression = "";
46
- switch(attr.type) {
47
- case AttributeTypes.any:
48
- case AttributeTypes.list:
49
- const defaultValue = options.createValue('default_value', []);
50
- expression = `${path} = list_append(if_not_exists(${path}, ${defaultValue}), ${value})`;
51
- operation = ItemOperations.set;
52
- break;
53
- default:
54
- throw new Error(`Invalid Update Attribute Operation: "APPEND" Operation can only be performed on attributes with type "list" or "any".`);
55
- }
56
- return {operation, expression};
57
- }
42
+ },
43
+ append: {
44
+ canNest: false,
45
+ template: function append(options, attr, path, value) {
46
+ let operation = "";
47
+ let expression = "";
48
+ switch (attr.type) {
49
+ case AttributeTypes.any:
50
+ case AttributeTypes.list:
51
+ const defaultValue = options.createValue("default_value", []);
52
+ expression = `${path} = list_append(if_not_exists(${path}, ${defaultValue}), ${value})`;
53
+ operation = ItemOperations.set;
54
+ break;
55
+ default:
56
+ throw new Error(
57
+ `Invalid Update Attribute Operation: "APPEND" Operation can only be performed on attributes with type "list" or "any".`,
58
+ );
59
+ }
60
+ return { operation, expression };
58
61
  },
59
- add: {
60
- canNest: false,
61
- template: function add(options, attr, path, value, defaultValue) {
62
- let operation = "";
63
- let expression = "";
64
- let type = attr.type;
65
- if (type === AttributeTypes.any) {
66
- type = typeof value === 'number'
67
- ? AttributeTypes.number
68
- : AttributeTypes.any;
69
- }
70
- switch(type) {
71
- case AttributeTypes.any:
72
- case AttributeTypes.set: {
73
- operation = ItemOperations.add;
74
- expression = `${path} ${value}`;
75
- break;
76
- }
77
- case AttributeTypes.number: {
78
- if (options.nestedValue) {
79
- operation = ItemOperations.set;
80
- expression = `${path} = ${path} + ${value}`;
81
- } else if (defaultValue !== undefined) {
82
- // const defaultValueName = options.createValue(`default_value`, defaultValue)
83
- operation = ItemOperations.set;
84
- expression = `${path} = (if_not_exists(${path}, ${defaultValue}) + ${value})`
85
- } else {
86
- operation = ItemOperations.add;
87
- expression = `${path} ${value}`;
88
- }
89
- break;
90
- }
91
- default:
92
- throw new Error(`Invalid Update Attribute Operation: "ADD" Operation can only be performed on attributes with type "number", "set", or "any".`);
93
- }
94
- return { operation, expression };
62
+ },
63
+ add: {
64
+ canNest: false,
65
+ template: function add(options, attr, path, value, defaultValue) {
66
+ let operation = "";
67
+ let expression = "";
68
+ let type = attr.type;
69
+ if (type === AttributeTypes.any) {
70
+ type =
71
+ typeof value === "number"
72
+ ? AttributeTypes.number
73
+ : AttributeTypes.any;
74
+ }
75
+ switch (type) {
76
+ case AttributeTypes.any:
77
+ case AttributeTypes.set: {
78
+ operation = ItemOperations.add;
79
+ expression = `${path} ${value}`;
80
+ break;
95
81
  }
96
- },
97
- subtract: {
98
- canNest: false,
99
- template: function subtract(options, attr, path, value, defaultValue = 0) {
100
- let operation = "";
101
- let expression = "";
102
- switch(attr.type) {
103
- case AttributeTypes.any:
104
- case AttributeTypes.number: {
105
- let resolvedDefaultValue;
106
- if (typeof defaultValue === 'string' && defaultValue.startsWith(':')) {
107
- resolvedDefaultValue = defaultValue;
108
- } else if (defaultValue !== undefined) {
109
- resolvedDefaultValue = options.createValue('default_value', defaultValue);
110
- } else {
111
- resolvedDefaultValue = options.createValue('default_value', 0);
112
- }
113
- // const defaultValuePath = options.createValue('default_value', resolvedDefaultValue);
114
- operation = ItemOperations.set;
115
- expression = `${path} = (if_not_exists(${path}, ${resolvedDefaultValue}) - ${value})`;
116
- break;
117
- }
118
- default:
119
- throw new Error(`Invalid Update Attribute Operation: "SUBTRACT" Operation can only be performed on attributes with type "number" or "any".`);
120
- }
121
-
122
- return {operation, expression};
82
+ case AttributeTypes.number: {
83
+ if (options.nestedValue) {
84
+ operation = ItemOperations.set;
85
+ expression = `${path} = ${path} + ${value}`;
86
+ } else if (defaultValue !== undefined) {
87
+ // const defaultValueName = options.createValue(`default_value`, defaultValue)
88
+ operation = ItemOperations.set;
89
+ expression = `${path} = (if_not_exists(${path}, ${defaultValue}) + ${value})`;
90
+ } else {
91
+ operation = ItemOperations.add;
92
+ expression = `${path} ${value}`;
93
+ }
94
+ break;
123
95
  }
96
+ default:
97
+ throw new Error(
98
+ `Invalid Update Attribute Operation: "ADD" Operation can only be performed on attributes with type "number", "set", or "any".`,
99
+ );
100
+ }
101
+ return { operation, expression };
124
102
  },
125
- set: {
126
- canNest: false,
127
- template: function set(options, attr, path, value) {
128
- let operation = "";
129
- let expression = "";
130
- switch(attr.type) {
131
- case AttributeTypes.set:
132
- case AttributeTypes.list:
133
- case AttributeTypes.map:
134
- case AttributeTypes.enum:
135
- case AttributeTypes.string:
136
- case AttributeTypes.number:
137
- case AttributeTypes.boolean:
138
- case AttributeTypes.any:
139
- operation = ItemOperations.set;
140
- expression = `${path} = ${value}`;
141
- break;
142
- default:
143
- throw new Error(`Invalid Update Attribute Operation: "SET" Operation can only be performed on attributes with type "list", "map", "string", "number", "boolean", or "any".`);
144
- }
145
- return {operation, expression};
103
+ },
104
+ subtract: {
105
+ canNest: false,
106
+ template: function subtract(options, attr, path, value, defaultValue = 0) {
107
+ let operation = "";
108
+ let expression = "";
109
+ switch (attr.type) {
110
+ case AttributeTypes.any:
111
+ case AttributeTypes.number: {
112
+ let resolvedDefaultValue;
113
+ if (
114
+ typeof defaultValue === "string" &&
115
+ defaultValue.startsWith(":")
116
+ ) {
117
+ resolvedDefaultValue = defaultValue;
118
+ } else if (defaultValue !== undefined) {
119
+ resolvedDefaultValue = options.createValue(
120
+ "default_value",
121
+ defaultValue,
122
+ );
123
+ } else {
124
+ resolvedDefaultValue = options.createValue("default_value", 0);
125
+ }
126
+ // const defaultValuePath = options.createValue('default_value', resolvedDefaultValue);
127
+ operation = ItemOperations.set;
128
+ expression = `${path} = (if_not_exists(${path}, ${resolvedDefaultValue}) - ${value})`;
129
+ break;
146
130
  }
131
+ default:
132
+ throw new Error(
133
+ `Invalid Update Attribute Operation: "SUBTRACT" Operation can only be performed on attributes with type "number" or "any".`,
134
+ );
135
+ }
136
+
137
+ return { operation, expression };
147
138
  },
148
- remove: {
149
- canNest: false,
150
- template: function remove(options, attr, ...paths) {
151
- let operation = "";
152
- let expression = "";
153
- switch(attr.type) {
154
- case AttributeTypes.set:
155
- case AttributeTypes.any:
156
- case AttributeTypes.list:
157
- case AttributeTypes.map:
158
- case AttributeTypes.string:
159
- case AttributeTypes.number:
160
- case AttributeTypes.boolean:
161
- case AttributeTypes.enum:
162
- operation = ItemOperations.remove;
163
- expression = paths.join(", ");
164
- break;
165
- default: {
166
- throw new Error(`Invalid Update Attribute Operation: "REMOVE" Operation can only be performed on attributes with type "map", "list", "string", "number", "boolean", or "any".`);
167
- }
168
- }
169
- return {operation, expression};
139
+ },
140
+ set: {
141
+ canNest: false,
142
+ template: function set(options, attr, path, value) {
143
+ let operation = "";
144
+ let expression = "";
145
+ switch (attr.type) {
146
+ case AttributeTypes.set:
147
+ case AttributeTypes.list:
148
+ case AttributeTypes.map:
149
+ case AttributeTypes.enum:
150
+ case AttributeTypes.string:
151
+ case AttributeTypes.number:
152
+ case AttributeTypes.boolean:
153
+ case AttributeTypes.any:
154
+ operation = ItemOperations.set;
155
+ expression = `${path} = ${value}`;
156
+ break;
157
+ default:
158
+ throw new Error(
159
+ `Invalid Update Attribute Operation: "SET" Operation can only be performed on attributes with type "list", "map", "string", "number", "boolean", or "any".`,
160
+ );
161
+ }
162
+ return { operation, expression };
163
+ },
164
+ },
165
+ remove: {
166
+ canNest: false,
167
+ template: function remove(options, attr, ...paths) {
168
+ let operation = "";
169
+ let expression = "";
170
+ switch (attr.type) {
171
+ case AttributeTypes.set:
172
+ case AttributeTypes.any:
173
+ case AttributeTypes.list:
174
+ case AttributeTypes.map:
175
+ case AttributeTypes.string:
176
+ case AttributeTypes.number:
177
+ case AttributeTypes.boolean:
178
+ case AttributeTypes.enum:
179
+ operation = ItemOperations.remove;
180
+ expression = paths.join(", ");
181
+ break;
182
+ default: {
183
+ throw new Error(
184
+ `Invalid Update Attribute Operation: "REMOVE" Operation can only be performed on attributes with type "map", "list", "string", "number", "boolean", or "any".`,
185
+ );
170
186
  }
187
+ }
188
+ return { operation, expression };
171
189
  },
172
- del: deleteOperations,
173
- delete: deleteOperations
174
- }
190
+ },
191
+ del: deleteOperations,
192
+ delete: deleteOperations,
193
+ };
175
194
 
176
195
  module.exports = {
177
- UpdateOperations
178
- }
196
+ UpdateOperations,
197
+ };