@webiny/api-headless-cms-storage 6.6.0-alpha.0 → 6.6.0-alpha.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.
Files changed (185) hide show
  1. package/package.json +20 -15
  2. package/__tests__/filtering/getValue.test.ts +0 -58
  3. package/__tests__/filtering/mocks/entry.noModel.ts +0 -110
  4. package/__tests__/path/plainObject.test.ts +0 -32
  5. package/__tests__/transforms/datetime.test.ts +0 -71
  6. package/dist/package.json +0 -30
  7. package/src/cms/storageHelpers.ts +0 -92
  8. package/src/features/FilterRegistriesFeature.ts +0 -40
  9. package/src/features/fieldFilterCreate/FieldFilterCreateRegistry.ts +0 -25
  10. package/src/features/fieldFilterCreate/abstractions.ts +0 -51
  11. package/src/features/fieldFilterPath/FieldFilterPathRegistry.ts +0 -13
  12. package/src/features/fieldFilterPath/abstractions.ts +0 -22
  13. package/src/features/fieldFilterValueTransform/FieldFilterValueTransformRegistry.ts +0 -15
  14. package/src/features/fieldFilterValueTransform/abstractions.ts +0 -21
  15. package/src/features/fieldSorting/FieldSortingRegistry.ts +0 -20
  16. package/src/features/fieldSorting/abstractions.ts +0 -49
  17. package/src/filtering/expressions/createExpressions.ts +0 -170
  18. package/src/filtering/fields/createFields.ts +0 -124
  19. package/src/filtering/fields/extractSort.ts +0 -97
  20. package/src/filtering/fields/systemFields.ts +0 -165
  21. package/src/filtering/fields/types.ts +0 -46
  22. package/src/filtering/filter.ts +0 -166
  23. package/src/filtering/fullTextSearch.ts +0 -40
  24. package/src/filtering/getValue.ts +0 -64
  25. package/src/filtering/mapPlugins.ts +0 -30
  26. package/src/filtering/plugins/defaultFilterCreate.ts +0 -36
  27. package/src/filtering/plugins/index.ts +0 -13
  28. package/src/filtering/plugins/objectFilterCreate.ts +0 -100
  29. package/src/filtering/plugins/refFilterCreate.ts +0 -83
  30. package/src/filtering/plugins/searchableJsonFilterCreate.ts +0 -78
  31. package/src/filtering/sort.ts +0 -79
  32. package/src/filtering/transform.ts +0 -11
  33. package/src/filtering/values.ts +0 -24
  34. package/src/filtering/where.ts +0 -24
  35. package/src/handlers/datetimeTransformHandler.ts +0 -52
  36. package/src/handlers/defaultFilterCreateHandler.ts +0 -31
  37. package/src/handlers/locationFolderIdPathHandler.ts +0 -24
  38. package/src/handlers/objectFilterCreateHandler.ts +0 -86
  39. package/src/handlers/plainObjectPathHandler.ts +0 -17
  40. package/src/handlers/refFilterCreateHandler.ts +0 -65
  41. package/src/handlers/searchableJsonFilterCreateHandler.ts +0 -61
  42. package/src/index.ts +0 -63
  43. package/src/path/locationFolderId.ts +0 -29
  44. package/src/path/plainObject.ts +0 -21
  45. package/src/plugins/CmsEntryFieldFilterPathPlugin.ts +0 -58
  46. package/src/plugins/CmsEntryFieldFilterPlugin.ts +0 -59
  47. package/src/plugins/CmsEntryFieldSortingPlugin.ts +0 -63
  48. package/src/plugins/CmsFieldFilterValueTransformPlugin.ts +0 -43
  49. package/src/plugins/index.ts +0 -10
  50. package/src/transforms/datetime.ts +0 -62
  51. package/tsconfig.build.json +0 -33
  52. package/tsconfig.json +0 -33
  53. package/vitest.config.ts +0 -5
  54. package/webiny.config.js +0 -8
  55. /package/{dist/LICENSE → LICENSE} +0 -0
  56. /package/{dist/README.md → README.md} +0 -0
  57. /package/{dist/cms → cms}/storageHelpers.d.ts +0 -0
  58. /package/{dist/cms → cms}/storageHelpers.js +0 -0
  59. /package/{dist/cms → cms}/storageHelpers.js.map +0 -0
  60. /package/{dist/features → features}/FilterRegistriesFeature.d.ts +0 -0
  61. /package/{dist/features → features}/FilterRegistriesFeature.js +0 -0
  62. /package/{dist/features → features}/FilterRegistriesFeature.js.map +0 -0
  63. /package/{dist/features → features}/fieldFilterCreate/FieldFilterCreateRegistry.d.ts +0 -0
  64. /package/{dist/features → features}/fieldFilterCreate/FieldFilterCreateRegistry.js +0 -0
  65. /package/{dist/features → features}/fieldFilterCreate/FieldFilterCreateRegistry.js.map +0 -0
  66. /package/{dist/features → features}/fieldFilterCreate/abstractions.d.ts +0 -0
  67. /package/{dist/features → features}/fieldFilterCreate/abstractions.js +0 -0
  68. /package/{dist/features → features}/fieldFilterCreate/abstractions.js.map +0 -0
  69. /package/{dist/features → features}/fieldFilterPath/FieldFilterPathRegistry.d.ts +0 -0
  70. /package/{dist/features → features}/fieldFilterPath/FieldFilterPathRegistry.js +0 -0
  71. /package/{dist/features → features}/fieldFilterPath/FieldFilterPathRegistry.js.map +0 -0
  72. /package/{dist/features → features}/fieldFilterPath/abstractions.d.ts +0 -0
  73. /package/{dist/features → features}/fieldFilterPath/abstractions.js +0 -0
  74. /package/{dist/features → features}/fieldFilterPath/abstractions.js.map +0 -0
  75. /package/{dist/features → features}/fieldFilterValueTransform/FieldFilterValueTransformRegistry.d.ts +0 -0
  76. /package/{dist/features → features}/fieldFilterValueTransform/FieldFilterValueTransformRegistry.js +0 -0
  77. /package/{dist/features → features}/fieldFilterValueTransform/FieldFilterValueTransformRegistry.js.map +0 -0
  78. /package/{dist/features → features}/fieldFilterValueTransform/abstractions.d.ts +0 -0
  79. /package/{dist/features → features}/fieldFilterValueTransform/abstractions.js +0 -0
  80. /package/{dist/features → features}/fieldFilterValueTransform/abstractions.js.map +0 -0
  81. /package/{dist/features → features}/fieldSorting/FieldSortingRegistry.d.ts +0 -0
  82. /package/{dist/features → features}/fieldSorting/FieldSortingRegistry.js +0 -0
  83. /package/{dist/features → features}/fieldSorting/FieldSortingRegistry.js.map +0 -0
  84. /package/{dist/features → features}/fieldSorting/abstractions.d.ts +0 -0
  85. /package/{dist/features → features}/fieldSorting/abstractions.js +0 -0
  86. /package/{dist/features → features}/fieldSorting/abstractions.js.map +0 -0
  87. /package/{dist/filtering → filtering}/expressions/createExpressions.d.ts +0 -0
  88. /package/{dist/filtering → filtering}/expressions/createExpressions.js +0 -0
  89. /package/{dist/filtering → filtering}/expressions/createExpressions.js.map +0 -0
  90. /package/{dist/filtering → filtering}/fields/createFields.d.ts +0 -0
  91. /package/{dist/filtering → filtering}/fields/createFields.js +0 -0
  92. /package/{dist/filtering → filtering}/fields/createFields.js.map +0 -0
  93. /package/{dist/filtering → filtering}/fields/extractSort.d.ts +0 -0
  94. /package/{dist/filtering → filtering}/fields/extractSort.js +0 -0
  95. /package/{dist/filtering → filtering}/fields/extractSort.js.map +0 -0
  96. /package/{dist/filtering → filtering}/fields/systemFields.d.ts +0 -0
  97. /package/{dist/filtering → filtering}/fields/systemFields.js +0 -0
  98. /package/{dist/filtering → filtering}/fields/systemFields.js.map +0 -0
  99. /package/{dist/filtering → filtering}/fields/types.d.ts +0 -0
  100. /package/{dist/filtering → filtering}/fields/types.js +0 -0
  101. /package/{dist/filtering → filtering}/filter.d.ts +0 -0
  102. /package/{dist/filtering → filtering}/filter.js +0 -0
  103. /package/{dist/filtering → filtering}/filter.js.map +0 -0
  104. /package/{dist/filtering → filtering}/fullTextSearch.d.ts +0 -0
  105. /package/{dist/filtering → filtering}/fullTextSearch.js +0 -0
  106. /package/{dist/filtering → filtering}/fullTextSearch.js.map +0 -0
  107. /package/{dist/filtering → filtering}/getValue.d.ts +0 -0
  108. /package/{dist/filtering → filtering}/getValue.js +0 -0
  109. /package/{dist/filtering → filtering}/getValue.js.map +0 -0
  110. /package/{dist/filtering → filtering}/mapPlugins.d.ts +0 -0
  111. /package/{dist/filtering → filtering}/mapPlugins.js +0 -0
  112. /package/{dist/filtering → filtering}/mapPlugins.js.map +0 -0
  113. /package/{dist/filtering → filtering}/plugins/defaultFilterCreate.d.ts +0 -0
  114. /package/{dist/filtering → filtering}/plugins/defaultFilterCreate.js +0 -0
  115. /package/{dist/filtering → filtering}/plugins/defaultFilterCreate.js.map +0 -0
  116. /package/{dist/filtering → filtering}/plugins/index.d.ts +0 -0
  117. /package/{dist/filtering → filtering}/plugins/index.js +0 -0
  118. /package/{dist/filtering → filtering}/plugins/index.js.map +0 -0
  119. /package/{dist/filtering → filtering}/plugins/objectFilterCreate.d.ts +0 -0
  120. /package/{dist/filtering → filtering}/plugins/objectFilterCreate.js +0 -0
  121. /package/{dist/filtering → filtering}/plugins/objectFilterCreate.js.map +0 -0
  122. /package/{dist/filtering → filtering}/plugins/refFilterCreate.d.ts +0 -0
  123. /package/{dist/filtering → filtering}/plugins/refFilterCreate.js +0 -0
  124. /package/{dist/filtering → filtering}/plugins/refFilterCreate.js.map +0 -0
  125. /package/{dist/filtering → filtering}/plugins/searchableJsonFilterCreate.d.ts +0 -0
  126. /package/{dist/filtering → filtering}/plugins/searchableJsonFilterCreate.js +0 -0
  127. /package/{dist/filtering → filtering}/plugins/searchableJsonFilterCreate.js.map +0 -0
  128. /package/{dist/filtering → filtering}/sort.d.ts +0 -0
  129. /package/{dist/filtering → filtering}/sort.js +0 -0
  130. /package/{dist/filtering → filtering}/sort.js.map +0 -0
  131. /package/{dist/filtering → filtering}/transform.d.ts +0 -0
  132. /package/{dist/filtering → filtering}/transform.js +0 -0
  133. /package/{dist/filtering → filtering}/transform.js.map +0 -0
  134. /package/{dist/filtering → filtering}/values.d.ts +0 -0
  135. /package/{dist/filtering → filtering}/values.js +0 -0
  136. /package/{dist/filtering → filtering}/values.js.map +0 -0
  137. /package/{dist/filtering → filtering}/where.d.ts +0 -0
  138. /package/{dist/filtering → filtering}/where.js +0 -0
  139. /package/{dist/filtering → filtering}/where.js.map +0 -0
  140. /package/{dist/handlers → handlers}/datetimeTransformHandler.d.ts +0 -0
  141. /package/{dist/handlers → handlers}/datetimeTransformHandler.js +0 -0
  142. /package/{dist/handlers → handlers}/datetimeTransformHandler.js.map +0 -0
  143. /package/{dist/handlers → handlers}/defaultFilterCreateHandler.d.ts +0 -0
  144. /package/{dist/handlers → handlers}/defaultFilterCreateHandler.js +0 -0
  145. /package/{dist/handlers → handlers}/defaultFilterCreateHandler.js.map +0 -0
  146. /package/{dist/handlers → handlers}/locationFolderIdPathHandler.d.ts +0 -0
  147. /package/{dist/handlers → handlers}/locationFolderIdPathHandler.js +0 -0
  148. /package/{dist/handlers → handlers}/locationFolderIdPathHandler.js.map +0 -0
  149. /package/{dist/handlers → handlers}/objectFilterCreateHandler.d.ts +0 -0
  150. /package/{dist/handlers → handlers}/objectFilterCreateHandler.js +0 -0
  151. /package/{dist/handlers → handlers}/objectFilterCreateHandler.js.map +0 -0
  152. /package/{dist/handlers → handlers}/plainObjectPathHandler.d.ts +0 -0
  153. /package/{dist/handlers → handlers}/plainObjectPathHandler.js +0 -0
  154. /package/{dist/handlers → handlers}/plainObjectPathHandler.js.map +0 -0
  155. /package/{dist/handlers → handlers}/refFilterCreateHandler.d.ts +0 -0
  156. /package/{dist/handlers → handlers}/refFilterCreateHandler.js +0 -0
  157. /package/{dist/handlers → handlers}/refFilterCreateHandler.js.map +0 -0
  158. /package/{dist/handlers → handlers}/searchableJsonFilterCreateHandler.d.ts +0 -0
  159. /package/{dist/handlers → handlers}/searchableJsonFilterCreateHandler.js +0 -0
  160. /package/{dist/handlers → handlers}/searchableJsonFilterCreateHandler.js.map +0 -0
  161. /package/{dist/index.d.ts → index.d.ts} +0 -0
  162. /package/{dist/index.js → index.js} +0 -0
  163. /package/{dist/path → path}/locationFolderId.d.ts +0 -0
  164. /package/{dist/path → path}/locationFolderId.js +0 -0
  165. /package/{dist/path → path}/locationFolderId.js.map +0 -0
  166. /package/{dist/path → path}/plainObject.d.ts +0 -0
  167. /package/{dist/path → path}/plainObject.js +0 -0
  168. /package/{dist/path → path}/plainObject.js.map +0 -0
  169. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPathPlugin.d.ts +0 -0
  170. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPathPlugin.js +0 -0
  171. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPathPlugin.js.map +0 -0
  172. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPlugin.d.ts +0 -0
  173. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPlugin.js +0 -0
  174. /package/{dist/plugins → plugins}/CmsEntryFieldFilterPlugin.js.map +0 -0
  175. /package/{dist/plugins → plugins}/CmsEntryFieldSortingPlugin.d.ts +0 -0
  176. /package/{dist/plugins → plugins}/CmsEntryFieldSortingPlugin.js +0 -0
  177. /package/{dist/plugins → plugins}/CmsEntryFieldSortingPlugin.js.map +0 -0
  178. /package/{dist/plugins → plugins}/CmsFieldFilterValueTransformPlugin.d.ts +0 -0
  179. /package/{dist/plugins → plugins}/CmsFieldFilterValueTransformPlugin.js +0 -0
  180. /package/{dist/plugins → plugins}/CmsFieldFilterValueTransformPlugin.js.map +0 -0
  181. /package/{dist/plugins → plugins}/index.d.ts +0 -0
  182. /package/{dist/plugins → plugins}/index.js +0 -0
  183. /package/{dist/transforms → transforms}/datetime.d.ts +0 -0
  184. /package/{dist/transforms → transforms}/datetime.js +0 -0
  185. /package/{dist/transforms → transforms}/datetime.js.map +0 -0
