@webiny/api-headless-cms-ddb-es 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 (115) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +35 -0
  3. package/configurations.d.ts +12 -0
  4. package/configurations.js +32 -0
  5. package/definitions/entry.d.ts +8 -0
  6. package/definitions/entry.js +91 -0
  7. package/definitions/entryElasticsearch.d.ts +8 -0
  8. package/definitions/entryElasticsearch.js +46 -0
  9. package/definitions/group.d.ts +8 -0
  10. package/definitions/group.js +74 -0
  11. package/definitions/model.d.ts +8 -0
  12. package/definitions/model.js +96 -0
  13. package/definitions/settings.d.ts +8 -0
  14. package/definitions/settings.js +62 -0
  15. package/definitions/system.d.ts +8 -0
  16. package/definitions/system.js +50 -0
  17. package/definitions/table.d.ts +8 -0
  18. package/definitions/table.js +24 -0
  19. package/definitions/tableElasticsearch.d.ts +8 -0
  20. package/definitions/tableElasticsearch.js +24 -0
  21. package/dynamoDb/index.d.ts +2 -0
  22. package/dynamoDb/index.js +18 -0
  23. package/dynamoDb/storage/date.d.ts +3 -0
  24. package/dynamoDb/storage/date.js +65 -0
  25. package/dynamoDb/storage/longText.d.ts +7 -0
  26. package/dynamoDb/storage/longText.js +83 -0
  27. package/dynamoDb/storage/richText.d.ts +8 -0
  28. package/dynamoDb/storage/richText.js +110 -0
  29. package/elasticsearch/index.d.ts +2 -0
  30. package/elasticsearch/index.js +16 -0
  31. package/elasticsearch/indexing/dateTimeIndexing.d.ts +3 -0
  32. package/elasticsearch/indexing/dateTimeIndexing.js +89 -0
  33. package/elasticsearch/indexing/defaultFieldIndexing.d.ts +3 -0
  34. package/elasticsearch/indexing/defaultFieldIndexing.js +47 -0
  35. package/elasticsearch/indexing/index.d.ts +2 -0
  36. package/elasticsearch/indexing/index.js +24 -0
  37. package/elasticsearch/indexing/longTextIndexing.d.ts +3 -0
  38. package/elasticsearch/indexing/longTextIndexing.js +36 -0
  39. package/elasticsearch/indexing/numberIndexing.d.ts +3 -0
  40. package/elasticsearch/indexing/numberIndexing.js +48 -0
  41. package/elasticsearch/indexing/objectIndexing.d.ts +3 -0
  42. package/elasticsearch/indexing/objectIndexing.js +200 -0
  43. package/elasticsearch/indexing/richTextIndexing.d.ts +3 -0
  44. package/elasticsearch/indexing/richTextIndexing.js +34 -0
  45. package/elasticsearch/search/index.d.ts +3 -0
  46. package/elasticsearch/search/index.js +16 -0
  47. package/elasticsearch/search/refSearch.d.ts +3 -0
  48. package/elasticsearch/search/refSearch.js +24 -0
  49. package/elasticsearch/search/timeSearch.d.ts +3 -0
  50. package/elasticsearch/search/timeSearch.js +25 -0
  51. package/helpers/createElasticsearchQueryBody.d.ts +11 -0
  52. package/helpers/createElasticsearchQueryBody.js +375 -0
  53. package/helpers/entryIndexHelpers.d.ts +18 -0
  54. package/helpers/entryIndexHelpers.js +189 -0
  55. package/helpers/fields.d.ts +77 -0
  56. package/helpers/fields.js +174 -0
  57. package/helpers/index.d.ts +2 -0
  58. package/helpers/index.js +31 -0
  59. package/helpers/operatorPluginsList.d.ts +7 -0
  60. package/helpers/operatorPluginsList.js +30 -0
  61. package/helpers/searchPluginsList.d.ts +6 -0
  62. package/helpers/searchPluginsList.js +26 -0
  63. package/helpers/transformValueForSearch.d.ts +9 -0
  64. package/helpers/transformValueForSearch.js +26 -0
  65. package/index.d.ts +2 -0
  66. package/index.js +171 -0
  67. package/operations/entry/dataLoaders.d.ts +47 -0
  68. package/operations/entry/dataLoaders.js +347 -0
  69. package/operations/entry/elasticsearchFields.d.ts +2 -0
  70. package/operations/entry/elasticsearchFields.js +32 -0
  71. package/operations/entry/fields.d.ts +3 -0
  72. package/operations/entry/fields.js +60 -0
  73. package/operations/entry/index.d.ts +13 -0
  74. package/operations/entry/index.js +1152 -0
  75. package/operations/entry/keys.d.ts +12 -0
  76. package/operations/entry/keys.js +40 -0
  77. package/operations/group/index.d.ts +8 -0
  78. package/operations/group/index.js +202 -0
  79. package/operations/model/index.d.ts +8 -0
  80. package/operations/model/index.js +205 -0
  81. package/operations/settings/index.d.ts +6 -0
  82. package/operations/settings/index.js +141 -0
  83. package/operations/system/createElasticsearchTemplate.d.ts +5 -0
  84. package/operations/system/createElasticsearchTemplate.js +62 -0
  85. package/operations/system/index.d.ts +6 -0
  86. package/operations/system/index.js +105 -0
  87. package/package.json +73 -0
  88. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.d.ts +17 -0
  89. package/plugins/CmsEntryElasticsearchBodyModifierPlugin.js +24 -0
  90. package/plugins/CmsEntryElasticsearchFieldPlugin.d.ts +12 -0
  91. package/plugins/CmsEntryElasticsearchFieldPlugin.js +24 -0
  92. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.d.ts +17 -0
  93. package/plugins/CmsEntryElasticsearchQueryModifierPlugin.js +24 -0
  94. package/plugins/CmsEntryElasticsearchSortModifierPlugin.d.ts +17 -0
  95. package/plugins/CmsEntryElasticsearchSortModifierPlugin.js +24 -0
  96. package/types.d.ts +191 -0
  97. package/types.js +60 -0
  98. package/upgrades/index.d.ts +2 -0
  99. package/upgrades/index.js +16 -0
  100. package/upgrades/utils.d.ts +1 -0
  101. package/upgrades/utils.js +16 -0
  102. package/upgrades/v5.0.0/cleanDatabaseRecord.d.ts +6 -0
  103. package/upgrades/v5.0.0/cleanDatabaseRecord.js +16 -0
  104. package/upgrades/v5.0.0/createOldVersionIndiceName.d.ts +2 -0
  105. package/upgrades/v5.0.0/createOldVersionIndiceName.js +12 -0
  106. package/upgrades/v5.0.0/entryValueFixer.d.ts +4 -0
  107. package/upgrades/v5.0.0/entryValueFixer.js +124 -0
  108. package/upgrades/v5.0.0/fieldFinder.d.ts +6 -0
  109. package/upgrades/v5.0.0/fieldFinder.js +42 -0
  110. package/upgrades/v5.0.0/helpers.d.ts +4 -0
  111. package/upgrades/v5.0.0/helpers.js +57 -0
  112. package/upgrades/v5.0.0/index.d.ts +4 -0
  113. package/upgrades/v5.0.0/index.js +232 -0
  114. package/upgrades/v5.8.0/index.d.ts +4 -0
  115. package/upgrades/v5.8.0/index.js +426 -0
