@webiny/api-headless-cms-storage 0.0.0-unstable.9f53ea597d

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.
Files changed (84) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -0
  3. package/cms/storageHelpers.d.ts +11 -0
  4. package/cms/storageHelpers.js +50 -0
  5. package/cms/storageHelpers.js.map +1 -0
  6. package/filtering/expressions/createExpressions.d.ts +27 -0
  7. package/filtering/expressions/createExpressions.js +122 -0
  8. package/filtering/expressions/createExpressions.js.map +1 -0
  9. package/filtering/fields/createFields.d.ts +11 -0
  10. package/filtering/fields/createFields.js +93 -0
  11. package/filtering/fields/createFields.js.map +1 -0
  12. package/filtering/fields/extractSort.d.ts +17 -0
  13. package/filtering/fields/extractSort.js +60 -0
  14. package/filtering/fields/extractSort.js.map +1 -0
  15. package/filtering/fields/systemFields.d.ts +2 -0
  16. package/filtering/fields/systemFields.js +149 -0
  17. package/filtering/fields/systemFields.js.map +1 -0
  18. package/filtering/fields/types.d.ts +19 -0
  19. package/filtering/fields/types.js +0 -0
  20. package/filtering/filter.d.ts +17 -0
  21. package/filtering/filter.js +83 -0
  22. package/filtering/filter.js.map +1 -0
  23. package/filtering/fullTextSearch.d.ts +11 -0
  24. package/filtering/fullTextSearch.js +25 -0
  25. package/filtering/fullTextSearch.js.map +1 -0
  26. package/filtering/getValue.d.ts +1 -0
  27. package/filtering/getValue.js +37 -0
  28. package/filtering/getValue.js.map +1 -0
  29. package/filtering/mapPlugins.d.ts +8 -0
  30. package/filtering/mapPlugins.js +18 -0
  31. package/filtering/mapPlugins.js.map +1 -0
  32. package/filtering/plugins/defaultFilterCreate.d.ts +2 -0
  33. package/filtering/plugins/defaultFilterCreate.js +33 -0
  34. package/filtering/plugins/defaultFilterCreate.js.map +1 -0
  35. package/filtering/plugins/index.d.ts +1 -0
  36. package/filtering/plugins/index.js +13 -0
  37. package/filtering/plugins/index.js.map +1 -0
  38. package/filtering/plugins/objectFilterCreate.d.ts +2 -0
  39. package/filtering/plugins/objectFilterCreate.js +68 -0
  40. package/filtering/plugins/objectFilterCreate.js.map +1 -0
  41. package/filtering/plugins/refFilterCreate.d.ts +3 -0
  42. package/filtering/plugins/refFilterCreate.js +63 -0
  43. package/filtering/plugins/refFilterCreate.js.map +1 -0
  44. package/filtering/plugins/searchableJsonFilterCreate.d.ts +2 -0
  45. package/filtering/plugins/searchableJsonFilterCreate.js +56 -0
  46. package/filtering/plugins/searchableJsonFilterCreate.js.map +1 -0
  47. package/filtering/sort.d.ts +12 -0
  48. package/filtering/sort.js +41 -0
  49. package/filtering/sort.js.map +1 -0
  50. package/filtering/transform.d.ts +6 -0
  51. package/filtering/transform.js +7 -0
  52. package/filtering/transform.js.map +1 -0
  53. package/filtering/values.d.ts +2 -0
  54. package/filtering/values.js +15 -0
  55. package/filtering/values.js.map +1 -0
  56. package/filtering/where.d.ts +5 -0
  57. package/filtering/where.js +22 -0
  58. package/filtering/where.js.map +1 -0
  59. package/index.d.ts +19 -0
  60. package/index.js +14 -0
  61. package/package.json +34 -0
  62. package/path/locationFolderId.d.ts +2 -0
  63. package/path/locationFolderId.js +21 -0
  64. package/path/locationFolderId.js.map +1 -0
  65. package/path/plainObject.d.ts +2 -0
  66. package/path/plainObject.js +16 -0
  67. package/path/plainObject.js.map +1 -0
  68. package/plugins/CmsEntryFieldFilterPathPlugin.d.ts +23 -0
  69. package/plugins/CmsEntryFieldFilterPathPlugin.js +30 -0
  70. package/plugins/CmsEntryFieldFilterPathPlugin.js.map +1 -0
  71. package/plugins/CmsEntryFieldFilterPlugin.d.ts +39 -0
  72. package/plugins/CmsEntryFieldFilterPlugin.js +20 -0
  73. package/plugins/CmsEntryFieldFilterPlugin.js.map +1 -0
  74. package/plugins/CmsEntryFieldSortingPlugin.d.ts +37 -0
  75. package/plugins/CmsEntryFieldSortingPlugin.js +20 -0
  76. package/plugins/CmsEntryFieldSortingPlugin.js.map +1 -0
  77. package/plugins/CmsFieldFilterValueTransformPlugin.d.ts +21 -0
  78. package/plugins/CmsFieldFilterValueTransformPlugin.js +19 -0
  79. package/plugins/CmsFieldFilterValueTransformPlugin.js.map +1 -0
  80. package/plugins/index.d.ts +7 -0
  81. package/plugins/index.js +4 -0
  82. package/transforms/datetime.d.ts +2 -0
  83. package/transforms/datetime.js +37 -0
  84. package/transforms/datetime.js.map +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Webiny
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @webiny/api-headless-cms-storage
2
+
3
+ > [!NOTE]
4
+ > This package is part of the [Webiny](https://www.webiny.com) monorepo.
5
+ > It’s **included in every Webiny project by default** and is not meant to be used as a standalone package.
6
+
7
+ 📘 **Documentation:** [https://www.webiny.com/docs](https://www.webiny.com/docs)
8
+
9
+ ---
10
+
11
+ _This README file is automatically generated during the publish process._
@@ -0,0 +1,11 @@
1
+ import type { CmsEntry, CmsEntryUniqueValue, CmsEntryValues, CmsModel, StorageOperationsCmsModel } from "@webiny/api-headless-cms/types/index.js";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ import { StorageOperationsCmsModelPlugin } from "@webiny/api-headless-cms";
4
+ import type { StorageTransformRegistry } from "@webiny/api-headless-cms/exports/api/cms/storage.js";
5
+ import type { FilterItemFromStorage } from "../filtering/fields/types.js";
6
+ export declare const createStorageModelAccessor: (plugins: PluginsContainer) => {
7
+ getPlugin: () => StorageOperationsCmsModelPlugin;
8
+ getModel: <T extends CmsEntryValues = CmsEntryValues>(model: CmsModel) => StorageOperationsCmsModel<T>;
9
+ };
10
+ export declare const createStorageTransformCallable: (storageTransformRegistry: StorageTransformRegistry.Interface, model: StorageOperationsCmsModel) => FilterItemFromStorage;
11
+ export declare const aggregateUniqueFieldValues: <T extends CmsEntryValues = CmsEntryValues>(items: CmsEntry<T>[], fieldId: string) => CmsEntryUniqueValue[];
@@ -0,0 +1,50 @@
1
+ import { StorageOperationsCmsModelPlugin } from "@webiny/api-headless-cms";
2
+ import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
3
+ const createStorageModelAccessor = (plugins)=>{
4
+ let cached;
5
+ const getPlugin = ()=>{
6
+ if (cached) return cached;
7
+ cached = plugins.oneByType(StorageOperationsCmsModelPlugin.type);
8
+ return cached;
9
+ };
10
+ const getModel = (model)=>{
11
+ const plugin = getPlugin();
12
+ return plugin.getModel(model);
13
+ };
14
+ return {
15
+ getPlugin,
16
+ getModel
17
+ };
18
+ };
19
+ const createStorageTransformCallable = (storageTransformRegistry, model)=>async (field, value)=>{
20
+ const fieldType = getBaseFieldType(field);
21
+ const storageTransform = storageTransformRegistry.get(fieldType);
22
+ if (!storageTransform) return value;
23
+ const result = await storageTransform.fromStorage({
24
+ model,
25
+ field,
26
+ value,
27
+ getStorageTransform (ft) {
28
+ return storageTransformRegistry.get(ft) || storageTransformRegistry.get("*");
29
+ }
30
+ });
31
+ return result;
32
+ };
33
+ const aggregateUniqueFieldValues = (items, fieldId)=>{
34
+ const result = {};
35
+ for (const item of items){
36
+ const fieldValue = item.values[fieldId];
37
+ if (null == fieldValue) continue;
38
+ const values = Array.isArray(fieldValue) ? fieldValue : [
39
+ fieldValue
40
+ ];
41
+ if (0 !== values.length) for (const value of values)result[value] = {
42
+ value,
43
+ count: (result[value]?.count || 0) + 1
44
+ };
45
+ }
46
+ return Object.values(result).sort((a, b)=>a.value > b.value ? 1 : b.value > a.value ? -1 : 0).sort((a, b)=>b.count - a.count);
47
+ };
48
+ export { aggregateUniqueFieldValues, createStorageModelAccessor, createStorageTransformCallable };
49
+
50
+ //# sourceMappingURL=storageHelpers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cms/storageHelpers.js","sources":["../../src/cms/storageHelpers.ts"],"sourcesContent":["import type {\n CmsEntry,\n CmsEntryUniqueValue,\n CmsEntryValues,\n CmsModel,\n CmsModelField,\n StorageOperationsCmsModel\n} from \"@webiny/api-headless-cms/types/index.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport { StorageOperationsCmsModelPlugin } from \"@webiny/api-headless-cms\";\nimport { getBaseFieldType } from \"@webiny/api-headless-cms/utils/getBaseFieldType.js\";\nimport type { StorageTransformRegistry } from \"@webiny/api-headless-cms/exports/api/cms/storage.js\";\nimport type { FilterItemFromStorage } from \"../filtering/fields/types.js\";\n\n/* Creates a lazy-cached getter for StorageOperationsCmsModelPlugin. */\nexport const createStorageModelAccessor = (plugins: PluginsContainer) => {\n let cached: StorageOperationsCmsModelPlugin | undefined;\n\n const getPlugin = () => {\n if (cached) {\n return cached;\n }\n\n cached = plugins.oneByType<StorageOperationsCmsModelPlugin>(\n StorageOperationsCmsModelPlugin.type\n );\n\n return cached;\n };\n\n const getModel = <T extends CmsEntryValues = CmsEntryValues>(\n model: CmsModel\n ): StorageOperationsCmsModel<T> => {\n const plugin = getPlugin();\n return plugin.getModel<T>(model);\n };\n\n return { getPlugin, getModel };\n};\n\n/* Creates a fromStorage transform callable for a given model. */\nexport const createStorageTransformCallable = (\n storageTransformRegistry: StorageTransformRegistry.Interface,\n model: StorageOperationsCmsModel\n): FilterItemFromStorage => {\n return async (field: CmsModelField, value: unknown) => {\n const fieldType = getBaseFieldType(field);\n const storageTransform = storageTransformRegistry.get(fieldType);\n\n if (!storageTransform) {\n return value;\n }\n\n const result = await storageTransform.fromStorage({\n model,\n field,\n value,\n getStorageTransform(ft: string) {\n return storageTransformRegistry.get(ft) || storageTransformRegistry.get(\"*\")!;\n }\n });\n\n return result;\n };\n};\n\n/* Aggregates unique values for a given field across a list of CMS entries. */\nexport const aggregateUniqueFieldValues = <T extends CmsEntryValues = CmsEntryValues>(\n items: CmsEntry<T>[],\n fieldId: string\n): CmsEntryUniqueValue[] => {\n const result: Record<string, CmsEntryUniqueValue> = {};\n\n for (const item of items) {\n const fieldValue = item.values[fieldId] as string[] | string | undefined;\n\n if (fieldValue == null) {\n continue;\n }\n\n const values = Array.isArray(fieldValue) ? fieldValue : [fieldValue];\n\n if (values.length === 0) {\n continue;\n }\n\n for (const value of values) {\n result[value] = {\n value,\n count: (result[value]?.count || 0) + 1\n };\n }\n }\n\n return Object.values(result)\n .sort((a, b) => (a.value > b.value ? 1 : b.value > a.value ? -1 : 0))\n .sort((a, b) => b.count - a.count);\n};\n"],"names":["createStorageModelAccessor","plugins","cached","getPlugin","StorageOperationsCmsModelPlugin","getModel","model","plugin","createStorageTransformCallable","storageTransformRegistry","field","value","fieldType","getBaseFieldType","storageTransform","result","ft","aggregateUniqueFieldValues","items","fieldId","item","fieldValue","values","Array","Object","a","b"],"mappings":";;AAeO,MAAMA,6BAA6B,CAACC;IACvC,IAAIC;IAEJ,MAAMC,YAAY;QACd,IAAID,QACA,OAAOA;QAGXA,SAASD,QAAQ,SAAS,CACtBG,gCAAgC,IAAI;QAGxC,OAAOF;IACX;IAEA,MAAMG,WAAW,CACbC;QAEA,MAAMC,SAASJ;QACf,OAAOI,OAAO,QAAQ,CAAID;IAC9B;IAEA,OAAO;QAAEH;QAAWE;IAAS;AACjC;AAGO,MAAMG,iCAAiC,CAC1CC,0BACAH,QAEO,OAAOI,OAAsBC;QAChC,MAAMC,YAAYC,iBAAiBH;QACnC,MAAMI,mBAAmBL,yBAAyB,GAAG,CAACG;QAEtD,IAAI,CAACE,kBACD,OAAOH;QAGX,MAAMI,SAAS,MAAMD,iBAAiB,WAAW,CAAC;YAC9CR;YACAI;YACAC;YACA,qBAAoBK,EAAU;gBAC1B,OAAOP,yBAAyB,GAAG,CAACO,OAAOP,yBAAyB,GAAG,CAAC;YAC5E;QACJ;QAEA,OAAOM;IACX;AAIG,MAAME,6BAA6B,CACtCC,OACAC;IAEA,MAAMJ,SAA8C,CAAC;IAErD,KAAK,MAAMK,QAAQF,MAAO;QACtB,MAAMG,aAAaD,KAAK,MAAM,CAACD,QAAQ;QAEvC,IAAIE,AAAc,QAAdA,YACA;QAGJ,MAAMC,SAASC,MAAM,OAAO,CAACF,cAAcA,aAAa;YAACA;SAAW;QAEpE,IAAIC,AAAkB,MAAlBA,OAAO,MAAM,EAIjB,KAAK,MAAMX,SAASW,OAChBP,MAAM,CAACJ,MAAM,GAAG;YACZA;YACA,OAAQI,AAAAA,CAAAA,MAAM,CAACJ,MAAM,EAAE,SAAS,KAAK;QACzC;IAER;IAEA,OAAOa,OAAO,MAAM,CAACT,QAChB,IAAI,CAAC,CAACU,GAAGC,IAAOD,EAAE,KAAK,GAAGC,EAAE,KAAK,GAAG,IAAIA,EAAE,KAAK,GAAGD,EAAE,KAAK,GAAG,KAAK,GACjE,IAAI,CAAC,CAACA,GAAGC,IAAMA,EAAE,KAAK,GAAGD,EAAE,KAAK;AACzC"}
@@ -0,0 +1,27 @@
1
+ import type { CmsEntryListWhere } from "@webiny/api-headless-cms/types/index.js";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ import type { Field } from "../fields/types.js";
4
+ import { ValueFilter, ValueFilterRegistry } from "@webiny/db-utils";
5
+ interface ICreateExpressionsParams {
6
+ plugins: PluginsContainer;
7
+ valueFilterRegistry: ValueFilterRegistry.Interface;
8
+ where: Partial<CmsEntryListWhere>;
9
+ fields: Record<string, Field>;
10
+ }
11
+ export type ExpressionCondition = "AND" | "OR";
12
+ export interface Expression {
13
+ expressions: Expression[];
14
+ filters: Filter[];
15
+ condition: ExpressionCondition;
16
+ }
17
+ export interface Filter {
18
+ field: Field;
19
+ path: string;
20
+ fieldPathId: string;
21
+ filter: ValueFilter.Interface;
22
+ negate: boolean;
23
+ compareValue: any;
24
+ transformValue: <I = any, O = any>(value: I) => O;
25
+ }
26
+ export declare const createExpressions: (params: ICreateExpressionsParams) => Expression;
27
+ export {};
@@ -0,0 +1,122 @@
1
+ import error from "@webiny/error";
2
+ import { getMappedPlugins } from "../mapPlugins.js";
3
+ import { extractWhereParams } from "../where.js";
4
+ import { transformValue } from "../transform.js";
5
+ import { CmsEntryFieldFilterPlugin } from "../../plugins/CmsEntryFieldFilterPlugin.js";
6
+ import { getWhereValues } from "../values.js";
7
+ import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
8
+ const createExpressions = (params)=>{
9
+ const { where, plugins, fields, valueFilterRegistry } = params;
10
+ const transformValuePlugins = getMappedPlugins({
11
+ plugins,
12
+ type: "cms-field-filter-value-transform",
13
+ property: "fieldType"
14
+ });
15
+ const fieldFilterCreatePlugins = getMappedPlugins({
16
+ plugins,
17
+ type: CmsEntryFieldFilterPlugin.type,
18
+ property: "fieldType"
19
+ });
20
+ const defaultFilterCreatePlugin = fieldFilterCreatePlugins["*"];
21
+ const getFilterCreatePlugin = (type)=>{
22
+ const fieldType = getBaseFieldType({
23
+ type
24
+ });
25
+ const filterCreatePlugin = fieldFilterCreatePlugins[fieldType] || defaultFilterCreatePlugin;
26
+ if (filterCreatePlugin) return filterCreatePlugin;
27
+ throw new error(`There is no filter create plugin for the field type "${fieldType}".`, "MISSING_FILTER_CREATE_PLUGIN", {
28
+ fieldType
29
+ });
30
+ };
31
+ const createExpression = ({ where, condition })=>{
32
+ const expression = {
33
+ filters: [],
34
+ expressions: [],
35
+ condition
36
+ };
37
+ for(const key in where){
38
+ const value = where[key];
39
+ if (void 0 === value) continue;
40
+ if ("AND" === key) {
41
+ const childWhereList = getWhereValues(value, key);
42
+ const childExpression = {
43
+ condition: "AND",
44
+ filters: [],
45
+ expressions: []
46
+ };
47
+ for (const childWhere of childWhereList){
48
+ const result = createExpression({
49
+ where: childWhere,
50
+ condition: "AND"
51
+ });
52
+ childExpression.expressions.push(result);
53
+ }
54
+ expression.expressions.push(childExpression);
55
+ continue;
56
+ }
57
+ if ("OR" === key) {
58
+ const childWhereList = getWhereValues(value, key);
59
+ const childExpression = {
60
+ condition: "OR",
61
+ filters: [],
62
+ expressions: []
63
+ };
64
+ for (const childWhere of childWhereList){
65
+ const result = createExpression({
66
+ where: childWhere,
67
+ condition: "AND"
68
+ });
69
+ childExpression.expressions.push(result);
70
+ }
71
+ expression.expressions.push(childExpression);
72
+ continue;
73
+ }
74
+ const whereParams = extractWhereParams(key);
75
+ if (!whereParams) continue;
76
+ const { fieldId, operation, negate } = whereParams;
77
+ const field = fields[fieldId];
78
+ if (!field) throw new error(`There is no field with the fieldId "${fieldId}".`, "FIELD_ERROR", {
79
+ fieldId
80
+ });
81
+ const filterCreatePlugin = getFilterCreatePlugin(field.type);
82
+ const fieldType = getBaseFieldType(field);
83
+ const transformValuePlugin = transformValuePlugins[fieldType];
84
+ const transformValueCallable = (value)=>{
85
+ if (!transformValuePlugin) return value;
86
+ return transformValuePlugin.transform({
87
+ field,
88
+ value
89
+ });
90
+ };
91
+ const result = filterCreatePlugin.create({
92
+ key,
93
+ value,
94
+ valueFilterRegistry,
95
+ transformValuePlugins,
96
+ getFilterCreatePlugin,
97
+ operation,
98
+ negate,
99
+ field,
100
+ fields,
101
+ compareValue: transformValue({
102
+ value,
103
+ transform: transformValueCallable
104
+ }),
105
+ transformValue: transformValueCallable
106
+ });
107
+ if (!(!result || Array.isArray(result) && 0 === result.length)) expression.filters.push(...Array.isArray(result) ? result : [
108
+ result
109
+ ]);
110
+ }
111
+ return expression;
112
+ };
113
+ const expression = createExpression({
114
+ where,
115
+ condition: "AND"
116
+ });
117
+ if (expression.filters.length > 0 || 1 !== expression.expressions.length) return expression;
118
+ return expression.expressions[0];
119
+ };
120
+ export { createExpressions };
121
+
122
+ //# sourceMappingURL=createExpressions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filtering/expressions/createExpressions.js","sources":["../../../src/filtering/expressions/createExpressions.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { CmsEntryListWhere } from \"@webiny/api-headless-cms/types/index.js\";\nimport type { ICmsFieldFilterValueTransformPlugin } from \"../../plugins/CmsFieldFilterValueTransformPlugin.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport type { Field } from \"../fields/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 \"../values.js\";\nimport { getBaseFieldType } from \"@webiny/api-headless-cms/utils/getBaseFieldType.js\";\nimport { ValueFilter, ValueFilterRegistry } from \"@webiny/db-utils\";\n\ninterface CreateExpressionParams {\n where: Partial<CmsEntryListWhere>;\n condition: ExpressionCondition;\n}\n\ninterface ICreateExpressionsParams {\n plugins: PluginsContainer;\n valueFilterRegistry: ValueFilterRegistry.Interface;\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, valueFilterRegistry } = params;\n\n const transformValuePlugins = getMappedPlugins<ICmsFieldFilterValueTransformPlugin>({\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 * 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 /* OR conditional. */\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 /* We need a filter create plugin for this type. */\n const filterCreatePlugin = getFilterCreatePlugin(field.type);\n\n const fieldType = getBaseFieldType(field);\n\n const transformValuePlugin: ICmsFieldFilterValueTransformPlugin =\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","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":"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"}
@@ -0,0 +1,11 @@
1
+ import type { CmsModelField } from "@webiny/api-headless-cms/types/index.js";
2
+ import type { Field } from "./types.js";
3
+ import type { PluginsContainer } from "@webiny/plugins";
4
+ interface Params {
5
+ fields: CmsModelField[];
6
+ plugins: PluginsContainer;
7
+ }
8
+ export declare const createFields: (params: Params) => {
9
+ [x: string]: Field;
10
+ };
11
+ export {};
@@ -0,0 +1,93 @@
1
+ import { createSystemFields } from "./systemFields.js";
2
+ import { CmsEntryFieldFilterPathPlugin } from "../../plugins/index.js";
3
+ import { getMappedPlugins } from "../mapPlugins.js";
4
+ import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
5
+ const createFieldCollection = (params)=>{
6
+ const { fields, parents, transformValuePlugins, valuePathPlugins, system } = params;
7
+ return fields.reduce((collection, field)=>{
8
+ const fieldType = getBaseFieldType(field);
9
+ const transformPlugin = transformValuePlugins[fieldType];
10
+ const valuePathPlugin = valuePathPlugins[fieldType];
11
+ const fieldId = [
12
+ ...parents,
13
+ {
14
+ fieldId: field.fieldId,
15
+ list: field.list
16
+ }
17
+ ].map((f)=>f.fieldId).join(".");
18
+ collection[fieldId] = {
19
+ ...field,
20
+ parents,
21
+ system,
22
+ createPath: (params)=>{
23
+ if (valuePathPlugin && valuePathPlugin.canUse(field, parents.map((p)=>p.fieldId))) return valuePathPlugin.createPath(params);
24
+ return parents.map((parent)=>parent.fieldId).concat([
25
+ params.field.fieldId
26
+ ]).join(".");
27
+ },
28
+ transform: (value)=>{
29
+ if (!transformPlugin) return value;
30
+ return transformPlugin.transform({
31
+ field,
32
+ value
33
+ });
34
+ }
35
+ };
36
+ const childFields = field.settings?.fields;
37
+ if (!childFields?.length) return collection;
38
+ const result = createFieldCollection({
39
+ fields: childFields,
40
+ parents: [
41
+ ...parents,
42
+ {
43
+ fieldId: field.fieldId,
44
+ list: field.list
45
+ }
46
+ ],
47
+ transformValuePlugins,
48
+ valuePathPlugins,
49
+ system
50
+ });
51
+ Object.assign(collection, result);
52
+ return collection;
53
+ }, {});
54
+ };
55
+ const createFields = (params)=>{
56
+ const { fields, plugins } = params;
57
+ const transformValuePlugins = getMappedPlugins({
58
+ plugins,
59
+ type: "cms-field-filter-value-transform",
60
+ property: "fieldType"
61
+ });
62
+ const valuePathPlugins = getMappedPlugins({
63
+ plugins,
64
+ type: CmsEntryFieldFilterPathPlugin.type,
65
+ property: "fieldType"
66
+ });
67
+ const collection = createFieldCollection({
68
+ fields: createSystemFields(),
69
+ transformValuePlugins,
70
+ valuePathPlugins,
71
+ parents: [],
72
+ system: true
73
+ });
74
+ const result = createFieldCollection({
75
+ fields,
76
+ transformValuePlugins,
77
+ valuePathPlugins,
78
+ parents: [
79
+ {
80
+ fieldId: "values",
81
+ list: false
82
+ }
83
+ ],
84
+ system: false
85
+ });
86
+ return {
87
+ ...collection,
88
+ ...result
89
+ };
90
+ };
91
+ export { createFields };
92
+
93
+ //# sourceMappingURL=createFields.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filtering/fields/createFields.js","sources":["../../../src/filtering/fields/createFields.ts"],"sourcesContent":["import type { CmsModelField } from \"@webiny/api-headless-cms/types/index.js\";\nimport { createSystemFields } from \"./systemFields.js\";\nimport type { Field, FieldParent } from \"./types.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport type { ICmsFieldFilterValueTransformPlugin } from \"../../plugins/CmsFieldFilterValueTransformPlugin.js\";\nimport { CmsEntryFieldFilterPathPlugin } from \"../../plugins/index.js\";\nimport { getMappedPlugins } from \"../mapPlugins.js\";\nimport { getBaseFieldType } from \"@webiny/api-headless-cms/utils/getBaseFieldType.js\";\n\ninterface Params {\n fields: CmsModelField[];\n plugins: PluginsContainer;\n}\n\ninterface FieldCollection {\n [key: string]: Field;\n}\n\ninterface AddFieldsToCollectionParams {\n fields: CmsModelField[];\n parents: FieldParent[];\n transformValuePlugins: Record<string, ICmsFieldFilterValueTransformPlugin>;\n valuePathPlugins: Record<string, CmsEntryFieldFilterPathPlugin>;\n system: boolean;\n}\n\nconst createFieldCollection = (params: AddFieldsToCollectionParams): FieldCollection => {\n const { fields, parents, transformValuePlugins, valuePathPlugins, system } = params;\n return fields.reduce<FieldCollection>((collection, field) => {\n const fieldType = getBaseFieldType(field);\n const transformPlugin = transformValuePlugins[fieldType];\n const valuePathPlugin = valuePathPlugins[fieldType];\n\n /* The required fieldId is a product of all of its parents and its own fieldId. */\n const fieldId = [\n ...parents,\n {\n fieldId: field.fieldId,\n list: field.list\n }\n ]\n .map(f => f.fieldId)\n .join(\".\");\n\n collection[fieldId] = {\n ...field,\n parents,\n system,\n createPath: params => {\n if (\n valuePathPlugin &&\n valuePathPlugin.canUse(\n field,\n parents.map(p => p.fieldId)\n )\n ) {\n return valuePathPlugin.createPath(params);\n }\n\n return parents\n .map(parent => parent.fieldId)\n .concat([params.field.fieldId])\n .join(\".\");\n },\n transform: value => {\n if (!transformPlugin) {\n return value;\n }\n return transformPlugin.transform({\n field,\n value\n });\n }\n };\n const childFields = field.settings?.fields;\n if (!childFields?.length) {\n return collection;\n }\n\n const result = createFieldCollection({\n fields: childFields,\n parents: [\n ...parents,\n {\n fieldId: field.fieldId,\n list: field.list\n }\n ],\n transformValuePlugins,\n valuePathPlugins,\n system\n });\n Object.assign(collection, result);\n return collection;\n }, {});\n};\n/*\n * This method will map the fieldId (fieldId -> field) to the actual field.\n *\n * In case of nested fields, fieldId is all the parent fieldIds + current one, joined by the dot (.).\n */\nexport const createFields = (params: Params) => {\n const { fields, plugins } = params;\n\n const transformValuePlugins = getMappedPlugins<ICmsFieldFilterValueTransformPlugin>({\n plugins,\n type: \"cms-field-filter-value-transform\",\n property: \"fieldType\"\n });\n const valuePathPlugins = getMappedPlugins<CmsEntryFieldFilterPathPlugin>({\n plugins,\n type: CmsEntryFieldFilterPathPlugin.type,\n property: \"fieldType\"\n });\n\n const collection = createFieldCollection({\n fields: createSystemFields(),\n transformValuePlugins,\n valuePathPlugins,\n parents: [],\n system: true\n });\n\n const result = createFieldCollection({\n fields,\n transformValuePlugins,\n valuePathPlugins,\n parents: [\n {\n fieldId: \"values\",\n list: false\n }\n ],\n system: false\n });\n\n return {\n ...collection,\n ...result\n };\n};\n"],"names":["createFieldCollection","params","fields","parents","transformValuePlugins","valuePathPlugins","system","collection","field","fieldType","getBaseFieldType","transformPlugin","valuePathPlugin","fieldId","f","p","parent","value","childFields","result","Object","createFields","plugins","getMappedPlugins","CmsEntryFieldFilterPathPlugin","createSystemFields"],"mappings":";;;;AA0BA,MAAMA,wBAAwB,CAACC;IAC3B,MAAM,EAAEC,MAAM,EAAEC,OAAO,EAAEC,qBAAqB,EAAEC,gBAAgB,EAAEC,MAAM,EAAE,GAAGL;IAC7E,OAAOC,OAAO,MAAM,CAAkB,CAACK,YAAYC;QAC/C,MAAMC,YAAYC,iBAAiBF;QACnC,MAAMG,kBAAkBP,qBAAqB,CAACK,UAAU;QACxD,MAAMG,kBAAkBP,gBAAgB,CAACI,UAAU;QAGnD,MAAMI,UAAU;eACTV;YACH;gBACI,SAASK,MAAM,OAAO;gBACtB,MAAMA,MAAM,IAAI;YACpB;SACH,CACI,GAAG,CAACM,CAAAA,IAAKA,EAAE,OAAO,EAClB,IAAI,CAAC;QAEVP,UAAU,CAACM,QAAQ,GAAG;YAClB,GAAGL,KAAK;YACRL;YACAG;YACA,YAAYL,CAAAA;gBACR,IACIW,mBACAA,gBAAgB,MAAM,CAClBJ,OACAL,QAAQ,GAAG,CAACY,CAAAA,IAAKA,EAAE,OAAO,IAG9B,OAAOH,gBAAgB,UAAU,CAACX;gBAGtC,OAAOE,QACF,GAAG,CAACa,CAAAA,SAAUA,OAAO,OAAO,EAC5B,MAAM,CAAC;oBAACf,OAAO,KAAK,CAAC,OAAO;iBAAC,EAC7B,IAAI,CAAC;YACd;YACA,WAAWgB,CAAAA;gBACP,IAAI,CAACN,iBACD,OAAOM;gBAEX,OAAON,gBAAgB,SAAS,CAAC;oBAC7BH;oBACAS;gBACJ;YACJ;QACJ;QACA,MAAMC,cAAcV,MAAM,QAAQ,EAAE;QACpC,IAAI,CAACU,aAAa,QACd,OAAOX;QAGX,MAAMY,SAASnB,sBAAsB;YACjC,QAAQkB;YACR,SAAS;mBACFf;gBACH;oBACI,SAASK,MAAM,OAAO;oBACtB,MAAMA,MAAM,IAAI;gBACpB;aACH;YACDJ;YACAC;YACAC;QACJ;QACAc,OAAO,MAAM,CAACb,YAAYY;QAC1B,OAAOZ;IACX,GAAG,CAAC;AACR;AAMO,MAAMc,eAAe,CAACpB;IACzB,MAAM,EAAEC,MAAM,EAAEoB,OAAO,EAAE,GAAGrB;IAE5B,MAAMG,wBAAwBmB,iBAAsD;QAChFD;QACA,MAAM;QACN,UAAU;IACd;IACA,MAAMjB,mBAAmBkB,iBAAgD;QACrED;QACA,MAAME,8BAA8B,IAAI;QACxC,UAAU;IACd;IAEA,MAAMjB,aAAaP,sBAAsB;QACrC,QAAQyB;QACRrB;QACAC;QACA,SAAS,EAAE;QACX,QAAQ;IACZ;IAEA,MAAMc,SAASnB,sBAAsB;QACjCE;QACAE;QACAC;QACA,SAAS;YACL;gBACI,SAAS;gBACT,MAAM;YACV;SACH;QACD,QAAQ;IACZ;IAEA,OAAO;QACH,GAAGE,UAAU;QACb,GAAGY,MAAM;IACb;AACJ"}
@@ -0,0 +1,17 @@
1
+ import type { Field } from "./types.js";
2
+ import type { PluginsContainer } from "@webiny/plugins";
3
+ import type { CmsModel } from "@webiny/api-headless-cms/types/index.js";
4
+ interface IResponse {
5
+ valuePath: string;
6
+ reverse: boolean;
7
+ fieldId: string;
8
+ field: Field;
9
+ }
10
+ interface IParams {
11
+ model: CmsModel;
12
+ sortBy: string;
13
+ fields: Record<string, Field>;
14
+ plugins: PluginsContainer;
15
+ }
16
+ export declare const extractSort: (params: IParams) => IResponse;
17
+ export {};
@@ -0,0 +1,60 @@
1
+ import error from "@webiny/error";
2
+ import { CmsEntryFieldSortingPlugin } from "../../plugins/index.js";
3
+ const extractSortInfo = (sortBy)=>{
4
+ const rootSorting = sortBy.match(/^([a-zA-Z]+)_(ASC|DESC)$/);
5
+ if (rootSorting) return {
6
+ fieldId: rootSorting[1],
7
+ isValues: false,
8
+ order: rootSorting[2]
9
+ };
10
+ const valuesSorting = sortBy.match(/^values_([a-zA-Z0-9]+)_(ASC|DESC)$/);
11
+ if (valuesSorting) return {
12
+ fieldId: valuesSorting[1],
13
+ isValues: true,
14
+ order: valuesSorting[2]
15
+ };
16
+ throw new error("Problem in determining the sorting for the entry items.", "SORT_EXTRACT_ERROR", {
17
+ sortBy
18
+ });
19
+ };
20
+ const extractSort = (params)=>{
21
+ const { model, sortBy, fields, plugins } = params;
22
+ const { fieldId, isValues: isValuesSorting, order } = extractSortInfo(sortBy);
23
+ const field = Object.values(fields).find((f)=>{
24
+ const isValues = f.parents[0]?.fieldId === "values";
25
+ if (isValues && isValuesSorting) return f.fieldId === fieldId;
26
+ if (f.parents.length > 0) return false;
27
+ return f.fieldId === fieldId;
28
+ });
29
+ const plugin = plugins.byType(CmsEntryFieldSortingPlugin.type).reverse().find((plugin)=>plugin.canUse({
30
+ model,
31
+ field,
32
+ fieldId,
33
+ order,
34
+ sortBy
35
+ }));
36
+ if (plugin) return plugin.createSort({
37
+ model,
38
+ fieldId,
39
+ order,
40
+ sortBy,
41
+ field,
42
+ fields
43
+ });
44
+ if (!field) throw new error("Sorting field does not exist in the content model.", "SORTING_FIELD_ERROR", {
45
+ fieldId,
46
+ fields
47
+ });
48
+ const valuePath = field.createPath({
49
+ field
50
+ });
51
+ return {
52
+ field,
53
+ fieldId,
54
+ valuePath,
55
+ reverse: "DESC" === order
56
+ };
57
+ };
58
+ export { extractSort };
59
+
60
+ //# sourceMappingURL=extractSort.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filtering/fields/extractSort.js","sources":["../../../src/filtering/fields/extractSort.ts"],"sourcesContent":["import WebinyError from \"@webiny/error\";\nimport type { Field } from \"./types.js\";\nimport type { PluginsContainer } from \"@webiny/plugins\";\nimport { CmsEntryFieldSortingPlugin } from \"../../plugins/index.js\";\nimport type { CmsModel } from \"@webiny/api-headless-cms/types/index.js\";\n\nconst extractSortInfo = (sortBy: string) => {\n const rootSorting = sortBy.match(/^([a-zA-Z]+)_(ASC|DESC)$/);\n if (rootSorting) {\n return {\n fieldId: rootSorting[1],\n isValues: false,\n order: rootSorting[2] as \"ASC\" | \"DESC\"\n };\n }\n const valuesSorting = sortBy.match(/^values_([a-zA-Z0-9]+)_(ASC|DESC)$/);\n if (valuesSorting) {\n return {\n fieldId: valuesSorting[1],\n isValues: true,\n order: valuesSorting[2] as \"ASC\" | \"DESC\"\n };\n }\n throw new WebinyError(\n \"Problem in determining the sorting for the entry items.\",\n \"SORT_EXTRACT_ERROR\",\n {\n sortBy\n }\n );\n};\n\ninterface IResponse {\n valuePath: string;\n reverse: boolean;\n fieldId: string;\n field: Field;\n}\n\ninterface IParams {\n model: CmsModel;\n sortBy: string;\n fields: Record<string, Field>;\n plugins: PluginsContainer;\n}\n\nexport const extractSort = (params: IParams): IResponse => {\n const { model, sortBy, fields, plugins } = params;\n const { fieldId, isValues: isValuesSorting, order } = extractSortInfo(sortBy);\n\n const field = Object.values(fields).find(f => {\n /* We do not support sorting by nested fields. */\n const isValues = f.parents[0]?.fieldId === \"values\";\n if (isValues && isValuesSorting) {\n return f.fieldId === fieldId;\n }\n if (f.parents.length > 0) {\n return false;\n }\n return f.fieldId === fieldId;\n });\n\n const plugin = plugins\n .byType<CmsEntryFieldSortingPlugin>(CmsEntryFieldSortingPlugin.type)\n .reverse()\n .find(plugin => {\n return plugin.canUse({\n model,\n field,\n fieldId,\n order,\n sortBy\n });\n });\n\n if (plugin) {\n return plugin.createSort({\n model,\n fieldId,\n order,\n sortBy,\n field,\n fields\n });\n } else if (!field) {\n throw new WebinyError(\n \"Sorting field does not exist in the content model.\",\n \"SORTING_FIELD_ERROR\",\n {\n fieldId,\n fields\n }\n );\n }\n const valuePath = field.createPath({\n field\n });\n return {\n field,\n fieldId,\n valuePath,\n reverse: order === \"DESC\"\n };\n};\n"],"names":["extractSortInfo","sortBy","rootSorting","valuesSorting","WebinyError","extractSort","params","model","fields","plugins","fieldId","isValuesSorting","order","field","Object","f","isValues","plugin","CmsEntryFieldSortingPlugin","valuePath"],"mappings":";;AAMA,MAAMA,kBAAkB,CAACC;IACrB,MAAMC,cAAcD,OAAO,KAAK,CAAC;IACjC,IAAIC,aACA,OAAO;QACH,SAASA,WAAW,CAAC,EAAE;QACvB,UAAU;QACV,OAAOA,WAAW,CAAC,EAAE;IACzB;IAEJ,MAAMC,gBAAgBF,OAAO,KAAK,CAAC;IACnC,IAAIE,eACA,OAAO;QACH,SAASA,aAAa,CAAC,EAAE;QACzB,UAAU;QACV,OAAOA,aAAa,CAAC,EAAE;IAC3B;IAEJ,MAAM,IAAIC,MACN,2DACA,sBACA;QACIH;IACJ;AAER;AAgBO,MAAMI,cAAc,CAACC;IACxB,MAAM,EAAEC,KAAK,EAAEN,MAAM,EAAEO,MAAM,EAAEC,OAAO,EAAE,GAAGH;IAC3C,MAAM,EAAEI,OAAO,EAAE,UAAUC,eAAe,EAAEC,KAAK,EAAE,GAAGZ,gBAAgBC;IAEtE,MAAMY,QAAQC,OAAO,MAAM,CAACN,QAAQ,IAAI,CAACO,CAAAA;QAErC,MAAMC,WAAWD,EAAE,OAAO,CAAC,EAAE,EAAE,YAAY;QAC3C,IAAIC,YAAYL,iBACZ,OAAOI,EAAE,OAAO,KAAKL;QAEzB,IAAIK,EAAE,OAAO,CAAC,MAAM,GAAG,GACnB,OAAO;QAEX,OAAOA,EAAE,OAAO,KAAKL;IACzB;IAEA,MAAMO,SAASR,QACV,MAAM,CAA6BS,2BAA2B,IAAI,EAClE,OAAO,GACP,IAAI,CAACD,CAAAA,SACKA,OAAO,MAAM,CAAC;YACjBV;YACAM;YACAH;YACAE;YACAX;QACJ;IAGR,IAAIgB,QACA,OAAOA,OAAO,UAAU,CAAC;QACrBV;QACAG;QACAE;QACAX;QACAY;QACAL;IACJ;IACG,IAAI,CAACK,OACR,MAAM,IAAIT,MACN,sDACA,uBACA;QACIM;QACAF;IACJ;IAGR,MAAMW,YAAYN,MAAM,UAAU,CAAC;QAC/BA;IACJ;IACA,OAAO;QACHA;QACAH;QACAS;QACA,SAASP,AAAU,WAAVA;IACb;AACJ"}
@@ -0,0 +1,2 @@
1
+ import type { CmsModelField } from "@webiny/api-headless-cms/types/index.js";
2
+ export declare const createSystemFields: () => CmsModelField[];
@@ -0,0 +1,149 @@
1
+ import { ENTRY_META_FIELDS, isDateTimeEntryMetaField, isIdentityEntryMetaField } from "@webiny/api-headless-cms/constants.js";
2
+ import startCase from "lodash/startCase.js";
3
+ import { createModelField } from "@webiny/api-headless-cms";
4
+ const createSystemFields = ()=>{
5
+ const onMetaFields = ENTRY_META_FIELDS.filter(isDateTimeEntryMetaField).map((fieldName)=>createModelField({
6
+ id: fieldName,
7
+ type: "datetime",
8
+ storageId: fieldName,
9
+ fieldId: fieldName,
10
+ label: startCase(fieldName)
11
+ }));
12
+ const byMetaFields = ENTRY_META_FIELDS.filter(isIdentityEntryMetaField).map((fieldName)=>createModelField({
13
+ id: fieldName,
14
+ type: "plainObject",
15
+ storageId: fieldName,
16
+ fieldId: fieldName,
17
+ label: startCase(fieldName),
18
+ settings: {
19
+ path: `${fieldName}.id`
20
+ }
21
+ }));
22
+ return [
23
+ createModelField({
24
+ id: "id",
25
+ type: "text",
26
+ storageId: "id",
27
+ fieldId: "id",
28
+ label: "ID"
29
+ }),
30
+ createModelField({
31
+ id: "entryId",
32
+ type: "text",
33
+ storageId: "entryId",
34
+ fieldId: "entryId",
35
+ label: "Entry ID"
36
+ }),
37
+ ...onMetaFields,
38
+ ...byMetaFields,
39
+ createModelField({
40
+ id: "meta",
41
+ type: "plainObject",
42
+ storageId: "meta",
43
+ fieldId: "meta",
44
+ label: "Meta"
45
+ }),
46
+ createModelField({
47
+ id: "wbyAco_location",
48
+ type: "object",
49
+ storageId: "location",
50
+ label: "Location",
51
+ fieldId: "wbyAco_location",
52
+ settings: {
53
+ fields: [
54
+ createModelField({
55
+ id: "folderId",
56
+ type: "text",
57
+ fieldId: "folderId",
58
+ label: "Folder ID",
59
+ storageId: "folderId",
60
+ settings: {
61
+ path: "location.folderId"
62
+ }
63
+ })
64
+ ]
65
+ }
66
+ }),
67
+ createModelField({
68
+ id: "version",
69
+ type: "number",
70
+ storageId: "version",
71
+ fieldId: "version",
72
+ label: "Version"
73
+ }),
74
+ createModelField({
75
+ id: "status",
76
+ type: "text",
77
+ storageId: "status",
78
+ fieldId: "status",
79
+ label: "Status"
80
+ }),
81
+ createModelField({
82
+ id: "wbyDeleted",
83
+ type: "boolean",
84
+ storageId: "wbyDeleted",
85
+ fieldId: "wbyDeleted",
86
+ label: "Deleted"
87
+ }),
88
+ createModelField({
89
+ id: "state",
90
+ type: "object",
91
+ storageId: "object@state",
92
+ fieldId: "state",
93
+ label: "State",
94
+ settings: {
95
+ fields: [
96
+ createModelField({
97
+ id: "stepId",
98
+ type: "text",
99
+ fieldId: "stepId",
100
+ label: "Step ID",
101
+ storageId: "stepId"
102
+ }),
103
+ createModelField({
104
+ id: "stepName",
105
+ type: "text",
106
+ fieldId: "stepName",
107
+ label: "Step Name",
108
+ storageId: "stepName"
109
+ }),
110
+ createModelField({
111
+ id: "state",
112
+ type: "text",
113
+ fieldId: "state",
114
+ label: "State",
115
+ storageId: "state"
116
+ })
117
+ ]
118
+ }
119
+ }),
120
+ createModelField({
121
+ id: "values",
122
+ type: "object",
123
+ storageId: "values",
124
+ fieldId: "values",
125
+ label: "Values"
126
+ }),
127
+ createModelField({
128
+ id: "live",
129
+ type: "object",
130
+ storageId: "live",
131
+ fieldId: "live",
132
+ label: "Live",
133
+ settings: {
134
+ fields: [
135
+ createModelField({
136
+ id: "version",
137
+ type: "number",
138
+ fieldId: "version",
139
+ label: "Version",
140
+ storageId: "version"
141
+ })
142
+ ]
143
+ }
144
+ })
145
+ ];
146
+ };
147
+ export { createSystemFields };
148
+
149
+ //# sourceMappingURL=systemFields.js.map