@webiny/api-form-builder-so-ddb-es 0.0.0-ee-vpcs.549378cf03
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.
- package/LICENSE +21 -0
- package/README.md +12 -0
- package/configurations.d.ts +11 -0
- package/configurations.js +50 -0
- package/configurations.js.map +1 -0
- package/definitions/elasticsearch.d.ts +9 -0
- package/definitions/elasticsearch.js +43 -0
- package/definitions/elasticsearch.js.map +1 -0
- package/definitions/form.d.ts +9 -0
- package/definitions/form.js +100 -0
- package/definitions/form.js.map +1 -0
- package/definitions/settings.d.ts +9 -0
- package/definitions/settings.js +49 -0
- package/definitions/settings.js.map +1 -0
- package/definitions/submission.d.ts +9 -0
- package/definitions/submission.js +70 -0
- package/definitions/submission.js.map +1 -0
- package/definitions/system.d.ts +9 -0
- package/definitions/system.js +40 -0
- package/definitions/system.js.map +1 -0
- package/definitions/table.d.ts +8 -0
- package/definitions/table.js +23 -0
- package/definitions/table.js.map +1 -0
- package/definitions/tableElasticsearch.d.ts +8 -0
- package/definitions/tableElasticsearch.js +23 -0
- package/definitions/tableElasticsearch.js.map +1 -0
- package/elasticsearch/createElasticsearchIndex.d.ts +10 -0
- package/elasticsearch/createElasticsearchIndex.js +63 -0
- package/elasticsearch/createElasticsearchIndex.js.map +1 -0
- package/elasticsearch/indices/base.d.ts +2 -0
- package/elasticsearch/indices/base.js +15 -0
- package/elasticsearch/indices/base.js.map +1 -0
- package/elasticsearch/indices/index.d.ts +1 -0
- package/elasticsearch/indices/index.js +16 -0
- package/elasticsearch/indices/index.js.map +1 -0
- package/elasticsearch/indices/japanese.d.ts +2 -0
- package/elasticsearch/indices/japanese.js +16 -0
- package/elasticsearch/indices/japanese.js.map +1 -0
- package/index.d.ts +2 -0
- package/index.js +191 -0
- package/index.js.map +1 -0
- package/operations/form/elasticsearchBody.d.ts +13 -0
- package/operations/form/elasticsearchBody.js +170 -0
- package/operations/form/elasticsearchBody.js.map +1 -0
- package/operations/form/elasticsearchFields.d.ts +3 -0
- package/operations/form/elasticsearchFields.js +33 -0
- package/operations/form/elasticsearchFields.js.map +1 -0
- package/operations/form/fields.d.ts +3 -0
- package/operations/form/fields.js +15 -0
- package/operations/form/fields.js.map +1 -0
- package/operations/form/index.d.ts +17 -0
- package/operations/form/index.js +904 -0
- package/operations/form/index.js.map +1 -0
- package/operations/settings/index.d.ts +7 -0
- package/operations/settings/index.js +118 -0
- package/operations/settings/index.js.map +1 -0
- package/operations/submission/elasticsearchBody.d.ts +13 -0
- package/operations/submission/elasticsearchBody.js +187 -0
- package/operations/submission/elasticsearchBody.js.map +1 -0
- package/operations/submission/elasticsearchFields.d.ts +3 -0
- package/operations/submission/elasticsearchFields.js +30 -0
- package/operations/submission/elasticsearchFields.js.map +1 -0
- package/operations/submission/index.d.ts +12 -0
- package/operations/submission/index.js +335 -0
- package/operations/submission/index.js.map +1 -0
- package/operations/system/index.d.ts +7 -0
- package/operations/system/index.js +101 -0
- package/operations/system/index.js.map +1 -0
- package/package.json +63 -0
- package/plugins/FormDynamoDbFieldPlugin.d.ts +4 -0
- package/plugins/FormDynamoDbFieldPlugin.js +17 -0
- package/plugins/FormDynamoDbFieldPlugin.js.map +1 -0
- package/plugins/FormElasticsearchBodyModifierPlugin.d.ts +4 -0
- package/plugins/FormElasticsearchBodyModifierPlugin.js +17 -0
- package/plugins/FormElasticsearchBodyModifierPlugin.js.map +1 -0
- package/plugins/FormElasticsearchFieldPlugin.d.ts +4 -0
- package/plugins/FormElasticsearchFieldPlugin.js +17 -0
- package/plugins/FormElasticsearchFieldPlugin.js.map +1 -0
- package/plugins/FormElasticsearchIndexPlugin.d.ts +4 -0
- package/plugins/FormElasticsearchIndexPlugin.js +17 -0
- package/plugins/FormElasticsearchIndexPlugin.js.map +1 -0
- package/plugins/FormElasticsearchQueryModifierPlugin.d.ts +4 -0
- package/plugins/FormElasticsearchQueryModifierPlugin.js +17 -0
- package/plugins/FormElasticsearchQueryModifierPlugin.js.map +1 -0
- package/plugins/FormElasticsearchSortModifierPlugin.d.ts +4 -0
- package/plugins/FormElasticsearchSortModifierPlugin.js +17 -0
- package/plugins/FormElasticsearchSortModifierPlugin.js.map +1 -0
- package/plugins/SubmissionElasticsearchBodyModifierPlugin.d.ts +4 -0
- package/plugins/SubmissionElasticsearchBodyModifierPlugin.js +17 -0
- package/plugins/SubmissionElasticsearchBodyModifierPlugin.js.map +1 -0
- package/plugins/SubmissionElasticsearchFieldPlugin.d.ts +4 -0
- package/plugins/SubmissionElasticsearchFieldPlugin.js +17 -0
- package/plugins/SubmissionElasticsearchFieldPlugin.js.map +1 -0
- package/plugins/SubmissionElasticsearchQueryModifierPlugin.d.ts +4 -0
- package/plugins/SubmissionElasticsearchQueryModifierPlugin.js +17 -0
- package/plugins/SubmissionElasticsearchQueryModifierPlugin.js.map +1 -0
- package/plugins/SubmissionElasticsearchSortModifierPlugin.d.ts +4 -0
- package/plugins/SubmissionElasticsearchSortModifierPlugin.js +17 -0
- package/plugins/SubmissionElasticsearchSortModifierPlugin.js.map +1 -0
- package/types.d.ts +66 -0
- package/types.js +17 -0
- package/types.js.map +1 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createSubmissionStorageOperations = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
13
|
+
|
|
14
|
+
var _batchRead = require("@webiny/db-dynamodb/utils/batchRead");
|
|
15
|
+
|
|
16
|
+
var _sort = require("@webiny/db-dynamodb/utils/sort");
|
|
17
|
+
|
|
18
|
+
var _apiElasticsearch = require("@webiny/api-elasticsearch");
|
|
19
|
+
|
|
20
|
+
var _elasticsearchBody = require("./elasticsearchBody");
|
|
21
|
+
|
|
22
|
+
var _configurations = require("../../configurations");
|
|
23
|
+
|
|
24
|
+
var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
|
|
25
|
+
|
|
26
|
+
var _utils = require("@webiny/utils");
|
|
27
|
+
|
|
28
|
+
const createSubmissionStorageOperations = params => {
|
|
29
|
+
const {
|
|
30
|
+
entity,
|
|
31
|
+
esEntity,
|
|
32
|
+
table,
|
|
33
|
+
elasticsearch,
|
|
34
|
+
plugins
|
|
35
|
+
} = params;
|
|
36
|
+
|
|
37
|
+
const createSubmissionPartitionKey = params => {
|
|
38
|
+
const {
|
|
39
|
+
tenant,
|
|
40
|
+
locale,
|
|
41
|
+
formId
|
|
42
|
+
} = params;
|
|
43
|
+
const {
|
|
44
|
+
id
|
|
45
|
+
} = (0, _utils.parseIdentifier)(formId);
|
|
46
|
+
return `T#${tenant}#L#${locale}#FB#F#${id}`;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const createSubmissionSortKey = id => {
|
|
50
|
+
return `FS#${id}`;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const createSubmissionType = () => {
|
|
54
|
+
return "fb.formSubmission";
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const createSubmission = async params => {
|
|
58
|
+
const {
|
|
59
|
+
submission,
|
|
60
|
+
form
|
|
61
|
+
} = params;
|
|
62
|
+
const keys = {
|
|
63
|
+
PK: createSubmissionPartitionKey(form),
|
|
64
|
+
SK: createSubmissionSortKey(submission.id)
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
await entity.put((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, submission), keys), {}, {
|
|
69
|
+
TYPE: createSubmissionType()
|
|
70
|
+
}));
|
|
71
|
+
} catch (ex) {
|
|
72
|
+
throw new _error.default(ex.message || "Could not create form submission in the DynamoDB.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", {
|
|
73
|
+
submission,
|
|
74
|
+
form,
|
|
75
|
+
keys
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
const {
|
|
81
|
+
index
|
|
82
|
+
} = _configurations.configurations.es({
|
|
83
|
+
tenant: form.tenant,
|
|
84
|
+
locale: form.locale
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
await esEntity.put((0, _objectSpread2.default)({
|
|
88
|
+
index,
|
|
89
|
+
data: (0, _objectSpread2.default)((0, _objectSpread2.default)({}, submission), {}, {
|
|
90
|
+
__type: (0, _elasticsearchBody.createSubmissionElasticType)()
|
|
91
|
+
}),
|
|
92
|
+
TYPE: createSubmissionType()
|
|
93
|
+
}, keys));
|
|
94
|
+
} catch (ex) {
|
|
95
|
+
throw new _error.default(ex.message || "Could not create form submission in the Elasticsearch.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", {
|
|
96
|
+
submission,
|
|
97
|
+
form,
|
|
98
|
+
keys
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return submission;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* We do not save the data in the Elasticsearch because there is no need for that.
|
|
106
|
+
*/
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
const updateSubmission = async params => {
|
|
110
|
+
const {
|
|
111
|
+
submission,
|
|
112
|
+
form,
|
|
113
|
+
original
|
|
114
|
+
} = params;
|
|
115
|
+
const keys = {
|
|
116
|
+
PK: createSubmissionPartitionKey(form),
|
|
117
|
+
SK: createSubmissionSortKey(submission.id)
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
await entity.put((0, _objectSpread2.default)((0, _objectSpread2.default)((0, _objectSpread2.default)({}, submission), keys), {}, {
|
|
122
|
+
TYPE: createSubmissionType()
|
|
123
|
+
}));
|
|
124
|
+
return submission;
|
|
125
|
+
} catch (ex) {
|
|
126
|
+
throw new _error.default(ex.message || "Could not update form submission in the DynamoDB.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", {
|
|
127
|
+
submission,
|
|
128
|
+
original,
|
|
129
|
+
form,
|
|
130
|
+
keys
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const deleteSubmission = async params => {
|
|
136
|
+
const {
|
|
137
|
+
submission,
|
|
138
|
+
form
|
|
139
|
+
} = params;
|
|
140
|
+
const keys = {
|
|
141
|
+
PK: createSubmissionPartitionKey(form),
|
|
142
|
+
SK: createSubmissionSortKey(submission.id)
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await entity.delete(keys);
|
|
147
|
+
} catch (ex) {
|
|
148
|
+
throw new _error.default(ex.message || "Could not delete form submission from DynamoDB.", ex.code || "DELETE_FORM_SUBMISSION_ERROR", {
|
|
149
|
+
submission,
|
|
150
|
+
form,
|
|
151
|
+
keys
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
await esEntity.delete(keys);
|
|
157
|
+
} catch (ex) {
|
|
158
|
+
throw new _error.default(ex.message || "Could not delete form submission from Elasticsearch.", ex.code || "DELETE_FORM_SUBMISSION_ERROR", {
|
|
159
|
+
submission,
|
|
160
|
+
form,
|
|
161
|
+
keys
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return submission;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
*
|
|
169
|
+
* We are using this method because it is faster to fetch the exact data from the DynamoDB than Elasticsearch.
|
|
170
|
+
*
|
|
171
|
+
* @internal
|
|
172
|
+
*/
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
const listSubmissionsByIds = async params => {
|
|
176
|
+
const {
|
|
177
|
+
where,
|
|
178
|
+
sort
|
|
179
|
+
} = params;
|
|
180
|
+
const items = (where.id_in || []).map(id => {
|
|
181
|
+
return entity.getBatch({
|
|
182
|
+
PK: createSubmissionPartitionKey((0, _objectSpread2.default)({}, where)),
|
|
183
|
+
SK: createSubmissionSortKey(id)
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
let results = [];
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
results = await (0, _batchRead.batchReadAll)({
|
|
190
|
+
table,
|
|
191
|
+
items
|
|
192
|
+
});
|
|
193
|
+
} catch (ex) {
|
|
194
|
+
throw new _error.default(ex.message || "Could not batch read form submissions.", ex.code || "BATCH_READ_SUBMISSIONS_ERROR", {
|
|
195
|
+
where,
|
|
196
|
+
sort
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* We need to remove empty results because it is a possibility that batch read returned null for non-existing record.
|
|
201
|
+
*/
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
const submissions = results.filter(Boolean).map(submission => {
|
|
205
|
+
return (0, _cleanup.cleanupItem)(entity, submission);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
if (!sort) {
|
|
209
|
+
return submissions;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return (0, _sort.sortItems)({
|
|
213
|
+
items: submissions,
|
|
214
|
+
sort,
|
|
215
|
+
fields: []
|
|
216
|
+
});
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
const listSubmissions = async params => {
|
|
220
|
+
const {
|
|
221
|
+
where,
|
|
222
|
+
sort = [],
|
|
223
|
+
limit: initialLimit,
|
|
224
|
+
after
|
|
225
|
+
} = params;
|
|
226
|
+
|
|
227
|
+
if (where.id_in) {
|
|
228
|
+
const items = await listSubmissionsByIds(params);
|
|
229
|
+
return {
|
|
230
|
+
items,
|
|
231
|
+
meta: {
|
|
232
|
+
hasMoreItems: false,
|
|
233
|
+
cursor: null,
|
|
234
|
+
totalCount: items.length
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const limit = (0, _apiElasticsearch.createLimit)(initialLimit);
|
|
240
|
+
const body = (0, _elasticsearchBody.createElasticsearchBody)({
|
|
241
|
+
plugins,
|
|
242
|
+
sort,
|
|
243
|
+
limit: limit + 1,
|
|
244
|
+
where,
|
|
245
|
+
after: (0, _apiElasticsearch.decodeCursor)(after)
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
const esConfig = _configurations.configurations.es({
|
|
249
|
+
tenant: where.tenant,
|
|
250
|
+
locale: where.locale
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const query = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, esConfig), {}, {
|
|
254
|
+
body
|
|
255
|
+
});
|
|
256
|
+
let response;
|
|
257
|
+
|
|
258
|
+
try {
|
|
259
|
+
response = await elasticsearch.search(query);
|
|
260
|
+
} catch (ex) {
|
|
261
|
+
throw new _error.default(ex.message || "Could list form submissions.", ex.code || "LIST_SUBMISSIONS_ERROR", {
|
|
262
|
+
where,
|
|
263
|
+
query
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const {
|
|
268
|
+
hits,
|
|
269
|
+
total
|
|
270
|
+
} = response.body.hits;
|
|
271
|
+
const items = hits.map(item => item._source);
|
|
272
|
+
const hasMoreItems = items.length > limit;
|
|
273
|
+
|
|
274
|
+
if (hasMoreItems) {
|
|
275
|
+
/**
|
|
276
|
+
* Remove the last item from results, we don't want to include it.
|
|
277
|
+
*/
|
|
278
|
+
items.pop();
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Cursor is the `sort` value of the last item in the array.
|
|
282
|
+
* https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after
|
|
283
|
+
*/
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
const meta = {
|
|
287
|
+
hasMoreItems,
|
|
288
|
+
totalCount: total.value,
|
|
289
|
+
cursor: items.length > 0 ? (0, _apiElasticsearch.encodeCursor)(hits[items.length - 1].sort) || null : null
|
|
290
|
+
};
|
|
291
|
+
return {
|
|
292
|
+
items,
|
|
293
|
+
meta
|
|
294
|
+
};
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const getSubmission = async params => {
|
|
298
|
+
const {
|
|
299
|
+
where
|
|
300
|
+
} = params;
|
|
301
|
+
const keys = {
|
|
302
|
+
PK: createSubmissionPartitionKey((0, _objectSpread2.default)((0, _objectSpread2.default)({}, where), {}, {
|
|
303
|
+
formId: where.formId
|
|
304
|
+
})),
|
|
305
|
+
SK: createSubmissionSortKey(where.id)
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
try {
|
|
309
|
+
const result = await entity.get(keys);
|
|
310
|
+
|
|
311
|
+
if (!result || !result.Item) {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return (0, _cleanup.cleanupItem)(entity, result.Item);
|
|
316
|
+
} catch (ex) {
|
|
317
|
+
throw new _error.default(ex.message || "Could not oad submission.", ex.code || "GET_SUBMISSION_ERROR", {
|
|
318
|
+
where,
|
|
319
|
+
keys
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
return {
|
|
325
|
+
createSubmission,
|
|
326
|
+
deleteSubmission,
|
|
327
|
+
updateSubmission,
|
|
328
|
+
listSubmissions,
|
|
329
|
+
getSubmission,
|
|
330
|
+
createSubmissionPartitionKey,
|
|
331
|
+
createSubmissionSortKey
|
|
332
|
+
};
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
exports.createSubmissionStorageOperations = createSubmissionStorageOperations;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createSubmissionStorageOperations","params","entity","esEntity","table","elasticsearch","plugins","createSubmissionPartitionKey","tenant","locale","formId","id","parseIdentifier","createSubmissionSortKey","createSubmissionType","createSubmission","submission","form","keys","PK","SK","put","TYPE","ex","WebinyError","message","code","index","configurations","es","data","__type","createSubmissionElasticType","updateSubmission","original","deleteSubmission","delete","listSubmissionsByIds","where","sort","items","id_in","map","getBatch","results","batchReadAll","submissions","filter","Boolean","cleanupItem","sortItems","fields","listSubmissions","limit","initialLimit","after","meta","hasMoreItems","cursor","totalCount","length","createLimit","body","createElasticsearchBody","decodeCursor","esConfig","query","response","search","hits","total","item","_source","pop","value","encodeCursor","getSubmission","result","get","Item"],"sources":["index.ts"],"sourcesContent":["import {\n FbSubmission,\n FormBuilderStorageOperationsCreateSubmissionParams,\n FormBuilderStorageOperationsDeleteSubmissionParams,\n FormBuilderStorageOperationsGetSubmissionParams,\n FormBuilderStorageOperationsListSubmissionsParams,\n FormBuilderStorageOperationsListSubmissionsResponse,\n FormBuilderStorageOperationsUpdateSubmissionParams\n} from \"@webiny/api-form-builder/types\";\nimport { Entity, Table } from \"dynamodb-toolbox\";\nimport { Client } from \"@elastic/elasticsearch\";\nimport WebinyError from \"@webiny/error\";\nimport { batchReadAll } from \"@webiny/db-dynamodb/utils/batchRead\";\nimport { sortItems } from \"@webiny/db-dynamodb/utils/sort\";\nimport { createLimit, encodeCursor, decodeCursor } from \"@webiny/api-elasticsearch\";\nimport {\n createElasticsearchBody,\n createSubmissionElasticType\n} from \"~/operations/submission/elasticsearchBody\";\nimport { PluginsContainer } from \"@webiny/plugins\";\nimport {\n FormBuilderSubmissionStorageOperations,\n FormBuilderSubmissionStorageOperationsCreatePartitionKeyParams\n} from \"~/types\";\nimport { configurations } from \"~/configurations\";\nimport { cleanupItem } from \"@webiny/db-dynamodb/utils/cleanup\";\nimport { parseIdentifier } from \"@webiny/utils\";\nimport { ElasticsearchSearchResponse } from \"@webiny/api-elasticsearch/types\";\n\nexport interface CreateSubmissionStorageOperationsParams {\n entity: Entity<any>;\n esEntity: Entity<any>;\n table: Table;\n elasticsearch: Client;\n plugins: PluginsContainer;\n}\n\nexport const createSubmissionStorageOperations = (\n params: CreateSubmissionStorageOperationsParams\n): FormBuilderSubmissionStorageOperations => {\n const { entity, esEntity, table, elasticsearch, plugins } = params;\n\n const createSubmissionPartitionKey = (\n params: FormBuilderSubmissionStorageOperationsCreatePartitionKeyParams\n ) => {\n const { tenant, locale, formId } = params;\n\n const { id } = parseIdentifier(formId);\n\n return `T#${tenant}#L#${locale}#FB#F#${id}`;\n };\n const createSubmissionSortKey = (id: string) => {\n return `FS#${id}`;\n };\n\n const createSubmissionType = () => {\n return \"fb.formSubmission\";\n };\n\n const createSubmission = async (\n params: FormBuilderStorageOperationsCreateSubmissionParams\n ): Promise<FbSubmission> => {\n const { submission, form } = params;\n const keys = {\n PK: createSubmissionPartitionKey(form),\n SK: createSubmissionSortKey(submission.id)\n };\n\n try {\n await entity.put({\n ...submission,\n ...keys,\n TYPE: createSubmissionType()\n });\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not create form submission in the DynamoDB.\",\n ex.code || \"UPDATE_FORM_SUBMISSION_ERROR\",\n {\n submission,\n form,\n keys\n }\n );\n }\n\n try {\n const { index } = configurations.es({\n tenant: form.tenant,\n locale: form.locale\n });\n await esEntity.put({\n index,\n data: {\n ...submission,\n __type: createSubmissionElasticType()\n },\n TYPE: createSubmissionType(),\n ...keys\n });\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not create form submission in the Elasticsearch.\",\n ex.code || \"UPDATE_FORM_SUBMISSION_ERROR\",\n {\n submission,\n form,\n keys\n }\n );\n }\n\n return submission;\n };\n /**\n * We do not save the data in the Elasticsearch because there is no need for that.\n */\n const updateSubmission = async (\n params: FormBuilderStorageOperationsUpdateSubmissionParams\n ): Promise<FbSubmission> => {\n const { submission, form, original } = params;\n const keys = {\n PK: createSubmissionPartitionKey(form),\n SK: createSubmissionSortKey(submission.id)\n };\n\n try {\n await entity.put({\n ...submission,\n ...keys,\n TYPE: createSubmissionType()\n });\n return submission;\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not update form submission in the DynamoDB.\",\n ex.code || \"UPDATE_FORM_SUBMISSION_ERROR\",\n {\n submission,\n original,\n form,\n keys\n }\n );\n }\n };\n\n const deleteSubmission = async (\n params: FormBuilderStorageOperationsDeleteSubmissionParams\n ): Promise<FbSubmission> => {\n const { submission, form } = params;\n\n const keys = {\n PK: createSubmissionPartitionKey(form),\n SK: createSubmissionSortKey(submission.id)\n };\n\n try {\n await entity.delete(keys);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not delete form submission from DynamoDB.\",\n ex.code || \"DELETE_FORM_SUBMISSION_ERROR\",\n {\n submission,\n form,\n keys\n }\n );\n }\n\n try {\n await esEntity.delete(keys);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not delete form submission from Elasticsearch.\",\n ex.code || \"DELETE_FORM_SUBMISSION_ERROR\",\n {\n submission,\n form,\n keys\n }\n );\n }\n\n return submission;\n };\n\n /**\n *\n * We are using this method because it is faster to fetch the exact data from the DynamoDB than Elasticsearch.\n *\n * @internal\n */\n const listSubmissionsByIds = async (\n params: FormBuilderStorageOperationsListSubmissionsParams\n ): Promise<FbSubmission[]> => {\n const { where, sort } = params;\n const items = (where.id_in || []).map(id => {\n return entity.getBatch({\n PK: createSubmissionPartitionKey({\n ...where\n }),\n SK: createSubmissionSortKey(id)\n });\n });\n\n let results: FbSubmission[] = [];\n\n try {\n results = await batchReadAll<FbSubmission>({\n table,\n items\n });\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not batch read form submissions.\",\n ex.code || \"BATCH_READ_SUBMISSIONS_ERROR\",\n {\n where,\n sort\n }\n );\n }\n /**\n * We need to remove empty results because it is a possibility that batch read returned null for non-existing record.\n */\n const submissions = results.filter(Boolean).map(submission => {\n return cleanupItem(entity, submission);\n }) as FbSubmission[];\n if (!sort) {\n return submissions;\n }\n return sortItems<FbSubmission>({\n items: submissions,\n sort,\n fields: []\n });\n };\n\n const listSubmissions = async (\n params: FormBuilderStorageOperationsListSubmissionsParams\n ): Promise<FormBuilderStorageOperationsListSubmissionsResponse> => {\n const { where, sort = [], limit: initialLimit, after } = params;\n\n if (where.id_in) {\n const items = await listSubmissionsByIds(params);\n\n return {\n items,\n meta: {\n hasMoreItems: false,\n cursor: null,\n totalCount: items.length\n }\n };\n }\n\n const limit = createLimit(initialLimit);\n\n const body = createElasticsearchBody({\n plugins,\n sort,\n limit: limit + 1,\n where,\n after: decodeCursor(after) as any\n });\n\n const esConfig = configurations.es({\n tenant: where.tenant,\n locale: where.locale\n });\n\n const query = {\n ...esConfig,\n body\n };\n\n let response: ElasticsearchSearchResponse<FbSubmission>;\n try {\n response = await elasticsearch.search(query);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could list form submissions.\",\n ex.code || \"LIST_SUBMISSIONS_ERROR\",\n {\n where,\n query\n }\n );\n }\n\n const { hits, total } = response.body.hits;\n const items = hits.map(item => item._source);\n\n const hasMoreItems = items.length > limit;\n if (hasMoreItems) {\n /**\n * Remove the last item from results, we don't want to include it.\n */\n items.pop();\n }\n /**\n * Cursor is the `sort` value of the last item in the array.\n * https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after\n */\n const meta = {\n hasMoreItems,\n totalCount: total.value,\n cursor: items.length > 0 ? encodeCursor(hits[items.length - 1].sort) || null : null\n };\n\n return {\n items,\n meta\n };\n };\n\n const getSubmission = async (\n params: FormBuilderStorageOperationsGetSubmissionParams\n ): Promise<FbSubmission | null> => {\n const { where } = params;\n\n const keys = {\n PK: createSubmissionPartitionKey({\n ...where,\n formId: where.formId as string\n }),\n SK: createSubmissionSortKey(where.id)\n };\n\n try {\n const result = await entity.get(keys);\n\n if (!result || !result.Item) {\n return null;\n }\n\n return cleanupItem(entity, result.Item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not oad submission.\",\n ex.code || \"GET_SUBMISSION_ERROR\",\n {\n where,\n keys\n }\n );\n }\n };\n\n return {\n createSubmission,\n deleteSubmission,\n updateSubmission,\n listSubmissions,\n getSubmission,\n createSubmissionPartitionKey,\n createSubmissionSortKey\n };\n};\n"],"mappings":";;;;;;;;;;;AAWA;;AACA;;AACA;;AACA;;AACA;;AASA;;AACA;;AACA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AAlB;IAEA,MAAMiB,IAAI,GAAG;MACTC,EAAE,EAAEZ,4BAA4B,6DACzB+B,KADyB;QAE5B5B,MAAM,EAAE4B,KAAK,CAAC5B;MAFc,GADvB;MAKTU,EAAE,EAAEP,uBAAuB,CAACyB,KAAK,CAAC3B,EAAP;IALlB,CAAb;;IAQA,IAAI;MACA,MAAMkE,MAAM,GAAG,MAAM3E,MAAM,CAAC4E,GAAP,CAAW5D,IAAX,CAArB;;MAEA,IAAI,CAAC2D,MAAD,IAAW,CAACA,MAAM,CAACE,IAAvB,EAA6B;QACzB,OAAO,IAAP;MACH;;MAED,OAAO,IAAA9B,oBAAA,EAAY/C,MAAZ,EAAoB2E,MAAM,CAACE,IAA3B,CAAP;IACH,CARD,CAQE,OAAOxD,EAAP,EAAW;MACT,MAAM,IAAIC,cAAJ,CACFD,EAAE,CAACE,OAAH,IAAc,2BADZ,EAEFF,EAAE,CAACG,IAAH,IAAW,sBAFT,EAGF;QACIY,KADJ;QAEIpB;MAFJ,CAHE,CAAN;IAQH;EACJ,CA/BD;;EAiCA,OAAO;IACHH,gBADG;IAEHoB,gBAFG;IAGHF,gBAHG;IAIHmB,eAJG;IAKHwB,aALG;IAMHrE,4BANG;IAOHM;EAPG,CAAP;AASH,CAnUM"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Entity, Table } from "dynamodb-toolbox";
|
|
2
|
+
import { FormBuilderSystemStorageOperations } from "../../types";
|
|
3
|
+
export interface CreateSystemStorageOperationsParams {
|
|
4
|
+
entity: Entity<any>;
|
|
5
|
+
table: Table;
|
|
6
|
+
}
|
|
7
|
+
export declare const createSystemStorageOperations: (params: CreateSystemStorageOperationsParams) => FormBuilderSystemStorageOperations;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.createSystemStorageOperations = void 0;
|
|
9
|
+
|
|
10
|
+
var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
|
|
11
|
+
|
|
12
|
+
var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
|
|
13
|
+
|
|
14
|
+
var _error = _interopRequireDefault(require("@webiny/error"));
|
|
15
|
+
|
|
16
|
+
const createSystemStorageOperations = params => {
|
|
17
|
+
const {
|
|
18
|
+
entity
|
|
19
|
+
} = params;
|
|
20
|
+
|
|
21
|
+
const createSystemPartitionKey = ({
|
|
22
|
+
tenant
|
|
23
|
+
}) => {
|
|
24
|
+
return `T#${tenant}#SYSTEM`;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const createSystemSortKey = () => {
|
|
28
|
+
return "FB";
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const createKeys = params => {
|
|
32
|
+
return {
|
|
33
|
+
PK: createSystemPartitionKey(params),
|
|
34
|
+
SK: createSystemSortKey()
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const createSystem = async params => {
|
|
39
|
+
const {
|
|
40
|
+
system
|
|
41
|
+
} = params;
|
|
42
|
+
const keys = createKeys(system);
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
await entity.put((0, _objectSpread2.default)((0, _objectSpread2.default)({}, system), keys));
|
|
46
|
+
return system;
|
|
47
|
+
} catch (ex) {
|
|
48
|
+
throw new _error.default(ex.message || "Could not create the system record by given keys.", ex.code || "CREATE_SYSTEM_ERROR", {
|
|
49
|
+
keys,
|
|
50
|
+
system
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const getSystem = async params => {
|
|
56
|
+
const keys = createKeys(params);
|
|
57
|
+
|
|
58
|
+
try {
|
|
59
|
+
const result = await entity.get(keys);
|
|
60
|
+
|
|
61
|
+
if (!result || !result.Item) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return (0, _cleanup.cleanupItem)(entity, result.Item);
|
|
66
|
+
} catch (ex) {
|
|
67
|
+
throw new _error.default(ex.message || "Could not get the system record by given keys.", ex.code || "LOAD_SYSTEM_ERROR", {
|
|
68
|
+
keys
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const updateSystem = async params => {
|
|
74
|
+
const {
|
|
75
|
+
system,
|
|
76
|
+
original
|
|
77
|
+
} = params;
|
|
78
|
+
const keys = createKeys(system);
|
|
79
|
+
|
|
80
|
+
try {
|
|
81
|
+
await entity.put((0, _objectSpread2.default)((0, _objectSpread2.default)({}, system), keys));
|
|
82
|
+
return system;
|
|
83
|
+
} catch (ex) {
|
|
84
|
+
throw new _error.default(ex.message || "Could not update the system record by given keys.", ex.code || "UPDATE_SYSTEM_ERROR", {
|
|
85
|
+
keys,
|
|
86
|
+
original,
|
|
87
|
+
system
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
createSystem,
|
|
94
|
+
getSystem,
|
|
95
|
+
updateSystem,
|
|
96
|
+
createSystemPartitionKey,
|
|
97
|
+
createSystemSortKey
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
exports.createSystemStorageOperations = createSystemStorageOperations;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["createSystemStorageOperations","params","entity","createSystemPartitionKey","tenant","createSystemSortKey","createKeys","PK","SK","createSystem","system","keys","put","ex","WebinyError","message","code","getSystem","result","get","Item","cleanupItem","updateSystem","original"],"sources":["index.ts"],"sourcesContent":["import {\n FormBuilderStorageOperationsCreateSystemParams,\n FormBuilderStorageOperationsGetSystemParams,\n FormBuilderStorageOperationsUpdateSystemParams,\n System\n} from \"@webiny/api-form-builder/types\";\nimport { Entity, Table } from \"dynamodb-toolbox\";\nimport { FormBuilderSystemCreateKeysParams, FormBuilderSystemStorageOperations } from \"~/types\";\nimport { cleanupItem } from \"@webiny/db-dynamodb/utils/cleanup\";\nimport WebinyError from \"@webiny/error\";\n\nexport interface CreateSystemStorageOperationsParams {\n entity: Entity<any>;\n table: Table;\n}\n\nexport const createSystemStorageOperations = (\n params: CreateSystemStorageOperationsParams\n): FormBuilderSystemStorageOperations => {\n const { entity } = params;\n\n const createSystemPartitionKey = ({ tenant }: FormBuilderSystemCreateKeysParams): string => {\n return `T#${tenant}#SYSTEM`;\n };\n\n const createSystemSortKey = (): string => {\n return \"FB\";\n };\n\n const createKeys = (params: FormBuilderSystemCreateKeysParams) => {\n return {\n PK: createSystemPartitionKey(params),\n SK: createSystemSortKey()\n };\n };\n\n const createSystem = async (\n params: FormBuilderStorageOperationsCreateSystemParams\n ): Promise<System> => {\n const { system } = params;\n const keys = createKeys(system);\n\n try {\n await entity.put({\n ...system,\n ...keys\n });\n return system;\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not create the system record by given keys.\",\n ex.code || \"CREATE_SYSTEM_ERROR\",\n {\n keys,\n system\n }\n );\n }\n };\n\n const getSystem = async (\n params: FormBuilderStorageOperationsGetSystemParams\n ): Promise<System | null> => {\n const keys = createKeys(params);\n\n try {\n const result = await entity.get(keys);\n if (!result || !result.Item) {\n return null;\n }\n return cleanupItem(entity, result.Item);\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not get the system record by given keys.\",\n ex.code || \"LOAD_SYSTEM_ERROR\",\n {\n keys\n }\n );\n }\n };\n\n const updateSystem = async (\n params: FormBuilderStorageOperationsUpdateSystemParams\n ): Promise<System> => {\n const { system, original } = params;\n const keys = createKeys(system);\n\n try {\n await entity.put({\n ...system,\n ...keys\n });\n return system;\n } catch (ex) {\n throw new WebinyError(\n ex.message || \"Could not update the system record by given keys.\",\n ex.code || \"UPDATE_SYSTEM_ERROR\",\n {\n keys,\n original,\n system\n }\n );\n }\n };\n\n return {\n create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,EAAoBgB,MAAM,CAACE,IAA3B,CAAP;IACH,CAND,CAME,OAAOP,EAAP,EAAW;MACT,MAAM,IAAIC,cAAJ,CACFD,EAAE,CAACE,OAAH,IAAc,gDADZ,EAEFF,EAAE,CAACG,IAAH,IAAW,mBAFT,EAGF;QACIL;MADJ,CAHE,CAAN;IAOH;EACJ,CApBD;;EAsBA,MAAMW,YAAY,GAAG,MACjBrB,MADiB,IAEC;IAClB,MAAM;MAAES,MAAF;MAAUa;IAAV,IAAuBtB,MAA7B;IACA,MAAMU,IAAI,GAAGL,UAAU,CAACI,MAAD,CAAvB;;IAEA,IAAI;MACA,MAAMR,MAAM,CAACU,GAAP,6DACCF,MADD,GAECC,IAFD,EAAN;MAIA,OAAOD,MAAP;IACH,CAND,CAME,OAAOG,EAAP,EAAW;MACT,MAAM,IAAIC,cAAJ,CACFD,EAAE,CAACE,OAAH,IAAc,mDADZ,EAEFF,EAAE,CAACG,IAAH,IAAW,qBAFT,EAGF;QACIL,IADJ;QAEIY,QAFJ;QAGIb;MAHJ,CAHE,CAAN;IASH;EACJ,CAvBD;;EAyBA,OAAO;IACHD,YADG;IAEHQ,SAFG;IAGHK,YAHG;IAIHnB,wBAJG;IAKHE;EALG,CAAP;AAOH,CAlGM"}
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@webiny/api-form-builder-so-ddb-es",
|
|
3
|
+
"version": "0.0.0-ee-vpcs.549378cf03",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"@webiny/api-form-builder",
|
|
7
|
+
"storage-operations",
|
|
8
|
+
"dynamodb",
|
|
9
|
+
"elasticsearch",
|
|
10
|
+
"fb:ddb-es"
|
|
11
|
+
],
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "https://github.com/webiny/webiny-js.git",
|
|
15
|
+
"directory": "packages/api-form-builder-so-ddb-es"
|
|
16
|
+
},
|
|
17
|
+
"contributors": [
|
|
18
|
+
"Pavel Denisjuk <pavel@webiny.com>",
|
|
19
|
+
"Sven Al Hamad <sven@webiny.com>",
|
|
20
|
+
"Adrian Smijulj <adrian@webiny.com>"
|
|
21
|
+
],
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@babel/runtime": "7.19.0",
|
|
25
|
+
"@elastic/elasticsearch": "7.12.0",
|
|
26
|
+
"@webiny/api": "0.0.0-ee-vpcs.549378cf03",
|
|
27
|
+
"@webiny/api-elasticsearch": "0.0.0-ee-vpcs.549378cf03",
|
|
28
|
+
"@webiny/api-form-builder": "0.0.0-ee-vpcs.549378cf03",
|
|
29
|
+
"@webiny/db-dynamodb": "0.0.0-ee-vpcs.549378cf03",
|
|
30
|
+
"@webiny/error": "0.0.0-ee-vpcs.549378cf03",
|
|
31
|
+
"@webiny/plugins": "0.0.0-ee-vpcs.549378cf03",
|
|
32
|
+
"@webiny/utils": "0.0.0-ee-vpcs.549378cf03",
|
|
33
|
+
"dynamodb-toolbox": "0.3.5",
|
|
34
|
+
"elastic-ts": "0.8.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/cli": "^7.19.3",
|
|
38
|
+
"@babel/core": "^7.19.3",
|
|
39
|
+
"@babel/preset-env": "^7.19.4",
|
|
40
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
41
|
+
"@webiny/api-dynamodb-to-elasticsearch": "^0.0.0-ee-vpcs.549378cf03",
|
|
42
|
+
"@webiny/cli": "^0.0.0-ee-vpcs.549378cf03",
|
|
43
|
+
"@webiny/handler-aws": "^0.0.0-ee-vpcs.549378cf03",
|
|
44
|
+
"@webiny/handler-db": "^0.0.0-ee-vpcs.549378cf03",
|
|
45
|
+
"@webiny/project-utils": "^0.0.0-ee-vpcs.549378cf03",
|
|
46
|
+
"csvtojson": "^2.0.10",
|
|
47
|
+
"jest": "^28.1.0",
|
|
48
|
+
"jest-dynalite": "^3.2.0",
|
|
49
|
+
"jest-environment-node": "^27.2.4",
|
|
50
|
+
"rimraf": "^3.0.2",
|
|
51
|
+
"ttypescript": "^1.5.12",
|
|
52
|
+
"typescript": "4.7.4"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"directory": "dist"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"build": "yarn webiny run build",
|
|
60
|
+
"watch": "yarn webiny run watch"
|
|
61
|
+
},
|
|
62
|
+
"gitHead": "549378cf03fcd27845fc3fa23d1dc6b32896f630"
|
|
63
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FormDynamoDbFieldPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _FieldPlugin = require("@webiny/db-dynamodb/plugins/definitions/FieldPlugin");
|
|
13
|
+
|
|
14
|
+
class FormDynamoDbFieldPlugin extends _FieldPlugin.FieldPlugin {}
|
|
15
|
+
|
|
16
|
+
exports.FormDynamoDbFieldPlugin = FormDynamoDbFieldPlugin;
|
|
17
|
+
(0, _defineProperty2.default)(FormDynamoDbFieldPlugin, "type", "formBuilder.dynamodb.field.form");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["FormDynamoDbFieldPlugin","FieldPlugin"],"sources":["FormDynamoDbFieldPlugin.ts"],"sourcesContent":["import { FieldPlugin } from \"@webiny/db-dynamodb/plugins/definitions/FieldPlugin\";\n\nexport class FormDynamoDbFieldPlugin extends FieldPlugin {\n public static override readonly type: string = \"formBuilder.dynamodb.field.form\";\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AAEO,MAAMA,uBAAN,SAAsCC,wBAAtC,CAAkD;;;8BAA5CD,uB,UACsC,iC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FormElasticsearchBodyModifierPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _apiElasticsearch = require("@webiny/api-elasticsearch");
|
|
13
|
+
|
|
14
|
+
class FormElasticsearchBodyModifierPlugin extends _apiElasticsearch.ElasticsearchBodyModifierPlugin {}
|
|
15
|
+
|
|
16
|
+
exports.FormElasticsearchBodyModifierPlugin = FormElasticsearchBodyModifierPlugin;
|
|
17
|
+
(0, _defineProperty2.default)(FormElasticsearchBodyModifierPlugin, "type", "formBuilder.elasticsearch.modifier.body.form");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["FormElasticsearchBodyModifierPlugin","ElasticsearchBodyModifierPlugin"],"sources":["FormElasticsearchBodyModifierPlugin.ts"],"sourcesContent":["import { ElasticsearchBodyModifierPlugin } from \"@webiny/api-elasticsearch\";\n\nexport class FormElasticsearchBodyModifierPlugin extends ElasticsearchBodyModifierPlugin {\n public static override readonly type: string = \"formBuilder.elasticsearch.modifier.body.form\";\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AAEO,MAAMA,mCAAN,SAAkDC,iDAAlD,CAAkF;;;8BAA5ED,mC,UACsC,8C"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FormElasticsearchFieldPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _apiElasticsearch = require("@webiny/api-elasticsearch");
|
|
13
|
+
|
|
14
|
+
class FormElasticsearchFieldPlugin extends _apiElasticsearch.ElasticsearchFieldPlugin {}
|
|
15
|
+
|
|
16
|
+
exports.FormElasticsearchFieldPlugin = FormElasticsearchFieldPlugin;
|
|
17
|
+
(0, _defineProperty2.default)(FormElasticsearchFieldPlugin, "type", "formBuilder.elasticsearch.fieldDefinition.form");
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["FormElasticsearchFieldPlugin","ElasticsearchFieldPlugin"],"sources":["FormElasticsearchFieldPlugin.ts"],"sourcesContent":["import { ElasticsearchFieldPlugin } from \"@webiny/api-elasticsearch\";\n\nexport class FormElasticsearchFieldPlugin extends ElasticsearchFieldPlugin {\n public static override readonly type: string = \"formBuilder.elasticsearch.fieldDefinition.form\";\n}\n"],"mappings":";;;;;;;;;;;AAAA;;AAEO,MAAMA,4BAAN,SAA2CC,0CAA3C,CAAoE;;;8BAA9DD,4B,UACsC,gD"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
+
|
|
5
|
+
Object.defineProperty(exports, "__esModule", {
|
|
6
|
+
value: true
|
|
7
|
+
});
|
|
8
|
+
exports.FormElasticsearchIndexPlugin = void 0;
|
|
9
|
+
|
|
10
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
|
|
12
|
+
var _apiElasticsearch = require("@webiny/api-elasticsearch");
|
|
13
|
+
|
|
14
|
+
class FormElasticsearchIndexPlugin extends _apiElasticsearch.ElasticsearchIndexPlugin {}
|
|
15
|
+
|
|
16
|
+
exports.FormElasticsearchIndexPlugin = FormElasticsearchIndexPlugin;
|
|
17
|
+
(0, _defineProperty2.default)(FormElasticsearchIndexPlugin, "type", "formBuilder.form.elasticsearch.index");
|