electrodb 2.9.2 → 2.10.0

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.
@@ -0,0 +1,178 @@
1
+ const {AttributeTypes, ItemOperations} = require("./types");
2
+
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
+ },
19
+ };
20
+
21
+ 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
+ }
28
+ },
29
+ name: {
30
+ canNest: true,
31
+ template: function name(options, attr, path) {
32
+ return path;
33
+ }
34
+ },
35
+ value: {
36
+ canNest: true,
37
+ template: function value(options, attr, path, value) {
38
+ return value;
39
+ }
40
+ },
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
+ }
58
+ },
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 };
95
+ }
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};
123
+ }
124
+ },
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};
146
+ }
147
+ },
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};
170
+ }
171
+ },
172
+ del: deleteOperations,
173
+ delete: deleteOperations
174
+ }
175
+
176
+ module.exports = {
177
+ UpdateOperations
178
+ }
package/src/util.js CHANGED
@@ -8,7 +8,7 @@ function parseJSONPath(path = "") {
8
8
  }
9
9
  path = path.replace(/\[/g, ".");
10
10
  path = path.replace(/\]/g, "");
11
- return path.split(".");
11
+ return path.split(".").filter(part => part !== '');
12
12
  }
13
13
 
14
14
  function genericizeJSONPath(path = "") {