@webiny/api-form-builder-so-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 (63) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +12 -0
  3. package/configurations.d.ts +9 -0
  4. package/configurations.js +28 -0
  5. package/definitions/elasticsearch.d.ts +8 -0
  6. package/definitions/elasticsearch.js +47 -0
  7. package/definitions/form.d.ts +8 -0
  8. package/definitions/form.js +104 -0
  9. package/definitions/settings.d.ts +8 -0
  10. package/definitions/settings.js +53 -0
  11. package/definitions/submission.d.ts +8 -0
  12. package/definitions/submission.js +74 -0
  13. package/definitions/system.d.ts +8 -0
  14. package/definitions/system.js +44 -0
  15. package/definitions/table.d.ts +7 -0
  16. package/definitions/table.js +23 -0
  17. package/definitions/tableElasticsearch.d.ts +7 -0
  18. package/definitions/tableElasticsearch.js +23 -0
  19. package/index.d.ts +2 -0
  20. package/index.js +188 -0
  21. package/operations/form/elasticsearchBody.d.ts +13 -0
  22. package/operations/form/elasticsearchBody.js +187 -0
  23. package/operations/form/elasticsearchFields.d.ts +3 -0
  24. package/operations/form/elasticsearchFields.js +33 -0
  25. package/operations/form/fields.d.ts +3 -0
  26. package/operations/form/fields.js +15 -0
  27. package/operations/form/index.d.ts +17 -0
  28. package/operations/form/index.js +903 -0
  29. package/operations/settings/index.d.ts +7 -0
  30. package/operations/settings/index.js +122 -0
  31. package/operations/submission/elasticsearchBody.d.ts +13 -0
  32. package/operations/submission/elasticsearchBody.js +197 -0
  33. package/operations/submission/elasticsearchFields.d.ts +3 -0
  34. package/operations/submission/elasticsearchFields.js +30 -0
  35. package/operations/submission/index.d.ts +12 -0
  36. package/operations/submission/index.js +340 -0
  37. package/operations/system/createElasticsearchIndex.d.ts +6 -0
  38. package/operations/system/createElasticsearchIndex.js +70 -0
  39. package/operations/system/index.d.ts +7 -0
  40. package/operations/system/index.js +105 -0
  41. package/package.json +67 -0
  42. package/plugins/FormDynamoDbFieldPlugin.d.ts +4 -0
  43. package/plugins/FormDynamoDbFieldPlugin.js +17 -0
  44. package/plugins/FormElasticsearchBodyModifierPlugin.d.ts +4 -0
  45. package/plugins/FormElasticsearchBodyModifierPlugin.js +17 -0
  46. package/plugins/FormElasticsearchFieldPlugin.d.ts +4 -0
  47. package/plugins/FormElasticsearchFieldPlugin.js +17 -0
  48. package/plugins/FormElasticsearchQueryModifierPlugin.d.ts +4 -0
  49. package/plugins/FormElasticsearchQueryModifierPlugin.js +17 -0
  50. package/plugins/FormElasticsearchSortModifierPlugin.d.ts +4 -0
  51. package/plugins/FormElasticsearchSortModifierPlugin.js +17 -0
  52. package/plugins/SubmissionElasticsearchBodyModifierPlugin.d.ts +4 -0
  53. package/plugins/SubmissionElasticsearchBodyModifierPlugin.js +17 -0
  54. package/plugins/SubmissionElasticsearchFieldPlugin.d.ts +4 -0
  55. package/plugins/SubmissionElasticsearchFieldPlugin.js +17 -0
  56. package/plugins/SubmissionElasticsearchQueryModifierPlugin.d.ts +4 -0
  57. package/plugins/SubmissionElasticsearchQueryModifierPlugin.js +17 -0
  58. package/plugins/SubmissionElasticsearchSortModifierPlugin.d.ts +4 -0
  59. package/plugins/SubmissionElasticsearchSortModifierPlugin.js +17 -0
  60. package/types.d.ts +66 -0
  61. package/types.js +17 -0
  62. package/upgrades/5.16.0/index.d.ts +8 -0
  63. package/upgrades/5.16.0/index.js +141 -0
