UNPKG

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

Version:

The DynamoDB storage operations Webiny Page Builder API.

100 lines (98 loc) 2.17 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createSystemStorageOperations = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _dbDynamodb = require("@webiny/db-dynamodb"); const createPartitionKey = ({ tenant }) => { return `T#${tenant}#SYSTEM`; }; const createSortKey = () => { return "PB"; }; const createSystemStorageOperations = ({ entity }) => { const get = async params => { const { tenant } = params; const keys = { PK: createPartitionKey({ tenant }), SK: createSortKey() }; try { return await (0, _dbDynamodb.getClean)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not load system record.", ex.code || "SYSTEM_GET_ERROR", { keys }); } }; const create = async params => { const { system } = params; const keys = { PK: createPartitionKey(system), SK: createSortKey() }; try { await (0, _dbDynamodb.put)({ entity, item: { ...system, ...keys } }); return system; } catch (ex) { throw new _error.default(ex.message || "Could not create system record.", ex.code || "SYSTEM_CREATE_ERROR", { system, keys }); } }; const update = async params => { const { original, system } = params; const keys = { PK: createPartitionKey(original), SK: createSortKey() }; try { await (0, _dbDynamodb.put)({ entity, item: { ...system, ...keys } }); return system; } catch (ex) { throw new _error.default(ex.message || "Could not update system record.", ex.code || "SYSTEM_UPDATE_ERROR", { original, system, keys }); } }; return { get, create, update }; }; exports.createSystemStorageOperations = createSystemStorageOperations; //# sourceMappingURL=index.js.map