electrodb 2.10.0 → 2.10.2
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/.prettierignore +112 -0
- package/.prettierrc +11 -0
- package/README.md +55 -47
- package/index.d.ts +5288 -2409
- package/index.js +24 -12
- package/package.json +19 -11
- package/src/clauses.js +1557 -1310
- package/src/client.js +255 -235
- package/src/entity.js +4512 -3737
- package/src/errors.js +39 -28
- package/src/events.js +26 -16
- package/src/filterOperations.js +120 -122
- package/src/filters.js +99 -101
- package/src/operations.js +326 -263
- package/src/schema.js +1825 -1473
- package/src/service.js +1081 -852
- package/src/set.js +22 -26
- package/src/transaction.js +179 -153
- package/src/types.js +260 -264
- package/src/update.js +94 -90
- package/src/updateOperations.js +179 -160
- package/src/util.js +45 -32
- package/src/validations.js +337 -325
- package/src/where.js +146 -124
- package/tsconfig.json +12 -11
package/src/update.js
CHANGED
|
@@ -1,108 +1,112 @@
|
|
|
1
|
-
const { UpdateOperations } = require(
|
|
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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
add(type, expression) {
|
|
33
|
+
this.operations[type].add(expression);
|
|
34
|
+
}
|
|
32
35
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
unadd(type, expression) {
|
|
37
|
+
this.operations[type].delete(expression);
|
|
38
|
+
}
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
constructor(attributes = {}, operations = {}) {
|
|
89
|
+
this.attributes = { ...attributes };
|
|
90
|
+
this.operations = { ...operations };
|
|
91
|
+
}
|
|
88
92
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
110
|
+
UpdateEntity,
|
|
111
|
+
UpdateExpression,
|
|
112
|
+
};
|
package/src/updateOperations.js
CHANGED
|
@@ -1,178 +1,197 @@
|
|
|
1
|
-
const {AttributeTypes, ItemOperations} = require("./types");
|
|
1
|
+
const { AttributeTypes, ItemOperations } = require("./types");
|
|
2
2
|
|
|
3
3
|
const deleteOperations = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
},
|
|
31
|
+
name: {
|
|
32
|
+
canNest: true,
|
|
33
|
+
template: function name(options, attr, path) {
|
|
34
|
+
return path;
|
|
34
35
|
},
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
},
|
|
37
|
+
value: {
|
|
38
|
+
canNest: true,
|
|
39
|
+
template: function value(options, attr, path, value) {
|
|
40
|
+
return value;
|
|
40
41
|
},
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
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
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
190
|
+
},
|
|
191
|
+
del: deleteOperations,
|
|
192
|
+
delete: deleteOperations,
|
|
193
|
+
};
|
|
175
194
|
|
|
176
195
|
module.exports = {
|
|
177
|
-
|
|
178
|
-
}
|
|
196
|
+
UpdateOperations,
|
|
197
|
+
};
|