-
-
Notifications
You must be signed in to change notification settings - Fork 361
/
Condition.ts
333 lines (309 loc) · 13.6 KB
/
Condition.ts
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import {Document} from "./Document";
import CustomError = require("./Error");
import utils = require("./utils");
const OR = Symbol("OR");
import {DynamoDB} from "aws-sdk";
import {ObjectType} from "./General";
const isRawConditionObject = (object): boolean => Object.keys(object).length === 3 && ["ExpressionAttributeValues", "ExpressionAttributeNames"].every((item) => Boolean(object[item]) && typeof object[item] === "object");
export type ConditionFunction = (condition: Condition) => Condition;
// TODO: There is a problem where you can have multiple keys in one `ConditionStorageType`, which will cause problems. We need to fix that. Likely be refactoring it so that the key is part of `ConditionsConditionStorageObject`.
type ConditionStorageType = {[key: string]: ConditionsConditionStorageObject} | typeof OR;
type ConditionStorageTypeNested = ConditionStorageType | Array<ConditionStorageTypeNested>;
type ConditionStorageSettingsConditions = ConditionStorageTypeNested[];
// TODO: the return value of the function below is incorrect. We need to add a property to the object that is a required string, where the property/key name is always equal to `settings.conditionString`
type ConditionRequestObjectResult = {ExpressionAttributeNames?: DynamoDB.Types.ExpressionAttributeNameMap; ExpressionAttributeValues?: DynamoDB.Types.ExpressionAttributeValueMap};
interface ConditionComparisonType {
name: ConditionComparisonComparatorName;
typeName: ConditionComparisonComparatorDynamoName;
not?: ConditionComparisonComparatorDynamoName;
multipleArguments?: boolean;
}
enum ConditionComparisonComparatorName {
equals = "eq",
lessThan = "lt",
lessThanEquals = "le",
greaterThan = "gt",
greaterThanEquals = "ge",
beginsWith = "beginsWith",
contains = "contains",
exists = "exists",
in = "in",
between = "between"
}
enum ConditionComparisonComparatorDynamoName {
equals = "EQ",
notEquals = "NE",
lessThan = "LT",
lessThanEquals = "LE",
greaterThan = "GT",
greaterThanEquals = "GE",
beginsWith = "BEGINS_WITH",
contains = "CONTAINS",
notContains = "NOT_CONTAINS",
exists = "EXISTS",
notExists = "NOT_EXISTS",
in = "IN",
between = "BETWEEN"
}
const types: ConditionComparisonType[] = [
{"name": ConditionComparisonComparatorName.equals, "typeName": ConditionComparisonComparatorDynamoName.equals, "not": ConditionComparisonComparatorDynamoName.notEquals},
{"name": ConditionComparisonComparatorName.lessThan, "typeName": ConditionComparisonComparatorDynamoName.lessThan, "not": ConditionComparisonComparatorDynamoName.greaterThanEquals},
{"name": ConditionComparisonComparatorName.lessThanEquals, "typeName": ConditionComparisonComparatorDynamoName.lessThanEquals, "not": ConditionComparisonComparatorDynamoName.greaterThan},
{"name": ConditionComparisonComparatorName.greaterThan, "typeName": ConditionComparisonComparatorDynamoName.greaterThan, "not": ConditionComparisonComparatorDynamoName.lessThanEquals},
{"name": ConditionComparisonComparatorName.greaterThanEquals, "typeName": ConditionComparisonComparatorDynamoName.greaterThanEquals, "not": ConditionComparisonComparatorDynamoName.lessThan},
{"name": ConditionComparisonComparatorName.beginsWith, "typeName": ConditionComparisonComparatorDynamoName.beginsWith},
{"name": ConditionComparisonComparatorName.contains, "typeName": ConditionComparisonComparatorDynamoName.contains, "not": ConditionComparisonComparatorDynamoName.notContains},
{"name": ConditionComparisonComparatorName.exists, "typeName": ConditionComparisonComparatorDynamoName.exists, "not": ConditionComparisonComparatorDynamoName.notExists},
{"name": ConditionComparisonComparatorName.in, "typeName": ConditionComparisonComparatorDynamoName.in},
{"name": ConditionComparisonComparatorName.between, "typeName": ConditionComparisonComparatorDynamoName.between, "multipleArguments": true}
];
export type ConditionInitalizer = Condition | ObjectType | string;
export interface BasicOperators<T = Condition> {
and: () => T;
or: () => T;
not: () => T;
parenthesis: (value: T | ConditionFunction) => T;
group: (value: T | ConditionFunction) => T;
where: (key: string) => T;
filter: (key: string) => T;
attribute: (key: string) => T;
eq: (value: any) => T;
lt: (value: number) => T;
le: (value: number) => T;
gt: (value: number) => T;
ge: (value: number) => T;
beginsWith: (value: any) => T;
contains: (value: any) => T;
exists: (value: any) => T;
in: (value: any) => T;
between: (...values: any[]) => T;
}
export class Condition implements BasicOperators {
settings: {
// TODO: fix this below, it should be a reference to `OR` not Symbol, you are only allowed to pass in OR here, not any other Symbol.
conditions: ConditionStorageSettingsConditions;
pending: {
key?: string;
type?: ConditionComparisonType;
value?: any;
not?: boolean;
};
raw?: ConditionInitalizer;
};
and: () => Condition;
or: () => Condition;
not: () => Condition;
parenthesis: (value: Condition | ConditionFunction) => Condition;
group: (value: Condition | ConditionFunction) => Condition;
where: (key: string) => Condition;
filter: (key: string) => Condition;
attribute: (key: string) => Condition;
eq: (value: any) => Condition;
lt: (value: number) => Condition;
le: (value: number) => Condition;
gt: (value: number) => Condition;
ge: (value: number) => Condition;
beginsWith: (value: any) => Condition;
contains: (value: any) => Condition;
exists: (value: any) => Condition;
in: (value: any) => Condition;
between: (...values: any[]) => Condition;
requestObject: (settings?: ConditionRequestObjectSettings) => ConditionRequestObjectResult;
constructor (object?: ConditionInitalizer) {
if (object instanceof Condition) {
Object.entries(object).forEach((entry) => {
const [key, value] = entry;
this[key] = value;
});
} else {
this.settings = {
"conditions": [],
"pending": {} // represents the pending chain of filter data waiting to be attached to the `conditions` parameter. For example, storing the key before we know what the comparison operator is.
};
if (typeof object === "object") {
if (!isRawConditionObject(object)) {
Object.keys(object).forEach((key) => {
const value = object[key];
const valueType = typeof value === "object" && Object.keys(value).length > 0 ? Object.keys(value)[0] : "eq";
const comparisonType = types.find((item) => item.name === valueType);
if (!comparisonType) {
throw new CustomError.InvalidFilterComparison(`The type: ${valueType} is invalid.`);
}
this.settings.conditions.push({
[key]: {
"type": comparisonType.typeName,
"value": typeof value[valueType] !== "undefined" && value[valueType] !== null ? value[valueType] : value
}
});
});
}
} else if (object) {
this.settings.pending.key = object;
}
}
this.settings.raw = object;
return this;
}
}
interface ConditionsConditionStorageObject {
type: ConditionComparisonComparatorDynamoName;
value: any;
}
function finalizePending (instance: Condition): void {
const pending = instance.settings.pending;
let dynamoNameType: ConditionComparisonComparatorDynamoName;
if (pending.not === true) {
if (!pending.type.not) {
throw new CustomError.InvalidFilterComparison(`${pending.type.typeName} can not follow not()`);
}
dynamoNameType = pending.type.not;
} else {
dynamoNameType = pending.type.typeName;
}
instance.settings.conditions.push({
[pending.key]: {
"type": dynamoNameType,
"value": pending.value
}
});
instance.settings.pending = {};
}
Condition.prototype.parenthesis = Condition.prototype.group = function (this: Condition, value: Condition | ConditionFunction): Condition {
value = typeof value === "function" ? value(new Condition()) : value;
this.settings.conditions.push(value.settings.conditions);
return this;
};
Condition.prototype.or = function (this: Condition): Condition {
this.settings.conditions.push(OR);
return this;
};
Condition.prototype.and = function (this: Condition): Condition {
return this;
};
Condition.prototype.not = function (this: Condition): Condition {
this.settings.pending.not = !this.settings.pending.not;
return this;
};
Condition.prototype.where = Condition.prototype.filter = Condition.prototype.attribute = function (this: Condition, key: string): Condition {
this.settings.pending = {key};
return this;
};
// TODO: I don't think this prototypes are being exposed which is gonna cause a lot of problems with our type definition file. Need to figure out a better way to do this since they aren't defined and are dynamic.
types.forEach((type) => {
Condition.prototype[type.name] = function (this: Condition, ...args: any[]): Condition {
this.settings.pending.value = type.multipleArguments ? args : args[0];
this.settings.pending.type = type;
finalizePending(this);
return this;
};
});
interface ConditionRequestObjectSettings {
conditionString: string;
index?: {
start: number;
set: (newIndex: number) => void;
};
conditionStringType: "array" | "string";
}
Condition.prototype.requestObject = function (this: Condition, settings: ConditionRequestObjectSettings = {"conditionString": "ConditionExpression", "conditionStringType": "string"}): ConditionRequestObjectResult {
if (this.settings.raw && utils.object.equals(Object.keys(this.settings.raw).sort(), [settings.conditionString, "ExpressionAttributeValues", "ExpressionAttributeNames"].sort())) {
return Object.entries((this.settings.raw as ObjectType).ExpressionAttributeValues).reduce((obj, entry) => {
const [key, value] = entry;
// TODO: we should fix this so that we can do `isDynamoItem(value)`
if (!Document.isDynamoObject({"key": value})) {
obj.ExpressionAttributeValues[key] = Document.objectToDynamo(value, {"type": "value"});
}
return obj;
}, this.settings.raw as ObjectType);
} else if (this.settings.conditions.length === 0) {
return {};
}
let index = (settings.index || {}).start || 0;
const setIndex = (i: number): void => {
index = i; (settings.index || {"set": utils.empty_function}).set(i);
};
function main (input: ConditionStorageSettingsConditions): ConditionRequestObjectResult {
return input.reduce((object: ConditionRequestObjectResult, entry: ConditionStorageTypeNested, i: number, arr: any[]) => {
let expression = "";
if (Array.isArray(entry)) {
const result = main(entry);
const newData = utils.merge_objects.main({"combineMethod": "object_combine"})({...result}, {...object});
const returnObject = utils.object.pick(newData, ["ExpressionAttributeNames", "ExpressionAttributeValues"]);
expression = settings.conditionStringType === "array" ? result[settings.conditionString] : `(${result[settings.conditionString]})`;
object = {...object, ...returnObject};
} else if (entry !== OR) {
const [key, condition] = Object.entries(entry)[0];
const {value} = condition;
const keys = {"name": `#a${index}`, "value": `:v${index}`};
setIndex(++index);
const keyParts = key.split(".");
if (keyParts.length === 1) {
object.ExpressionAttributeNames[keys.name] = key;
} else {
keys.name = keyParts.reduce((finalName, part, index) => {
const name = `${keys.name}_${index}`;
object.ExpressionAttributeNames[name] = part;
finalName.push(name);
return finalName;
}, []).join(".");
}
const toDynamo = (value: ObjectType): DynamoDB.AttributeValue => {
return Document.objectToDynamo(value, {"type": "value"});
};
object.ExpressionAttributeValues[keys.value] = toDynamo(value);
switch (condition.type) {
case "EQ":
case "NE":
expression = `${keys.name} ${condition.type === "EQ" ? "=" : "<>"} ${keys.value}`;
break;
case "IN":
delete object.ExpressionAttributeValues[keys.value];
expression = `${keys.name} IN (${value.map((_v: any, i: number) => `${keys.value}_${i + 1}`).join(", ")})`;
value.forEach((valueItem: any, i: number) => {
object.ExpressionAttributeValues[`${keys.value}_${i + 1}`] = toDynamo(valueItem);
});
break;
case "GT":
case "GE":
case "LT":
case "LE":
expression = `${keys.name} ${condition.type.startsWith("G") ? ">" : "<"}${condition.type.endsWith("E") ? "=" : ""} ${keys.value}`;
break;
case "BETWEEN":
expression = `${keys.name} BETWEEN ${keys.value}_1 AND ${keys.value}_2`;
object.ExpressionAttributeValues[`${keys.value}_1`] = toDynamo(value[0]);
object.ExpressionAttributeValues[`${keys.value}_2`] = toDynamo(value[1]);
delete object.ExpressionAttributeValues[keys.value];
break;
case "CONTAINS":
case "NOT_CONTAINS":
expression = `${condition.type === "NOT_CONTAINS" ? "NOT " : ""}contains (${keys.name}, ${keys.value})`;
break;
case "EXISTS":
case "NOT_EXISTS":
expression = `attribute_${condition.type === "NOT_EXISTS" ? "not_" : ""}exists (${keys.name})`;
delete object.ExpressionAttributeValues[keys.value];
break;
case "BEGINS_WITH":
expression = `begins_with (${keys.name}, ${keys.value})`;
break;
}
} else {
return object;
}
const conditionStringNewItems: string[] = [expression];
if (object[settings.conditionString].length > 0) {
conditionStringNewItems.unshift(` ${arr[i - 1] === OR ? "OR" : "AND"} `);
}
conditionStringNewItems.forEach((item) => {
if (typeof object[settings.conditionString] === "string") {
object[settings.conditionString] = `${object[settings.conditionString]}${item}`;
} else {
object[settings.conditionString].push(Array.isArray(item) ? item : item.trim());
}
});
return object;
}, {[settings.conditionString]: settings.conditionStringType === "array" ? [] : "", "ExpressionAttributeNames": {}, "ExpressionAttributeValues": {}});
}
return main(this.settings.conditions);
};