@@ -0,0 +1,7 @@
1
+ import { Entity, Table } from "dynamodb-toolbox";
2
+ import { FormBuilderSettingsStorageOperations } from "../../types";
3
+ export interface Params {
4
+ entity: Entity<any>;
5
+ table: Table;
6
+ }
7
+ export declare const createSettingsStorageOperations: (params: Params) => FormBuilderSettingsStorageOperations;
@@ -0,0 +1,122 @@
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 _error = _interopRequireDefault(require("@webiny/error"));
13
+
14
+ var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
15
+
16
+ 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; }
17
+
18
+ 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; }
19
+
20
+ const createSettingsStorageOperations = params => {
21
+ const {
22
+ entity
23
+ } = params;
24
+
25
+ const createSettingsPartitionKey = ({
26
+ tenant,
27
+ locale
28
+ }) => {
29
+ return `T#${tenant}#L#${locale}#FB#SETTINGS`;
30
+ };
31
+
32
+ const createSettingsSortKey = () => {
33
+ return "default";
34
+ };
35
+
36
+ const createKeys = params => {
37
+ return {
38
+ PK: createSettingsPartitionKey(params),
39
+ SK: createSettingsSortKey()
40
+ };
41
+ };
42
+
43
+ const createSettings = async params => {
44
+ const {
45
+ settings
46
+ } = params;
47
+ const keys = createKeys(settings);
48
+
49
+ try {
50
+ await entity.put(_objectSpread(_objectSpread({}, settings), keys));
51
+ return settings;
52
+ } catch (ex) {
53
+ throw new _error.default(ex.message || "Could not create the settings record by given keys.", ex.code || "CREATE_SETTINGS_ERROR", {
54
+ keys,
55
+ settings
56
+ });
57
+ }
58
+ };
59
+
60
+ const getSettings = async params => {
61
+ const keys = createKeys(params);
62
+
63
+ try {
64
+ const result = await entity.get(keys);
65
+
66
+ if (!result || !result.Item) {
67
+ return null;
68
+ }
69
+
70
+ return (0, _cleanup.cleanupItem)(entity, result.Item);
71
+ } catch (ex) {
72
+ throw new _error.default(ex.message || "Could not get the settings record by given keys.", ex.code || "LOAD_SETTINGS_ERROR", {
73
+ keys
74
+ });
75
+ }
76
+ };
77
+
78
+ const updateSettings = async params => {
79
+ const {
80
+ settings,
81
+ original
82
+ } = params;
83
+ const keys = createKeys(settings);
84
+
85
+ try {
86
+ await entity.put(_objectSpread(_objectSpread({}, settings), keys));
87
+ return settings;
88
+ } catch (ex) {
89
+ throw new _error.default(ex.message || "Could not update the settings record by given keys.", ex.code || "UPDATE_SETTINGS_ERROR", {
90
+ keys,
91
+ original,
92
+ settings
93
+ });
94
+ }
95
+ };
96
+
97
+ const deleteSettings = async params => {
98
+ const {
99
+ settings
100
+ } = params;
101
+ const keys = createKeys(settings);
102
+
103
+ try {
104
+ await entity.delete();
105
+ } catch (ex) {
106
+ throw new _error.default(ex.message || "Could not delete the settings record by given keys.", ex.code || "DELETE_SETTINGS_ERROR", {
107
+ keys
108
+ });
109
+ }
110
+ };
111
+
112
+ return {
113
+ createSettings,
114
+ getSettings,
115
+ updateSettings,
116
+ deleteSettings,
117
+ createSettingsPartitionKey,
118
+ createSettingsSortKey
119
+ };
120
+ };
121
+
122
+ exports.createSettingsStorageOperations = createSettingsStorageOperations;
@@ -0,0 +1,13 @@
1
+ import { SearchBody as esSearchBody } from "elastic-ts";
2
+ import { FormBuilderStorageOperationsListSubmissionsParams } from "@webiny/api-form-builder/types";
3
+ import { PluginsContainer } from "@webiny/plugins";
4
+ export declare const createSubmissionElasticType: () => string;
5
+ interface CreateElasticsearchBodyParams {
6
+ plugins: PluginsContainer;
7
+ where: FormBuilderStorageOperationsListSubmissionsParams["where"];
8
+ limit: number;
9
+ after?: string;
10
+ sort: string[];
11
+ }
12
+ export declare const createElasticsearchBody: (params: CreateElasticsearchBodyParams) => esSearchBody;
13
+ export {};
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.createSubmissionElasticType = exports.createElasticsearchBody = void 0;
9
+
10
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
11
+
12
+ var _cursors = require("@webiny/api-elasticsearch/cursors");
13
+
14
+ var _sort = require("@webiny/api-elasticsearch/sort");
15
+
16
+ var _limit = require("@webiny/api-elasticsearch/limit");
17
+
18
+ var _ElasticsearchQueryBuilderOperatorPlugin = require("@webiny/api-elasticsearch/plugins/definition/ElasticsearchQueryBuilderOperatorPlugin");
19
+
20
+ var _SubmissionElasticsearchFieldPlugin = require("../../plugins/SubmissionElasticsearchFieldPlugin");
21
+
22
+ var _SubmissionElasticsearchSortModifierPlugin = require("../../plugins/SubmissionElasticsearchSortModifierPlugin");
23
+
24
+ var _SubmissionElasticsearchBodyModifierPlugin = require("../../plugins/SubmissionElasticsearchBodyModifierPlugin");
25
+
26
+ var _SubmissionElasticsearchQueryModifierPlugin = require("../../plugins/SubmissionElasticsearchQueryModifierPlugin");
27
+
28
+ var _where = require("@webiny/api-elasticsearch/where");
29
+
30
+ 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; }
31
+
32
+ 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; }
33
+
34
+ const createInitialQueryValue = () => {
35
+ return {
36
+ must: [
37
+ /**
38
+ * We add the __type filtering in the initial query because it must be applied.
39
+ */
40
+ {
41
+ term: {
42
+ "__type.keyword": "fb.submission"
43
+ }
44
+ }],
45
+ must_not: [],
46
+ should: [],
47
+ filter: []
48
+ };
49
+ };
50
+
51
+ const createSubmissionElasticType = () => {
52
+ return "fb.submission";
53
+ };
54
+
55
+ exports.createSubmissionElasticType = createSubmissionElasticType;
56
+
57
+ const createElasticsearchQuery = params => {
58
+ const {
59
+ plugins,
60
+ where: initialWhere,
61
+ fieldPlugins
62
+ } = params;
63
+ const query = createInitialQueryValue();
64
+ /**
65
+ * Be aware that, if having more registered operator plugins of same type, the last one will be used.
66
+ */
67
+
68
+ const operatorPlugins = plugins.byType(_ElasticsearchQueryBuilderOperatorPlugin.ElasticsearchQueryBuilderOperatorPlugin.type).reduce((acc, plugin) => {
69
+ acc[plugin.getOperator()] = plugin;
70
+ return acc;
71
+ }, {});
72
+
73
+ const where = _objectSpread({}, initialWhere);
74
+ /**
75
+ * !!! IMPORTANT !!! There are few specific cases where we hardcode the query conditions.
76
+ *
77
+ * When ES index is shared between tenants, we need to filter records by tenant ID.
78
+ * No need for the tenant filtering otherwise as each index is for single tenant.
79
+ */
80
+
81
+
82
+ const sharedIndex = process.env.ELASTICSEARCH_SHARED_INDEXES === "true";
83
+
84
+ if (sharedIndex) {
85
+ query.must.push({
86
+ term: {
87
+ "tenant.keyword": where.tenant
88
+ }
89
+ });
90
+ }
91
+ /**
92
+ * Remove tenant so it is not applied again later.
93
+ * Possibly tenant is not defined, but just in case, remove it.
94
+ */
95
+
96
+
97
+ delete where.tenant;
98
+ /**
99
+ * Add the locale to filtering.
100
+ */
101
+
102
+ query.must.push({
103
+ term: {
104
+ "locale.keyword": where.locale
105
+ }
106
+ });
107
+ delete where.locale;
108
+ /**
109
+ * And add the parent (form) to the filtering, if it exists.
110
+ */
111
+
112
+ query.must.push({
113
+ term: {
114
+ "form.parent.keyword": where.formId
115
+ }
116
+ });
117
+ delete where.formId;
118
+ /**
119
+ * We apply other conditions as they are passed via the where value.
120
+ */
121
+
122
+ (0, _where.applyWhere)({
123
+ query,
124
+ where,
125
+ fields: fieldPlugins,
126
+ operators: operatorPlugins
127
+ });
128
+ return query;
129
+ };
130
+
131
+ const createElasticsearchBody = params => {
132
+ const {
133
+ plugins,
134
+ where,
135
+ limit: initialLimit,
136
+ sort: initialSort,
137
+ after
138
+ } = params;
139
+ const fieldPlugins = plugins.byType(_SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin.type).reduce((acc, plugin) => {
140
+ acc[plugin.field] = plugin;
141
+ return acc;
142
+ }, {});
143
+ const limit = (0, _limit.createLimit)(initialLimit, 100);
144
+ const query = createElasticsearchQuery(_objectSpread(_objectSpread({}, params), {}, {
145
+ fieldPlugins
146
+ }));
147
+ const sort = (0, _sort.createSort)({
148
+ sort: initialSort,
149
+ fieldPlugins
150
+ });
151
+ const queryModifiers = plugins.byType(_SubmissionElasticsearchQueryModifierPlugin.SubmissionElasticsearchQueryModifierPlugin.type);
152
+
153
+ for (const plugin of queryModifiers) {
154
+ plugin.modifyQuery({
155
+ query,
156
+ where
157
+ });
158
+ }
159
+
160
+ const sortModifiers = plugins.byType(_SubmissionElasticsearchSortModifierPlugin.SubmissionElasticsearchSortModifierPlugin.type);
161
+
162
+ for (const plugin of sortModifiers) {
163
+ plugin.modifySort({
164
+ sort
165
+ });
166
+ }
167
+
168
+ const body = {
169
+ query: {
170
+ constant_score: {
171
+ filter: {
172
+ bool: _objectSpread({}, query)
173
+ }
174
+ }
175
+ },
176
+ size: limit + 1,
177
+
178
+ /**
179
+ * Casting as any is required due to search_after is accepting an array of values.
180
+ * Which is correct in some cases. In our case, it is not.
181
+ * https://www.elastic.co/guide/en/elasticsearch/reference/7.13/paginate-search-results.html
182
+ */
183
+ search_after: (0, _cursors.decodeCursor)(after),
184
+ sort
185
+ };
186
+ const bodyModifiers = plugins.byType(_SubmissionElasticsearchBodyModifierPlugin.SubmissionElasticsearchBodyModifierPlugin.type);
187
+
188
+ for (const plugin of bodyModifiers) {
189
+ plugin.modifyBody({
190
+ body
191
+ });
192
+ }
193
+
194
+ return body;
195
+ };
196
+
197
+ exports.createElasticsearchBody = createElasticsearchBody;
@@ -0,0 +1,3 @@
1
+ import { SubmissionElasticsearchFieldPlugin } from "../../plugins/SubmissionElasticsearchFieldPlugin";
2
+ declare const _default: () => SubmissionElasticsearchFieldPlugin[];
3
+ export default _default;
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+
8
+ var _SubmissionElasticsearchFieldPlugin = require("../../plugins/SubmissionElasticsearchFieldPlugin");
9
+
10
+ var _default = () => [new _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin({
11
+ field: "parent",
12
+ path: "form.parent"
13
+ }), new _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin({
14
+ field: "ownedBy",
15
+ path: "ownedBy.id"
16
+ }), new _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin({
17
+ field: "createdOn",
18
+ unmappedType: "date"
19
+ }), new _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin({
20
+ field: "savedOn",
21
+ unmappedType: "date"
22
+ }),
23
+ /**
24
+ * Always add the ALL fields plugin because of the keyword/path build.
25
+ */
26
+ new _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin({
27
+ field: _SubmissionElasticsearchFieldPlugin.SubmissionElasticsearchFieldPlugin.ALL
28
+ })];
29
+
30
+ exports.default = _default;
@@ -0,0 +1,12 @@
1
+ import { Entity, Table } from "dynamodb-toolbox";
2
+ import { Client } from "@elastic/elasticsearch";
3
+ import { PluginsContainer } from "@webiny/plugins";
4
+ import { FormBuilderSubmissionStorageOperations } from "../../types";
5
+ export interface Params {
6
+ entity: Entity<any>;
7
+ esEntity: Entity<any>;
8
+ table: Table;
9
+ elasticsearch: Client;
10
+ plugins: PluginsContainer;
11
+ }
12
+ export declare const createSubmissionStorageOperations: (params: Params) => FormBuilderSubmissionStorageOperations;