@webiny/api-headless-cms-ddb 6.3.0 → 6.4.0-beta.1
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/definitions/entry.js +7 -9
- package/definitions/entry.js.map +1 -1
- package/definitions/group.js +7 -9
- package/definitions/group.js.map +1 -1
- package/definitions/model.js +7 -9
- package/definitions/model.js.map +1 -1
- package/definitions/table.js +6 -7
- package/definitions/table.js.map +1 -1
- package/definitions/types.d.ts +1 -0
- package/definitions/types.js +0 -3
- package/dynamoDb/index.js +6 -1
- package/dynamoDb/index.js.map +1 -1
- package/dynamoDb/path/locationFolderId.js +16 -29
- package/dynamoDb/path/locationFolderId.js.map +1 -1
- package/dynamoDb/path/plainObject.js +11 -21
- package/dynamoDb/path/plainObject.js.map +1 -1
- package/dynamoDb/transformValue/datetime.js +21 -32
- package/dynamoDb/transformValue/datetime.js.map +1 -1
- package/index.js +67 -77
- package/index.js.map +1 -1
- package/operations/entry/dataLoader/DataLoaderCache.js +22 -26
- package/operations/entry/dataLoader/DataLoaderCache.js.map +1 -1
- package/operations/entry/dataLoader/constants.js +2 -1
- package/operations/entry/dataLoader/constants.js.map +1 -1
- package/operations/entry/dataLoader/createBatchScheduleFn.js +6 -15
- package/operations/entry/dataLoader/createBatchScheduleFn.js.map +1 -1
- package/operations/entry/dataLoader/getAllEntryRevisions.js +18 -29
- package/operations/entry/dataLoader/getAllEntryRevisions.js.map +1 -1
- package/operations/entry/dataLoader/getLatestRevisionByEntryId.js +31 -41
- package/operations/entry/dataLoader/getLatestRevisionByEntryId.js.map +1 -1
- package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js +31 -41
- package/operations/entry/dataLoader/getPublishedRevisionByEntryId.js.map +1 -1
- package/operations/entry/dataLoader/getRevisionById.js +33 -47
- package/operations/entry/dataLoader/getRevisionById.js.map +1 -1
- package/operations/entry/dataLoader/index.js +8 -9
- package/operations/entry/dataLoader/index.js.map +1 -1
- package/operations/entry/dataLoader/types.js +0 -3
- package/operations/entry/dataLoaders.js +81 -99
- package/operations/entry/dataLoaders.js.map +1 -1
- package/operations/entry/filtering/createExpressions.js +109 -157
- package/operations/entry/filtering/createExpressions.js.map +1 -1
- package/operations/entry/filtering/createFields.js +83 -96
- package/operations/entry/filtering/createFields.js.map +1 -1
- package/operations/entry/filtering/extractSort.js +50 -74
- package/operations/entry/filtering/extractSort.js.map +1 -1
- package/operations/entry/filtering/filter.js +72 -140
- package/operations/entry/filtering/filter.js.map +1 -1
- package/operations/entry/filtering/fullTextSearch.js +21 -38
- package/operations/entry/filtering/fullTextSearch.js.map +1 -1
- package/operations/entry/filtering/getValue.js +31 -53
- package/operations/entry/filtering/getValue.js.map +1 -1
- package/operations/entry/filtering/index.js +0 -2
- package/operations/entry/filtering/mapPlugins.js +15 -22
- package/operations/entry/filtering/mapPlugins.js.map +1 -1
- package/operations/entry/filtering/plugins/defaultFilterCreate.js +29 -33
- package/operations/entry/filtering/plugins/defaultFilterCreate.js.map +1 -1
- package/operations/entry/filtering/plugins/index.js +7 -3
- package/operations/entry/filtering/plugins/index.js.map +1 -1
- package/operations/entry/filtering/plugins/objectFilterCreate.js +60 -76
- package/operations/entry/filtering/plugins/objectFilterCreate.js.map +1 -1
- package/operations/entry/filtering/plugins/refFilterCreate.js +55 -66
- package/operations/entry/filtering/plugins/refFilterCreate.js.map +1 -1
- package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js +43 -56
- package/operations/entry/filtering/plugins/searchableJsonFilterCreate.js.map +1 -1
- package/operations/entry/filtering/sort.js +34 -60
- package/operations/entry/filtering/sort.js.map +1 -1
- package/operations/entry/filtering/systemFields.js +144 -126
- package/operations/entry/filtering/systemFields.js.map +1 -1
- package/operations/entry/filtering/transform.js +4 -8
- package/operations/entry/filtering/transform.js.map +1 -1
- package/operations/entry/filtering/types.js +0 -3
- package/operations/entry/filtering/values.js +11 -12
- package/operations/entry/filtering/values.js.map +1 -1
- package/operations/entry/filtering/where.js +17 -23
- package/operations/entry/filtering/where.js.map +1 -1
- package/operations/entry/index.js +894 -1148
- package/operations/entry/index.js.map +1 -1
- package/operations/entry/keys.js +54 -77
- package/operations/entry/keys.js.map +1 -1
- package/operations/group/index.js +113 -134
- package/operations/group/index.js.map +1 -1
- package/operations/model/index.js +100 -121
- package/operations/model/index.js.map +1 -1
- package/package.json +16 -16
- package/plugins/CmsEntryFieldFilterPathPlugin.js +23 -33
- package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -1
- package/plugins/CmsEntryFieldFilterPlugin.js +16 -17
- package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -1
- package/plugins/CmsEntryFieldSortingPlugin.js +16 -15
- package/plugins/CmsEntryFieldSortingPlugin.js.map +1 -1
- package/plugins/CmsFieldFilterValueTransformPlugin.js +15 -12
- package/plugins/CmsFieldFilterValueTransformPlugin.js.map +1 -1
- package/plugins/index.js +0 -2
- package/types.js +6 -5
- package/types.js.map +1 -1
- package/definitions/types.js.map +0 -1
- package/operations/entry/dataLoader/types.js.map +0 -1
- package/operations/entry/filtering/index.js.map +0 -1
- package/operations/entry/filtering/types.js.map +0 -1
- package/plugins/index.js.map +0 -1
|
@@ -1,108 +1,90 @@
|
|
|
1
|
-
import
|
|
1
|
+
import error from "@webiny/error";
|
|
2
2
|
import { DataLoaderCache } from "./dataLoader/DataLoaderCache.js";
|
|
3
3
|
import { getDataLoaderFactory } from "./dataLoader/index.js";
|
|
4
4
|
import { parseIdentifier } from "@webiny/utils";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
} = parseIdentifier(id);
|
|
27
|
-
return entryId;
|
|
28
|
-
});
|
|
29
|
-
return await this.loadMany("getPublishedRevisionByEntryId", params, ids);
|
|
30
|
-
}
|
|
31
|
-
async getLatestRevisionByEntryId(params) {
|
|
32
|
-
const ids = params.ids.map(id => {
|
|
33
|
-
const {
|
|
34
|
-
id: entryId
|
|
35
|
-
} = parseIdentifier(id);
|
|
36
|
-
return entryId;
|
|
37
|
-
});
|
|
38
|
-
return await this.loadMany("getLatestRevisionByEntryId", params, ids);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* TODO @ts-refactor
|
|
43
|
-
* Maybe pass on the generics to DataLoader definition?
|
|
44
|
-
*/
|
|
45
|
-
getLoader(name, params) {
|
|
46
|
-
const {
|
|
47
|
-
model
|
|
48
|
-
} = params;
|
|
49
|
-
const cacheParams = {
|
|
50
|
-
tenant: model.tenant,
|
|
51
|
-
modelId: model.modelId,
|
|
52
|
-
name
|
|
53
|
-
};
|
|
54
|
-
let loader = this.cache.getDataLoader(cacheParams);
|
|
55
|
-
if (loader) {
|
|
56
|
-
return loader;
|
|
5
|
+
class DataLoadersHandler {
|
|
6
|
+
constructor(params){
|
|
7
|
+
this.cache = new DataLoaderCache();
|
|
8
|
+
this.entity = params.entity;
|
|
9
|
+
}
|
|
10
|
+
async getAllEntryRevisions(params) {
|
|
11
|
+
const ids = params.ids.map((id)=>{
|
|
12
|
+
const { id: entryId } = parseIdentifier(id);
|
|
13
|
+
return entryId;
|
|
14
|
+
});
|
|
15
|
+
return await this.loadMany("getAllEntryRevisions", params, ids);
|
|
16
|
+
}
|
|
17
|
+
async getRevisionById(params) {
|
|
18
|
+
return await this.loadMany("getRevisionById", params, params.ids);
|
|
19
|
+
}
|
|
20
|
+
async getPublishedRevisionByEntryId(params) {
|
|
21
|
+
const ids = params.ids.map((id)=>{
|
|
22
|
+
const { id: entryId } = parseIdentifier(id);
|
|
23
|
+
return entryId;
|
|
24
|
+
});
|
|
25
|
+
return await this.loadMany("getPublishedRevisionByEntryId", params, ids);
|
|
57
26
|
}
|
|
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
|
-
|
|
27
|
+
async getLatestRevisionByEntryId(params) {
|
|
28
|
+
const ids = params.ids.map((id)=>{
|
|
29
|
+
const { id: entryId } = parseIdentifier(id);
|
|
30
|
+
return entryId;
|
|
31
|
+
});
|
|
32
|
+
return await this.loadMany("getLatestRevisionByEntryId", params, ids);
|
|
33
|
+
}
|
|
34
|
+
getLoader(name, params) {
|
|
35
|
+
const { model } = params;
|
|
36
|
+
const cacheParams = {
|
|
37
|
+
tenant: model.tenant,
|
|
38
|
+
modelId: model.modelId,
|
|
39
|
+
name
|
|
40
|
+
};
|
|
41
|
+
let loader = this.cache.getDataLoader(cacheParams);
|
|
42
|
+
if (loader) return loader;
|
|
43
|
+
const factory = getDataLoaderFactory(name);
|
|
44
|
+
loader = factory({
|
|
45
|
+
entity: this.entity,
|
|
46
|
+
tenant: model.tenant,
|
|
47
|
+
modelId: model.modelId
|
|
48
|
+
});
|
|
49
|
+
this.cache.setDataLoader(cacheParams, loader);
|
|
50
|
+
return loader;
|
|
51
|
+
}
|
|
52
|
+
async loadMany(loader, params, ids) {
|
|
53
|
+
let results = [];
|
|
54
|
+
try {
|
|
55
|
+
results = await this.getLoader(loader, params).loadMany(ids);
|
|
56
|
+
if (true === Array.isArray(results)) return results.reduce((acc, res)=>{
|
|
57
|
+
if (false === Array.isArray(res)) {
|
|
58
|
+
if (res && res.message) throw new error(res.message, res.code, {
|
|
59
|
+
...res,
|
|
60
|
+
data: JSON.stringify(res.data || {})
|
|
61
|
+
});
|
|
62
|
+
throw new error("Result from the data loader must be an array of arrays which contain requested items.", "DATA_LOADER_RESULTS_ERROR", {
|
|
63
|
+
...params,
|
|
64
|
+
loader
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
acc.push(...res);
|
|
68
|
+
return acc;
|
|
69
|
+
}, []);
|
|
70
|
+
} catch (ex) {
|
|
71
|
+
throw new error(ex.message || "Data loader error.", ex.code || "DATA_LOADER_ERROR", {
|
|
72
|
+
error: ex,
|
|
73
|
+
...params,
|
|
74
|
+
loader,
|
|
75
|
+
ids
|
|
83
76
|
});
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
loader,
|
|
94
|
-
ids
|
|
95
|
-
});
|
|
77
|
+
}
|
|
78
|
+
throw new error("Data loader did not return array of items or empty array.", "INVALID_DATA_LOADER_RESULT", {
|
|
79
|
+
loader,
|
|
80
|
+
ids,
|
|
81
|
+
results
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
clearAll(params) {
|
|
85
|
+
this.cache.clearAll(params?.model);
|
|
96
86
|
}
|
|
97
|
-
throw new WebinyError(`Data loader did not return array of items or empty array.`, "INVALID_DATA_LOADER_RESULT", {
|
|
98
|
-
loader,
|
|
99
|
-
ids,
|
|
100
|
-
results
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
clearAll(params) {
|
|
104
|
-
this.cache.clearAll(params?.model);
|
|
105
|
-
}
|
|
106
87
|
}
|
|
88
|
+
export { DataLoadersHandler };
|
|
107
89
|
|
|
108
90
|
//# sourceMappingURL=dataLoaders.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"operations/entry/dataLoaders.js","sources":["../../../src/operations/entry/dataLoaders.ts"],"sourcesContent":["import type DataLoader from \"dataloader\";\nimport WebinyError from \"@webiny/error\";\nimport type {\n CmsEntryValues,\n CmsModel,\n CmsStorageEntry\n} from \"@webiny/api-headless-cms/types/index.js\";\nimport type { CacheKeyParams } from \"~/operations/entry/dataLoader/DataLoaderCache.js\";\nimport { DataLoaderCache } from \"~/operations/entry/dataLoader/DataLoaderCache.js\";\nimport type { DataLoaders } from \"~/operations/entry/dataLoader/index.js\";\nimport { getDataLoaderFactory } from \"~/operations/entry/dataLoader/index.js\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport type {\n DataLoadersHandlerInterfaceClearAllParams,\n IDataLoadersHandler,\n IEntryEntity\n} from \"~/types.js\";\n\ninterface DataLoaderParams {\n model: Pick<CmsModel, \"tenant\" | \"modelId\">;\n ids: readonly string[];\n}\n\ninterface GetLoaderParams {\n model: Pick<CmsModel, \"tenant\" | \"modelId\">;\n}\n\ninterface IDataLoadersHandlerParams {\n entity: IEntryEntity;\n}\n\nexport class DataLoadersHandler implements IDataLoadersHandler {\n private readonly entity;\n private readonly cache = new DataLoaderCache();\n\n public constructor(params: IDataLoadersHandlerParams) {\n this.entity = params.entity;\n }\n\n public async getAllEntryRevisions<T extends CmsEntryValues = CmsEntryValues>(\n params: DataLoaderParams\n ): Promise<CmsStorageEntry<T>[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany<T>(\"getAllEntryRevisions\", params, ids);\n }\n\n public async getRevisionById<T extends CmsEntryValues = CmsEntryValues>(\n params: DataLoaderParams\n ): Promise<CmsStorageEntry<T>[]> {\n return await this.loadMany<T>(\"getRevisionById\", params, params.ids);\n }\n\n public async getPublishedRevisionByEntryId<T extends CmsEntryValues = CmsEntryValues>(\n params: DataLoaderParams\n ): Promise<CmsStorageEntry<T>[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany<T>(\"getPublishedRevisionByEntryId\", params, ids);\n }\n\n public async getLatestRevisionByEntryId<T extends CmsEntryValues = CmsEntryValues>(\n params: DataLoaderParams\n ): Promise<CmsStorageEntry<T>[]> {\n const ids = params.ids.map(id => {\n const { id: entryId } = parseIdentifier(id);\n return entryId;\n });\n return await this.loadMany<T>(\"getLatestRevisionByEntryId\", params, ids);\n }\n\n /**\n * TODO @ts-refactor\n * Maybe pass on the generics to DataLoader definition?\n */\n private getLoader(name: DataLoaders, params: GetLoaderParams): DataLoader<any, any> {\n const { model } = params;\n const cacheParams: CacheKeyParams = {\n tenant: model.tenant,\n modelId: model.modelId,\n name\n };\n let loader = this.cache.getDataLoader(cacheParams);\n if (loader) {\n return loader;\n }\n const factory = getDataLoaderFactory(name);\n loader = factory({\n entity: this.entity,\n tenant: model.tenant,\n modelId: model.modelId\n });\n this.cache.setDataLoader(cacheParams, loader);\n return loader;\n }\n\n private async loadMany<T extends CmsEntryValues = CmsEntryValues>(\n loader: DataLoaders,\n params: GetLoaderParams,\n ids: readonly string[]\n ): Promise<CmsStorageEntry<T>[]> {\n let results: any[] = [];\n try {\n results = await this.getLoader(loader, params).loadMany(ids);\n if (Array.isArray(results) === true) {\n return results.reduce((acc, res) => {\n if (Array.isArray(res) === false) {\n if (res && res.message) {\n throw new WebinyError(res.message, res.code, {\n ...res,\n data: JSON.stringify(res.data || {})\n });\n }\n throw new WebinyError(\n \"Result from the data loader must be an array of arrays which contain requested items.\",\n \"DATA_LOADER_RESULTS_ERROR\",\n {\n ...params,\n loader\n }\n );\n }\n acc.push(...res);\n return acc;\n }, []);\n }\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Data loader error.\",\n ex.code || \"DATA_LOADER_ERROR\",\n {\n error: ex,\n ...params,\n loader,\n ids\n }\n );\n }\n throw new WebinyError(\n `Data loader did not return array of items or empty array.`,\n \"INVALID_DATA_LOADER_RESULT\",\n {\n loader,\n ids,\n results\n }\n );\n }\n\n public clearAll(params?: DataLoadersHandlerInterfaceClearAllParams): void {\n this.cache.clearAll(params?.model);\n }\n}\n"],"names":["DataLoadersHandler","params","DataLoaderCache","ids","id","entryId","parseIdentifier","name","model","cacheParams","loader","factory","getDataLoaderFactory","results","Array","acc","res","WebinyError","JSON","ex"],"mappings":";;;;AA+BO,MAAMA;IAIT,YAAmBC,MAAiC,CAAE;aAFrC,KAAK,GAAG,IAAIC;QAGzB,IAAI,CAAC,MAAM,GAAGD,OAAO,MAAM;IAC/B;IAEA,MAAa,qBACTA,MAAwB,EACK;QAC7B,MAAME,MAAMF,OAAO,GAAG,CAAC,GAAG,CAACG,CAAAA;YACvB,MAAM,EAAE,IAAIC,OAAO,EAAE,GAAGC,gBAAgBF;YACxC,OAAOC;QACX;QACA,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAI,wBAAwBJ,QAAQE;IAClE;IAEA,MAAa,gBACTF,MAAwB,EACK;QAC7B,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAI,mBAAmBA,QAAQA,OAAO,GAAG;IACvE;IAEA,MAAa,8BACTA,MAAwB,EACK;QAC7B,MAAME,MAAMF,OAAO,GAAG,CAAC,GAAG,CAACG,CAAAA;YACvB,MAAM,EAAE,IAAIC,OAAO,EAAE,GAAGC,gBAAgBF;YACxC,OAAOC;QACX;QACA,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAI,iCAAiCJ,QAAQE;IAC3E;IAEA,MAAa,2BACTF,MAAwB,EACK;QAC7B,MAAME,MAAMF,OAAO,GAAG,CAAC,GAAG,CAACG,CAAAA;YACvB,MAAM,EAAE,IAAIC,OAAO,EAAE,GAAGC,gBAAgBF;YACxC,OAAOC;QACX;QACA,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAI,8BAA8BJ,QAAQE;IACxE;IAMQ,UAAUI,IAAiB,EAAEN,MAAuB,EAAwB;QAChF,MAAM,EAAEO,KAAK,EAAE,GAAGP;QAClB,MAAMQ,cAA8B;YAChC,QAAQD,MAAM,MAAM;YACpB,SAASA,MAAM,OAAO;YACtBD;QACJ;QACA,IAAIG,SAAS,IAAI,CAAC,KAAK,CAAC,aAAa,CAACD;QACtC,IAAIC,QACA,OAAOA;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getMappedPlugins } from "./mapPlugins.js";
|
|
3
3
|
import { extractWhereParams } from "./where.js";
|
|
4
4
|
import { transformValue } from "./transform.js";
|
|
@@ -6,167 +6,119 @@ import { CmsEntryFieldFilterPlugin } from "../../../plugins/CmsEntryFieldFilterP
|
|
|
6
6
|
import { getWhereValues } from "./values.js";
|
|
7
7
|
import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
|
|
8
8
|
import { ValueFilterRegistry } from "@webiny/db-dynamodb/exports/api/db.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const valueFilterRegistry = container.resolve(ValueFilterRegistry);
|
|
17
|
-
const transformValuePlugins = getMappedPlugins({
|
|
18
|
-
plugins,
|
|
19
|
-
type: "cms-field-filter-value-transform",
|
|
20
|
-
property: "fieldType"
|
|
21
|
-
});
|
|
22
|
-
const fieldFilterCreatePlugins = getMappedPlugins({
|
|
23
|
-
plugins,
|
|
24
|
-
type: CmsEntryFieldFilterPlugin.type,
|
|
25
|
-
property: "fieldType"
|
|
26
|
-
});
|
|
27
|
-
const defaultFilterCreatePlugin = fieldFilterCreatePlugins["*"];
|
|
28
|
-
const getFilterCreatePlugin = type => {
|
|
29
|
-
const fieldType = getBaseFieldType({
|
|
30
|
-
type
|
|
9
|
+
const createExpressions = (params)=>{
|
|
10
|
+
const { where, plugins, fields, container } = params;
|
|
11
|
+
const valueFilterRegistry = container.resolve(ValueFilterRegistry);
|
|
12
|
+
const transformValuePlugins = getMappedPlugins({
|
|
13
|
+
plugins,
|
|
14
|
+
type: "cms-field-filter-value-transform",
|
|
15
|
+
property: "fieldType"
|
|
31
16
|
});
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
throw new WebinyError(`There is no filter create plugin for the field type "${fieldType}".`, "MISSING_FILTER_CREATE_PLUGIN", {
|
|
37
|
-
fieldType
|
|
17
|
+
const fieldFilterCreatePlugins = getMappedPlugins({
|
|
18
|
+
plugins,
|
|
19
|
+
type: CmsEntryFieldFilterPlugin.type,
|
|
20
|
+
property: "fieldType"
|
|
38
21
|
});
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
22
|
+
const defaultFilterCreatePlugin = fieldFilterCreatePlugins["*"];
|
|
23
|
+
const getFilterCreatePlugin = (type)=>{
|
|
24
|
+
const fieldType = getBaseFieldType({
|
|
25
|
+
type
|
|
26
|
+
});
|
|
27
|
+
const filterCreatePlugin = fieldFilterCreatePlugins[fieldType] || defaultFilterCreatePlugin;
|
|
28
|
+
if (filterCreatePlugin) return filterCreatePlugin;
|
|
29
|
+
throw new error(`There is no filter create plugin for the field type "${fieldType}".`, "MISSING_FILTER_CREATE_PLUGIN", {
|
|
30
|
+
fieldType
|
|
31
|
+
});
|
|
48
32
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* If there are "AND" or "OR" keys, let's sort them out first.
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* AND conditional
|
|
60
|
-
*/
|
|
61
|
-
if (key === "AND") {
|
|
62
|
-
const childWhereList = getWhereValues(value, key);
|
|
63
|
-
const childExpression = {
|
|
64
|
-
condition: "AND",
|
|
65
|
-
filters: [],
|
|
66
|
-
expressions: []
|
|
33
|
+
const createExpression = ({ where, condition })=>{
|
|
34
|
+
const expression = {
|
|
35
|
+
filters: [],
|
|
36
|
+
expressions: [],
|
|
37
|
+
condition
|
|
67
38
|
};
|
|
68
|
-
for
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
39
|
+
for(const key in where){
|
|
40
|
+
const value = where[key];
|
|
41
|
+
if (void 0 === value) continue;
|
|
42
|
+
if ("AND" === key) {
|
|
43
|
+
const childWhereList = getWhereValues(value, key);
|
|
44
|
+
const childExpression = {
|
|
45
|
+
condition: "AND",
|
|
46
|
+
filters: [],
|
|
47
|
+
expressions: []
|
|
48
|
+
};
|
|
49
|
+
for (const childWhere of childWhereList){
|
|
50
|
+
const result = createExpression({
|
|
51
|
+
where: childWhere,
|
|
52
|
+
condition: "AND"
|
|
53
|
+
});
|
|
54
|
+
childExpression.expressions.push(result);
|
|
55
|
+
}
|
|
56
|
+
expression.expressions.push(childExpression);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if ("OR" === key) {
|
|
60
|
+
const childWhereList = getWhereValues(value, key);
|
|
61
|
+
const childExpression = {
|
|
62
|
+
condition: "OR",
|
|
63
|
+
filters: [],
|
|
64
|
+
expressions: []
|
|
65
|
+
};
|
|
66
|
+
for (const childWhere of childWhereList){
|
|
67
|
+
const result = createExpression({
|
|
68
|
+
where: childWhere,
|
|
69
|
+
condition: "AND"
|
|
70
|
+
});
|
|
71
|
+
childExpression.expressions.push(result);
|
|
72
|
+
}
|
|
73
|
+
expression.expressions.push(childExpression);
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
const whereParams = extractWhereParams(key);
|
|
77
|
+
if (!whereParams) continue;
|
|
78
|
+
const { fieldId, operation, negate } = whereParams;
|
|
79
|
+
const field = fields[fieldId];
|
|
80
|
+
if (!field) throw new error(`There is no field with the fieldId "${fieldId}".`, "FIELD_ERROR", {
|
|
81
|
+
fieldId
|
|
82
|
+
});
|
|
83
|
+
const filterCreatePlugin = getFilterCreatePlugin(field.type);
|
|
84
|
+
const fieldType = getBaseFieldType(field);
|
|
85
|
+
const transformValuePlugin = transformValuePlugins[fieldType];
|
|
86
|
+
const transformValueCallable = (value)=>{
|
|
87
|
+
if (!transformValuePlugin) return value;
|
|
88
|
+
return transformValuePlugin.transform({
|
|
89
|
+
field,
|
|
90
|
+
value
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
const result = filterCreatePlugin.create({
|
|
94
|
+
key,
|
|
95
|
+
value,
|
|
96
|
+
valueFilterRegistry,
|
|
97
|
+
transformValuePlugins,
|
|
98
|
+
getFilterCreatePlugin,
|
|
99
|
+
operation,
|
|
100
|
+
negate,
|
|
101
|
+
field,
|
|
102
|
+
fields,
|
|
103
|
+
compareValue: transformValue({
|
|
104
|
+
value,
|
|
105
|
+
transform: transformValueCallable
|
|
106
|
+
}),
|
|
107
|
+
transformValue: transformValueCallable
|
|
108
|
+
});
|
|
109
|
+
if (!(!result || Array.isArray(result) && 0 === result.length)) expression.filters.push(...Array.isArray(result) ? result : [
|
|
110
|
+
result
|
|
111
|
+
]);
|
|
74
112
|
}
|
|
75
|
-
expression
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const childExpression = {
|
|
84
|
-
condition: "OR",
|
|
85
|
-
filters: [],
|
|
86
|
-
expressions: []
|
|
87
|
-
};
|
|
88
|
-
for (const childWhere of childWhereList) {
|
|
89
|
-
const result = createExpression({
|
|
90
|
-
where: childWhere,
|
|
91
|
-
condition: "AND"
|
|
92
|
-
});
|
|
93
|
-
childExpression.expressions.push(result);
|
|
94
|
-
}
|
|
95
|
-
expression.expressions.push(childExpression);
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
const whereParams = extractWhereParams(key);
|
|
99
|
-
if (!whereParams) {
|
|
100
|
-
continue;
|
|
101
|
-
}
|
|
102
|
-
const {
|
|
103
|
-
fieldId,
|
|
104
|
-
operation,
|
|
105
|
-
negate
|
|
106
|
-
} = whereParams;
|
|
107
|
-
const field = fields[fieldId];
|
|
108
|
-
if (!field) {
|
|
109
|
-
throw new WebinyError(`There is no field with the fieldId "${fieldId}".`, "FIELD_ERROR", {
|
|
110
|
-
fieldId
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* We need a filter create plugin for this type.
|
|
116
|
-
*/
|
|
117
|
-
const filterCreatePlugin = getFilterCreatePlugin(field.type);
|
|
118
|
-
const fieldType = getBaseFieldType(field);
|
|
119
|
-
const transformValuePlugin = transformValuePlugins[fieldType];
|
|
120
|
-
const transformValueCallable = value => {
|
|
121
|
-
if (!transformValuePlugin) {
|
|
122
|
-
return value;
|
|
123
|
-
}
|
|
124
|
-
return transformValuePlugin.transform({
|
|
125
|
-
field,
|
|
126
|
-
value
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
const result = filterCreatePlugin.create({
|
|
130
|
-
key,
|
|
131
|
-
value,
|
|
132
|
-
valueFilterRegistry,
|
|
133
|
-
transformValuePlugins,
|
|
134
|
-
getFilterCreatePlugin,
|
|
135
|
-
operation,
|
|
136
|
-
negate,
|
|
137
|
-
field,
|
|
138
|
-
fields,
|
|
139
|
-
compareValue: transformValue({
|
|
140
|
-
value,
|
|
141
|
-
transform: transformValueCallable
|
|
142
|
-
}),
|
|
143
|
-
transformValue: transformValueCallable
|
|
144
|
-
});
|
|
145
|
-
/**
|
|
146
|
-
* There is a possibility of
|
|
147
|
-
* - no result
|
|
148
|
-
* - result being an array
|
|
149
|
-
* - result being an object
|
|
150
|
-
*/
|
|
151
|
-
if (!result || Array.isArray(result) && result.length === 0) {
|
|
152
|
-
continue;
|
|
153
|
-
}
|
|
154
|
-
expression.filters.push(...(Array.isArray(result) ? result : [result]));
|
|
155
|
-
}
|
|
156
|
-
return expression;
|
|
157
|
-
};
|
|
158
|
-
const expression = createExpression({
|
|
159
|
-
where,
|
|
160
|
-
condition: "AND"
|
|
161
|
-
});
|
|
162
|
-
/**
|
|
163
|
-
* If the first expression has no filters and has only one expression, put that expression as main one.
|
|
164
|
-
* This will mostly be used when having an OR condition as the single expression in the root level of the where
|
|
165
|
-
*/
|
|
166
|
-
if (expression.filters.length > 0 || expression.expressions.length !== 1) {
|
|
167
|
-
return expression;
|
|
168
|
-
}
|
|
169
|
-
return expression.expressions[0];
|
|
113
|
+
return expression;
|
|
114
|
+
};
|
|
115
|
+
const expression = createExpression({
|
|
116
|
+
where,
|
|
117
|
+
condition: "AND"
|
|
118
|
+
});
|
|
119
|
+
if (expression.filters.length > 0 || 1 !== expression.expressions.length) return expression;
|
|
120
|
+
return expression.expressions[0];
|
|
170
121
|
};
|
|
122
|
+
export { createExpressions };
|
|
171
123
|
|
|
172
124
|
//# sourceMappingURL=createExpressions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["WebinyError","getMappedPlugins","extractWhereParams","transformValue","CmsEntryFieldFilterPlugin","getWhereValues","getBaseFieldType","ValueFilterRegistry","createExpressions","params","where","plugins","fields","container","valueFilterRegistry","resolve","transformValuePlugins","type","property","fieldFilterCreatePlugins","defaultFilterCreatePlugin","getFilterCreatePlugin","fieldType","filterCreatePlugin","createExpression","condition","expression","filters","expressions","key","value","undefined","childWhereList","childExpression","childWhere","result","push","whereParams","fieldId","operation","negate","field","transformValuePlugin","transformValueCallable","transform","create","compareValue","Array","isArray","length"],"sources":["createExpressions.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { CmsContext, CmsEntryListWhere } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { CmsFieldFilterValueTransformPlugin } from \"~/types.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport type { Field } from \"./types.js\";\nimport { getMappedPlugins } from \"./mapPlugins.js\";\nimport { extractWhereParams } from \"./where.js\";\nimport { transformValue } from \"./transform.js\";\nimport { CmsEntryFieldFilterPlugin } from \"~/plugins/CmsEntryFieldFilterPlugin.js\";\nimport { getWhereValues } from \"~/operations/entry/filtering/values.js\";\nimport { getBaseFieldType } from \"@webiny/api-headless-cms/utils/getBaseFieldType.js\";\nimport { ValueFilter } from \"@webiny/db-dynamodb/feature/ValueFilter/index.js\";\nimport { ValueFilterRegistry } from \"@webiny/db-dynamodb/exports/api/db.js\";\n\ninterface CreateExpressionParams {\n where: Partial<CmsEntryListWhere>;\n condition: ExpressionCondition;\n}\n\ninterface ICreateExpressionsParams {\n plugins: PluginsContainer;\n container: CmsContext[\"container\"];\n where: Partial<CmsEntryListWhere>;\n fields: Record<string, Field>;\n}\n\nexport type ExpressionCondition = \"AND\" | \"OR\";\n\nexport interface Expression {\n expressions: Expression[];\n filters: Filter[];\n condition: ExpressionCondition;\n}\n\nexport interface Filter {\n field: Field;\n path: string;\n fieldPathId: string;\n filter: ValueFilter.Interface;\n negate: boolean;\n compareValue: any;\n transformValue: <I = any, O = any>(value: I) => O;\n}\n\nexport const createExpressions = (params: ICreateExpressionsParams): Expression => {\n const { where, plugins, fields, container } = params;\n const valueFilterRegistry = container.resolve(ValueFilterRegistry);\n\n const transformValuePlugins = getMappedPlugins<CmsFieldFilterValueTransformPlugin>({\n plugins,\n type: \"cms-field-filter-value-transform\",\n property: \"fieldType\"\n });\n const fieldFilterCreatePlugins = getMappedPlugins<CmsEntryFieldFilterPlugin>({\n plugins,\n type: CmsEntryFieldFilterPlugin.type,\n property: \"fieldType\"\n });\n\n const defaultFilterCreatePlugin = fieldFilterCreatePlugins[\"*\"] as CmsEntryFieldFilterPlugin;\n\n const getFilterCreatePlugin = (type: string) => {\n const fieldType = getBaseFieldType({\n type\n });\n const filterCreatePlugin = fieldFilterCreatePlugins[fieldType] || defaultFilterCreatePlugin;\n if (filterCreatePlugin) {\n return filterCreatePlugin;\n }\n throw new WebinyError(\n `There is no filter create plugin for the field type \"${fieldType}\".`,\n \"MISSING_FILTER_CREATE_PLUGIN\",\n {\n fieldType\n }\n );\n };\n\n const createExpression = ({ where, condition }: CreateExpressionParams): Expression => {\n const expression: Expression = {\n filters: [],\n expressions: [],\n condition\n };\n\n for (const key in where) {\n const value = where[key as keyof typeof where];\n if (value === undefined) {\n continue;\n }\n\n /**\n * If there are \"AND\" or \"OR\" keys, let's sort them out first.\n *\n *\n * AND conditional\n */\n if (key === \"AND\") {\n const childWhereList = getWhereValues(value, key);\n const childExpression: Expression = {\n condition: \"AND\",\n filters: [],\n expressions: []\n };\n for (const childWhere of childWhereList) {\n const result = createExpression({\n where: childWhere,\n condition: \"AND\"\n });\n childExpression.expressions.push(result);\n }\n expression.expressions.push(childExpression);\n continue;\n }\n /**\n * OR conditional\n */\n if (key === \"OR\") {\n const childWhereList = getWhereValues(value, key);\n\n const childExpression: Expression = {\n condition: \"OR\",\n filters: [],\n expressions: []\n };\n for (const childWhere of childWhereList) {\n const result = createExpression({\n where: childWhere,\n condition: \"AND\"\n });\n childExpression.expressions.push(result);\n }\n expression.expressions.push(childExpression);\n continue;\n }\n\n const whereParams = extractWhereParams(key);\n if (!whereParams) {\n continue;\n }\n\n const { fieldId, operation, negate } = whereParams;\n\n const field = fields[fieldId];\n if (!field) {\n throw new WebinyError(\n `There is no field with the fieldId \"${fieldId}\".`,\n \"FIELD_ERROR\",\n {\n fieldId\n }\n );\n }\n\n /**\n * We need a filter create plugin for this type.\n */\n const filterCreatePlugin = getFilterCreatePlugin(field.type);\n\n const fieldType = getBaseFieldType(field);\n\n const transformValuePlugin: CmsFieldFilterValueTransformPlugin =\n transformValuePlugins[fieldType];\n\n const transformValueCallable = (value: any) => {\n if (!transformValuePlugin) {\n return value;\n }\n return transformValuePlugin.transform({\n field,\n value\n });\n };\n\n const result = filterCreatePlugin.create({\n key,\n value,\n valueFilterRegistry,\n transformValuePlugins,\n getFilterCreatePlugin,\n operation,\n negate,\n field,\n fields,\n compareValue: transformValue({\n value,\n transform: transformValueCallable\n }),\n transformValue: transformValueCallable\n });\n /**\n * There is a possibility of\n * - no result\n * - result being an array\n * - result being an object\n */\n if (!result || (Array.isArray(result) && result.length === 0)) {\n continue;\n }\n\n expression.filters.push(...(Array.isArray(result) ? result : [result]));\n }\n\n return expression;\n };\n\n const expression = createExpression({\n where,\n condition: \"AND\"\n });\n /**\n * If the first expression has no filters and has only one expression, put that expression as main one.\n * This will mostly be used when having an OR condition as the single expression in the root level of the where\n */\n if (expression.filters.length > 0 || expression.expressions.length !== 1) {\n return expression;\n }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,IAAIW,KAAK,CAACC,OAAO,CAACb,MAAM,CAAC,GAAGA,MAAM,GAAG,CAACA,MAAM,CAAC,CAAC,CAAC;IAC3E;IAEA,OAAOT,UAAU;EACrB,CAAC;EAED,MAAMA,UAAU,GAAGF,gBAAgB,CAAC;IAChCd,KAAK;IACLe,SAAS,EAAE;EACf,CAAC,CAAC;EACF;AACJ;AACA;AACA;EACI,IAAIC,UAAU,CAACC,OAAO,CAACsB,MAAM,GAAG,CAAC,IAAIvB,UAAU,CAACE,WAAW,CAACqB,MAAM,KAAK,CAAC,EAAE;IACtE,OAAOvB,UAAU;EACrB;EACA,OAAOA,UAAU,CAACE,WAAW,CAAC,CAAC,CAAC;AACpC,CAAC","ignoreList":[]}
|
|
1
|
+
{"version":3,"file":"operations/entry/filtering/createExpressions.js","sources":["../../../../src/operations/entry/filtering/createExpressions.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { CmsContext, CmsEntryListWhere } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { CmsFieldFilterValueTransformPlugin } from \"~/types.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport type { Field } from \"./types.js\";\nimport { getMappedPlugins } from \"./mapPlugins.js\";\nimport { extractWhereParams } from \"./where.js\";\nimport { transformValue } from \"./transform.js\";\nimport { CmsEntryFieldFilterPlugin } from \"~/plugins/CmsEntryFieldFilterPlugin.js\";\nimport { getWhereValues } from \"~/operations/entry/filtering/values.js\";\nimport { getBaseFieldType } from \"@webiny/api-headless-cms/utils/getBaseFieldType.js\";\nimport { ValueFilter } from \"@webiny/db-dynamodb/feature/ValueFilter/index.js\";\nimport { ValueFilterRegistry } from \"@webiny/db-dynamodb/exports/api/db.js\";\n\ninterface CreateExpressionParams {\n where: Partial<CmsEntryListWhere>;\n condition: ExpressionCondition;\n}\n\ninterface ICreateExpressionsParams {\n plugins: PluginsContainer;\n container: CmsContext[\"container\"];\n where: Partial<CmsEntryListWhere>;\n fields: Record<string, Field>;\n}\n\nexport type ExpressionCondition = \"AND\" | \"OR\";\n\nexport interface Expression {\n expressions: Expression[];\n filters: Filter[];\n condition: ExpressionCondition;\n}\n\nexport interface Filter {\n field: Field;\n path: string;\n fieldPathId: string;\n filter: ValueFilter.Interface;\n negate: boolean;\n compareValue: any;\n transformValue: <I = any, O = any>(value: I) => O;\n}\n\nexport const createExpressions = (params: ICreateExpressionsParams): Expression => {\n const { where, plugins, fields, container } = params;\n const valueFilterRegistry = container.resolve(ValueFilterRegistry);\n\n const transformValuePlugins = getMappedPlugins<CmsFieldFilterValueTransformPlugin>({\n plugins,\n type: \"cms-field-filter-value-transform\",\n property: \"fieldType\"\n });\n const fieldFilterCreatePlugins = getMappedPlugins<CmsEntryFieldFilterPlugin>({\n plugins,\n type: CmsEntryFieldFilterPlugin.type,\n property: \"fieldType\"\n });\n\n const defaultFilterCreatePlugin = fieldFilterCreatePlugins[\"*\"] as CmsEntryFieldFilterPlugin;\n\n const getFilterCreatePlugin = (type: string) => {\n const fieldType = getBaseFieldType({\n type\n });\n const filterCreatePlugin = fieldFilterCreatePlugins[fieldType] || defaultFilterCreatePlugin;\n if (filterCreatePlugin) {\n return filterCreatePlugin;\n }\n throw new WebinyError(\n `There is no filter create plugin for the field type \"${fieldType}\".`,\n \"MISSING_FILTER_CREATE_PLUGIN\",\n {\n fieldType\n }\n );\n };\n\n const createExpression = ({ where, condition }: CreateExpressionParams): Expression => {\n const expression: Expression = {\n filters: [],\n expressions: [],\n condition\n };\n\n for (const key in where) {\n const value = where[key as keyof typeof where];\n if (value === undefined) {\n continue;\n }\n\n /**\n * If there are \"AND\" or \"OR\" keys, let's sort them out first.\n *\n *\n * AND conditional\n */\n if (key === \"AND\") {\n const childWhereList = getWhereValues(value, key);\n const childExpression: Expression = {\n condition: \"AND\",\n filters: [],\n expressions: []\n };\n for (const childWhere of childWhereList) {\n const result = createExpression({\n where: childWhere,\n condition: \"AND\"\n });\n childExpression.expressions.push(result);\n }\n expression.expressions.push(childExpression);\n continue;\n }\n /**\n * OR conditional\n */\n if (key === \"OR\") {\n const childWhereList = getWhereValues(value, key);\n\n const childExpression: Expression = {\n condition: \"OR\",\n filters: [],\n expressions: []\n };\n for (const childWhere of childWhereList) {\n const result = createExpression({\n where: childWhere,\n condition: \"AND\"\n });\n childExpression.expressions.push(result);\n }\n expression.expressions.push(childExpression);\n continue;\n }\n\n const whereParams = extractWhereParams(key);\n if (!whereParams) {\n continue;\n }\n\n const { fieldId, operation, negate } = whereParams;\n\n const field = fields[fieldId];\n if (!field) {\n throw new WebinyError(\n `There is no field with the fieldId \"${fieldId}\".`,\n \"FIELD_ERROR\",\n {\n fieldId\n }\n );\n }\n\n /**\n * We need a filter create plugin for this type.\n */\n const filterCreatePlugin = getFilterCreatePlugin(field.type);\n\n const fieldType = getBaseFieldType(field);\n\n const transformValuePlugin: CmsFieldFilterValueTransformPlugin =\n transformValuePlugins[fieldType];\n\n const transformValueCallable = (value: any) => {\n if (!transformValuePlugin) {\n return value;\n }\n return transformValuePlugin.transform({\n field,\n value\n });\n };\n\n const result = filterCreatePlugin.create({\n key,\n value,\n valueFilterRegistry,\n transformValuePlugins,\n getFilterCreatePlugin,\n operation,\n negate,\n field,\n fields,\n compareValue: transformValue({\n value,\n transform: transformValueCallable\n }),\n transformValue: transformValueCallable\n });\n /**\n * There is a possibility of\n * - no result\n * - result being an array\n * - result being an object\n */\n if (!result || (Array.isArray(result) && result.length === 0)) {\n continue;\n }\n\n expression.filters.push(...(Array.isArray(result) ? result : [result]));\n }\n\n return expression;\n };\n\n const expression = createExpression({\n where,\n condition: \"AND\"\n });\n /**\n * If the first expression has no filters and has only one expression, put that expression as main one.\n * This will mostly be used when having an OR condition as the single expression in the root level of the where\n */\n if (expression.filters.length > 0 || expression.expressions.length !== 1) {\n return expression;\n }\n return expression.expressions[0];\n};\n"],"names":["createExpressions","params","where","plugins","fields","container","valueFilterRegistry","ValueFilterRegistry","transformValuePlugins","getMappedPlugins","fieldFilterCreatePlugins","CmsEntryFieldFilterPlugin","defaultFilterCreatePlugin","getFilterCreatePlugin","type","fieldType","getBaseFieldType","filterCreatePlugin","WebinyError","createExpression","condition","expression","key","value","undefined","childWhereList","getWhereValues","childExpression","childWhere","result","whereParams","extractWhereParams","fieldId","operation","negate","field","transformValuePlugin","transformValueCallable","transformValue","Array"],"mappings":";;;;;;;;AA4CO,MAAMA,oBAAoB,CAACC;IAC9B,MAAM,EAAEC,KAAK,EAAEC,OAAO,EAAEC,MAAM,EAAEC,SAAS,EAAE,GAAGJ;IAC9C,MAAMK,sBAAsBD,UAAU,OAAO,CAACE;IAE9C,MAAMC,wBAAwBC,iBAAqD;QAC/EN;QACA,MAAM;QACN,UAAU;IACd;IACA,MAAMO,2BAA2BD,iBAA4C;QACzEN;QACA,MAAMQ,0BAA0B,IAAI;QACpC,UAAU;IACd;IAEA,MAAMC,4BAA4BF,wBAAwB,CAAC,IAAI;IAE/D,MAAMG,wBAAwB,CAACC;QAC3B,MAAMC,YAAYC,iBAAiB;YAC/BF;QACJ;QACA,MAAMG,qBAAqBP,wBAAwB,CAACK,UAAU,IAAIH;QAClE,IAAIK,oBACA,OAAOA;QAEX,MAAM,IAAIC,MACN,CAAC,qDAAqD,EAAEH,UAAU,EAAE,CAAC,EACrE,gCACA;YACIA;QACJ;IAER;IAEA,MAAMI,mBAAmB,CAAC,EAAEjB,KAAK,EAAEkB,SAAS,EAA0B;QAClE,MAAMC,aAAyB;YAC3B,SAAS,EAAE;YACX,aAAa,EAAE;YACfD;QACJ;QAEA,IAAK,MAAME,OAAOpB,MAAO;YACrB,MAAMqB,QAAQrB,KAAK,CAACoB,IAA0B;YAC9C,IAAIC,AAAUC,WAAVD,OACA;YASJ,IAAID,AAAQ,UAARA,KAAe;gBACf,MAAMG,iBAAiBC,eAAeH,OAAOD;gBAC7C,MAAMK,kBAA8B;oBAChC,WAAW;oBACX,SAAS,EAAE;oBACX,aAAa,EAAE;gBACnB;gBACA,KAAK,MAAMC,cAAcH,eAAgB;oBACrC,MAAMI,S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