@webiny/db-dynamodb 0.0.0-ee-vpcs.549378cf03
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/BatchProcess.d.ts +47 -0
- package/BatchProcess.js +198 -0
- package/BatchProcess.js.map +1 -0
- package/DynamoDbDriver.d.ts +35 -0
- package/DynamoDbDriver.js +274 -0
- package/DynamoDbDriver.js.map +1 -0
- package/LICENSE +21 -0
- package/QueryGenerator.d.ts +21 -0
- package/QueryGenerator.js +78 -0
- package/QueryGenerator.js.map +1 -0
- package/README.md +67 -0
- package/index.d.ts +2 -0
- package/index.js +15 -0
- package/index.js.map +1 -0
- package/operators/comparison/beginsWith.d.ts +3 -0
- package/operators/comparison/beginsWith.js +24 -0
- package/operators/comparison/beginsWith.js.map +1 -0
- package/operators/comparison/between.d.ts +3 -0
- package/operators/comparison/between.js +30 -0
- package/operators/comparison/between.js.map +1 -0
- package/operators/comparison/eq.d.ts +3 -0
- package/operators/comparison/eq.js +34 -0
- package/operators/comparison/eq.js.map +1 -0
- package/operators/comparison/gt.d.ts +3 -0
- package/operators/comparison/gt.js +24 -0
- package/operators/comparison/gt.js.map +1 -0
- package/operators/comparison/gte.d.ts +3 -0
- package/operators/comparison/gte.js +24 -0
- package/operators/comparison/gte.js.map +1 -0
- package/operators/comparison/lt.d.ts +3 -0
- package/operators/comparison/lt.js +24 -0
- package/operators/comparison/lt.js.map +1 -0
- package/operators/comparison/lte.d.ts +3 -0
- package/operators/comparison/lte.js +24 -0
- package/operators/comparison/lte.js.map +1 -0
- package/operators/index.d.ts +12 -0
- package/operators/index.js +39 -0
- package/operators/index.js.map +1 -0
- package/operators/logical/and.d.ts +3 -0
- package/operators/logical/and.js +63 -0
- package/operators/logical/and.js.map +1 -0
- package/operators/logical/or.d.ts +3 -0
- package/operators/logical/or.js +63 -0
- package/operators/logical/or.js.map +1 -0
- package/package.json +46 -0
- package/plugins/definitions/AttributePlugin.d.ts +20 -0
- package/plugins/definitions/AttributePlugin.js +61 -0
- package/plugins/definitions/AttributePlugin.js.map +1 -0
- package/plugins/definitions/DateTimeTransformPlugin.d.ts +4 -0
- package/plugins/definitions/DateTimeTransformPlugin.js +54 -0
- package/plugins/definitions/DateTimeTransformPlugin.js.map +1 -0
- package/plugins/definitions/FieldPathPlugin.d.ts +22 -0
- package/plugins/definitions/FieldPathPlugin.js +46 -0
- package/plugins/definitions/FieldPathPlugin.js.map +1 -0
- package/plugins/definitions/FieldPlugin.d.ts +34 -0
- package/plugins/definitions/FieldPlugin.js +73 -0
- package/plugins/definitions/FieldPlugin.js.map +1 -0
- package/plugins/definitions/NumberTransformPlugin.d.ts +4 -0
- package/plugins/definitions/NumberTransformPlugin.js +45 -0
- package/plugins/definitions/NumberTransformPlugin.js.map +1 -0
- package/plugins/definitions/TimeTransformPlugin.d.ts +4 -0
- package/plugins/definitions/TimeTransformPlugin.js +61 -0
- package/plugins/definitions/TimeTransformPlugin.js.map +1 -0
- package/plugins/definitions/ValueFilterPlugin.d.ts +20 -0
- package/plugins/definitions/ValueFilterPlugin.js +49 -0
- package/plugins/definitions/ValueFilterPlugin.js.map +1 -0
- package/plugins/definitions/ValueTransformPlugin.d.ts +22 -0
- package/plugins/definitions/ValueTransformPlugin.js +50 -0
- package/plugins/definitions/ValueTransformPlugin.js.map +1 -0
- package/plugins/definitions/assignFields.d.ts +1 -0
- package/plugins/definitions/assignFields.js +37 -0
- package/plugins/definitions/assignFields.js.map +1 -0
- package/plugins/filters/andIn.d.ts +3 -0
- package/plugins/filters/andIn.js +36 -0
- package/plugins/filters/andIn.js.map +1 -0
- package/plugins/filters/between.d.ts +3 -0
- package/plugins/filters/between.js +37 -0
- package/plugins/filters/between.js.map +1 -0
- package/plugins/filters/contains.d.ts +3 -0
- package/plugins/filters/contains.js +33 -0
- package/plugins/filters/contains.js.map +1 -0
- package/plugins/filters/eq.d.ts +3 -0
- package/plugins/filters/eq.js +34 -0
- package/plugins/filters/eq.js.map +1 -0
- package/plugins/filters/fuzzy.d.ts +3 -0
- package/plugins/filters/fuzzy.js +31 -0
- package/plugins/filters/fuzzy.js.map +1 -0
- package/plugins/filters/gt.d.ts +3 -0
- package/plugins/filters/gt.js +21 -0
- package/plugins/filters/gt.js.map +1 -0
- package/plugins/filters/gte.d.ts +3 -0
- package/plugins/filters/gte.js +21 -0
- package/plugins/filters/gte.js.map +1 -0
- package/plugins/filters/in.d.ts +3 -0
- package/plugins/filters/in.js +36 -0
- package/plugins/filters/in.js.map +1 -0
- package/plugins/filters/index.d.ts +2 -0
- package/plugins/filters/index.js +34 -0
- package/plugins/filters/index.js.map +1 -0
- package/plugins/filters/lt.d.ts +3 -0
- package/plugins/filters/lt.js +21 -0
- package/plugins/filters/lt.js.map +1 -0
- package/plugins/filters/lte.d.ts +3 -0
- package/plugins/filters/lte.js +21 -0
- package/plugins/filters/lte.js.map +1 -0
- package/plugins/filters/startsWith.d.ts +3 -0
- package/plugins/filters/startsWith.js +34 -0
- package/plugins/filters/startsWith.js.map +1 -0
- package/plugins/index.d.ts +2 -0
- package/plugins/index.js +19 -0
- package/plugins/index.js.map +1 -0
- package/statements/createKeyConditionExpressionArgs.d.ts +15 -0
- package/statements/createKeyConditionExpressionArgs.js +52 -0
- package/statements/createKeyConditionExpressionArgs.js.map +1 -0
- package/statements/processStatement.d.ts +3 -0
- package/statements/processStatement.js +42 -0
- package/statements/processStatement.js.map +1 -0
- package/types.d.ts +58 -0
- package/types.js +5 -0
- package/types.js.map +1 -0
- package/utils/attributes.d.ts +10 -0
- package/utils/attributes.js +31 -0
- package/utils/attributes.js.map +1 -0
- package/utils/batchRead.d.ts +15 -0
- package/utils/batchRead.js +89 -0
- package/utils/batchRead.js.map +1 -0
- package/utils/batchWrite.d.ts +17 -0
- package/utils/batchWrite.js +30 -0
- package/utils/batchWrite.js.map +1 -0
- package/utils/cleanup.d.ts +3 -0
- package/utils/cleanup.js +45 -0
- package/utils/cleanup.js.map +1 -0
- package/utils/cursor.d.ts +2 -0
- package/utils/cursor.js +26 -0
- package/utils/cursor.js.map +1 -0
- package/utils/filter.d.ts +12 -0
- package/utils/filter.js +213 -0
- package/utils/filter.js.map +1 -0
- package/utils/get.d.ts +17 -0
- package/utils/get.js +30 -0
- package/utils/get.js.map +1 -0
- package/utils/listResponse.d.ts +13 -0
- package/utils/listResponse.js +33 -0
- package/utils/listResponse.js.map +1 -0
- package/utils/query.d.ts +32 -0
- package/utils/query.js +119 -0
- package/utils/query.js.map +1 -0
- package/utils/sort.d.ts +17 -0
- package/utils/sort.js +91 -0
- package/utils/sort.js.map +1 -0
- package/utils/table.d.ts +7 -0
- package/utils/table.js +27 -0
- package/utils/table.js.map +1 -0
package/utils/filter.js
ADDED
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.filterItems = filterItems;
|
|
9
|
+
|
|
10
|
+
var _dotProp = _interopRequireDefault(require("dot-prop"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
var _ValueFilterPlugin = require("../plugins/definitions/ValueFilterPlugin");
|
|
15
|
+
|
|
16
|
+
const getMappedPlugins = params => {
|
|
17
|
+
return params.plugins.byType(params.type).reduce((plugins, plugin) => {
|
|
18
|
+
/**
|
|
19
|
+
* We expect op to be a string, that is why we cast.
|
|
20
|
+
*/
|
|
21
|
+
const op = plugin[params.property];
|
|
22
|
+
plugins[op] = plugin;
|
|
23
|
+
return plugins;
|
|
24
|
+
}, {});
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const extractWhereArgs = key => {
|
|
28
|
+
const result = key.split("_");
|
|
29
|
+
const field = result.shift();
|
|
30
|
+
const rawOp = result.length === 0 ? "eq" : result.join("_");
|
|
31
|
+
/**
|
|
32
|
+
* When rawOp is not, it means it is equal negated so just return that.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
if (rawOp === "not") {
|
|
36
|
+
return {
|
|
37
|
+
field,
|
|
38
|
+
operation: "eq",
|
|
39
|
+
negate: true
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const negate = rawOp.match("not_") !== null;
|
|
44
|
+
const operation = rawOp.replace("not_", "");
|
|
45
|
+
return {
|
|
46
|
+
field,
|
|
47
|
+
operation,
|
|
48
|
+
negate
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const findFilterPlugin = (plugins, operation) => {
|
|
53
|
+
if (plugins[operation]) {
|
|
54
|
+
return plugins[operation];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
throw new _error.default(`Missing filter plugin definition.`, "FILTER_PLUGIN_ERROR", {
|
|
58
|
+
operation
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const multiSearchFieldOperations = ["contains", "fuzzy"];
|
|
63
|
+
|
|
64
|
+
const createFilters = params => {
|
|
65
|
+
const {
|
|
66
|
+
plugins,
|
|
67
|
+
where,
|
|
68
|
+
fields
|
|
69
|
+
} = params;
|
|
70
|
+
const keys = Object.keys(where);
|
|
71
|
+
/**
|
|
72
|
+
* Skip everything if there are no conditions to be applied.
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
if (keys.length === 0) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const filterPlugins = getMappedPlugins({
|
|
80
|
+
plugins,
|
|
81
|
+
type: _ValueFilterPlugin.ValueFilterPlugin.type,
|
|
82
|
+
property: "operation"
|
|
83
|
+
});
|
|
84
|
+
return keys.reduce((filters, key) => {
|
|
85
|
+
const compareValue = where[key];
|
|
86
|
+
|
|
87
|
+
if (compareValue === undefined) {
|
|
88
|
+
return filters;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* @see DynamoDbContainsFilter
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
if (multiSearchFieldOperations.includes(key) === true) {
|
|
96
|
+
const data = compareValue;
|
|
97
|
+
let transformValue = undefined;
|
|
98
|
+
const paths = data.fields.map(field => {
|
|
99
|
+
const fieldPlugin = fields.find(plugin => plugin.getField() === field);
|
|
100
|
+
|
|
101
|
+
if (fieldPlugin) {
|
|
102
|
+
transformValue = value => {
|
|
103
|
+
return fieldPlugin.transformValue(value);
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
return fieldPlugin.getPath();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return field;
|
|
110
|
+
});
|
|
111
|
+
filters.push({
|
|
112
|
+
compareValue: data.value,
|
|
113
|
+
filterPlugin: findFilterPlugin(filterPlugins, key),
|
|
114
|
+
transformValue,
|
|
115
|
+
paths,
|
|
116
|
+
negate: false
|
|
117
|
+
});
|
|
118
|
+
return filters;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const {
|
|
122
|
+
field,
|
|
123
|
+
operation,
|
|
124
|
+
negate
|
|
125
|
+
} = extractWhereArgs(key);
|
|
126
|
+
const filterPlugin = findFilterPlugin(filterPlugins, operation);
|
|
127
|
+
const fieldPlugin = fields.find(plugin => plugin.getField() === field);
|
|
128
|
+
let path = field;
|
|
129
|
+
let transformValue = undefined;
|
|
130
|
+
|
|
131
|
+
if (fieldPlugin) {
|
|
132
|
+
transformValue = value => {
|
|
133
|
+
return fieldPlugin.transformValue(value);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
path = fieldPlugin.getPath();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
filters.push({
|
|
140
|
+
compareValue,
|
|
141
|
+
filterPlugin,
|
|
142
|
+
transformValue,
|
|
143
|
+
paths: [path],
|
|
144
|
+
negate
|
|
145
|
+
});
|
|
146
|
+
return filters;
|
|
147
|
+
}, []);
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* Transforms the value with given transformer callable.
|
|
151
|
+
*/
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
const transform = (value, transformValue) => {
|
|
155
|
+
if (!transformValue) {
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (Array.isArray(value)) {
|
|
160
|
+
return value.map(v => transformValue(v));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
return transformValue(value);
|
|
164
|
+
};
|
|
165
|
+
/**
|
|
166
|
+
* Creates a filter callable that we can send to the .filter() method of the array.
|
|
167
|
+
*/
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
const createFilterCallable = params => {
|
|
171
|
+
const filters = createFilters(params);
|
|
172
|
+
/**
|
|
173
|
+
* Just return null so there are no filters to be applied.
|
|
174
|
+
* Later in the code we check for null so we do not loop through the items.
|
|
175
|
+
*/
|
|
176
|
+
|
|
177
|
+
if (filters.length === 0) {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return item => {
|
|
182
|
+
for (const filter of filters) {
|
|
183
|
+
const result = filter.paths.some(path => {
|
|
184
|
+
const value = transform(_dotProp.default.get(item, path), filter.transformValue);
|
|
185
|
+
const compareValue = transform(filter.compareValue, filter.transformValue);
|
|
186
|
+
const matched = filter.filterPlugin.matches({
|
|
187
|
+
value,
|
|
188
|
+
compareValue
|
|
189
|
+
});
|
|
190
|
+
return filter.negate ? !matched : matched;
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
if (result === false) {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return true;
|
|
199
|
+
};
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
function filterItems(params) {
|
|
203
|
+
const filter = createFilterCallable(params);
|
|
204
|
+
/**
|
|
205
|
+
* No point in going through all the items when there are no filters to be applied.
|
|
206
|
+
*/
|
|
207
|
+
|
|
208
|
+
if (!filter) {
|
|
209
|
+
return params.items;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return params.items.filter(filter);
|
|
213
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getMappedPlugins","params","plugins","byType","type","reduce","plugin","op","property","extractWhereArgs","key","result","split","field","shift","rawOp","length","join","operation","negate","match","replace","findFilterPlugin","WebinyError","multiSearchFieldOperations","createFilters","where","fields","keys","Object","filterPlugins","ValueFilterPlugin","filters","compareValue","undefined","includes","data","transformValue","paths","map","fieldPlugin","find","getField","value","getPath","push","filterPlugin","path","transform","Array","isArray","v","createFilterCallable","item","filter","some","dotProp","get","matched","matches","filterItems","items"],"sources":["filter.ts"],"sourcesContent":["import dotProp from \"dot-prop\";\nimport WebinyError from \"@webiny/error\";\nimport { Plugin, PluginsContainer } from \"@webiny/plugins\";\nimport { ValueFilterPlugin } from \"~/plugins/definitions/ValueFilterPlugin\";\nimport { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\nimport { DynamoDbContainsFilter } from \"~/types\";\n\ntype TransformValue = (value: any) => any;\n\nexport interface Params<T = any> {\n plugins: PluginsContainer;\n items: T[];\n where: Record<string, any>;\n /**\n * An array of fields that require some special operation.\n */\n fields: FieldPlugin[];\n}\n\ninterface MappedPluginParams<T extends Plugin = Plugin> {\n plugins: PluginsContainer;\n type: string;\n property: keyof T;\n}\n\ninterface Filter {\n compareValue: any;\n filterPlugin: ValueFilterPlugin;\n transformValue?: TransformValue;\n paths: string[];\n negate: boolean;\n}\n\nconst getMappedPlugins = <T extends Plugin>(params: MappedPluginParams<T>): Record<string, T> => {\n return params.plugins.byType<T>(params.type).reduce((plugins, plugin) => {\n /**\n * We expect op to be a string, that is why we cast.\n */\n const op = plugin[params.property] as unknown as string;\n plugins[op] = plugin;\n return plugins;\n }, {} as Record<string, T>);\n};\n\ninterface ExtractWhereArgsResult {\n field: string;\n operation: string;\n negate: boolean;\n}\nconst extractWhereArgs = (key: string): ExtractWhereArgsResult => {\n const result = key.split(\"_\");\n const field = result.shift() as string;\n const rawOp = result.length === 0 ? \"eq\" : result.join(\"_\");\n /**\n * When rawOp is not, it means it is equal negated so just return that.\n */\n if (rawOp === \"not\") {\n return {\n field,\n operation: \"eq\",\n negate: true\n };\n }\n const negate = rawOp.match(\"not_\") !== null;\n const operation = rawOp.replace(\"not_\", \"\");\n return {\n field,\n operation,\n negate\n };\n};\n\nconst findFilterPlugin = (\n plugins: Record<string, ValueFilterPlugin>,\n operation: string\n): ValueFilterPlugin => {\n if (plugins[operation]) {\n return plugins[operation];\n }\n throw new WebinyError(`Missing filter plugin definition.`, \"FILTER_PLUGIN_ERROR\", {\n operation\n });\n};\n\nconst multiSearchFieldOperations = [\"contains\", \"fuzzy\"];\n\nconst createFilters = (params: Omit<Params, \"items\">): Filter[] => {\n const { plugins, where, fields } = params;\n\n const keys = Object.keys(where);\n /**\n * Skip everything if there are no conditions to be applied.\n */\n if (keys.length === 0) {\n return [];\n }\n const filterPlugins = getMappedPlugins<ValueFilterPlugin>({\n plugins,\n type: ValueFilterPlugin.type,\n property: \"operation\"\n });\n\n return keys.reduce((filters, key) => {\n const compareValue = where[key];\n if (compareValue === undefined) {\n return filters;\n }\n /**\n * @see DynamoDbContainsFilter\n */\n if (multiSearchFieldOperations.includes(key) === true) {\n const data: DynamoDbContainsFilter = compareValue;\n let transformValue: TransformValue | undefined = undefined;\n const paths = data.fields.map(field => {\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n return fieldPlugin.getPath();\n }\n return field;\n });\n filters.push({\n compareValue: data.value,\n filterPlugin: findFilterPlugin(filterPlugins, key),\n transformValue,\n paths,\n negate: false\n });\n return filters;\n }\n\n const { field, operation, negate } = extractWhereArgs(key);\n\n const filterPlugin = findFilterPlugin(filterPlugins, operation);\n\n const fieldPlugin = fields.find(plugin => plugin.getField() === field);\n let path: string = field;\n let transformValue: TransformValue | undefined = undefined;\n if (fieldPlugin) {\n transformValue = (value: any) => {\n return fieldPlugin.transformValue(value);\n };\n path = fieldPlugin.getPath();\n }\n\n filters.push({\n compareValue,\n filterPlugin,\n transformValue,\n paths: [path],\n negate\n });\n\n return filters;\n }, [] as Filter[]);\n};\n/**\n * Transforms the value with given transformer callable.\n */\nconst transform = (value: any, transformValue?: TransformValue): any => {\n if (!transformValue) {\n return value;\n }\n if (Array.isArray(value)) {\n return value.map(v => transformValue(v));\n }\n return transformValue(value);\n};\n/**\n * Creates a filter callable that we can send to the .filter() method of the array.\n */\nconst createFilterCallable = (params: Omit<Params, \"items\">): ((item: any) => boolean) | null => {\n const filters = createFilters(params);\n /**\n * Just return null so there are no filters to be applied.\n * Later in the code we check for null so we do not loop through the items.\n */\n if (filters.length === 0) {\n return null;\n }\n\n return (item: any) => {\n for (const filter of filters) {\n const result = filter.paths.some(path => {\n const value = transform(dotProp.get(item, path), filter.transformValue);\n const compareValue = transform(filter.compareValue, filter.transformValue);\n const matched = filter.filterPlugin.matches({\n value,\n compareValue\n });\n\n return filter.negate ? !matched : matched;\n });\n if (result === false) {\n return false;\n }\n }\n return true;\n };\n};\n\nexport function filterItems<T = any>(params: Params<T>): T[] {\n const filter = createFilterCallable(params);\n /**\n * No point in going through all the items when there are no filters to be applied.\n */\n if (!filter) {\n return params.items;\n }\n return params.items.filter(filter);\n}\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AAEA;;AA8BA,MAAMA,gBAAgB,GAAsBC,MAAnB,IAAwE;EAC7F,OAAOA,MAAM,CAACC,OAAP,CAAeC,MAAf,CAAyBF,MAAM,CAACG,IAAhC,EAAsCC,MAAtC,CAA6C,CAACH,OAAD,EAAUI,MAAV,KAAqB;IACrE;AACR;AACA;IACQ,MAAMC,EAAE,GAAGD,MAAM,CAACL,MAAM,CAACO,QAAR,CAAjB;IACAN,OAAO,CAACK,EAAD,CAAP,GAAcD,MAAd;IACA,OAAOJ,OAAP;EACH,CAPM,EA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C1B,MAAMrB,MAAM,GAAG2C,MAAM,CAAChB,KAAP,CAAaiB,IAAb,CAAkBR,IAAI,IAAI;QACrC,MAAMJ,KAAK,GAAGK,SAAS,CAACQ,gBAAA,CAAQC,GAAR,CAAYJ,IAAZ,EAAkBN,IAAlB,CAAD,EAA0BO,MAAM,CAACjB,cAAjC,CAAvB;QACA,MAAMJ,YAAY,GAAGe,SAAS,CAACM,MAAM,CAACrB,YAAR,EAAsBqB,MAAM,CAACjB,cAA7B,CAA9B;QACA,MAAMqB,OAAO,GAAGJ,MAAM,CAACR,YAAP,CAAoBa,OAApB,CAA4B;UACxChB,KADwC;UAExCV;QAFwC,CAA5B,CAAhB;QAKA,OAAOqB,MAAM,CAACnC,MAAP,GAAgB,CAACuC,OAAjB,GAA2BA,OAAlC;MACH,CATc,CAAf;;MAUA,IAAI/C,MAAM,KAAK,KAAf,EAAsB;QAClB,OAAO,KAAP;MACH;IACJ;;IACD,OAAO,IAAP;EACH,CAjBD;AAkBH,CA5BD;;AA8BO,SAASiD,WAAT,CAA8B3D,MAA9B,EAAsD;EACzD,MAAMqD,MAAM,GAAGF,oBAAoB,CAACnD,MAAD,CAAnC;EACA;AACJ;AACA;;EACI,IAAI,CAACqD,MAAL,EAAa;IACT,OAAOrD,MAAM,CAAC4D,KAAd;EACH;;EACD,OAAO5D,MAAM,CAAC4D,KAAP,CAAaP,MAAb,CAAoBA,MAApB,CAAP;AACH"}
|
package/utils/get.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Entity } from "dynamodb-toolbox";
|
|
2
|
+
export interface GetRecordParams {
|
|
3
|
+
entity: Entity<any>;
|
|
4
|
+
keys: {
|
|
5
|
+
PK: string;
|
|
6
|
+
SK: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Gets a single record from the DynamoDB table.
|
|
11
|
+
* Returns either record or null.
|
|
12
|
+
*
|
|
13
|
+
* Be aware to wrap in try/catch to avoid the error killing your app.
|
|
14
|
+
*
|
|
15
|
+
* @throws
|
|
16
|
+
*/
|
|
17
|
+
export declare const get: <T>(params: GetRecordParams) => Promise<T | null>;
|
package/utils/get.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.get = void 0;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Gets a single record from the DynamoDB table.
|
|
10
|
+
* Returns either record or null.
|
|
11
|
+
*
|
|
12
|
+
* Be aware to wrap in try/catch to avoid the error killing your app.
|
|
13
|
+
*
|
|
14
|
+
* @throws
|
|
15
|
+
*/
|
|
16
|
+
const get = async params => {
|
|
17
|
+
const {
|
|
18
|
+
entity,
|
|
19
|
+
keys
|
|
20
|
+
} = params;
|
|
21
|
+
const result = await entity.get(keys);
|
|
22
|
+
|
|
23
|
+
if (!result || !result.Item) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return result.Item;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
exports.get = get;
|
package/utils/get.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["get","params","entity","keys","result","Item"],"sources":["get.ts"],"sourcesContent":["import { Entity } from \"dynamodb-toolbox\";\n\nexport interface GetRecordParams {\n entity: Entity<any>;\n keys: {\n PK: string;\n SK: string;\n };\n}\n\n/**\n * Gets a single record from the DynamoDB table.\n * Returns either record or null.\n *\n * Be aware to wrap in try/catch to avoid the error killing your app.\n *\n * @throws\n */\nexport const get = async <T>(params: GetRecordParams): Promise<T | null> => {\n const { entity, keys } = params;\n\n const result = await entity.get(keys);\n\n if (!result || !result.Item) {\n return null;\n }\n return result.Item;\n};\n"],"mappings":";;;;;;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,MAAMA,GAAG,GAAG,MAAUC,MAAV,IAAyD;EACxE,MAAM;IAAEC,MAAF;IAAUC;EAAV,IAAmBF,MAAzB;EAEA,MAAMG,MAAM,GAAG,MAAMF,MAAM,CAACF,GAAP,CAAWG,IAAX,CAArB;;EAEA,IAAI,CAACC,MAAD,IAAW,CAACA,MAAM,CAACC,IAAvB,EAA6B;IACzB,OAAO,IAAP;EACH;;EACD,OAAOD,MAAM,CAACC,IAAd;AACH,CATM"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface MetaResponse {
|
|
2
|
+
cursor: string | null;
|
|
3
|
+
totalCount: number;
|
|
4
|
+
hasMoreItems: boolean;
|
|
5
|
+
}
|
|
6
|
+
interface CreateListResponseParams<T> {
|
|
7
|
+
items: T[];
|
|
8
|
+
after?: string | null;
|
|
9
|
+
totalCount: number;
|
|
10
|
+
limit: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse];
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createListResponse = createListResponse;
|
|
7
|
+
|
|
8
|
+
var _cursor = require("./cursor");
|
|
9
|
+
|
|
10
|
+
function createListResponse(params) {
|
|
11
|
+
const {
|
|
12
|
+
items: initialItems,
|
|
13
|
+
after,
|
|
14
|
+
totalCount,
|
|
15
|
+
limit
|
|
16
|
+
} = params;
|
|
17
|
+
let start = Number((0, _cursor.decodeCursor)(after));
|
|
18
|
+
|
|
19
|
+
if (isNaN(start) === true) {
|
|
20
|
+
start = 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const hasMoreItems = totalCount > start + limit;
|
|
24
|
+
const end = limit > totalCount + start + limit ? undefined : start + limit;
|
|
25
|
+
const items = end ? initialItems.slice(start, end) : initialItems;
|
|
26
|
+
const cursor = items.length > 0 ? (0, _cursor.encodeCursor)(start + limit) : null;
|
|
27
|
+
const meta = {
|
|
28
|
+
hasMoreItems,
|
|
29
|
+
totalCount,
|
|
30
|
+
cursor
|
|
31
|
+
};
|
|
32
|
+
return [items, meta];
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createListResponse","params","items","initialItems","after","totalCount","limit","start","Number","decodeCursor","isNaN","hasMoreItems","end","undefined","slice","cursor","length","encodeCursor","meta"],"sources":["listResponse.ts"],"sourcesContent":["import { decodeCursor, encodeCursor } from \"~/utils/cursor\";\n\ninterface MetaResponse {\n cursor: string | null;\n totalCount: number;\n hasMoreItems: boolean;\n}\n\ninterface CreateListResponseParams<T> {\n items: T[];\n after?: string | null;\n totalCount: number;\n limit: number;\n}\n\nexport function createListResponse<T>(params: CreateListResponseParams<T>): [T[], MetaResponse] {\n const { items: initialItems, after, totalCount, limit } = params;\n let start = Number(decodeCursor(after));\n if (isNaN(start) === true) {\n start = 0;\n }\n const hasMoreItems = totalCount > start + limit;\n const end = limit > totalCount + start + limit ? undefined : start + limit;\n const items = end ? initialItems.slice(start, end) : initialItems;\n\n const cursor = items.length > 0 ? encodeCursor(start + limit) : null;\n\n const meta = {\n hasMoreItems,\n totalCount,\n cursor\n };\n\n return [items, meta];\n}\n"],"mappings":";;;;;;;AAAA;;AAeO,SAASA,kBAAT,CAA+BC,MAA/B,EAAyF;EAC5F,MAAM;IAAEC,KAAK,EAAEC,YAAT;IAAuBC,KAAvB;IAA8BC,UAA9B;IAA0CC;EAA1C,IAAoDL,MAA1D;EACA,IAAIM,KAAK,GAAGC,MAAM,CAAC,IAAAC,oBAAA,EAAaL,KAAb,CAAD,CAAlB;;EACA,IAAIM,KAAK,CAACH,KAAD,CAAL,KAAiB,IAArB,EAA2B;IACvBA,KAAK,GAAG,CAAR;EACH;;EACD,MAAMI,YAAY,GAAGN,UAAU,GAAGE,KAAK,GAAGD,KAA1C;EACA,MAAMM,GAAG,GAAGN,KAAK,GAAGD,UAAU,GAAGE,KAAb,GAAqBD,KAA7B,GAAqCO,SAArC,GAAiDN,KAAK,GAAGD,KAArE;EACA,MAAMJ,KAAK,GAAGU,GAAG,GAAGT,YAAY,CAACW,KAAb,CAAmBP,KAAnB,EAA0BK,GAA1B,CAAH,GAAoCT,YAArD;EAEA,MAAMY,MAAM,GAAGb,KAAK,CAACc,MAAN,GAAe,CAAf,GAAmB,IAAAC,oBAAA,EAAaV,KAAK,GAAGD,KAArB,CAAnB,GAAiD,IAAhE;EAEA,MAAMY,IAAI,GAAG;IACTP,YADS;IAETN,UAFS;IAGTU;EAHS,CAAb;EAMA,OAAO,CAACb,KAAD,EAAQgB,IAAR,CAAP;AACH"}
|
package/utils/query.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { queryOptions as DynamoDBToolboxQueryOptions } from "dynamodb-toolbox/dist/classes/Table";
|
|
2
|
+
import { Entity } from "dynamodb-toolbox";
|
|
3
|
+
export interface QueryAllParams {
|
|
4
|
+
entity: Entity<any>;
|
|
5
|
+
partitionKey: string;
|
|
6
|
+
options?: DynamoDBToolboxQueryOptions;
|
|
7
|
+
}
|
|
8
|
+
export interface QueryOneParams extends QueryAllParams {
|
|
9
|
+
options?: Omit<DynamoDBToolboxQueryOptions, "limit">;
|
|
10
|
+
}
|
|
11
|
+
export interface QueryParams extends QueryAllParams {
|
|
12
|
+
previous?: any;
|
|
13
|
+
}
|
|
14
|
+
export interface QueryResult<T> {
|
|
15
|
+
result: any | null;
|
|
16
|
+
items: T[];
|
|
17
|
+
}
|
|
18
|
+
export declare type DbItem<T> = T & {
|
|
19
|
+
PK: string;
|
|
20
|
+
SK: string;
|
|
21
|
+
TYPE: string;
|
|
22
|
+
GSI1_PK?: string;
|
|
23
|
+
GSI1_SK?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Will run the query to fetch the first possible item from the database.
|
|
27
|
+
*/
|
|
28
|
+
export declare const queryOne: <T>(params: QueryOneParams) => Promise<DbItem<T> | null>;
|
|
29
|
+
/**
|
|
30
|
+
* Will run the query to fetch the results no matter how much iterations it needs to go through.
|
|
31
|
+
*/
|
|
32
|
+
export declare const queryAll: <T>(params: QueryAllParams) => Promise<DbItem<T>[]>;
|
package/utils/query.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.queryOne = exports.queryAll = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Will run query only once. Pass the previous to run the query again to fetch new data.
|
|
16
|
+
* It returns the result and the items it found.
|
|
17
|
+
* Result is required to fetch the items that were not fetched in the previous run.
|
|
18
|
+
*/
|
|
19
|
+
const query = async params => {
|
|
20
|
+
const {
|
|
21
|
+
entity,
|
|
22
|
+
previous,
|
|
23
|
+
partitionKey,
|
|
24
|
+
options
|
|
25
|
+
} = params;
|
|
26
|
+
let result;
|
|
27
|
+
/**
|
|
28
|
+
* In case there is no previous result we must make a new query.
|
|
29
|
+
* This is the first query on the given partition key.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
if (!previous) {
|
|
33
|
+
result = await entity.query(partitionKey, options);
|
|
34
|
+
} else if (typeof previous.next === "function") {
|
|
35
|
+
/**
|
|
36
|
+
* In case we have a previous result and it has a next method, we run it.
|
|
37
|
+
* In case result of the next method is false, it means it has nothing else to read
|
|
38
|
+
* and we return a null to keep the query from repeating.
|
|
39
|
+
*/
|
|
40
|
+
result = await previous.next();
|
|
41
|
+
|
|
42
|
+
if (result === false) {
|
|
43
|
+
return {
|
|
44
|
+
result: null,
|
|
45
|
+
items: []
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
/**
|
|
50
|
+
* This could probably never happen but keep it here just in case to break the query loop.
|
|
51
|
+
* Basically, either previous does not exist or it exists and it does not have the next method
|
|
52
|
+
* and at that point a result returned will be null and loop should not start again.
|
|
53
|
+
*/
|
|
54
|
+
return {
|
|
55
|
+
result: null,
|
|
56
|
+
items: []
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* We expect the result to contain an Items array and if not, something went wrong, very wrong.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
if (!result || !result.Items || !Array.isArray(result.Items)) {
|
|
65
|
+
throw new _error.default("Error when querying for content entries - no result.", "QUERY_ERROR", {
|
|
66
|
+
partitionKey,
|
|
67
|
+
options
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
result,
|
|
73
|
+
items: result.Items
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Will run the query to fetch the first possible item from the database.
|
|
78
|
+
*/
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
const queryOne = async params => {
|
|
82
|
+
const {
|
|
83
|
+
items
|
|
84
|
+
} = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
|
|
85
|
+
options: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, params.options || {}), {}, {
|
|
86
|
+
limit: 1
|
|
87
|
+
})
|
|
88
|
+
}));
|
|
89
|
+
const item = items.shift();
|
|
90
|
+
return item ? item : null;
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* Will run the query to fetch the results no matter how much iterations it needs to go through.
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
exports.queryOne = queryOne;
|
|
98
|
+
|
|
99
|
+
const queryAll = async params => {
|
|
100
|
+
const items = [];
|
|
101
|
+
let results;
|
|
102
|
+
let previousResult = undefined;
|
|
103
|
+
|
|
104
|
+
while (results = await query((0, _objectSpread2.default)((0, _objectSpread2.default)({}, params), {}, {
|
|
105
|
+
previous: previousResult
|
|
106
|
+
}))) {
|
|
107
|
+
items.push(...results.items);
|
|
108
|
+
|
|
109
|
+
if (!results.result) {
|
|
110
|
+
return items;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
previousResult = results.result;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return items;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
exports.queryAll = queryAll;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["query","params","entity","previous","partitionKey","options","result","next","items","Items","Array","isArray","WebinyError","queryOne","limit","item","shift","queryAll","results","previousResult","undefined","push"],"sources":["query.ts"],"sourcesContent":["import { queryOptions as DynamoDBToolboxQueryOptions } from \"dynamodb-toolbox/dist/classes/Table\";\nimport WebinyError from \"@webiny/error\";\nimport { Entity } from \"dynamodb-toolbox\";\n\nexport interface QueryAllParams {\n entity: Entity<any>;\n partitionKey: string;\n options?: DynamoDBToolboxQueryOptions;\n}\n\nexport interface QueryOneParams extends QueryAllParams {\n options?: Omit<DynamoDBToolboxQueryOptions, \"limit\">;\n}\n\nexport interface QueryParams extends QueryAllParams {\n previous?: any;\n}\n\nexport interface QueryResult<T> {\n result: any | null;\n items: T[];\n}\n\nexport type DbItem<T> = T & {\n PK: string;\n SK: string;\n TYPE: string;\n GSI1_PK?: string;\n GSI1_SK?: string;\n};\n\n/**\n * Will run query only once. Pass the previous to run the query again to fetch new data.\n * It returns the result and the items it found.\n * Result is required to fetch the items that were not fetched in the previous run.\n */\nconst query = async <T>(params: QueryParams): Promise<QueryResult<T>> => {\n const { entity, previous, partitionKey, options } = params;\n let result;\n /**\n * In case there is no previous result we must make a new query.\n * This is the first query on the given partition key.\n */\n if (!previous) {\n result = await entity.query(partitionKey, options);\n } else if (typeof previous.next === \"function\") {\n /**\n * In case we have a previous result and it has a next method, we run it.\n * In case result of the next method is false, it means it has nothing else to read\n * and we return a null to keep the query from repeating.\n */\n result = await previous.next();\n if (result === false) {\n return {\n result: null,\n items: []\n };\n }\n } else {\n /**\n * This could probably never happen but keep it here just in case to break the query loop.\n * Basically, either previous does not exist or it exists and it does not have the next method\n * and at that point a result returned will be null and loop should not start again.\n */\n return {\n result: null,\n items: []\n };\n }\n /**\n * We expect the result to contain an Items array and if not, something went wrong, very wrong.\n */\n if (!result || !result.Items || !Array.isArray(result.Items)) {\n throw new WebinyError(\n \"Error when querying for content entries - no result.\",\n \"QUERY_ERROR\",\n {\n partitionKey,\n options\n }\n );\n }\n return {\n result,\n items: result.Items\n };\n};\n/**\n * Will run the query to fetch the first possible item from the database.\n */\nexport const queryOne = async <T>(params: QueryOneParams): Promise<DbItem<T> | null> => {\n const { items } = await query<DbItem<T>>({\n ...params,\n options: {\n ...(params.options || {}),\n limit: 1\n }\n });\n const item = items.shift();\n return item ? item : null;\n};\n/**\n * Will run the query to fetch the results no matter how much iterations it needs to go through.\n */\nexport const queryAll = async <T>(params: QueryAllParams): Promise<DbItem<T>[]> => {\n const items: DbItem<T>[] = [];\n let results: QueryResult<DbItem<T>>;\n let previousResult: any = undefined;\n while ((results = await query({ ...params, previous: previousResult }))) {\n items.push(...results.items);\n if (!results.result) {\n return items;\n }\n previousResult = results.result;\n }\n return items;\n};\n"],"mappings":";;;;;;;;;;;AACA;;AA8BA;AACA;AACA;AACA;AACA;AACA,MAAMA,KAAK,GAAG,MAAUC,MAAV,IAA2D;EACrE,MAAM;IAAEC,MAAF;IAAUC,QAAV;IAAoBC,YAApB;IAAkCC;EAAlC,IAA8CJ,MAApD;EACA,IAAIK,MAAJ;EACA;AACJ;AACA;AACA;;EACI,IAAI,CAACH,QAAL,EAAe;IACXG,MAAM,GAAG,MAAMJ,MAAM,CAACF,KAAP,CAAaI,YAAb,EAA2BC,OAA3B,CAAf;EACH,CAFD,MAEO,I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AAEgB;EAAxB,GAA7B,EAAyE;IACrEX,KAAK,CAACa,IAAN,CAAW,GAAGH,OAAO,CAACV,KAAtB;;IACA,IAAI,CAACU,OAAO,CAACZ,MAAb,EAAqB;MACjB,OAAOE,KAAP;IACH;;IACDW,cAAc,GAAGD,OAAO,CAACZ,MAAzB;EACH;;EACD,OAAOE,KAAP;AACH,CAZM"}
|
package/utils/sort.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { FieldPlugin } from "../plugins/definitions/FieldPlugin";
|
|
2
|
+
interface Params<T> {
|
|
3
|
+
/**
|
|
4
|
+
* The items we are sorting.
|
|
5
|
+
*/
|
|
6
|
+
items: T[];
|
|
7
|
+
/**
|
|
8
|
+
* Sort options. For example: ["id_ASC"]
|
|
9
|
+
*/
|
|
10
|
+
sort?: string[];
|
|
11
|
+
/**
|
|
12
|
+
* Fields we can sort by.
|
|
13
|
+
*/
|
|
14
|
+
fields?: FieldPlugin[];
|
|
15
|
+
}
|
|
16
|
+
export declare function sortItems<T = any>(params: Params<T>): T[];
|
|
17
|
+
export {};
|
package/utils/sort.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.sortItems = sortItems;
|
|
9
|
+
|
|
10
|
+
var _orderBy = _interopRequireDefault(require("lodash/orderBy"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
const extractSort = (sortBy, fields) => {
|
|
15
|
+
const result = sortBy.split("_");
|
|
16
|
+
|
|
17
|
+
if (result.length !== 2) {
|
|
18
|
+
throw new _error.default("Problem in determining the sorting for the entry items.", "SORT_ERROR", {
|
|
19
|
+
sortBy
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const [field, order] = result;
|
|
24
|
+
|
|
25
|
+
if (!field) {
|
|
26
|
+
throw new _error.default("Sorting field does not exist.", "SORTING_FIELD_ERROR", {
|
|
27
|
+
field,
|
|
28
|
+
order,
|
|
29
|
+
fields
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const fieldPlugin = fields.find(f => f.getField() === field);
|
|
34
|
+
const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;
|
|
35
|
+
|
|
36
|
+
if (isSortable === false) {
|
|
37
|
+
throw new _error.default(`Cannot sort by given field: "${field}".`, "UNSUPPORTED_SORT_ERROR", {
|
|
38
|
+
fields,
|
|
39
|
+
field
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return {
|
|
44
|
+
field,
|
|
45
|
+
reverse: order.toUpperCase() === "DESC"
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
function sortItems(params) {
|
|
50
|
+
const {
|
|
51
|
+
items,
|
|
52
|
+
sort: initialSort = [],
|
|
53
|
+
fields = []
|
|
54
|
+
} = params;
|
|
55
|
+
/**
|
|
56
|
+
* Skip sorting if nothing was passed to sort by or nothing to sort.
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
if (items.length <= 1 || Array.isArray(initialSort) === false || initialSort.length === 0) {
|
|
60
|
+
return items;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const info = {
|
|
64
|
+
sorters: [],
|
|
65
|
+
orders: []
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
for (const sort of initialSort) {
|
|
69
|
+
/**
|
|
70
|
+
* Possibly empty array item was passed.
|
|
71
|
+
*/
|
|
72
|
+
if (!sort) {
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const {
|
|
77
|
+
field,
|
|
78
|
+
reverse
|
|
79
|
+
} = extractSort(sort, fields);
|
|
80
|
+
const fieldPlugin = fields.find(f => f.getField() === field);
|
|
81
|
+
const path = fieldPlugin ? fieldPlugin.getPath() : field;
|
|
82
|
+
info.sorters.push(path);
|
|
83
|
+
info.orders.push(reverse === true ? "desc" : "asc");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (info.sorters.length === 0) {
|
|
87
|
+
return items;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return (0, _orderBy.default)(items, info.sorters, info.orders);
|
|
91
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["extractSort","sortBy","fields","result","split","length","WebinyError","field","order","fieldPlugin","find","f","getField","isSortable","reverse","toUpperCase","sortItems","params","items","sort","initialSort","Array","isArray","info","sorters","orders","path","getPath","push","lodashOrderBy"],"sources":["sort.ts"],"sourcesContent":["import lodashOrderBy from \"lodash/orderBy\";\nimport WebinyError from \"@webiny/error\";\nimport { FieldPlugin } from \"~/plugins/definitions/FieldPlugin\";\n\ninterface Info {\n sorters: string[];\n orders: (boolean | \"asc\" | \"desc\")[];\n}\n\ninterface Response {\n reverse: boolean;\n field: string;\n}\n\nconst extractSort = (sortBy: string, fields: FieldPlugin[]): Response => {\n const result = sortBy.split(\"_\");\n if (result.length !== 2) {\n throw new WebinyError(\n \"Problem in determining the sorting for the entry items.\",\n \"SORT_ERROR\",\n {\n sortBy\n }\n );\n }\n const [field, order] = result;\n\n if (!field) {\n throw new WebinyError(\"Sorting field does not exist.\", \"SORTING_FIELD_ERROR\", {\n field,\n order,\n fields\n });\n }\n const fieldPlugin = fields.find(f => f.getField() === field);\n const isSortable = fieldPlugin ? fieldPlugin.isSortable() : true;\n if (isSortable === false) {\n throw new WebinyError(`Cannot sort by given field: \"${field}\".`, \"UNSUPPORTED_SORT_ERROR\", {\n fields,\n field\n });\n }\n\n return {\n field,\n reverse: order.toUpperCase() === \"DESC\"\n };\n};\n\ninterface Params<T> {\n /**\n * The items we are sorting.\n */\n items: T[];\n /**\n * Sort options. For example: [\"id_ASC\"]\n */\n sort?: string[];\n /**\n * Fields we can sort by.\n */\n fields?: FieldPlugin[];\n}\n\nexport function sortItems<T = any>(params: Params<T>): T[] {\n const { items, sort: initialSort = [], fields = [] } = params;\n /**\n * Skip sorting if nothing was passed to sort by or nothing to sort.\n */\n if (items.length <= 1 || Array.isArray(initialSort) === false || initialSort.length === 0) {\n return items;\n }\n\n const info: Info = {\n sorters: [],\n orders: []\n };\n\n for (const sort of initialSort) {\n /**\n * Possibly empty array item was passed.\n */\n if (!sort) {\n continue;\n }\n const { field, reverse } = extractSort(sort, fields);\n const fieldPlugin = fields.find(f => f.getField() === field);\n const path = fieldPlugin ? fieldPlugin.getPath() : field;\n\n info.sorters.push(path);\n info.orders.push(reverse === true ? \"desc\" : \"asc\");\n }\n\n if (info.sorters.length === 0) {\n return items;\n }\n\n return lodashOrderBy(items, info.sorters, info.orders);\n}\n"],"mappings":";;;;;;;;;AAAA;;AACA;;AAaA,MAAMA,WAAW,GAAG,CAACC,MAAD,EAAiBC,MAAjB,KAAqD;EACrE,MAAMC,MAAM,GAAGF,MAAM,CAACG,KAAP,CAAa,GAAb,CAAf;;EACA,IAAID,MAAM,CAACE,MAAP,KAAkB,CAAtB,EAAyB;IACrB,MAAM,IAAIC,cAAJ,CACF,yDADE,EAEF,YAFE,EAGF;MACIL;IADJ,CAHE,CAAN;EAOH;;EACD,MAAM,CAACM,KAAD,EAAQC,KAAR,IAAiBL,MAAvB;;EAEA,IAAI,CAACI,KAAL,EAAY;IACR,MAAM,IAAID,cAAJ,CAAgB,+BAAhB,EAAiD,qBAAjD,EAAwE;MAC1EC,KAD0E;MAE1EC,KAF0E;MAG1EN;IAH0E,CAAxE,CAAN;EAKH;;EACD,MAAMO,WAAW,GAAGP,MAAM,CAACQ,IAAP,CAAYC,CAAC,IAAIA,CAAC,CAACC,QAAF,OAAiBL,KAAlC,CAApB;EACA,MAAMM,UAAU,GAAGJ,WAAW,GAAGA,WAAW,CAACI,UAAZ,EAAH,GA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EAED,IAAIS,IAAI,CAACC,OAAL,CAAanB,MAAb,KAAwB,CAA5B,EAA+B;IAC3B,OAAOa,KAAP;EACH;;EAED,OAAO,IAAAW,gBAAA,EAAcX,KAAd,EAAqBK,IAAI,CAACC,OAA1B,EAAmCD,IAAI,CAACE,MAAxC,CAAP;AACH"}
|
package/utils/table.d.ts
ADDED
package/utils/table.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.getTable = void 0;
|
|
9
|
+
|
|
10
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Will be removed in favor of passing the table name directly to the storage operations.
|
|
14
|
+
*
|
|
15
|
+
* @deprecated
|
|
16
|
+
*/
|
|
17
|
+
const getTable = context => {
|
|
18
|
+
if (!context.db) {
|
|
19
|
+
throw new _error.default("Missing db on context.", "DB_ERROR");
|
|
20
|
+
} else if (!context.db.table) {
|
|
21
|
+
throw new _error.default("Missing table on context.db.", "TABLE_ERROR");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return context.db.table;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
exports.getTable = getTable;
|