@webiny/api-headless-cms-ddb 5.18.3 → 5.19.0-beta.3

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.
@@ -1,2 +1,2 @@
1
- declare const _default: () => (import("@webiny/api-headless-cms").StorageTransformPlugin<any, any> | import("../types").CmsFieldFilterValueTransformPlugin | import("../types").CmsFieldFilterPathPlugin)[];
1
+ declare const _default: () => (import("@webiny/api-headless-cms").StorageTransformPlugin<any, any> | import("../plugins/CmsEntryFieldFilterPathPlugin").CmsEntryFieldFilterPathPlugin | import("../types").CmsFieldFilterValueTransformPlugin)[];
2
2
  export default _default;
package/dynamoDb/index.js CHANGED
@@ -13,12 +13,10 @@ var _longText = _interopRequireDefault(require("./storage/longText"));
13
13
 
14
14
  var _date = _interopRequireDefault(require("./storage/date"));
15
15
 
16
- var _plainObject = _interopRequireDefault(require("./path/plainObject"));
17
-
18
- var _ref = _interopRequireDefault(require("./path/ref"));
16
+ var _plainObject = require("./path/plainObject");
19
17
 
20
18
  var _datetime = _interopRequireDefault(require("./transformValue/datetime"));
21
19
 
22
- var _default = () => [(0, _richText.default)(), (0, _longText.default)(), (0, _date.default)(), (0, _plainObject.default)(), (0, _ref.default)(), (0, _datetime.default)()];
20
+ var _default = () => [(0, _richText.default)(), (0, _longText.default)(), (0, _date.default)(), (0, _plainObject.createPlainObjectPathPlugin)(), (0, _datetime.default)()];
23
21
 
24
22
  exports.default = _default;
@@ -1,3 +1,2 @@
1
- import { CmsFieldFilterPathPlugin } from "../../types";
2
- declare const _default: () => CmsFieldFilterPathPlugin;
3
- export default _default;
1
+ import { CmsEntryFieldFilterPathPlugin } from "../../plugins/CmsEntryFieldFilterPathPlugin";
2
+ export declare const createPlainObjectPathPlugin: () => CmsEntryFieldFilterPathPlugin;
@@ -5,29 +5,33 @@ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefau
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = void 0;
8
+ exports.createPlainObjectPathPlugin = void 0;
9
9
 
10
10
  var _error = _interopRequireDefault(require("@webiny/error"));
11
11
 
12
- var _default = () => ({
13
- type: "cms-field-filter-path",
14
- name: "cms-field-filter-path-plain-object",
15
- fieldType: "plainObject",
16
- createPath: ({
17
- field
18
- }) => {
19
- const {
20
- path
21
- } = field.settings || {};
22
-
23
- if (!path) {
24
- throw new _error.default("Missing path settings value.", "FIELD_SETTINGS_ERROR", {
25
- field
26
- });
27
- }
28
-
29
- return path;
12
+ var _CmsEntryFieldFilterPathPlugin = require("../../plugins/CmsEntryFieldFilterPathPlugin");
13
+
14
+ const createPath = ({
15
+ field
16
+ }) => {
17
+ const {
18
+ path
19
+ } = field.settings || {};
20
+
21
+ if (!path) {
22
+ throw new _error.default("Missing path settings value.", "FIELD_SETTINGS_ERROR", {
23
+ field
24
+ });
30
25
  }
31
- });
32
26
 
33
- exports.default = _default;
27
+ return path;
28
+ };
29
+
30
+ const createPlainObjectPathPlugin = () => {
31
+ return new _CmsEntryFieldFilterPathPlugin.CmsEntryFieldFilterPathPlugin({
32
+ fieldType: "plainObject",
33
+ path: createPath
34
+ });
35
+ };
36
+
37
+ exports.createPlainObjectPathPlugin = createPlainObjectPathPlugin;
@@ -37,7 +37,7 @@ const getAllEntryRevisions = params => {
37
37
  locale
38
38
  } = model;
39
39
  return new _dataloader.default(async ids => {
40
- const results = [];
40
+ const results = {};
41
41
 
42
42
  for (const id of ids) {
43
43
  const queryAllParams = {
@@ -52,11 +52,12 @@ const getAllEntryRevisions = params => {
52
52
  }
53
53
  };
54
54
  const items = await (0, _query.queryAll)(queryAllParams);
55
- const entries = (0, _cleanup.cleanupItems)(entity, items);
56
- results.push(entries);
55
+ results[id] = (0, _cleanup.cleanupItems)(entity, items);
57
56
  }
58
57
 
59
- return results;
58
+ return ids.map(id => {
59
+ return results[id] || [];
60
+ });
60
61
  });
61
62
  };