@@ -1,166 +0,0 @@
1
- import type {
2
- CmsEntry,
3
- CmsEntryListWhere,
4
- CmsEntryValues
5
- } from "@webiny/api-headless-cms/types/index.js";
6
- import WebinyError from "@webiny/error";
7
- import type { Field } from "./fields/types.js";
8
- import { createFullTextSearch } from "./fullTextSearch.js";
9
- import type { Expression, ExpressionCondition, Filter } from "./expressions/createExpressions.js";
10
- import { createExpressions } from "./expressions/createExpressions.js";
11
- import { transformValue } from "./transform.js";
12
- import { getValue } from "./getValue.js";
13
- import { ValueFilterRegistry } from "@webiny/db-utils";
14
- import type { FieldFilterCreateRegistry } from "../features/fieldFilterCreate/abstractions.js";
15
- import type { FieldFilterValueTransformRegistry } from "../features/fieldFilterValueTransform/abstractions.js";
16
-
17
- interface ExecuteFilterParams {
18
- value: any;
19
- filter: Filter;
20
- }
21
-
22
- const executeFilter = (params: ExecuteFilterParams) => {
23
- const { value, filter } = params;
24
-
25
- const canUse = filter.filter.canUse({
26
- value,
27
- compareValue: filter.compareValue
28
- });
29
- if (!canUse) {
30
- return true;
31
- }
32
-
33
- const matched = filter.filter.matches({
34
- value,
35
- compareValue: filter.compareValue
36
- });
37
- if (filter.negate) {
38
- return matched === false;
39
- }
40
- return matched;
41
- };
42
-
43
- interface ExecuteExpressionsParams {
44
- getCachedValue: (filter: Filter) => Promise<any>;
45
- expressions: Expression[];
46
- filters: Filter[];
47
- condition: ExpressionCondition;
48
- }
49
-
50
- const executeExpressions = (params: ExecuteExpressionsParams): boolean => {
51
- const { expressions, getCachedValue, filters, condition } = params;
52
- if (expressions.length === 0 && filters.length === 0) {
53
- return true;
54
- }
55
- for (const filter of filters) {
56
- const value = getCachedValue(filter);
57
-
58
- const result = executeFilter({
59
- value,
60
- filter
61
- });
62
- if (!result) {
63
- return false;
64
- }
65
- }
66
- for (const expression of expressions) {
67
- const result = executeExpressions({
68
- ...expression,
69
- getCachedValue
70
- });
71
- if (result && condition === "OR") {
72
- return true;
73
- } else if (!result && condition == "AND") {
74
- return false;
75
- }
76
- }
77
- return condition === "OR" ? false : true;
78
- };
79
-
80
- interface IFilterParams<T extends CmsEntryValues = CmsEntryValues> {
81
- filterCreateRegistry: FieldFilterCreateRegistry.Interface;
82
- transformRegistry: FieldFilterValueTransformRegistry.Interface;
83
- valueFilterRegistry: ValueFilterRegistry.Interface;
84
- items: CmsEntry<T>[];
85
- where: Partial<CmsEntryListWhere>;
86
- fields: Record<string, Field>;
87
- fullTextSearch?: {
88
- term?: string;
89
- fields?: string[];
90
- };
91
- }
92
-
93
- export const filter = <T extends CmsEntryValues = CmsEntryValues>(
94
- params: IFilterParams<T>
95
- ): CmsEntry<T>[] => {
96
- const {
97
- items: records,
98
- where,
99
- filterCreateRegistry,
100
- transformRegistry,
101
- fields,
102
- fullTextSearch,
103
- valueFilterRegistry
104
- } = params;
105
-
106
- const keys = Object.keys(where);
107
- if (keys.length === 0 && !fullTextSearch) {
108
- return records;
109
- }
110
- const expression = createExpressions({
111
- filterCreateRegistry,
112
- transformRegistry,
113
- where,
114
- fields,
115
- valueFilterRegistry
116
- });
117
-
118
- if (
119
- expression.filters.length === 0 &&
120
- expression.expressions.length === 0 &&
121
- !fullTextSearch?.term
122
- ) {
123
- return records;
124
- }
125
- const fullTextSearchFilter = valueFilterRegistry.get("contains");
126
- if (!fullTextSearchFilter) {
127
- throw new WebinyError(
128
- `Missing "contains" plugin to run the full-text search.`,
129
- "MISSING_PLUGIN"
130
- );
131
- }
132
-
133
- const search = createFullTextSearch({
134
- term: fullTextSearch?.term,
135
- targetFields: fullTextSearch?.fields,
136
- fields,
137
- filter: fullTextSearchFilter
138
- });
139
-
140
- return records.filter(record => {
141
- const cachedValues: Record<string, any> = {};
142
-
143
- const getCachedValue = (filter: Filter) => {
144
- const { path } = filter;
145
- if (cachedValues[path] !== undefined) {
146
- return cachedValues[path];
147
- }
148
- const plainValue = getValue(record, path);
149
-
150
- const rawValue = transformValue({
151
- value: plainValue,
152
- transform: filter.transformValue
153
- });
154
-
155
- cachedValues[path] = rawValue;
156
- return rawValue;
157
- };
158
-
159
- const exprResult = executeExpressions({ ...expression, getCachedValue });
160
- if (!exprResult || !search) {
161
- return exprResult;
162
- }
163
-
164
- return search(record);
165
- });
166
- };
@@ -1,40 +0,0 @@
1
- import WebinyError from "@webiny/error";
2
- import type { CmsEntry } from "@webiny/api-headless-cms/types/index.js";
3
- import type { Field } from "./fields/types.js";
4
- import { getValue } from "./getValue.js";
5
- import { ValueFilter } from "@webiny/db-utils";
6
-
7
- interface Params {
8
- term?: string;
9
- targetFields?: string[];
10
- fields: Record<string, Field>;
11
- filter: ValueFilter.Interface;
12
- }
13
-
14
- /* Unfortunately we must use the contains plugin directly as plugins do not support multi field searching. */
15
- export const createFullTextSearch = (params: Params) => {
16
- const { term, targetFields, fields: fieldDefinitions, filter } = params;
17
- if (!term || term.trim().length === 0 || !targetFields || targetFields.length === 0) {
18
- return null;
19
- }
20
- return (item: CmsEntry) => {
21
- for (const target of targetFields) {
22
- /* As fields is a mapped Field objects where key is a path to the value, we can directly find the related field. */
23
- const field = fieldDefinitions[target];
24
-
25
- if (!field) {
26
- throw new WebinyError(`Unknown field "${target}" in the model.`, "UNKNOWN_FIELD", {
27
- target
28
- });
29
- }
30
- const value = getValue(item, target);
31
- if (!value) {
32
- continue;
33
- }
34
- if (filter.matches({ value, compareValue: term }) === true) {
35
- return true;
36
- }
37
- }
38
- return false;
39
- };
40
- };
@@ -1,64 +0,0 @@
1
- const addArrayResult = (target: any[], result: any[]): void => {
2
- for (const r of result) {
3
- if (target.some(t => r === t)) {
4
- continue;
5
- }
6
- target.push(r);
7
- }
8
- };
9
- /*
10
- * A recursive function which goes through given input paths and returns the value in it.
11
- * In case a path is an array, it goes through the array of those values to get values further down the path line.
12
- */
13
- const find = (target: Record<string, any> | undefined, input: string[]): any[] | undefined => {
14
- const paths = [...input];
15
- const path = paths.shift();
16
-
17
- if (!path) {
18
- return undefined;
19
- }
20
-
21
- const value = target?.[path];
22
- if (value === undefined) {
23
- return undefined;
24
- }
25
- if (paths.length === 0) {
26
- return value;
27
- } else if (Array.isArray(value)) {
28
- if (value.length === 0) {
29
- return undefined;
30
- }
31
- return value.reduce<any[]>((collection, v) => {
32
- const result = find(v, paths);
33
- if (result === undefined) {
34
- return collection;
35
- } else if (Array.isArray(result)) {
36
- addArrayResult(collection, result);
37
- return collection;
38
- }
39
- collection.push(result);
40
- return collection;
41
- }, []);
42
- }
43
- return find(value, paths);
44
- };
45
- /*
46
- * A wrapper function for the find function.
47
- * Basically it transforms input paths to an array (and runs various checks).
48
- */
49
- export const getValue = (target: Record<string, any>, input: string | string[]): any => {
50
- const paths = Array.isArray(input) ? input : input.split(".");
51
- if (paths.length === 0) {
52
- throw new Error(`Path is empty!`);
53
- }
54
- const filtered = paths.filter(Boolean);
55
- if (paths.length !== filtered.length) {
56
- throw new Error(
57
- `Input path is different than the filtered empty path string. (${paths.join(
58
- "."
59
- )} to ${filtered.join(".")})`
60
- );
61
- }
62
-
63
- return find(target, paths);
64
- };
@@ -1,30 +0,0 @@
1
- import type { Plugin, PluginsContainer } from "@webiny/plugins/types.js";
2
- import WebinyError from "@webiny/error";
3
-
4
- interface Params {
5
- plugins: PluginsContainer;
6
- type: string;
7
- property: string;
8
- }
9
- export const getMappedPlugins = <T extends Plugin>(params: Params) => {
10
- const { plugins: pluginsContainer, type, property } = params;
11
- const plugins = pluginsContainer.byType<T>(type);
12
- if (plugins.length === 0) {
13
- return {};
14
- }
15
- return plugins.reduce<Record<string, T>>((collection, plugin) => {
16
- const key: keyof typeof plugin = plugin[property];
17
- if (typeof key !== "string") {
18
- throw new WebinyError(
19
- "Property to map the plugins on must be a string.",
20
- "PLUGIN_PROPERTY_ERROR",
21
- {
22
- type,
23
- property
24
- }
25
- );
26
- }
27
- collection[key] = plugin;
28
- return collection;
29
- }, {});
30
- };
@@ -1,36 +0,0 @@
1
- import WebinyError from "@webiny/error";
2
- import { CmsEntryFieldFilterPlugin } from "../../plugins/CmsEntryFieldFilterPlugin.js";
3
-
4
- export const createDefaultFilterCreate = () => {
5
- const plugin = new CmsEntryFieldFilterPlugin({
6
- fieldType: CmsEntryFieldFilterPlugin.ALL,
7
- create: params => {
8
- const { negate, transformValue, field, compareValue, valueFilterRegistry } = params;
9
- const filter = valueFilterRegistry.get(params.operation);
10
- if (!filter) {
11
- throw new WebinyError(
12
- `Missing ValueFilterPlugin for operation "${params.operation}".`,
13
- "MISSING_OPERATION_PLUGIN",
14
- {
15
- operation: params.operation
16
- }
17
- );
18
- }
19
- return {
20
- negate,
21
- transformValue,
22
- field,
23
- compareValue,
24
- fieldPathId: [...field.parents.map(f => f.fieldId), field.fieldId].join("."),
25
- path: field.createPath({
26
- field
27
- }),
28
- filter
29
- };
30
- }
31
- });
32
-
33
- plugin.name = `headless-cms.ddb.filter.default`;
34
-
35
- return plugin;
36
- };
@@ -1,13 +0,0 @@
1
- import { createDefaultFilterCreate } from "./defaultFilterCreate.js";
2
- import { createRefFilterCreate } from "./refFilterCreate.js";
3
- import { objectFilterCreate } from "./objectFilterCreate.js";
4
- import { searchableJsonFilterCreate } from "./searchableJsonFilterCreate.js";
5
-
6
- export const createFilterCreatePlugins = () => {
7
- return [
8
- createDefaultFilterCreate(),
9
- createRefFilterCreate(),
10
- objectFilterCreate(),
11
- searchableJsonFilterCreate()
12
- ];
13
- };
@@ -1,100 +0,0 @@
1
- import { CmsEntryFieldFilterPlugin } from "../../plugins/CmsEntryFieldFilterPlugin.js";
2
- import { extractWhereParams } from "../where.js";
3
- import WebinyError from "@webiny/error";
4
- import type { ICmsFieldFilterValueTransformPlugin } from "../../plugins/CmsFieldFilterValueTransformPlugin.js";
5
- import { transformValue } from "../transform.js";
6
- import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
7
-
8
- export const objectFilterCreate = () => {
9
- const plugin = new CmsEntryFieldFilterPlugin({
10
- fieldType: "object",
11
- create: params => {
12
- const {
13
- value: objectValue,
14
- valueFilterRegistry,
15
- transformValuePlugins,
16
- getFilterCreatePlugin,
17
- field: parentField,
18
- fields
19
- } = params;
20
-
21
- const filters = [];
22
-
23
- for (const key in objectValue) {
24
- const value = objectValue[key];
25
- if (value === undefined) {
26
- continue;
27
- }
28
- const whereParams = extractWhereParams(key);
29
- if (!whereParams) {
30
- continue;
31
- }
32
- const { negate, fieldId, operation } = whereParams;
33
-
34
- const fieldPath = parentField.parents
35
- .map(p => p.fieldId)
36
- .concat([parentField.fieldId, fieldId])
37
- .join(".");
38
-
39
- const field = fields[fieldPath];
40
- if (!field) {
41
- throw new WebinyError(
42
- `There is no field with the field path "${fieldPath}".`,
43
- "FIELD_ERROR",
44
- {
45
- fieldId
46
- }
47
- );
48
- }
49
-
50
- const fieldType = getBaseFieldType(field);
51
-
52
- const filterCreatePlugin = getFilterCreatePlugin(fieldType);
53
-
54
- const transformValuePlugin: ICmsFieldFilterValueTransformPlugin =
55
- transformValuePlugins[fieldType];
56
-
57
- const transformValueCallable = (value: any) => {
58
- if (!transformValuePlugin) {
59
- return value;
60
- }
61
- return transformValuePlugin.transform({
62
- field,
63
- value
64
- });
65
- };
66
-
67
- const result = filterCreatePlugin.create({
68
- key,
69
- value,
70
- valueFilterRegistry,
71
- transformValuePlugins,
72
- getFilterCreatePlugin,
73
- operation,
74
- negate,
75
- field,
76
- fields,
77
- compareValue: transformValue({
78
- value,
79
- transform: transformValueCallable
80
- }),
81
- transformValue: transformValueCallable
82
- });
83
- if (!result) {
84
- continue;
85
- }
86
- if (Array.isArray(result)) {
87
- filters.push(...result);
88
- continue;
89
- }
90
-
91
- filters.push(result);
92
- }
93
- return filters;
94
- }
95
- });
96
-
97
- plugin.name = `headless-cms.ddb.filter.object`;
98
-
99
- return plugin;
100
- };
@@ -1,83 +0,0 @@
1
- import WebinyError from "@webiny/error";
2
- import type { CmsEntryFieldFilterPluginCreateResponse } from "../../plugins/CmsEntryFieldFilterPlugin.js";
3
- import { CmsEntryFieldFilterPlugin } from "../../plugins/CmsEntryFieldFilterPlugin.js";
4
- import { extractWhereParams } from "../where.js";
5
- import { transformValue } from "../transform.js";
6
- import type { GenericRecord } from "@webiny/api/types.js";
7
- import { getBaseFieldType } from "@webiny/api-headless-cms/utils/getBaseFieldType.js";
8
-
9
- export const createRefFilterCreate = () => {
10
- const plugin = new CmsEntryFieldFilterPlugin<GenericRecord | null | undefined>({
11
- fieldType: "ref",
12
- create: params => {
13
- const { valueFilterRegistry, transformValuePlugins, field } = params;
14
- let value = params.value;
15
- if (!value) {
16
- value = {
17
- entryId: null
18
- };
19
- }
20
- const propertyFilters = Object.keys(value);
21
- if (propertyFilters.length === 0) {
22
- return null;
23
- }
24
- const filters: CmsEntryFieldFilterPluginCreateResponse[] = [];
25
-
26
- for (const propertyFilter of propertyFilters) {
27
- const whereParams = extractWhereParams(propertyFilter);
28
- if (!whereParams) {
29
- continue;
30
- }
31
- const { fieldId: propertyId, operation: propertyOperation, negate } = whereParams;
32
-
33
- const fieldType = getBaseFieldType(field);
34
-
35
- const transformValuePlugin = transformValuePlugins[fieldType];
36
-
37
- const transformValueCallable = (value: any) => {
38
- if (!transformValuePlugin) {
39
- return value;
40
- }
41
- return transformValuePlugin.transform({
42
- field,
43
- value
44
- });
45
- };
46
-
47
- const filter = valueFilterRegistry.get(propertyOperation);
48
- if (!filter) {
49
- throw new WebinyError(
50
- `Missing operation filter for "${propertyOperation}".`,
51
- "MISSING_OPERATION_FILTER"
52
- );
53
- }
54
-
55
- const paths = [
56
- field.createPath({
57
- field
58
- }),
59
- propertyId
60
- ];
61
-
62
- filters.push({
63
- field,
64
- path: paths.join("."),
65
- fieldPathId: [...field.parents.map(f => f.fieldId), field.fieldId].join("."),
66
- filter,
67
- negate,
68
- compareValue: transformValue({
69
- value: value[propertyFilter],
70
- transform: transformValueCallable
71
- }),
72
- transformValue: transformValueCallable
73
- });
74
- }
75
-
76
- return filters;
77
- }
78
- });
79
-
80
- plugin.name = `headless-cms.ddb.filter.ref`;
81
-
82
- return plugin;
83
- };
@@ -1,78 +0,0 @@
1
- import type { CmsEntryFieldFilterPluginCreateResponse } from "../../plugins/CmsEntryFieldFilterPlugin.js";
2
- import { CmsEntryFieldFilterPlugin } from "../../plugins/CmsEntryFieldFilterPlugin.js";
3
- import { extractWhereParams } from "../where.js";
4
-
5
- function dotFlatten(obj: Record<string, any>, prefix = ""): Record<string, any> {
6
- return Object.entries(obj).reduce<Record<string, any>>((acc, [key, val]) => {
7
- const path = prefix ? `${prefix}.${key}` : key;
8
- if (val && typeof val === "object" && !Array.isArray(val)) {
9
- Object.assign(acc, dotFlatten(val, path));
10
- } else {
11
- acc[path] = val;
12
- }
13
- return acc;
14
- }, {});
15
- }
16
-
17
- export const searchableJsonFilterCreate = () => {
18
- const plugin = new CmsEntryFieldFilterPlugin({
19
- fieldType: "searchable-json",
20
- create: params => {
21
- const { value: objectValue, valueFilterRegistry, field: parentField } = params;
22
-
23
- const filters = [];
24
-
25
- const accessPatterns = dotFlatten(objectValue);
26
-
27
- for (const key in accessPatterns) {
28
- const value = accessPatterns[key];
29
- if (value === undefined) {
30
- continue;
31
- }
32
-
33
- const whereParams = extractWhereParams(key);
34
- if (!whereParams) {
35
- continue;
36
- }
37
- const { negate, operation } = whereParams;
38
-
39
- const transformValueCallable = (value: any) => {
40
- return value;
41
- };
42
-
43
- const fieldId = `${parentField.fieldId}.${whereParams.fieldId ?? key}`;
44
-
45
- const filter = valueFilterRegistry.get(operation);
46
- if (!filter) {
47
- console.error(`Missing operation filter for "${operation}".`);
48
- continue;
49
- }
50
-
51
- const result: CmsEntryFieldFilterPluginCreateResponse = {
52
- field: parentField,
53
- path: `values.${fieldId}`,
54
- fieldPathId: `values.${fieldId}`,
55
- negate,
56
- filter,
57
- compareValue: value,
58
- transformValue: transformValueCallable
59
- };
60
-
61
- if (!result) {
62
- continue;
63
- }
64
- if (Array.isArray(result)) {
65
- filters.push(...result);
66
- continue;
67
- }
68
-
69
- filters.push(result);
70
- }
71
- return filters;
72
- }
73
- });
74
-
75
- plugin.name = `headless-cms.ddb.filter.searchable-json`;
76
-
77
- return plugin;
78
- };
@@ -1,79 +0,0 @@
1
- import type { CmsEntry, CmsEntryValues, CmsModel } from "@webiny/api-headless-cms/types/index.js";
2
- import WebinyError from "@webiny/error";
3
- import * as dotProp from "dot-prop";
4
- import lodashSortBy from "lodash/sortBy.js";
5
- import { extractSort } from "./fields/extractSort.js";
6
- import type { Field } from "./fields/types.js";
7
- import type { FieldSortingRegistry } from "../features/fieldSorting/abstractions.js";
8
-
9
- interface Params<T extends CmsEntryValues = CmsEntryValues> {
10
- model: CmsModel;
11
- items: CmsEntry<T>[];
12
- sort?: string[];
13
- fields: Record<string, Field>;
14
- sortingRegistry: FieldSortingRegistry.Interface;
15
- }
16
-
17
- interface SortedItem {
18
- id: string;
19
- value: any;
20
- }
21
-
22
- export const sort = <T extends CmsEntryValues = CmsEntryValues>(
23
- params: Params<T>
24
- ): CmsEntry<T>[] => {
25
- const { model, items, sort = [], fields, sortingRegistry } = params;
26
- if (items.length <= 1) {
27
- return items;
28
- } else if (sort.length === 0) {
29
- sort.push("savedOn_DESC");
30
- } else if (sort.length > 1) {
31
- throw new WebinyError(
32
- "Sorting is limited to a single field.",
33
- "SORT_MULTIPLE_FIELDS_ERROR",
34
- {
35
- sort
36
- }
37
- );
38
- }
39
- const [firstSort] = sort;
40
- if (!firstSort) {
41
- throw new WebinyError("Empty sort array item.", "SORT_EMPTY_ERROR", {
42
- sort
43
- });
44
- }
45
-
46
- const { fieldId, field, valuePath, reverse } = extractSort({
47
- model,
48
- sortBy: firstSort,
49
- fields,
50
- sortingRegistry
51
- });
52
-
53
- const itemsToSort = items.map(item => {
54
- return {
55
- id: item.id,
56
- value: field.transform(dotProp.getProperty(item, valuePath))
57
- };
58
- });
59
- const sortedItems: SortedItem[] = lodashSortBy(itemsToSort, "value");
60
- const newItems = sortedItems.map(s => {
61
- const item = items.find(i => i.id === s.id);
62
- if (item) {
63
- return item;
64
- }
65
- throw new WebinyError(
66
- "Could not find item by given id after the sorting.",
67
- "SORTING_ITEMS_ERROR",
68
- {
69
- id: s.id,
70
- sortingBy: fieldId,
71
- reverse
72
- }
73
- );
74
- });
75
- if (!reverse) {
76
- return newItems;
77
- }
78
- return newItems.reverse();
79
- };