UNPKG

@webiny/api-page-builder-import-export-so-ddb

Version:

The DynamoDB storage operations for import export feature in the Webiny Page Builder API.

376 lines (372 loc) 9.6 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createStorageOperations = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _query = require("@webiny/db-dynamodb/utils/query"); var _listResponse = require("@webiny/db-dynamodb/utils/listResponse"); var _table = require("./definitions/table"); var _importExportTaskEntity = require("./definitions/importExportTaskEntity"); var _dbDynamodb = require("@webiny/db-dynamodb"); const PARENT_TASK_GSI1_PK = "PB#IE_TASKS"; const createPartitionKey = ({ tenant, locale, id }) => { return `T#${tenant}#L#${locale}#PB#IE_TASK#${id}`; }; const createSortKey = input => { return `SUB#${input}`; }; const createGsiPartitionKey = ({ tenant, locale, id }) => { return `T#${tenant}#L#${locale}#PB#IE_TASK#${id}`; }; const createGsiSortKey = (status, id) => { return `S#${status}#${id}`; }; const createType = () => { return "pb.importExportTask"; }; const createStorageOperations = params => { const { table: tableName, documentClient, attributes = {} } = params; const table = (0, _table.createTable)({ table: tableName, documentClient }); const entity = (0, _importExportTaskEntity.createImportExportTaskEntity)({ entityName: "ImportExportTask", table, attributes }); return { getTable() { return table; }, getEntity() { return entity; }, async getTask(params) { const { where } = params; const keys = { PK: createPartitionKey(where), SK: "A" }; try { return await (0, _dbDynamodb.getClean)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not load element by given parameters.", ex.code || "IMPORT_EXPORT_TASK_GET_ERROR", { where }); } }, async listTasks(params) { const { limit = 100 } = params; const queryAllParams = { entity: entity, partitionKey: PARENT_TASK_GSI1_PK, options: { index: "GSI1", limit: limit || undefined } }; let results = []; try { results = await (0, _query.queryAllClean)(queryAllParams); } catch (ex) { throw new _error.default(ex.message || "Could not list import export tasks by given parameters.", ex.code || "IMPORT_EXPORT_TASKS_LIST_ERROR", { partitionKey: queryAllParams.partitionKey, options: queryAllParams.options }); } // TODO: Implement sort and filter return (0, _listResponse.createListResponse)({ items: results, limit, totalCount: results.length, after: null }); }, async createTask(params) { const { task } = params; const keys = { PK: createPartitionKey({ tenant: task.tenant, locale: task.locale, id: task.id }), SK: "A", GSI1_PK: PARENT_TASK_GSI1_PK, GSI1_SK: task.createdOn }; try { await (0, _dbDynamodb.put)({ entity, item: { ...task, TYPE: createType(), ...keys } }); return task; } catch (ex) { throw new _error.default(ex.message || "Could not create importExportTask.", ex.code || "IMPORT_EXPORT_TASK_CREATE_ERROR", { keys, importExportTask: task }); } }, async updateTask(params) { const { task, original } = params; const keys = { PK: createPartitionKey({ tenant: task.tenant, locale: task.locale, id: task.id }), SK: "A", GSI1_PK: PARENT_TASK_GSI1_PK, GSI1_SK: task.createdOn }; try { await (0, _dbDynamodb.put)({ entity, item: { ...task, TYPE: createType(), ...keys } }); return task; } catch (ex) { throw new _error.default(ex.message || "Could not update importExportTask.", ex.code || "IMPORT_EXPORT_TASK_UPDATE_ERROR", { keys, original, task: task }); } }, async deleteTask(params) { const { task } = params; const keys = { PK: createPartitionKey({ tenant: task.tenant, locale: task.locale, id: task.id }), SK: "A" }; try { await (0, _dbDynamodb.deleteItem)({ entity, keys }); return task; } catch (ex) { throw new _error.default(ex.message || "Could not delete importExportTask.", ex.code || "IMPORT_EXPORT_TASK_DELETE_ERROR", { keys, task: task }); } }, async updateTaskStats(params) { const { original, input: { prevStatus, nextStatus } } = params; const keys = { PK: createPartitionKey({ tenant: original.tenant, locale: original.locale, id: original.id }), SK: "A", GSI1_PK: PARENT_TASK_GSI1_PK, GSI1_SK: original.createdOn }; try { await (0, _dbDynamodb.update)({ entity, item: { TYPE: createType(), ...keys, stats: { $set: { [prevStatus]: { $add: -1 }, [nextStatus]: { $add: 1 } } } } }); return original; } catch (ex) { throw new _error.default(ex.message || "Could not update importExportTask.", ex.code || "IMPORT_EXPORT_TASK_UPDATE_ERROR", { keys, original }); } }, async createSubTask(params) { const { subTask } = params; const pkParams = { tenant: subTask.tenant, locale: subTask.locale, id: subTask.parent }; const keys = { PK: createPartitionKey(pkParams), SK: createSortKey(subTask.id), GSI1_PK: createGsiPartitionKey(pkParams), GSI1_SK: createGsiSortKey(subTask.status, subTask.id) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...subTask, TYPE: createType(), ...keys } }); return subTask; } catch (ex) { throw new _error.default(ex.message || "Could not create importExportSubTask.", ex.code || "CREATE_IMPORT_EXPORT_SUB_TASK_ERROR", { keys, subTask: subTask }); } }, async updateSubTask(params) { const { subTask, original } = params; const pkParams = { tenant: subTask.tenant, locale: subTask.locale, id: subTask.parent }; const keys = { PK: createPartitionKey(pkParams), SK: createSortKey(subTask.id), GSI1_PK: createGsiPartitionKey(pkParams), GSI1_SK: createGsiSortKey(subTask.status, subTask.id) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...subTask, TYPE: createType(), ...keys } }); return subTask; } catch (ex) { throw new _error.default(ex.message || "Could not update importExportSubTask.", ex.code || "UPDATE_IMPORT_EXPORT_SUB_TASK_ERROR", { keys, original, subTask: subTask }); } }, async getSubTask(params) { const { where } = params; const keys = { PK: createPartitionKey({ tenant: where.tenant, locale: where.locale, id: where.parent }), SK: createSortKey(where.id) }; try { return await (0, _dbDynamodb.getClean)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not load import export subTask by given parameters.", ex.code || "IMPORT_EXPORT_TASK_GET_ERROR", { where }); } }, async listSubTasks(params) { const { where, limit = 100 } = params; const { tenant, locale, parent, status } = where; const queryAllParams = { entity: entity, partitionKey: createGsiPartitionKey({ tenant, locale, id: parent }), options: { beginsWith: `S#${status}`, limit: limit || undefined, index: "GSI1" } }; let results = []; try { results = await (0, _query.queryAllClean)(queryAllParams); } catch (ex) { throw new _error.default(ex.message || "Could not list import export tasks by given parameters.", ex.code || "LIST_IMPORT_EXPORT_SUB_TASKS_ERROR", { partitionKey: queryAllParams.partitionKey, options: queryAllParams.options }); } return (0, _listResponse.createListResponse)({ items: results, limit, totalCount: results.length, after: null }); } }; }; exports.createStorageOperations = createStorageOperations; //# sourceMappingURL=index.js.map