62
63
 
@@ -331,13 +331,6 @@ const createEntriesStorageOperations = params => {
331
331
  }
332
332
  };
333
333
 
334
- const getAllRevisionsByIds = async (model, params) => {
335
- return await dataLoaders.getAllEntryRevisions({
336
- model,
337
- ids: params.ids
338
- });
339
- };
340
-
341
334
  const getLatestRevisionByEntryId = async (model, params) => {
342
335
  const result = await dataLoaders.getLatestRevisionByEntryId({
343
336
  model,
@@ -408,7 +401,9 @@ const createEntriesStorageOperations = params => {
408
401
  const getPreviousRevision = async (model, params) => {
409
402
  const {
410
403
  tenant,
411
- locale,
404
+ locale
405
+ } = model;
406
+ const {
412
407
  entryId,
413
408
  version
414
409
  } = params;
@@ -456,7 +451,7 @@ const createEntriesStorageOperations = params => {
456
451
  after,
457
452
  sort
458
453
  } = params;
459
- const limit = initialLimit <= 0 || initialLimit >= 100 ? 100 : initialLimit;
454
+ const limit = initialLimit <= 0 || initialLimit >= 10000 ? 10000 : initialLimit;
460
455
  const type = originalWhere.published ? "P" : "L";
461
456
  const queryAllParams = {
462
457
  entity,
@@ -709,7 +704,7 @@ const createEntriesStorageOperations = params => {
709
704
  })));
710
705
  }
711
706
 
712
- if (publishedStorageEntry) {
707
+ if (publishedStorageEntry && publishedStorageEntry.id !== entry.id) {
713
708
  items.push(entity.putBatch(_objectSpread(_objectSpread({}, publishedStorageEntry), {}, {
714
709
  PK: partitionKey,
715
710
  SK: (0, _keys.createRevisionSortKey)(publishedStorageEntry),
@@ -806,7 +801,6 @@ const createEntriesStorageOperations = params => {
806
801
  getByIds,
807
802
  getRevisionById,
808
803
  getPublishedRevisionByEntryId,
809
- getAllRevisionsByIds,
810
804
  getLatestRevisionByEntryId,
811
805
  get,
812
806
  getRevisions,
@@ -45,6 +45,11 @@ const systemFields = {
45
45
  settings: {
46
46
  path: "ownedBy.id"
47
47
  }
48
+ }),
49
+ version: createSystemField({
50
+ id: "version",
51
+ type: "number",
52
+ fieldId: "version"
48
53
  })
49
54
  };
50
55
  exports.systemFields = systemFields;
@@ -1,9 +1,10 @@
1
1
  import { CmsEntry, CmsEntryListWhere, CmsModel, CmsModelField } from "@webiny/api-headless-cms/types";
2
2
  import { PluginsContainer } from "@webiny/plugins";
3
+ import { CreatePathCallable as CmsEntryFieldFieldCreatePathCallable } from "../../plugins/CmsEntryFieldFilterPathPlugin";
3
4
  interface ModelField {
4
5
  def: CmsModelField;
5
6
  valueTransformer: (value: any) => any;
6
- valuePath: string;
7
+ createPath: CmsEntryFieldFieldCreatePathCallable;
7
8
  isSystemField?: boolean;
8
9
  }
9
10
  declare type ModelFieldRecords = Record<string, ModelField>;
@@ -17,6 +17,8 @@ var _systemFields = require("./systemFields");
17
17
 
18
18
  var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin");
19
19
 
20
+ var _CmsEntryFieldFilterPathPlugin = require("../../plugins/CmsEntryFieldFilterPathPlugin");
21
+
20
22
  const VALUES_ATTRIBUTE = "values";
21
23
 
22
24
  const extractWhereParams = key => {
@@ -52,6 +54,63 @@ const transformValue = (value, transform) => {
52
54
  return transform(value);
53
55
  };
54
56
 
57
+ const getFilterPlugin = params => {
58
+ const {
59
+ plugins,
60
+ operation
61
+ } = params;
62
+ const plugin = plugins[operation];
63
+
64
+ if (plugin) {
65
+ return plugin;
66
+ }
67
+
68
+ throw new _error.default(`There is no filter plugin for operation "${operation}".`, "FILTER_PLUGIN_ERROR", {
69
+ operation
70
+ });
71
+ };
72
+
73
+ const createValuePath = params => {
74
+ const {
75
+ field,
76
+ plugins,
77
+ index
78
+ } = params;
79
+ const {
80
+ fieldId
81
+ } = field;
82
+ const valuePathPlugin = plugins[field.type];
83
+ const basePath = _systemFields.systemFields[fieldId] ? "" : `${VALUES_ATTRIBUTE}.`;
84
+
85
+ if (!valuePathPlugin || valuePathPlugin.canUse(field) === false) {
86
+ return `${basePath}${field.fieldId}`;
87
+ }
88
+
89
+ const path = valuePathPlugin.createPath({
90
+ field,
91
+ index
92
+ });
93
+ return `${basePath}${path}`;
94
+ };
95
+
96
+ const isObjectFiltering = params => {
97
+ const {
98
+ value
99
+ } = params;
100
+
101
+ if (!value) {
102
+ return false;
103
+ } else if (Array.isArray(value) === true) {
104
+ return false;
105
+ } else if (value instanceof Date || !!value.toISOString) {
106
+ return false;
107
+ } else if (typeof value !== "object") {
108
+ return false;
109
+ }
110
+
111
+ return true;
112
+ };
113
+
55
114
  const createFilters = params => {
56
115
  const {
57
116
  where,
@@ -70,10 +129,16 @@ const createFilters = params => {
70
129
  });
71
130
  const valuePathPlugins = getMappedPlugins({
72
131
  plugins,
73
- type: "cms-field-filter-path",
132
+ type: _CmsEntryFieldFilterPathPlugin.CmsEntryFieldFilterPathPlugin.type,
74
133
  property: "fieldType"
75
134
  });
76
- return Object.keys(where).map(key => {
135
+ const filters = [];
136
+
137
+ for (const key in where) {
138
+ if (where.hasOwnProperty(key) === false) {
139
+ continue;
140
+ }
141
+
77
142
  const {
78
143
  fieldId,
79
144
  operation,
@@ -88,33 +153,6 @@ const createFilters = params => {
88
153
  }
89
154
 
90
155
  const transformValuePlugin = transformValuePlugins[field.def.type];
91
- const valuePathPlugin = valuePathPlugins[field.def.type];
92
- let targetValuePath;
93
- /**
94
- * add the base path if field is not a system field
95
- * pathPlugin should not know about that
96
- */
97
-
98
- const basePath = _systemFields.systemFields[fieldId] ? "" : `${VALUES_ATTRIBUTE}.`;
99
-
100
- if (valuePathPlugin) {
101
- targetValuePath = valuePathPlugin.createPath({
102
- field: field.def
103
- });
104
- } else if (_systemFields.systemFields[fieldId]) {
105
- targetValuePath = fieldId;
106
- } else {
107
- targetValuePath = field.def.fieldId;
108
- }
109
-
110
- const valuePath = `${basePath}${targetValuePath}`;
111
- const filterPlugin = filterPlugins[operation];
112
-
113
- if (!filterPlugin) {
114
- throw new _error.default(`There is no filter plugin for operation "${operation}".`, "FILTER_PLUGIN_ERROR", {
115
- operation
116
- });
117
- }
118
156
 
119
157
  const transformValueCallable = value => {
120
158
  if (!transformValuePlugin) {
@@ -127,20 +165,111 @@ const createFilters = params => {
127
165
  });
128
166
  };
129
167
 
130
- return {
168
+ const objectFilteringParams = {
169
+ key,
170
+ value: where[key],
171
+ field: field.def
172
+ };
173
+
174
+ if (isObjectFiltering(objectFilteringParams)) {
175
+ const propertyFilters = Object.keys(where[key]);
176
+
177
+ if (propertyFilters.length === 0) {
178
+ continue;
179
+ }
180
+
181
+ for (const propertyFilter of propertyFilters) {
182
+ const {
183
+ fieldId: propertyId,
184
+ operation: propertyOperation,
185
+ negate: propertyNegate
186
+ } = extractWhereParams(propertyFilter);
187
+ const filterPlugin = getFilterPlugin({
188
+ plugins: filterPlugins,
189
+ operation: propertyOperation
190
+ });
191
+ const basePath = createValuePath({
192
+ field: field.def,
193
+ plugins: valuePathPlugins
194
+ });
195
+ const multiValuesPath = field.def.multipleValues ? "%s." : "";
196
+ filters.push({
197
+ fieldId,
198
+ path: `${basePath}.${multiValuesPath}${propertyId}`,
199
+ filterPlugin,
200
+ negate: propertyNegate,
201
+ compareValue: transformValue(where[key][propertyFilter], transformValueCallable),
202
+ transformValue: transformValueCallable
203
+ });
204
+ }
205
+
206
+ continue;
207
+ }
208
+
209
+ const filterPlugin = getFilterPlugin({
210
+ plugins: filterPlugins,
211
+ operation
212
+ });
213
+ filters.push({
131
214
  fieldId,
132
- path: valuePath,
215
+ path: createValuePath({
216
+ field: field.def,
217
+ plugins: valuePathPlugins
218
+ }),
133
219
  filterPlugin,
134
220
  negate,
135
221
  compareValue: transformValue(where[key], transformValueCallable),
136
222
  transformValue: transformValueCallable
137
- };
223
+ });
224
+ }
225
+
226
+ return filters;
227
+ };
228
+ /**
229
+ * In case filter field is not multiple values one, return exact path.
230
+ * If is multiple values field, use path without the last part
231
+ */
232
+
233
+
234
+ const getFilterValuePath = filter => {
235
+ if (filter.path.includes(".%s.") === false) {
236
+ return filter.path;
237
+ }
238
+
239
+ const paths = filter.path.split(".%s.");
240
+ return paths.shift();
241
+ };
242
+
243
+ const getFilterValuePropertyPath = filter => {
244
+ if (filter.path.includes(".%s.") === false) {
245
+ return null;
246
+ }
247
+
248
+ const paths = filter.path.split(".%s.");
249
+ return paths.pop();
250
+ };
251
+
252
+ const execFilter = params => {
253
+ const {
254
+ value: plainValue,
255
+ filter
256
+ } = params;
257
+ const value = transformValue(plainValue, filter.transformValue);
258
+ const matched = filter.filterPlugin.matches({
259
+ value,
260
+ compareValue: filter.compareValue
138
261
  });
262
+
263
+ if (filter.negate) {
264
+ return matched === false;
265
+ }
266
+
267
+ return matched;
139
268
  };
140
269
 
141
270
  const filterItems = async params => {
142
271
  const {
143
- items,
272
+ items: records,
144
273
  where,
145
274
  plugins,
146
275
  fields,
@@ -151,18 +280,55 @@ const filterItems = async params => {
151
280
  where,
152
281
  fields
153
282
  });
154
- const results = [];
283
+ const promises = records.map(async record => {
284
+ /**
285
+ * We need to go through all the filters and apply them to the given record.
286
+ */
287
+ for (const filter of filters) {
288
+ /**
289
+ * In case is multiple values field, last part is removed from path.
290
+ * -> values.categories.id -> values.categories
291
+ */
292
+ const valuePath = getFilterValuePath(filter);
155
293
 
156
- for (const key in items) {
157
- if (items.hasOwnProperty(key) === false) {
158
- continue;
159
- }
294
+ const rawValue = _dotProp.default.get(record, valuePath);
160
295
 
161
- const item = items[key];
162
- let passed = true;
296
+ if (valuePath !== filter.path) {
297
+ /**
298
+ * Calculated is different than original because we need to search in the array of objects.
299
+ */
300
+ const propertyPath = getFilterValuePropertyPath(filter);
163
301
 
164
- for (const filter of filters) {
165
- const rawValue = _dotProp.default.get(item, filter.path);
302
+ if (!propertyPath) {
303
+ console.log(`Cannot determine the property path of "${filter.path}".`);
304
+ continue;
305
+ }
306
+
307
+ const plainValue = await fromStorage(fields[filter.fieldId].def, rawValue);
308
+ /**
309
+ * We cannot go through the value because it is not array. Log the error and continue.
310
+ */
311
+
312
+ if (Array.isArray(plainValue) === false) {
313
+ console.log(`Cannot go through the value on ${valuePath} because it is not an array, and we expect it to be.`);
314
+ continue;
315
+ }
316
+
317
+ record = _dotProp.default.set(record, valuePath, plainValue);
318
+ const values = plainValue.map(value => {
319
+ return value[propertyPath];
320
+ });
321
+ const result = execFilter({
322
+ value: values,
323
+ filter
324
+ });
325
+
326
+ if (!result) {
327
+ return null;
328
+ }
329
+
330
+ continue;
331
+ }
166
332
 
167
333
  const plainValue = await fromStorage(fields[filter.fieldId].def, rawValue);
168
334
  /**
@@ -170,46 +336,31 @@ const filterItems = async params => {
170
336
  */
171
337
 
172
338
  if (plainValue !== rawValue) {
173
- items[key] = _dotProp.default.set(item, filter.path, plainValue);
339
+ record = _dotProp.default.set(record, filter.path, plainValue);
174
340
  }
175
341
 
176
- const value = transformValue(plainValue, filter.transformValue);
177
- const matched = filter.filterPlugin.matches({
178
- value,
179
- compareValue: filter.compareValue
342
+ const result = execFilter({
343
+ value: plainValue,
344
+ filter
180
345
  });
181
346
 
182
- if ((filter.negate ? !matched : matched) === false) {
183
- passed = false;
184
- break;
347
+ if (result === false) {
348
+ return null;
185
349
  }
186
350
  }
187
351
 
188
- if (!passed) {
189
- continue;
190
- }
352
+ return record;
353
+ });
354
+ /**
355
+ * We run filtering as promises so it is a bit faster than in for ... of loop.
356
+ */
191
357
 
192
- results.push(item);
193
- }
358
+ const results = await Promise.all(promises);
359
+ /**
360
+ * And filter out the null values which are returned when filter is not satisfied.
361
+ */
194
362
 
195
- return results; // return items.filter(item => {
196
- // for (const filter of filters) {
197
- // const plainValue = await fromStorage(
198
- // fields[filter.fieldId].def,
199
- // dotProp.get(item, filter.path)
200
- // );
201
- //
202
- // const value = transformValue(plainValue, filter.transformValue);
203
- // const matched = filter.filterPlugin.matches({
204
- // value,
205
- // compareValue: filter.compareValue
206
- // });
207
- // if ((filter.negate ? !matched : matched) === false) {
208
- // return false;
209
- // }
210
- // }
211
- // return true;
212
- // });
363
+ return results.filter(Boolean);
213
364
  };
214
365
 
215
366
  exports.filterItems = filterItems;
@@ -233,7 +384,9 @@ const extractSort = (sortBy, fields) => {
233
384
  });
234
385
  }
235
386
 
236
- const valuePath = modelField.valuePath;
387
+ const valuePath = modelField.createPath({
388
+ field: modelField.def
389
+ });
237
390
  return {
238
391
  fieldId,
239
392
  valuePath,
@@ -342,18 +495,21 @@ const buildModelFields = ({
342
495
  });
343
496
  const valuePathPlugins = getMappedPlugins({
344
497
  plugins,
345
- type: "cms-field-filter-path",
498
+ type: _CmsEntryFieldFilterPathPlugin.CmsEntryFieldFilterPathPlugin.type,
346
499
  property: "fieldType"
347
500
  });
348
501
  const fields = Object.values(_systemFields.systemFields).reduce((collection, field) => {
349
502
  const transformValuePlugin = transformValuePlugins[field.type];
350
503
  const valuePathPlugin = valuePathPlugins[field.type];
351
- let valuePath;
504
+
505
+ let createPath = params => {
506
+ return params.field.fieldId;
507
+ };
352
508
 
353
509
  if (valuePathPlugin) {
354
- valuePath = valuePathPlugin.createPath({
355
- field
356
- });
510
+ createPath = params => {
511
+ return valuePathPlugin.createPath(params);
512
+ };
357
513
  }
358
514
 
359
515
  collection[field.fieldId] = {
@@ -368,7 +524,7 @@ const buildModelFields = ({
368
524
  value
369
525
  });
370
526
  },
371
- valuePath: valuePath || field.fieldId,
527
+ createPath,
372
528
  isSystemField: true
373
529
  };
374
530
  return collection;
@@ -376,15 +532,17 @@ const buildModelFields = ({
376
532
  return model.fields.reduce((collection, field) => {
377
533
  const transformValuePlugin = transformValuePlugins[field.type];
378
534
  const valuePathPlugin = valuePathPlugins[field.type];
379
- let valuePath;
535
+
536
+ let createPath = params => {
537
+ return `${VALUES_ATTRIBUTE}.${params.field.fieldId}`;
538
+ };
380
539
 
381
540
  if (valuePathPlugin) {
382
- valuePath = valuePathPlugin.createPath({
383
- field
384
- });
541
+ createPath = params => {
542
+ return valuePathPlugin.createPath(params);
543
+ };
385
544
  }
386
545
 
387
- const targetValuePath = `${VALUES_ATTRIBUTE}.${valuePath || field.fieldId}`;
388
546
  collection[field.fieldId] = {
389
547
  def: field,
390
548
  valueTransformer: value => {
@@ -397,7 +555,7 @@ const buildModelFields = ({
397
555
  value
398
556
  });
399
557
  },
400
- valuePath: targetValuePath || field.fieldId
558
+ createPath
401
559
  };
402
560
  return collection;
403
561
  }, fields);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webiny/api-headless-cms-ddb",
3
- "version": "5.18.3",
3
+ "version": "5.19.0-beta.3",
4
4
  "main": "index.js",
5
5
  "keywords": [
6
6
  "@webiny/api-headless-cms",
@@ -22,11 +22,11 @@
22
22
  "license": "MIT",
23
23
  "dependencies": {
24
24
  "@babel/runtime": "7.16.3",
25
- "@webiny/api-headless-cms": "5.18.3",
26
- "@webiny/db-dynamodb": "5.18.3",
27
- "@webiny/error": "5.18.3",
28
- "@webiny/handler-db": "5.18.3",
29
- "@webiny/utils": "5.18.3",
25
+ "@webiny/api-headless-cms": "5.19.0-beta.3",
26
+ "@webiny/db-dynamodb": "5.19.0-beta.3",
27
+ "@webiny/error": "5.19.0-beta.3",
28
+ "@webiny/handler-db": "5.19.0-beta.3",
29
+ "@webiny/utils": "5.19.0-beta.3",
30
30
  "aws-sdk": "2.1032.0",
31
31
  "dataloader": "2.0.0",
32
32
  "dot-prop": "5.3.0",
@@ -40,9 +40,9 @@
40
40
  "@babel/preset-env": "^7.5.5",
41
41
  "@babel/preset-flow": "^7.0.0",
42
42
  "@types/jsonpack": "^1.1.0",
43
- "@webiny/cli": "^5.18.3",
44
- "@webiny/plugins": "^5.18.3",
45
- "@webiny/project-utils": "^5.18.3",
43
+ "@webiny/cli": "^5.19.0-beta.3",
44
+ "@webiny/plugins": "^5.19.0-beta.3",
45
+ "@webiny/project-utils": "^5.19.0-beta.3",
46
46
  "jest": "^26.6.3",
47
47
  "jest-dynalite": "^3.2.0",
48
48
  "jest-environment-node": "^26.6.2",
@@ -57,5 +57,5 @@
57
57
  "build": "yarn webiny run build",
58
58
  "watch": "yarn webiny run watch"
59
59
  },
60
- "gitHead": "3ccdb07d10c57086a63eb42ba7484e17bc388297"
60
+ "gitHead": "a1cca819f83172183b907de16fd5746d07d13ad0"
61
61
  }
@@ -0,0 +1,22 @@
1
+ import { Plugin } from "@webiny/plugins/Plugin";
2
+ import { CmsModelField } from "@webiny/api-headless-cms/types";
3
+ export interface CreatePathCallableParams {
4
+ field: CmsModelField;
5
+ index?: number;
6
+ }
7
+ export interface CreatePathCallable {
8
+ (params: CreatePathCallableParams): string;
9
+ }
10
+ export interface Params {
11
+ fieldType: string;
12
+ fieldId?: string[];
13
+ path: string | CreatePathCallable;
14
+ }
15
+ export declare class CmsEntryFieldFilterPathPlugin extends Plugin {
16
+ static readonly type: string;
17
+ private readonly config;
18
+ get fieldType(): string;
19
+ constructor(config: Params);
20
+ canUse(field: CmsModelField): boolean;
21
+ createPath(params: CreatePathCallableParams): string;
22
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.CmsEntryFieldFilterPathPlugin = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _Plugin = require("@webiny/plugins/Plugin");
15
+
16
+ class CmsEntryFieldFilterPathPlugin extends _Plugin.Plugin {
17
+ get fieldType() {
18
+ return this.config.fieldType;
19
+ }
20
+
21
+ constructor(config) {
22
+ super();
23
+ (0, _defineProperty2.default)(this, "config", void 0);
24
+ this.config = config;
25
+ this.name = `${this.constructor.type}-${this.config.fieldType}`;
26
+ }
27
+
28
+ canUse(field) {
29
+ if (field.type !== this.config.fieldType) {
30
+ return false;
31
+ }
32
+
33
+ if (Array.isArray(this.config.fieldId) === false || this.config.fieldId.length === 0) {
34
+ return true;
35
+ }
36
+
37
+ return this.config.fieldId.includes(field.fieldId);
38
+ }
39
+
40
+ createPath(params) {
41
+ if (typeof this.config.path === "function") {
42
+ return this.config.path(params);
43
+ } else if (typeof this.config.path === "string") {
44
+ return this.config.path;
45
+ }
46
+
47
+ throw new _error.default(`Missing path in "${this.name}" plugin.`);
48
+ }
49
+
50
+ }
51
+
52
+ exports.CmsEntryFieldFilterPathPlugin = CmsEntryFieldFilterPathPlugin;
53
+ (0, _defineProperty2.default)(CmsEntryFieldFilterPathPlugin, "type", "cms-field-filter-path");
package/types.d.ts CHANGED
@@ -25,36 +25,6 @@ export interface CmsFieldFilterValueTransformPlugin extends Plugin {
25
25
  */
26
26
  transform: (params: CmsFieldFilterValueTransformParams) => any;
27
27
  }
28
- interface CmsFieldFilterPathParams {
29
- /**
30
- * A field for which we are creating the value path.
31
- */
32
- field: CmsModelField;
33
- /**
34
- * If value is an array we will need index position.
35
- * It is up to the developer to add.
36
- */
37
- index?: number | string;
38
- }
39
- export interface CmsFieldFilterPathPlugin extends Plugin {
40
- /**
41
- * A plugin type.
42
- */
43
- type: "cms-field-filter-path";
44
- /**
45
- * A field type this plugin is for.
46
- */
47
- fieldType: string;
48
- /**
49
- * A field id this plugin is for.
50
- * It is meant for targeting only specific fields in a certain type.
51
- */
52
- fieldId?: string[];
53
- /**
54
- * Get a path for given field.
55
- */
56
- createPath: (params: CmsFieldFilterPathParams) => string;
57
- }
58
28
  export declare type AttributeDefinition = DynamoDBTypes | EntityAttributeConfig | EntityCompositeAttributes;
59
29
  export declare type Attributes = Record<string, AttributeDefinition>;
60
30
  export declare enum ENTITIES {
@@ -1,3 +0,0 @@
1
- import { CmsFieldFilterPathPlugin } from "../../types";
2
- declare const _default: () => CmsFieldFilterPathPlugin;
3
- export default _default;
@@ -1,27 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var _default = () => ({
9
- type: "cms-field-filter-path",
10
- name: "cms-field-filter-path-ref",
11
- fieldType: "ref",
12
- createPath: ({
13
- field,
14
- index
15
- }) => {
16
- const paths = [field.fieldId];
17
-
18
- if (index !== undefined) {
19
- paths.push(String(index));
20
- }
21
-
22
- paths.push("entryId");
23
- return paths.join(".");
24
- }
25
- });
26
-
27
- exports.default = _default;