@webiny/api-headless-cms-ddb 0.0.0-unstable.97a151f74d → 0.0.0-unstable.aad28a72ae
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/dynamoDb/storage/longText.js +7 -0
- package/dynamoDb/storage/longText.js.map +1 -1
- package/dynamoDb/storage/richText.js.map +1 -1
- package/operations/entry/filtering/{createFilters.d.ts → createExpressions.d.ts} +9 -3
- package/operations/entry/filtering/createExpressions.js +217 -0
- package/operations/entry/filtering/createExpressions.js.map +1 -0
- package/operations/entry/filtering/createFields.d.ts +3 -7
- package/operations/entry/filtering/createFields.js +2 -3
- package/operations/entry/filtering/createFields.js.map +1 -1
- package/operations/entry/filtering/filter.d.ts +2 -3
- package/operations/entry/filtering/filter.js +92 -51
- package/operations/entry/filtering/filter.js.map +1 -1
- package/operations/entry/filtering/fullTextSearch.d.ts +2 -6
- package/operations/entry/filtering/fullTextSearch.js +2 -6
- package/operations/entry/filtering/fullTextSearch.js.map +1 -1
- package/operations/entry/filtering/values.d.ts +2 -0
- package/operations/entry/filtering/values.js +28 -0
- package/operations/entry/filtering/values.js.map +1 -0
- package/operations/entry/index.js +25 -12
- package/operations/entry/index.js.map +1 -1
- package/package.json +10 -10
- package/operations/entry/filtering/createFilters.js +0 -155
- package/operations/entry/filtering/createFilters.js.map +0 -1
- package/operations/entry/utils.d.ts +0 -36
- package/operations/entry/utils.js +0 -684
- package/operations/entry/utils.js.map +0 -1
|
@@ -7,27 +7,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
7
7
|
});
|
|
8
8
|
exports.filter = void 0;
|
|
9
9
|
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
10
12
|
var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin");
|
|
11
13
|
|
|
12
14
|
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
15
|
|
|
14
16
|
var _fullTextSearch = require("./fullTextSearch");
|
|
15
17
|
|
|
16
|
-
var
|
|
18
|
+
var _createExpressions = require("./createExpressions");
|
|
17
19
|
|
|
18
20
|
var _transform = require("./transform");
|
|
19
21
|
|
|
20
22
|
var _getValue = require("./getValue");
|
|
21
23
|
|
|
22
|
-
const
|
|
24
|
+
const executeFilter = params => {
|
|
23
25
|
const {
|
|
24
|
-
value
|
|
26
|
+
value,
|
|
25
27
|
filter
|
|
26
28
|
} = params;
|
|
27
|
-
const value = (0, _transform.transformValue)({
|
|
28
|
-
value: plainValue,
|
|
29
|
-
transform: filter.transformValue
|
|
30
|
-
});
|
|
31
29
|
const matched = filter.plugin.matches({
|
|
32
30
|
value,
|
|
33
31
|
compareValue: filter.compareValue
|
|
@@ -40,13 +38,69 @@ const execFilter = params => {
|
|
|
40
38
|
return matched;
|
|
41
39
|
};
|
|
42
40
|
|
|
43
|
-
const
|
|
41
|
+
const executeExpressions = params => {
|
|
42
|
+
const {
|
|
43
|
+
expressions,
|
|
44
|
+
getCachedValue,
|
|
45
|
+
filters,
|
|
46
|
+
condition
|
|
47
|
+
} = params;
|
|
48
|
+
|
|
49
|
+
if (expressions.length === 0 && filters.length === 0) {
|
|
50
|
+
return true;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Always run filters first as they might trigger an early return.
|
|
54
|
+
*/
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
for (const filter of filters) {
|
|
58
|
+
const value = getCachedValue(filter);
|
|
59
|
+
const result = executeFilter({
|
|
60
|
+
value,
|
|
61
|
+
filter
|
|
62
|
+
});
|
|
63
|
+
/**
|
|
64
|
+
* Filters are ALWAYS executed as an AND.
|
|
65
|
+
* So if even one is false, everything false.
|
|
66
|
+
*/
|
|
67
|
+
|
|
68
|
+
if (!result) {
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Then we move onto expressions, which are basically nested upon nested filters with different conditions.
|
|
74
|
+
*/
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
for (const expression of expressions) {
|
|
78
|
+
const result = executeExpressions((0, _objectSpread2.default)((0, _objectSpread2.default)({}, expression), {}, {
|
|
79
|
+
getCachedValue
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
if (result && condition === "OR") {
|
|
83
|
+
return true;
|
|
84
|
+
} else if (!result && condition == "AND") {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* If condition is an OR, we can fail the expressions check because the code would return a lot earlier than this line.
|
|
90
|
+
*
|
|
91
|
+
* Also, if condition is not an OR, we can say that the expressions check is ok, because it would fail a lot earlier than this line.
|
|
92
|
+
*/
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
return condition === "OR" ? false : true;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const filter = params => {
|
|
44
99
|
const {
|
|
45
100
|
items: records,
|
|
46
101
|
where,
|
|
47
102
|
plugins,
|
|
48
103
|
fields,
|
|
49
|
-
fromStorage,
|
|
50
104
|
fullTextSearch
|
|
51
105
|
} = params;
|
|
52
106
|
const keys = Object.keys(where);
|
|
@@ -55,21 +109,20 @@ const filter = async params => {
|
|
|
55
109
|
return records;
|
|
56
110
|
}
|
|
57
111
|
|
|
58
|
-
const
|
|
112
|
+
const expression = (0, _createExpressions.createExpressions)({
|
|
59
113
|
plugins,
|
|
60
114
|
where,
|
|
61
115
|
fields
|
|
62
116
|
});
|
|
63
117
|
/**
|
|
64
|
-
* No point in going further if there are no
|
|
118
|
+
* No point in going further if there are no expressions to be applied and no full text search to be executed.
|
|
65
119
|
*/
|
|
66
120
|
|
|
67
|
-
if (filters.length === 0 && !fullTextSearch) {
|
|
121
|
+
if (expression.filters.length === 0 && expression.expressions.length === 0 && !(fullTextSearch !== null && fullTextSearch !== void 0 && fullTextSearch.term)) {
|
|
68
122
|
return records;
|
|
69
123
|
}
|
|
70
124
|
/**
|
|
71
125
|
* We need the contains plugin to run the full text search.
|
|
72
|
-
* TODO check out to maybe implement fuzzy search.
|
|
73
126
|
*/
|
|
74
127
|
|
|
75
128
|
|
|
@@ -83,55 +136,43 @@ const filter = async params => {
|
|
|
83
136
|
term: fullTextSearch === null || fullTextSearch === void 0 ? void 0 : fullTextSearch.term,
|
|
84
137
|
targetFields: fullTextSearch === null || fullTextSearch === void 0 ? void 0 : fullTextSearch.fields,
|
|
85
138
|
fields,
|
|
86
|
-
fromStorage,
|
|
87
139
|
plugin: fullTextSearchPlugin
|
|
88
140
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
141
|
+
return records.filter(record => {
|
|
142
|
+
const cachedValues = {};
|
|
143
|
+
|
|
144
|
+
const getCachedValue = filter => {
|
|
145
|
+
const {
|
|
146
|
+
path
|
|
147
|
+
} = filter;
|
|
148
|
+
|
|
149
|
+
if (cachedValues[path] !== undefined) {
|
|
150
|
+
return cachedValues[path];
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const plainValue = (0, _getValue.getValue)(record, path);
|
|
154
|
+
const rawValue = (0, _transform.transformValue)({
|
|
98
155
|
value: plainValue,
|
|
99
|
-
filter
|
|
156
|
+
transform: filter.transformValue
|
|
100
157
|
});
|
|
158
|
+
cachedValues[path] = rawValue;
|
|
159
|
+
return rawValue;
|
|
160
|
+
};
|
|
101
161
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
162
|
+
const exprResult = executeExpressions((0, _objectSpread2.default)((0, _objectSpread2.default)({}, expression), {}, {
|
|
163
|
+
getCachedValue
|
|
164
|
+
}));
|
|
106
165
|
/**
|
|
107
|
-
* If
|
|
166
|
+
* If expression result is false we do not need to continue further.
|
|
167
|
+
* Also, if there is no full text search defined, just return the expression result.
|
|
108
168
|
*/
|
|
109
169
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
return record;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const result = await search({
|
|
116
|
-
item: record
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
if (!result) {
|
|
120
|
-
return null;
|
|
170
|
+
if (!exprResult || !search) {
|
|
171
|
+
return exprResult;
|
|
121
172
|
}
|
|
122
173
|
|
|
123
|
-
return record;
|
|
174
|
+
return search(record);
|
|
124
175
|
});
|
|
125
|
-
/**
|
|
126
|
-
* We run filtering as promises, so it is a bit faster than in for ... of loop.
|
|
127
|
-
*/
|
|
128
|
-
|
|
129
|
-
const results = await Promise.all(promises);
|
|
130
|
-
/**
|
|
131
|
-
* And filter out the null values which are returned when filter is not satisfied.
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
return results.filter(Boolean);
|
|
135
176
|
};
|
|
136
177
|
|
|
137
178
|
exports.filter = filter;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["
|
|
1
|
+
{"version":3,"names":["executeFilter","params","value","filter","matched","plugin","matches","compareValue","negate","executeExpressions","expressions","getCachedValue","filters","condition","length","result","expression","items","records","where","plugins","fields","fullTextSearch","keys","Object","createExpressions","term","fullTextSearchPlugin","byType","ValueFilterPlugin","type","find","getOperation","WebinyError","search","createFullTextSearch","targetFields","record","cachedValues","path","undefined","plainValue","getValue","rawValue","transformValue","transform","exprResult"],"sources":["filter.ts"],"sourcesContent":["import { CmsEntry, CmsEntryListWhere } from \"@webiny/api-headless-cms/types\";\nimport { ValueFilterPlugin } from \"@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin\";\nimport WebinyError from \"@webiny/error\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport { Field } from \"./types\";\nimport { createFullTextSearch } from \"./fullTextSearch\";\nimport { createExpressions, Expression, ExpressionCondition, Filter } from \"./createExpressions\";\nimport { transformValue } from \"./transform\";\nimport { getValue } from \"~/operations/entry/filtering/getValue\";\n\ninterface ExecuteFilterParams {\n value: any;\n filter: Filter;\n}\n\nconst executeFilter = (params: ExecuteFilterParams) => {\n const { value, filter } = params;\n\n const matched = filter.plugin.matches({\n value,\n compareValue: filter.compareValue\n });\n if (filter.negate) {\n return matched === false;\n }\n return matched;\n};\n\ninterface ExecuteExpressionsParams {\n getCachedValue: (filter: Filter) => Promise<any>;\n expressions: Expression[];\n filters: Filter[];\n condition: ExpressionCondition;\n}\n\nconst executeExpressions = (params: ExecuteExpressionsParams): boolean => {\n const { expressions, getCachedValue, filters, condition } = params;\n if (expressions.length === 0 && filters.length === 0) {\n return true;\n }\n /**\n * Always run filters first as they might trigger an early return.\n */\n for (const filter of filters) {\n const value = getCachedValue(filter);\n\n const result = executeFilter({\n value,\n filter\n });\n /**\n * Filters are ALWAYS executed as an AND.\n * So if even one is false, everything false.\n */\n if (!result) {\n return false;\n }\n }\n /**\n * Then we move onto expressions, which are basically nested upon nested filters with different conditions.\n */\n for (const expression of expressions) {\n const result = executeExpressions({\n ...expression,\n getCachedValue\n });\n if (result && condition === \"OR\") {\n return true;\n } else if (!result && condition == \"AND\") {\n return false;\n }\n }\n /**\n * If condition is an OR, we can fail the expressions check because the code would return a lot earlier than this line.\n *\n * Also, if condition is not an OR, we can say that the expressions check is ok, because it would fail a lot earlier than this line.\n */\n return condition === \"OR\" ? false : true;\n};\n\ninterface Params {\n items: CmsEntry[];\n where: Partial<CmsEntryListWhere>;\n plugins: PluginsContainer;\n fields: Record<string, Field>;\n fullTextSearch?: {\n term?: string;\n fields?: string[];\n };\n}\n\nexport const filter = (params: Params): CmsEntry[] => {\n const { items: records, where, plugins, fields, fullTextSearch } = params;\n\n const keys = Object.keys(where);\n if (keys.length === 0 && !fullTextSearch) {\n return records;\n }\n const expression = createExpressions({\n plugins,\n where,\n fields\n });\n\n /**\n * No point in going further if there are no expressions to be applied and no full text search to be executed.\n */\n if (\n expression.filters.length === 0 &&\n expression.expressions.length === 0 &&\n !fullTextSearch?.term\n ) {\n return records;\n }\n /**\n * We need the contains plugin to run the full text search.\n */\n const fullTextSearchPlugin = plugins\n .byType<ValueFilterPlugin>(ValueFilterPlugin.type)\n .find(plugin => plugin.getOperation() === \"contains\");\n if (!fullTextSearchPlugin) {\n throw new WebinyError(\n `Missing \"contains\" plugin to run the full-text search.`,\n \"MISSING_PLUGIN\"\n );\n }\n\n const search = createFullTextSearch({\n term: fullTextSearch?.term,\n targetFields: fullTextSearch?.fields,\n fields,\n plugin: fullTextSearchPlugin\n });\n\n return records.filter(record => {\n const cachedValues: Record<string, any> = {};\n\n const getCachedValue = (filter: Filter) => {\n const { path } = filter;\n if (cachedValues[path] !== undefined) {\n return cachedValues[path];\n }\n const plainValue = getValue(record, path);\n\n const rawValue = transformValue({\n value: plainValue,\n transform: filter.transformValue\n });\n\n cachedValues[path] = rawValue;\n return rawValue;\n };\n\n const exprResult = executeExpressions({ ...expression, getCachedValue });\n /**\n * If expression result is false we do not need to continue further.\n * Also, if there is no full text search defined, just return the expression result.\n */\n if (!exprResult || !search) {\n return exprResult;\n }\n\n return search(record);\n });\n};\n"],"mappings":";;;;;;;;;;;AACA;;AACA;;AAGA;;AACA;;AACA;;AACA;;AAOA,MAAMA,aAAa,GAAIC,MAAD,IAAiC;EACnD,MAAM;IAAEC,KAAF;IAASC;EAAT,IAAoBF,MAA1B;EAEA,MAAMG,OAAO,GAAGD,MAAM,CAACE,MAAP,CAAcC,OAAd,CAAsB;IAClCJ,KADkC;IAElCK,YAAY,EAAEJ,MAAM,CAACI;EAFa,CAAtB,CAAhB;;EAIA,IAAIJ,MAAM,CAACK,MAAX,EAAmB;IACf,OAAOJ,OAAO,KAAK,KAAnB;EACH;;EACD,OAAOA,OAAP;AACH,CAXD;;AAoBA,MAAMK,kBAAkB,GAAIR,MAAD,IAA+C;EACtE,MAAM;IAAES,WAAF;IAAeC,cAAf;IAA+BC,OAA/B;IAAwCC;EAAxC,IAAsDZ,MAA5D;;EACA,IAAIS,WAAW,CAACI,MAAZ,KAAuB,CAAvB,IAA4BF,OAAO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;IAgBA,MAAMG,UAAU,GAAGrC,kBAAkB,6DAAMO,UAAN;MAAkBL;IAAlB,GAArC;IACA;AACR;AACA;AACA;;IACQ,IAAI,CAACmC,UAAD,IAAe,CAACZ,MAApB,EAA4B;MACxB,OAAOY,UAAP;IACH;;IAED,OAAOZ,MAAM,CAACG,MAAD,CAAb;EACH,CA7BM,CAAP;AA8BH,CAzEM"}
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import { ValueFilterPlugin } from "@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin";
|
|
2
2
|
import { CmsEntry } from "@webiny/api-headless-cms/types";
|
|
3
|
-
import { Field
|
|
3
|
+
import { Field } from "./types";
|
|
4
4
|
interface Params {
|
|
5
5
|
term?: string;
|
|
6
6
|
targetFields?: string[];
|
|
7
|
-
fromStorage: FilterItemFromStorage;
|
|
8
7
|
fields: Record<string, Field>;
|
|
9
8
|
plugin: ValueFilterPlugin;
|
|
10
9
|
}
|
|
11
|
-
interface FtParams {
|
|
12
|
-
item: CmsEntry;
|
|
13
|
-
}
|
|
14
10
|
/**
|
|
15
11
|
* Unfortunately we must use the contains plugin directly as plugins do not support multi field searching.
|
|
16
12
|
*/
|
|
17
|
-
export declare const createFullTextSearch: (params: Params) => ((
|
|
13
|
+
export declare const createFullTextSearch: (params: Params) => ((item: CmsEntry) => boolean) | null;
|
|
18
14
|
export {};
|
|
@@ -18,7 +18,6 @@ const createFullTextSearch = params => {
|
|
|
18
18
|
const {
|
|
19
19
|
term,
|
|
20
20
|
targetFields,
|
|
21
|
-
fromStorage,
|
|
22
21
|
fields: fieldDefinitions,
|
|
23
22
|
plugin
|
|
24
23
|
} = params;
|
|
@@ -27,9 +26,7 @@ const createFullTextSearch = params => {
|
|
|
27
26
|
return null;
|
|
28
27
|
}
|
|
29
28
|
|
|
30
|
-
return
|
|
31
|
-
item
|
|
32
|
-
}) => {
|
|
29
|
+
return item => {
|
|
33
30
|
for (const target of targetFields) {
|
|
34
31
|
/**
|
|
35
32
|
* As fields is a mapped Field objects where key is a path to the value, we can directly find the related field.
|
|
@@ -42,8 +39,7 @@ const createFullTextSearch = params => {
|
|
|
42
39
|
});
|
|
43
40
|
}
|
|
44
41
|
|
|
45
|
-
const
|
|
46
|
-
const value = await fromStorage(field, targetValue);
|
|
42
|
+
const value = (0, _getValue.getValue)(item.values, target);
|
|
47
43
|
|
|
48
44
|
if (!value) {
|
|
49
45
|
continue;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["createFullTextSearch","params","term","targetFields","
|
|
1
|
+
{"version":3,"names":["createFullTextSearch","params","term","targetFields","fields","fieldDefinitions","plugin","trim","length","item","target","field","WebinyError","value","getValue","values","matches","compareValue"],"sources":["fullTextSearch.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport { ValueFilterPlugin } from \"@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin\";\nimport { CmsEntry } from \"@webiny/api-headless-cms/types\";\nimport { Field } from \"./types\";\nimport { getValue } from \"./getValue\";\n\ninterface Params {\n term?: string;\n targetFields?: string[];\n fields: Record<string, Field>;\n plugin: ValueFilterPlugin;\n}\n\n/**\n * Unfortunately we must use the contains plugin directly as plugins do not support multi field searching.\n */\nexport const createFullTextSearch = (params: Params) => {\n const { term, targetFields, fields: fieldDefinitions, plugin } = params;\n if (!term || term.trim().length === 0 || !targetFields || targetFields.length === 0) {\n return null;\n }\n return (item: CmsEntry) => {\n for (const target of targetFields) {\n /**\n * As fields is a mapped Field objects where key is a path to the value, we can directly find the related field.\n */\n const field = fieldDefinitions[target];\n\n if (!field) {\n throw new WebinyError(`Unknown field \"${target}\" in the model.`, \"UNKNOWN_FIELD\", {\n target\n });\n }\n const value = getValue(item.values, target);\n if (!value) {\n continue;\n }\n if (plugin.matches({ value, compareValue: term }) === true) {\n return true;\n }\n }\n return false;\n };\n};\n"],"mappings":";;;;;;;;;AAAA;;AAIA;;AASA;AACA;AACA;AACO,MAAMA,oBAAoB,GAAIC,MAAD,IAAoB;EACpD,MAAM;IAAEC,IAAF;IAAQC,YAAR;IAAsBC,MAAM,EAAEC,gBAA9B;IAAgDC;EAAhD,IAA2DL,MAAjE;;EACA,IAAI,CAACC,IAAD,IAASA,IAAI,CAACK,IAAL,GAAYC,MAAZ,KAAuB,CAAhC,IAAqC,CAACL,YAAtC,IAAsDA,YAAY,CAACK,MAAb,KAAwB,CAAlF,EAAqF;IACjF,OAAO,IAAP;EACH;;EACD,OAAQC,IAAD,IAAoB;IACvB,KAAK,MAAMC,MAAX,IAAqBP,YAArB,EAAmC;MAC/B;AACZ;AACA;MACY,MAAMQ,KAAK,GAAGN,gBAAgB,CAACK,MAAD,CAA9B;;MAEA,IAAI,CAACC,KAAL,EAAY;QACR,MAAM,IAAIC,cAAJ,CAAiB,kBAAiBF,MAAO,iBAAzC,EAA2D,eAA3D,EAA4E;UAC9EA;QAD8E,CAA5E,CAAN;MAGH;;MACD,MAAMG,KAAK,GAAG,IAAAC,kBAAA,EAASL,IAAI,CAACM,MAAd,EAAsBL,MAAtB,CAAd;;MACA,IAAI,CAACG,KAAL,EAAY;QACR;MACH;;MACD,IAAIP,MAAM,CAACU,OAAP,CAAe;QAAEH,KAAF;QAASI,YAAY,EAAEf;MAAvB,CAAf,MAAkD,IAAtD,EAA4D;QACxD,OAAO,IAAP;MACH;IACJ;;IACD,OAAO,KAAP;EACH,CArBD;AAsBH,CA3BM"}
|
|
@@ -0,0 +1,28 @@
|
|
|
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.getWhereValues = void 0;
|
|
9
|
+
|
|
10
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
11
|
+
|
|
12
|
+
const getWhereValues = (value, condition) => {
|
|
13
|
+
const values = value;
|
|
14
|
+
|
|
15
|
+
if (!Array.isArray(values)) {
|
|
16
|
+
throw new _error.default(`Trying to run filtering with "${condition}", but the value sent is not an array.`, `MALFORMED_${condition}_CONDITION`, {
|
|
17
|
+
value
|
|
18
|
+
});
|
|
19
|
+
} else if (values.length === 0) {
|
|
20
|
+
throw new _error.default(`Trying to run filtering with "${condition}", but the value sent is empty array.`, `MALFORMED_${condition}_CONDITION`, {
|
|
21
|
+
value
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return values;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
exports.getWhereValues = getWhereValues;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["getWhereValues","value","condition","values","Array","isArray","WebinyError","length"],"sources":["values.ts"],"sourcesContent":["import { CmsEntryListWhere } from \"@webiny/api-headless-cms/types\";\nimport WebinyError from \"@webiny/error\";\n\nexport const getWhereValues = (value: unknown, condition: \"AND\" | \"OR\") => {\n const values = value as CmsEntryListWhere[] | undefined;\n if (!Array.isArray(values)) {\n throw new WebinyError(\n `Trying to run filtering with \"${condition}\", but the value sent is not an array.`,\n `MALFORMED_${condition}_CONDITION`,\n {\n value\n }\n );\n } else if (values.length === 0) {\n throw new WebinyError(\n `Trying to run filtering with \"${condition}\", but the value sent is empty array.`,\n `MALFORMED_${condition}_CONDITION`,\n {\n value\n }\n );\n }\n return values;\n};\n"],"mappings":";;;;;;;;;AACA;;AAEO,MAAMA,cAAc,GAAG,CAACC,KAAD,EAAiBC,SAAjB,KAA6C;EACvE,MAAMC,MAAM,GAAGF,KAAf;;EACA,IAAI,CAACG,KAAK,CAACC,OAAN,CAAcF,MAAd,CAAL,EAA4B;IACxB,MAAM,IAAIG,cAAJ,CACD,iCAAgCJ,SAAU,wCADzC,EAED,aAAYA,SAAU,YAFrB,EAGF;MACID;IADJ,CAHE,CAAN;EAOH,CARD,MAQO,IAAIE,MAAM,CAACI,MAAP,KAAkB,CAAtB,EAAyB;IAC5B,MAAM,IAAID,cAAJ,CACD,iCAAgCJ,SAAU,uCADzC,EAED,aAAYA,SAAU,YAFrB,EAGF;MACID;IADJ,CAHE,CAAN;EAOH;;EACD,OAAOE,MAAP;AACH,CApBM"}
|
|
@@ -594,10 +594,10 @@ const createEntriesStorageOperations = params => {
|
|
|
594
594
|
gte: " "
|
|
595
595
|
}
|
|
596
596
|
};
|
|
597
|
-
let
|
|
597
|
+
let storageEntries = [];
|
|
598
598
|
|
|
599
599
|
try {
|
|
600
|
-
|
|
600
|
+
storageEntries = await (0, _query.queryAll)(queryAllParams);
|
|
601
601
|
} catch (ex) {
|
|
602
602
|
throw new _error.default(ex.message, "QUERY_ENTRIES_ERROR", {
|
|
603
603
|
error: ex,
|
|
@@ -606,7 +606,7 @@ const createEntriesStorageOperations = params => {
|
|
|
606
606
|
});
|
|
607
607
|
}
|
|
608
608
|
|
|
609
|
-
if (
|
|
609
|
+
if (storageEntries.length === 0) {
|
|
610
610
|
return {
|
|
611
611
|
hasMoreItems: false,
|
|
612
612
|
totalCount: 0,
|
|
@@ -625,24 +625,37 @@ const createEntriesStorageOperations = params => {
|
|
|
625
625
|
|
|
626
626
|
const modelFields = (0, _createFields.createFields)({
|
|
627
627
|
plugins,
|
|
628
|
-
model
|
|
628
|
+
fields: model.fields
|
|
629
629
|
});
|
|
630
|
+
const fromStorage = createStorageTransformCallable(model);
|
|
631
|
+
/**
|
|
632
|
+
* Let's transform records from storage ones to regular ones, so we do not need to do it later.
|
|
633
|
+
*
|
|
634
|
+
* This is always being done, but at least its in parallel.
|
|
635
|
+
*/
|
|
636
|
+
|
|
637
|
+
const records = await Promise.all(storageEntries.map(async storageEntry => {
|
|
638
|
+
const entry = convertFromStorageEntry({
|
|
639
|
+
storageEntry,
|
|
640
|
+
model
|
|
641
|
+
});
|
|
642
|
+
|
|
643
|
+
for (const field of model.fields) {
|
|
644
|
+
entry.values[field.fieldId] = await fromStorage(field, entry.values[field.fieldId]);
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
return entry;
|
|
648
|
+
}));
|
|
630
649
|
/**
|
|
631
650
|
* Filter the read items via the code.
|
|
632
651
|
* It will build the filters out of the where input and transform the values it is using.
|
|
633
652
|
*/
|
|
634
653
|
|
|
635
|
-
const filteredItems =
|
|
636
|
-
items: records
|
|
637
|
-
return convertFromStorageEntry({
|
|
638
|
-
storageEntry: record,
|
|
639
|
-
model
|
|
640
|
-
});
|
|
641
|
-
}),
|
|
654
|
+
const filteredItems = (0, _filtering.filter)({
|
|
655
|
+
items: records,
|
|
642
656
|
where,
|
|
643
657
|
plugins,
|
|
644
658
|
fields: modelFields,
|
|
645
|
-
fromStorage: createStorageTransformCallable(model),
|
|
646
659
|
fullTextSearch: {
|
|
647
660
|
term: search,
|
|
648
661
|
fields: fields || []
|