@@ -0,0 +1,12 @@
1
+ export interface PartitionKeyParams {
2
+ id: string;
3
+ tenant: string;
4
+ locale: string;
5
+ }
6
+ export declare const createPartitionKey: (params: PartitionKeyParams) => string;
7
+ export interface SortKeyParams {
8
+ version: number;
9
+ }
10
+ export declare const createRevisionSortKey: (params: SortKeyParams) => string;
11
+ export declare const createLatestSortKey: () => string;
12
+ export declare const createPublishedSortKey: () => string;
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createRevisionSortKey = exports.createPublishedSortKey = exports.createPartitionKey = exports.createLatestSortKey = void 0;
7
+
8
+ var _utils = require("@webiny/utils");
9
+
10
+ const createPartitionKey = params => {
11
+ const {
12
+ tenant,
13
+ locale,
14
+ id: initialId
15
+ } = params;
16
+ const {
17
+ id
18
+ } = (0, _utils.parseIdentifier)(initialId);
19
+ return `T#${tenant}#L#${locale}#CMS#CME#${id}`;
20
+ };
21
+
22
+ exports.createPartitionKey = createPartitionKey;
23
+
24
+ const createRevisionSortKey = params => {
25
+ return `REV#${(0, _utils.zeroPad)(params.version)}`;
26
+ };
27
+
28
+ exports.createRevisionSortKey = createRevisionSortKey;
29
+
30
+ const createLatestSortKey = () => {
31
+ return "L";
32
+ };
33
+
34
+ exports.createLatestSortKey = createLatestSortKey;
35
+
36
+ const createPublishedSortKey = () => {
37
+ return "P";
38
+ };
39
+
40
+ exports.createPublishedSortKey = createPublishedSortKey;
@@ -0,0 +1,8 @@
1
+ import { CmsGroupStorageOperations } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ import { PluginsContainer } from "@webiny/plugins";
4
+ export interface Params {
5
+ entity: Entity<any>;
6
+ plugins: PluginsContainer;
7
+ }
8
+ export declare const createGroupsStorageOperations: (params: Params) => CmsGroupStorageOperations;
@@ -0,0 +1,202 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createGroupsStorageOperations = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _get = require("@webiny/db-dynamodb/utils/get");
15
+
16
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
17
+
18
+ var _query = require("@webiny/db-dynamodb/utils/query");
19
+
20
+ var _filter = require("@webiny/db-dynamodb/utils/filter");
21
+
22
+ var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin");
23
+
24
+ var _sort = require("@webiny/db-dynamodb/utils/sort");
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ const createPartitionKey = params => {
31
+ const {
32
+ tenant,
33
+ locale
34
+ } = params;
35
+ return `T#${tenant}#L#${locale}#CMS#CMG`;
36
+ };
37
+
38
+ const createSortKeys = params => {
39
+ const {
40
+ id
41
+ } = params;
42
+ return id;
43
+ };
44
+
45
+ const createKeys = params => {
46
+ return {
47
+ PK: createPartitionKey(params),
48
+ SK: createSortKeys(params)
49
+ };
50
+ };
51
+
52
+ const createType = () => {
53
+ return "cms.group";
54
+ };
55
+
56
+ const createGroupsStorageOperations = params => {
57
+ const {
58
+ entity,
59
+ plugins
60
+ } = params;
61
+ const filteringPlugins = plugins.byType(_ValueFilterPlugin.ValueFilterPlugin.type);
62
+
63
+ if (filteringPlugins.length === 0) {
64
+ throw new _error.default("DynamoDB filtering plugins not loaded.", "MISSING_DYNAMODB_FILTERING_PLUGINS");
65
+ }
66
+
67
+ const create = async params => {
68
+ const {
69
+ group
70
+ } = params;
71
+ const keys = createKeys(group);
72
+
73
+ try {
74
+ await entity.put(_objectSpread(_objectSpread({}, group), {}, {
75
+ TYPE: createType()
76
+ }, keys));
77
+ return group;
78
+ } catch (ex) {
79
+ throw new _error.default(ex.message || "Could not create group.", ex.code || "CREATE_GROUP_ERROR", {
80
+ error: ex,
81
+ group,
82
+ keys
83
+ });
84
+ }
85
+ };
86
+
87
+ const update = async params => {
88
+ const {
89
+ group,
90
+ original
91
+ } = params;
92
+ const keys = createKeys(group);
93
+
94
+ try {
95
+ await entity.put(_objectSpread(_objectSpread({}, group), {}, {
96
+ TYPE: createType()
97
+ }, keys));
98
+ return group;
99
+ } catch (ex) {
100
+ throw new _error.default(ex.message || "Could not update group.", ex.code || "UPDATE_GROUP_ERROR", {
101
+ error: ex,
102
+ group,
103
+ original,
104
+ keys
105
+ });
106
+ }
107
+ };
108
+
109
+ const deleteGroup = async params => {
110
+ const {
111
+ group
112
+ } = params; // TODO make sure that group has locale and tenant on it - add it in the crud just in case
113
+
114
+ const keys = createKeys(group);
115
+
116
+ try {
117
+ await entity.delete(keys);
118
+ return group;
119
+ } catch (ex) {
120
+ throw new _error.default(ex.message || "Could not delete group.", ex.code || "DELETE_GROUP_ERROR", {
121
+ error: ex,
122
+ group,
123
+ keys
124
+ });
125
+ }
126
+ };
127
+
128
+ const get = async params => {
129
+ const keys = createKeys(params);
130
+
131
+ try {
132
+ const group = await (0, _get.get)({
133
+ entity,
134
+ keys
135
+ });
136
+ return (0, _cleanup.cleanupItem)(entity, group);
137
+ } catch (ex) {
138
+ throw new _error.default(ex.message || "Could not get group.", ex.code || "GET_GROUP_ERROR", _objectSpread(_objectSpread({
139
+ error: ex
140
+ }, params), {}, {
141
+ keys
142
+ }));
143
+ }
144
+ };
145
+
146
+ const list = async params => {
147
+ const {
148
+ sort,
149
+ where: initialWhere
150
+ } = params;
151
+ const queryAllParams = {
152
+ entity,
153
+ partitionKey: createPartitionKey(initialWhere),
154
+ options: {
155
+ gte: " "
156
+ }
157
+ };
158
+ let records = [];
159
+
160
+ try {
161
+ records = await (0, _query.queryAll)(queryAllParams);
162
+ } catch (ex) {
163
+ throw new _error.default(ex.message || "Could not list groups.", ex.code || "LIST_GROUP_ERROR", _objectSpread(_objectSpread({
164
+ error: ex
165
+ }, params), {}, {
166
+ sort,
167
+ where: initialWhere
168
+ }));
169
+ }
170
+
171
+ const where = _objectSpread({}, initialWhere);
172
+
173
+ delete where["tenant"];
174
+ delete where["locale"];
175
+ const filteredItems = (0, _filter.filterItems)({
176
+ items: records,
177
+ where,
178
+ fields: [],
179
+ plugins
180
+ });
181
+
182
+ if (!sort || sort.length === 0 || filteredItems.length === 0) {
183
+ return filteredItems;
184
+ }
185
+
186
+ return (0, _sort.sortItems)({
187
+ items: filteredItems,
188
+ sort,
189
+ fields: []
190
+ });
191
+ };
192
+
193
+ return {
194
+ create,
195
+ update,
196
+ delete: deleteGroup,
197
+ get,
198
+ list
199
+ };
200
+ };
201
+
202
+ exports.createGroupsStorageOperations = createGroupsStorageOperations;
@@ -0,0 +1,8 @@
1
+ import { CmsModelStorageOperations } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ import { Client } from "@elastic/elasticsearch";
4
+ export interface Params {
5
+ entity: Entity<any>;
6
+ elasticsearch: Client;
7
+ }
8
+ export declare const createModelsStorageOperations: (params: Params) => CmsModelStorageOperations;
@@ -0,0 +1,205 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createModelsStorageOperations = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _configurations = _interopRequireDefault(require("../../configurations"));
13
+
14
+ var _error = _interopRequireDefault(require("@webiny/error"));
15
+
16
+ var _get = require("@webiny/db-dynamodb/utils/get");
17
+
18
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
19
+
20
+ var _query = require("@webiny/db-dynamodb/utils/query");
21
+
22
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
23
+
24
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
25
+
26
+ const createPartitionKey = params => {
27
+ const {
28
+ tenant,
29
+ locale
30
+ } = params;
31
+ return `T#${tenant}#L#${locale}#CMS#CM`;
32
+ };
33
+
34
+ const createSortKey = params => {
35
+ return params.modelId;
36
+ };
37
+
38
+ const createKeys = params => {
39
+ return {
40
+ PK: createPartitionKey(params),
41
+ SK: createSortKey(params)
42
+ };
43
+ };
44
+
45
+ const createType = () => {
46
+ return "cms.model";
47
+ };
48
+
49
+ const createModelsStorageOperations = params => {
50
+ const {
51
+ entity,
52
+ elasticsearch
53
+ } = params;
54
+
55
+ const create = async params => {
56
+ const {
57
+ model
58
+ } = params;
59
+
60
+ const {
61
+ index
62
+ } = _configurations.default.es({
63
+ model
64
+ });
65
+
66
+ try {
67
+ const {
68
+ body: exists
69
+ } = await elasticsearch.indices.exists({
70
+ index
71
+ });
72
+
73
+ if (!exists) {
74
+ await elasticsearch.indices.create({
75
+ index
76
+ });
77
+ }
78
+ } catch (ex) {
79
+ throw new _error.default("Could not create Elasticsearch indice.", "ELASTICSEARCH_INDICE_CREATE_ERROR", {
80
+ error: ex,
81
+ index,
82
+ model
83
+ });
84
+ }
85
+
86
+ const keys = createKeys(model);
87
+ let error;
88
+
89
+ try {
90
+ await entity.put(_objectSpread(_objectSpread(_objectSpread({}, model), keys), {}, {
91
+ TYPE: createType()
92
+ }));
93
+ return model;
94
+ } catch (ex) {
95
+ error = ex;
96
+ }
97
+ /**
98
+ * In case of DynamoDB error we need to remove the index we created.
99
+ */
100
+
101
+
102
+ try {
103
+ await elasticsearch.indices.delete({
104
+ index
105
+ });
106
+ } catch (ex) {
107
+ throw new _error.default(`Could not delete elasticsearch index "${index}" after model record failed to be created.`, "DELETE_MODEL_INDICE_ERROR", {
108
+ dynamodbError: error,
109
+ elasticsearchError: ex
110
+ });
111
+ }
112
+
113
+ throw error;
114
+ };
115
+
116
+ const update = async params => {
117
+ const {
118
+ original,
119
+ model
120
+ } = params;
121
+ const keys = createKeys(model);
122
+
123
+ try {
124
+ await entity.put(_objectSpread(_objectSpread(_objectSpread({}, model), keys), {}, {
125
+ TYPE: createType()
126
+ }));
127
+ return model;
128
+ } catch (ex) {
129
+ throw new _error.default(ex.messatge || "Could not update model.", ex.code || "MODEL_UPDATE_ERROR", {
130
+ error: ex,
131
+ model,
132
+ original,
133
+ keys
134
+ });
135
+ }
136
+ };
137
+
138
+ const deleteModel = async params => {
139
+ const {
140
+ model
141
+ } = params;
142
+ const keys = createKeys(model);
143
+
144
+ try {
145
+ await entity.delete(keys);
146
+ return model;
147
+ } catch (ex) {
148
+ throw new _error.default(ex.messatge || "Could not delete model.", ex.code || "MODEL_DELETE_ERROR", {
149
+ error: ex,
150
+ model,
151
+ keys
152
+ });
153
+ }
154
+ };
155
+
156
+ const get = async params => {
157
+ const keys = createKeys(params);
158
+
159
+ try {
160
+ const item = await (0, _get.get)({
161
+ entity,
162
+ keys
163
+ });
164
+ return (0, _cleanup.cleanupItem)(entity, item);
165
+ } catch (ex) {
166
+ throw new _error.default(ex.messatge || "Could not get model.", ex.code || "MODEL_GET_ERROR", {
167
+ error: ex,
168
+ keys
169
+ });
170
+ }
171
+ };
172
+
173
+ const list = async params => {
174
+ const {
175
+ where
176
+ } = params;
177
+ const queryAllParams = {
178
+ entity,
179
+ partitionKey: createPartitionKey(where),
180
+ options: {
181
+ gte: " "
182
+ }
183
+ };
184
+
185
+ try {
186
+ const items = await (0, _query.queryAll)(queryAllParams);
187
+ return (0, _cleanup.cleanupItems)(entity, items);
188
+ } catch (ex) {
189
+ throw new _error.default(ex.messatge || "Could not list models.", ex.code || "MODEL_LIST_ERROR", {
190
+ error: ex,
191
+ partitionKey: queryAllParams.partitionKey
192
+ });
193
+ }
194
+ };
195
+
196
+ return {
197
+ create,
198
+ update,
199
+ delete: deleteModel,
200
+ get,
201
+ list
202
+ };
203
+ };
204
+
205
+ exports.createModelsStorageOperations = createModelsStorageOperations;
@@ -0,0 +1,6 @@
1
+ import { CmsSettingsStorageOperations } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ export interface Params {
4
+ entity: Entity<any>;
5
+ }
6
+ export declare const createSettingsStorageOperations: (params: Params) => CmsSettingsStorageOperations;
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createSettingsStorageOperations = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _get = require("@webiny/db-dynamodb/utils/get");
13
+
14
+ var _error = _interopRequireDefault(require("@webiny/error"));
15
+
16
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
17
+
18
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
19
+
20
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
21
+
22
+ const convertToDbData = settings => {
23
+ return _objectSpread(_objectSpread({}, settings), {}, {
24
+ contentModelLastChange: settings.contentModelLastChange.toISOString()
25
+ });
26
+ };
27
+
28
+ const convertFromDbData = settings => {
29
+ if (!settings) {
30
+ return null;
31
+ }
32
+
33
+ let contentModelLastChange;
34
+
35
+ try {
36
+ contentModelLastChange = new Date(settings.contentModelLastChange);
37
+ } catch {
38
+ contentModelLastChange = new Date();
39
+ }
40
+
41
+ return _objectSpread(_objectSpread({}, settings), {}, {
42
+ contentModelLastChange
43
+ });
44
+ };
45
+
46
+ const createPartitionKey = ({
47
+ tenant,
48
+ locale
49
+ }) => {
50
+ return `T#${tenant}#L#${locale}#CMS#SETTINGS`;
51
+ };
52
+
53
+ const createSortKey = () => {
54
+ return "settings";
55
+ };
56
+
57
+ const createKeys = params => {
58
+ return {
59
+ PK: createPartitionKey(params),
60
+ SK: createSortKey()
61
+ };
62
+ };
63
+
64
+ const createSettingsStorageOperations = params => {
65
+ const {
66
+ entity
67
+ } = params;
68
+
69
+ const create = async params => {
70
+ const {
71
+ settings
72
+ } = params;
73
+ const keys = createKeys(settings);
74
+ const dbSettings = convertToDbData(settings);
75
+
76
+ try {
77
+ await entity.put(_objectSpread(_objectSpread({}, dbSettings), keys));
78
+ return settings;
79
+ } catch (ex) {
80
+ throw new _error.default(ex.message || "Could not create settings.", ex.code || "CREATE_SETTINGS_ERROR", {
81
+ error: ex,
82
+ settings,
83
+ dbSettings,
84
+ keys
85
+ });
86
+ }
87
+ };
88
+
89
+ const update = async params => {
90
+ const {
91
+ settings,
92
+ original
93
+ } = params;
94
+ const keys = createKeys(settings);
95
+ const dbSettings = convertToDbData(settings);
96
+
97
+ try {
98
+ await entity.put(_objectSpread(_objectSpread({}, dbSettings), keys));
99
+ return settings;
100
+ } catch (ex) {
101
+ throw new _error.default(ex.message || "Could not update settings.", ex.code || "UPDATE_SETTINGS_ERROR", {
102
+ error: ex,
103
+ settings,
104
+ dbSettings,
105
+ original,
106
+ keys
107
+ });
108
+ }
109
+ };
110
+
111
+ const get = async params => {
112
+ const keys = createKeys(params);
113
+
114
+ try {
115
+ const record = await (0, _get.get)({
116
+ entity,
117
+ keys
118
+ });
119
+
120
+ if (!record) {
121
+ return null;
122
+ }
123
+
124
+ const settings = (0, _cleanup.cleanupItem)(entity, record);
125
+ return convertFromDbData(settings);
126
+ } catch (ex) {
127
+ throw new _error.default(ex.message || "Could not get settings.", ex.code || "GET_SETTINGS_ERROR", {
128
+ error: ex,
129
+ keys
130
+ });
131
+ }
132
+ };
133
+
134
+ return {
135
+ create,
136
+ get,
137
+ update
138
+ };
139
+ };
140
+
141
+ exports.createSettingsStorageOperations = createSettingsStorageOperations;
@@ -0,0 +1,5 @@
1
+ import { Client } from "@elastic/elasticsearch";
2
+ export interface Params {
3
+ elasticsearch: Client;
4
+ }
5
+ export declare const createElasticsearchTemplate: (params: Params) => Promise<void>;
@@ -0,0 +1,62 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createElasticsearchTemplate = void 0;
9
+
10
+ var _error = _interopRequireDefault(require("@webiny/error"));
11
+
12
+ const createElasticsearchTemplate = async params => {
13
+ const {
14
+ elasticsearch
15
+ } = params;
16
+ const options = {
17
+ name: "headless-cms-entries-index",
18
+ body: {
19
+ index_patterns: ["*headless-cms*"],
20
+ settings: {
21
+ analysis: {
22
+ analyzer: {
23
+ lowercase_analyzer: {
24
+ type: "custom",
25
+ filter: ["lowercase", "trim"],
26
+ tokenizer: "keyword"
27
+ }
28
+ }
29
+ }
30
+ },
31
+ mappings: {
32
+ properties: {
33
+ property: {
34
+ type: "text",
35
+ fields: {
36
+ keyword: {
37
+ type: "keyword",
38
+ ignore_above: 256
39
+ }
40
+ },
41
+ analyzer: "lowercase_analyzer"
42
+ },
43
+ rawValues: {
44
+ type: "object",
45
+ enabled: false
46
+ }
47
+ }
48
+ }
49
+ }
50
+ };
51
+
52
+ try {
53
+ await elasticsearch.indices.putTemplate(options);
54
+ } catch (ex) {
55
+ throw new _error.default(ex.message || "Could not create Elasticsearch index template for the Headless CMS.", ex.code || "CMS_ELASTICSEARCH_TEMPLATE_ERROR", {
56
+ error: ex,
57
+ options
58
+ });
59
+ }
60
+ };
61
+
62
+ exports.createElasticsearchTemplate = createElasticsearchTemplate;
@@ -0,0 +1,6 @@
1
+ import { CmsSystemStorageOperations } from "@webiny/api-headless-cms/types";
2
+ import { Entity } from "dynamodb-toolbox";
3
+ export interface Params {
4
+ entity: Entity<any>;
5
+ }
6
+ export declare const createSystemStorageOperations: (params: Params) => CmsSystemStorageOperations;