@webiny/api-headless-cms-ddb 0.0.0-mt-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 (51) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/definitions/entry.d.ts +8 -0
  4. package/definitions/entry.js +97 -0
  5. package/definitions/group.d.ts +8 -0
  6. package/definitions/group.js +74 -0
  7. package/definitions/model.d.ts +8 -0
  8. package/definitions/model.js +96 -0
  9. package/definitions/settings.d.ts +8 -0
  10. package/definitions/settings.js +62 -0
  11. package/definitions/system.d.ts +8 -0
  12. package/definitions/system.js +50 -0
  13. package/definitions/table.d.ts +8 -0
  14. package/definitions/table.js +30 -0
  15. package/dynamoDb/index.d.ts +2 -0
  16. package/dynamoDb/index.js +24 -0
  17. package/dynamoDb/path/plainObject.d.ts +3 -0
  18. package/dynamoDb/path/plainObject.js +33 -0
  19. package/dynamoDb/path/ref.d.ts +3 -0
  20. package/dynamoDb/path/ref.js +27 -0
  21. package/dynamoDb/storage/date.d.ts +3 -0
  22. package/dynamoDb/storage/date.js +65 -0
  23. package/dynamoDb/storage/longText.d.ts +7 -0
  24. package/dynamoDb/storage/longText.js +83 -0
  25. package/dynamoDb/storage/richText.d.ts +8 -0
  26. package/dynamoDb/storage/richText.js +110 -0
  27. package/dynamoDb/transformValue/datetime.d.ts +3 -0
  28. package/dynamoDb/transformValue/datetime.js +47 -0
  29. package/index.d.ts +2 -0
  30. package/index.js +125 -0
  31. package/operations/entry/dataLoaders.d.ts +38 -0
  32. package/operations/entry/dataLoaders.js +303 -0
  33. package/operations/entry/index.d.ts +8 -0
  34. package/operations/entry/index.js +823 -0
  35. package/operations/entry/keys.d.ts +25 -0
  36. package/operations/entry/keys.js +62 -0
  37. package/operations/entry/systemFields.d.ts +2 -0
  38. package/operations/entry/systemFields.js +50 -0
  39. package/operations/entry/utils.d.ts +31 -0
  40. package/operations/entry/utils.js +406 -0
  41. package/operations/group/index.d.ts +8 -0
  42. package/operations/group/index.js +198 -0
  43. package/operations/model/index.d.ts +6 -0
  44. package/operations/model/index.js +161 -0
  45. package/operations/settings/index.d.ts +6 -0
  46. package/operations/settings/index.js +141 -0
  47. package/operations/system/index.d.ts +6 -0
  48. package/operations/system/index.js +105 -0
  49. package/package.json +61 -0
  50. package/types.d.ts +84 -0
  51. package/types.js +16 -0
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _error = _interopRequireDefault(require("@webiny/error"));
11
+
12
+ var _StorageTransformPlugin = require("@webiny/api-headless-cms/content/plugins/storage/StorageTransformPlugin");
13
+
14
+ const excludeTypes = ["time", "dateTimeWithTimezone"];
15
+ const plugin = new _StorageTransformPlugin.StorageTransformPlugin({
16
+ fieldType: "datetime",
17
+ fromStorage: async ({
18
+ value,
19
+ field
20
+ }) => {
21
+ const {
22
+ type
23
+ } = field.settings || {};
24
+
25
+ if (!value || !type || excludeTypes.includes(type)) {
26
+ return value;
27
+ }
28
+
29
+ try {
30
+ return new Date(value);
31
+ } catch {
32
+ console.log(`Could not transform from storage for field type`);
33
+ return value;
34
+ }
35
+ },
36
+ toStorage: async ({
37
+ value,
38
+ field
39
+ }) => {
40
+ const {
41
+ type
42
+ } = field.settings || {};
43
+
44
+ if (!value || !type || excludeTypes.includes(type)) {
45
+ return value;
46
+ }
47
+
48
+ if (value.toISOString) {
49
+ return value.toISOString();
50
+ } else if (typeof value === "string") {
51
+ return value;
52
+ }
53
+
54
+ throw new _error.default("Error converting value to a storage type.", "TO_STORAGE_ERROR", {
55
+ value,
56
+ fieldId: field.fieldId
57
+ });
58
+ }
59
+ });
60
+
61
+ var _default = () => {
62
+ return plugin;
63
+ };
64
+
65
+ exports.default = _default;
@@ -0,0 +1,7 @@
1
+ import { StorageTransformPlugin } from "@webiny/api-headless-cms/content/plugins/storage/StorageTransformPlugin";
2
+ export interface StorageValue {
3
+ compression: string;
4
+ value: any;
5
+ }
6
+ declare const _default: () => StorageTransformPlugin<string, StorageValue>;
7
+ export default _default;
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _error = _interopRequireDefault(require("@webiny/error"));
11
+
12
+ var _gzip = require("@webiny/utils/compression/gzip");
13
+
14
+ var _StorageTransformPlugin = require("@webiny/api-headless-cms/content/plugins/storage/StorageTransformPlugin");
15
+
16
+ const GZIP = "gzip";
17
+ const TO_STORAGE_ENCODING = "base64";
18
+ const FROM_STORAGE_ENCODING = "utf8";
19
+
20
+ const convertToBuffer = value => {
21
+ if (typeof value === "string") {
22
+ return Buffer.from(value, TO_STORAGE_ENCODING);
23
+ }
24
+
25
+ return value;
26
+ };
27
+
28
+ const plugin = new _StorageTransformPlugin.StorageTransformPlugin({
29
+ fieldType: "long-text",
30
+ fromStorage: async ({
31
+ field,
32
+ value: storageValue
33
+ }) => {
34
+ const typeOf = typeof storageValue;
35
+
36
+ if (!storageValue || typeOf === "string" || typeOf === "number") {
37
+ return storageValue;
38
+ } else if (typeof storageValue !== "object") {
39
+ throw new _error.default(`LongText value received in "fromStorage" function is not an object in field "${field.fieldId}".`);
40
+ }
41
+
42
+ const {
43
+ compression,
44
+ value
45
+ } = storageValue;
46
+ /**
47
+ * Check if possibly undefined, null, empty...
48
+ */
49
+
50
+ if (!compression) {
51
+ throw new _error.default(`Missing compression in "fromStorage" function in field "${field.fieldId}": ${JSON.stringify(storageValue)}.`, "MISSING_COMPRESSION", {
52
+ value: storageValue
53
+ });
54
+ } else if (compression !== GZIP) {
55
+ throw new _error.default(`This plugin cannot transform something not compressed with "GZIP".`, "WRONG_COMPRESSION", {
56
+ compression
57
+ });
58
+ }
59
+
60
+ try {
61
+ const buf = await (0, _gzip.decompress)(convertToBuffer(value));
62
+ return buf.toString(FROM_STORAGE_ENCODING);
63
+ } catch (ex) {
64
+ console.log(ex.message);
65
+ return null;
66
+ }
67
+ },
68
+ toStorage: async ({
69
+ value
70
+ }) => {
71
+ const compressedValue = await (0, _gzip.compress)(value);
72
+ return {
73
+ compression: GZIP,
74
+ value: compressedValue.toString(TO_STORAGE_ENCODING)
75
+ };
76
+ }
77
+ });
78
+
79
+ var _default = () => {
80
+ return plugin;
81
+ };
82
+
83
+ exports.default = _default;
@@ -0,0 +1,8 @@
1
+ import { StorageTransformPlugin } from "@webiny/api-headless-cms/content/plugins/storage/StorageTransformPlugin";
2
+ export declare type OriginalValue = Record<string, any> | any[];
3
+ export interface StorageValue {
4
+ compression: string;
5
+ value: any;
6
+ }
7
+ declare const _default: () => StorageTransformPlugin<any, any>;
8
+ export default _default;
@@ -0,0 +1,110 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.default = void 0;
9
+
10
+ var _jsonpack = _interopRequireDefault(require("jsonpack"));
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _StorageTransformPlugin = require("@webiny/api-headless-cms/content/plugins/storage/StorageTransformPlugin");
15
+
16
+ /**
17
+ * Remove when jsonpack gets PR with a fix merged
18
+ * https://github.com/rgcl/jsonpack/pull/25/files
19
+ * NOTE 2021-07-28: it seems PR is not going to be merged so keep this.
20
+ */
21
+ const transformArray = value => {
22
+ if (!value) {
23
+ return value;
24
+ }
25
+
26
+ let isArray = Array.isArray(value);
27
+ const shouldBeArray = value instanceof Array === false && isArray;
28
+
29
+ if (shouldBeArray) {
30
+ value = Array.from(value);
31
+ isArray = true;
32
+ }
33
+
34
+ if (typeof value === "object" || isArray) {
35
+ for (const k in value) {
36
+ value[k] = transformArray(value[k]);
37
+ }
38
+ }
39
+
40
+ return value;
41
+ };
42
+
43
+ const plugin = new _StorageTransformPlugin.StorageTransformPlugin({
44
+ fieldType: "rich-text",
45
+ fromStorage: async ({
46
+ field,
47
+ value: storageValue
48
+ }) => {
49
+ if (!storageValue) {
50
+ return storageValue;
51
+ } else if (typeof storageValue !== "object") {
52
+ throw new _error.default(`RichText value received in "fromStorage" function is not an object in field "${field.fieldId}".`);
53
+ }
54
+ /**
55
+ * This is to circumvent a bug introduced with 5.8.0 storage operations.
56
+ * Do not remove.
57
+ */
58
+
59
+
60
+ if (storageValue.hasOwnProperty("compression") === false) {
61
+ return storageValue;
62
+ }
63
+
64
+ const {
65
+ compression,
66
+ value
67
+ } = storageValue;
68
+
69
+ if (!compression) {
70
+ throw new _error.default(`Missing compression in "fromStorage" function in field "${field.fieldId}": ${JSON.stringify(storageValue)}.`, "MISSING_COMPRESSION", {
71
+ value: storageValue
72
+ });
73
+ }
74
+
75
+ if (compression !== "jsonpack") {
76
+ throw new _error.default(`This plugin cannot transform something not packed with "jsonpack".`, "WRONG_COMPRESSION", {
77
+ compression
78
+ });
79
+ }
80
+
81
+ try {
82
+ return _jsonpack.default.unpack(value);
83
+ } catch {
84
+ return null;
85
+ }
86
+ },
87
+ toStorage: async ({
88
+ value
89
+ }) => {
90
+ /**
91
+ * There is a possibility that we are trying to compress already compressed value.
92
+ * Introduced a bug with 5.8.0 storage operations, so just return the value to correct it.
93
+ */
94
+ if (value && value.hasOwnProperty("compression") === true) {
95
+ return value;
96
+ }
97
+
98
+ value = transformArray(value);
99
+ return {
100
+ compression: "jsonpack",
101
+ value: value ? _jsonpack.default.pack(value) : value
102
+ };
103
+ }
104
+ });
105
+
106
+ var _default = () => {
107
+ return plugin;
108
+ };
109
+
110
+ exports.default = _default;
@@ -0,0 +1,3 @@
1
+ import { CmsFieldFilterValueTransformPlugin } from "../../types";
2
+ declare const _default: () => CmsFieldFilterValueTransformPlugin;
3
+ export default _default;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _TimeTransformPlugin = require("@webiny/db-dynamodb/plugins/definitions/TimeTransformPlugin");
9
+
10
+ var _DateTimeTransformPlugin = require("@webiny/db-dynamodb/plugins/definitions/DateTimeTransformPlugin");
11
+
12
+ const timeTransformer = new _TimeTransformPlugin.TimeTransformPlugin({
13
+ fields: ["*"]
14
+ });
15
+ const dateTimeTransformer = new _DateTimeTransformPlugin.DateTimeTransformPlugin({
16
+ fields: ["*"]
17
+ });
18
+
19
+ var _default = () => ({
20
+ type: "cms-field-filter-value-transform",
21
+ name: "cms-field-value-filter-transform-datetime",
22
+ fieldType: "datetime",
23
+
24
+ /**
25
+ * Always transform into the milliseconds.
26
+ */
27
+ transform: ({
28
+ field,
29
+ value
30
+ }) => {
31
+ const {
32
+ type
33
+ } = field.settings || {};
34
+
35
+ if (type === "time") {
36
+ return timeTransformer.transform({
37
+ value
38
+ });
39
+ }
40
+
41
+ return dateTimeTransformer.transform({
42
+ value
43
+ });
44
+ }
45
+ });
46
+
47
+ exports.default = _default;
package/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import { StorageOperationsFactory } from "./types";
2
+ export declare const createStorageOperations: StorageOperationsFactory;
package/index.js ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createStorageOperations = void 0;
9
+
10
+ var _filters = _interopRequireDefault(require("@webiny/db-dynamodb/plugins/filters"));
11
+
12
+ var _dynamoDb = _interopRequireDefault(require("./dynamoDb"));
13
+
14
+ var _types = require("./types");
15
+
16
+ var _table = require("./definitions/table");
17
+
18
+ var _settings = require("./definitions/settings");
19
+
20
+ var _system = require("./definitions/system");
21
+
22
+ var _group = require("./definitions/group");
23
+
24
+ var _model = require("./definitions/model");
25
+
26
+ var _entry = require("./definitions/entry");
27
+
28
+ var _plugins = require("@webiny/plugins");
29
+
30
+ var _system2 = require("./operations/system");
31
+
32
+ var _settings2 = require("./operations/settings");
33
+
34
+ var _group2 = require("./operations/group");
35
+
36
+ var _model2 = require("./operations/model");
37
+
38
+ var _entry2 = require("./operations/entry");
39
+
40
+ const createStorageOperations = params => {
41
+ const {
42
+ attributes = {},
43
+ table,
44
+ documentClient,
45
+ plugins: customPlugins,
46
+ modelFieldToGraphQLPlugins
47
+ } = params;
48
+ const tableInstance = (0, _table.createTable)({
49
+ table,
50
+ documentClient
51
+ });
52
+ const entities = {
53
+ settings: (0, _settings.createSettingsEntity)({
54
+ entityName: _types.ENTITIES.SETTINGS,
55
+ table: tableInstance,
56
+ attributes: attributes[_types.ENTITIES.SETTINGS]
57
+ }),
58
+ system: (0, _system.createSystemEntity)({
59
+ entityName: _types.ENTITIES.SYSTEM,
60
+ table: tableInstance,
61
+ attributes: attributes[_types.ENTITIES.SYSTEM]
62
+ }),
63
+ groups: (0, _group.createGroupEntity)({
64
+ entityName: _types.ENTITIES.GROUPS,
65
+ table: tableInstance,
66
+ attributes: attributes[_types.ENTITIES.GROUPS]
67
+ }),
68
+ models: (0, _model.createModelEntity)({
69
+ entityName: _types.ENTITIES.MODELS,
70
+ table: tableInstance,
71
+ attributes: attributes[_types.ENTITIES.MODELS]
72
+ }),
73
+ entries: (0, _entry.createEntryEntity)({
74
+ entityName: _types.ENTITIES.ENTRIES,
75
+ table: tableInstance,
76
+ attributes: attributes[_types.ENTITIES.ENTRIES]
77
+ })
78
+ };
79
+ const plugins = new _plugins.PluginsContainer([
80
+ /**
81
+ * User defined custom plugins.
82
+ */
83
+ ...(customPlugins || []),
84
+ /**
85
+ * Plugins of type CmsModelFieldToGraphQLPlugin.
86
+ */
87
+ modelFieldToGraphQLPlugins,
88
+ /**
89
+ * DynamoDB filter plugins for the where conditions.
90
+ */
91
+ (0, _filters.default)(),
92
+ /**
93
+ * Field plugins for DynamoDB.
94
+ */
95
+ (0, _dynamoDb.default)()]);
96
+ return {
97
+ plugins: [
98
+ /**
99
+ * Field plugins for DynamoDB.
100
+ * We must pass them to the base application.
101
+ */
102
+ (0, _dynamoDb.default)()],
103
+ getEntities: () => entities,
104
+ getTable: () => tableInstance,
105
+ system: (0, _system2.createSystemStorageOperations)({
106
+ entity: entities.system
107
+ }),
108
+ settings: (0, _settings2.createSettingsStorageOperations)({
109
+ entity: entities.settings
110
+ }),
111
+ groups: (0, _group2.createGroupsStorageOperations)({
112
+ entity: entities.groups,
113
+ plugins
114
+ }),
115
+ models: (0, _model2.createModelsStorageOperations)({
116
+ entity: entities.models
117
+ }),
118
+ entries: (0, _entry2.createEntriesStorageOperations)({
119
+ entity: entities.entries,
120
+ plugins
121
+ })
122
+ };
123
+ };
124
+
125
+ exports.createStorageOperations = createStorageOperations;
@@ -0,0 +1,38 @@
1
+ import { CmsEntry, CmsModel } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ export interface GetAllEntryRevisionsParams {
4
+ ids: readonly string[];
5
+ model: CmsModel;
6
+ }
7
+ export interface GetRevisionByIdParams {
8
+ ids: readonly string[];
9
+ model: CmsModel;
10
+ }
11
+ export interface GetPublishedRevisionByEntryIdParams {
12
+ ids: readonly string[];
13
+ model: CmsModel;
14
+ }
15
+ export interface GetLatestRevisionByEntryIdParams {
16
+ ids: readonly string[];
17
+ model: CmsModel;
18
+ }
19
+ interface ClearLoaderParams {
20
+ model: CmsModel;
21
+ entry?: CmsEntry;
22
+ }
23
+ export interface Params {
24
+ entity: Entity<any>;
25
+ }
26
+ export declare class DataLoadersHandler {
27
+ private readonly loaders;
28
+ private readonly entity;
29
+ constructor(params: any);
30
+ getAllEntryRevisions(params: GetAllEntryRevisionsParams): Promise<CmsEntry[]>;
31
+ getRevisionById(params: GetRevisionByIdParams): Promise<CmsEntry[]>;
32
+ getPublishedRevisionByEntryId(params: GetPublishedRevisionByEntryIdParams): Promise<CmsEntry[]>;
33
+ getLatestRevisionByEntryId(params: GetLatestRevisionByEntryIdParams): Promise<CmsEntry[]>;
34
+ private getLoader;
35
+ private loadMany;
36
+ clearAll(params: Omit<ClearLoaderParams, "entry">): void;
37
+ }
38
+ export {};