UNPKG

@webiny/api-form-builder-so-ddb-es

Version:

[![](https://img.shields.io/npm/dw/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny/api-form-builder-so-ddb-es) [![](https://img.shields.io/npm/v/@webiny/api-form-builder-so-ddb-es.svg)](https://www.npmjs.com/package/@webiny

312 lines (309 loc) 8.08 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSubmissionStorageOperations = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _dbDynamodb = require("@webiny/db-dynamodb"); var _sort = require("@webiny/db-dynamodb/utils/sort"); var _apiElasticsearch = require("@webiny/api-elasticsearch"); var _elasticsearchBody = require("./elasticsearchBody"); var _configurations = require("../../configurations"); var _cleanup = require("@webiny/db-dynamodb/utils/cleanup"); var _utils = require("@webiny/utils"); const createSubmissionStorageOperations = params => { const { entity, esEntity, table, elasticsearch, plugins } = params; const createSubmissionPartitionKey = params => { const { tenant, locale, formId } = params; const { id } = (0, _utils.parseIdentifier)(formId); return `T#${tenant}#L#${locale}#FB#F#${id}`; }; const createSubmissionSortKey = id => { return `FS#${id}`; }; const createSubmissionType = () => { return "fb.formSubmission"; }; const createSubmission = async params => { const { submission, form } = params; const keys = { PK: createSubmissionPartitionKey(form), SK: createSubmissionSortKey(submission.id) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...submission, ...keys, TYPE: createSubmissionType() } }); } catch (ex) { throw new _error.default(ex.message || "Could not create form submission in the DynamoDB.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", { submission, form, keys }); } try { const { index } = _configurations.configurations.es({ tenant: form.tenant, locale: form.locale }); await (0, _dbDynamodb.put)({ entity: esEntity, item: { index, data: { ...submission, __type: (0, _elasticsearchBody.createSubmissionElasticType)() }, TYPE: createSubmissionType(), ...keys } }); } catch (ex) { throw new _error.default(ex.message || "Could not create form submission in the Elasticsearch.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", { submission, form, keys }); } return submission; }; /** * We do not save the data in the Elasticsearch because there is no need for that. */ const updateSubmission = async params => { const { submission, form, original } = params; const keys = { PK: createSubmissionPartitionKey(form), SK: createSubmissionSortKey(submission.id) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...submission, ...keys, TYPE: createSubmissionType() } }); return submission; } catch (ex) { throw new _error.default(ex.message || "Could not update form submission in the DynamoDB.", ex.code || "UPDATE_FORM_SUBMISSION_ERROR", { submission, original, form, keys }); } }; const deleteSubmission = async params => { const { submission, form } = params; const keys = { PK: createSubmissionPartitionKey(form), SK: createSubmissionSortKey(submission.id) }; try { await (0, _dbDynamodb.deleteItem)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not delete form submission from DynamoDB.", ex.code || "DELETE_FORM_SUBMISSION_ERROR", { submission, form, keys }); } try { await (0, _dbDynamodb.deleteItem)({ entity: esEntity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not delete form submission from Elasticsearch.", ex.code || "DELETE_FORM_SUBMISSION_ERROR", { submission, form, keys }); } return submission; }; /** * * We are using this method because it is faster to fetch the exact data from the DynamoDB than Elasticsearch. * * @internal */ const listSubmissionsByIds = async params => { const { where, sort } = params; const items = (where.id_in || []).map(id => { return entity.getBatch({ PK: createSubmissionPartitionKey({ ...where }), SK: createSubmissionSortKey(id) }); }); let results = []; try { results = await (0, _dbDynamodb.batchReadAll)({ table, items }); } catch (ex) { throw new _error.default(ex.message || "Could not batch read form submissions.", ex.code || "BATCH_READ_SUBMISSIONS_ERROR", { where, sort }); } /** * We need to remove empty results because it is a possibility that batch read returned null for non-existing record. */ const submissions = results.filter(Boolean).map(submission => { return (0, _cleanup.cleanupItem)(entity, submission); }); if (!sort) { return submissions; } return (0, _sort.sortItems)({ items: submissions, sort, fields: [] }); }; const listSubmissions = async params => { const { where, sort = [], limit: initialLimit, after } = params; if (where.id_in) { const items = await listSubmissionsByIds(params); return { items, meta: { hasMoreItems: false, cursor: null, totalCount: items.length } }; } const limit = (0, _apiElasticsearch.createLimit)(initialLimit); const body = (0, _elasticsearchBody.createElasticsearchBody)({ plugins, sort, limit: limit + 1, where, after: (0, _apiElasticsearch.decodeCursor)(after) }); const esConfig = _configurations.configurations.es({ tenant: where.tenant, locale: where.locale }); const query = { ...esConfig, body }; let response; try { response = await elasticsearch.search(query); } catch (ex) { throw new _error.default(ex.message || "Could list form submissions.", ex.code || "LIST_SUBMISSIONS_ERROR", { where, query }); } const { hits, total } = response.body.hits; const items = hits.map(item => item._source); const hasMoreItems = items.length > limit; if (hasMoreItems) { /** * Remove the last item from results, we don't want to include it. */ items.pop(); } /** * Cursor is the `sort` value of the last item in the array. * https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#search-after */ const meta = { hasMoreItems, totalCount: total.value, cursor: items.length > 0 ? (0, _apiElasticsearch.encodeCursor)(hits[items.length - 1].sort) || null : null }; return { items, meta }; }; const getSubmission = async params => { const { where } = params; const keys = { PK: createSubmissionPartitionKey({ ...where, formId: where.formId }), SK: createSubmissionSortKey(where.id) }; try { return await (0, _dbDynamodb.getClean)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not oad submission.", ex.code || "GET_SUBMISSION_ERROR", { where, keys }); } }; return { createSubmission, deleteSubmission, updateSubmission, listSubmissions, getSubmission, createSubmissionPartitionKey, createSubmissionSortKey }; }; exports.createSubmissionStorageOperations = createSubmissionStorageOperations; //# sourceMappingURL=index.js.map