forked from tywalch/electrodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
where.js
169 lines (158 loc) · 4.75 KB
/
where.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { MethodTypes, ExpressionTypes, BuilderTypes } = require("./types");
const {
AttributeOperationProxy,
ExpressionState,
FilterOperations,
} = require("./operations");
const e = require("./errors");
class FilterExpression extends ExpressionState {
constructor(props) {
super(props);
this.expression = "";
this.type = BuilderTypes.filter;
}
_trim(expression) {
if (typeof expression === "string" && expression.length > 0) {
return expression.replace(/\n|\r/g, "").trim();
}
return "";
}
_isEmpty(expression) {
if (typeof expression !== "string") {
throw new Error("Invalid expression value type. Expected type string.");
}
return !expression.replace(/\n|\r|\w/g, "").trim();
}
add(newExpression) {
let expression = "";
let existingExpression = this.expression;
if (
typeof existingExpression === "string" &&
existingExpression.length > 0
) {
newExpression = this._trim(newExpression);
let isEmpty = this._isEmpty(newExpression);
if (isEmpty) {
return existingExpression;
}
let existingNeedsParens =
!existingExpression.startsWith("(") &&
!existingExpression.endsWith(")");
if (existingNeedsParens) {
existingExpression = `(${existingExpression})`;
}
expression = `${existingExpression} AND ${newExpression}`;
} else {
expression = this._trim(newExpression);
}
this.expression = expression;
}
// applies operations without verifying them against known attributes. Used internally for key conditions.
unsafeSet(operation, name, ...values) {
const { template } = FilterOperations[operation] || {};
if (template === undefined) {
throw new Error(
`Invalid operation: "${operation}". Please report this issue via a bug ticket.`,
);
}
const names = this.setName({}, name, name);
const valueExpressions = values.map((value) => this.setValue(name, value));
const condition = template(
{},
names.expression,
names.prop,
...valueExpressions,
);
this.add(condition);
}
build() {
return this.expression;
}
}
class WhereFactory {
constructor(attributes = {}, operations = {}) {
this.attributes = { ...attributes };
this.operations = { ...operations };
}
getExpressionType(methodType) {
switch (methodType) {
case MethodTypes.put:
case MethodTypes.create:
case MethodTypes.update:
case MethodTypes.patch:
case MethodTypes.delete:
case MethodTypes.remove:
case MethodTypes.upsert:
case MethodTypes.get:
case MethodTypes.check:
return ExpressionTypes.ConditionExpression;
default:
return ExpressionTypes.FilterExpression;
}
}
buildClause(cb) {
if (typeof cb !== "function") {
throw new e.ElectroError(
e.ErrorCodes.InvalidWhere,
'Where callback must be of type "function"',
);
}
return (entity, state, ...params) => {
const type = this.getExpressionType(state.query.method);
const builder = state.query.filter[type];
const proxy = new AttributeOperationProxy({
builder,
attributes: this.attributes,
operations: this.operations,
});
const expression = proxy.invokeCallback(cb, ...params);
if (typeof expression !== "string") {
throw new e.ElectroError(
e.ErrorCodes.InvalidWhere,
"Invalid response from where clause callback. Expected return result to be of type string",
);
}
builder.add(expression);
return state;
};
}
injectWhereClauses(clauses = {}, filters = {}) {
let injected = { ...clauses };
let filterParents = Object.entries(injected)
.filter((clause) => {
let [name, { children }] = clause;
return children.find((child) => ["go", "commit"].includes(child));
})
.map(([name]) => name);
let modelFilters = Object.keys(filters);
let filterChildren = [];
for (let [name, filter] of Object.entries(filters)) {
filterChildren.push(name);
injected[name] = {
name,
action: this.buildClause(filter),
children: ["params", "go", "commit", "where", ...modelFilters],
};
}
filterChildren.push("where");
injected["where"] = {
name: "where",
action: (entity, state, fn) => {
return this.buildClause(fn)(entity, state);
},
children: ["params", "go", "commit", "where", ...modelFilters],
};
for (let parent of filterParents) {
injected[parent] = { ...injected[parent] };
injected[parent].children = [
...filterChildren,
...injected[parent].children,
];
}
return injected;
}
}
module.exports = {
WhereFactory,
FilterExpression,
};