UNPKG

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

Version:

The DynamoDB storage operations Webiny Page Builder API.

219 lines (217 loc) 5.94 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPageBlockStorageOperations = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _query = require("@webiny/db-dynamodb/utils/query"); var _sort = require("@webiny/db-dynamodb/utils/sort"); var _filter = require("@webiny/db-dynamodb/utils/filter"); var _dataLoader = require("./dataLoader"); var _listResponse = require("@webiny/db-dynamodb/utils/listResponse"); var _PageBlockDynamoDbFieldPlugin = require("../../plugins/definitions/PageBlockDynamoDbFieldPlugin"); var _keys = require("./keys"); var _dbDynamodb = require("@webiny/db-dynamodb"); var _compression = require("./compression"); const createType = () => { return "pb.pageBlock"; }; const createPageBlockStorageOperations = ({ entity, plugins }) => { const dataLoader = new _dataLoader.PageBlockDataLoader({ entity }); const get = async params => { const { where } = params; try { const pageBlock = await dataLoader.getOne(where); return (0, _compression.decompress)(pageBlock); } catch (ex) { throw new _error.default(ex.message || "Could not load page block by given parameters.", ex.code || "PAGE_BLOCK_GET_ERROR", { where }); } }; const list = async params => { const { where, sort, limit } = params; const { tenant, locale, blockCategory, ...restWhere } = where; const queryAllParams = { entity, partitionKey: (0, _keys.createGSIPartitionKey)({ tenant, locale }), options: blockCategory ? { index: "GSI1", beginsWith: `${blockCategory}#` } : { index: "GSI1", gt: " " } }; let items = []; try { items = await (0, _query.queryAll)(queryAllParams); } catch (ex) { throw new _error.default(ex.message || "Could not list page blocks by given parameters.", ex.code || "PAGE_BLOCK_LIST_ERROR", { partitionKey: queryAllParams.partitionKey, options: queryAllParams.options }); } const fields = plugins.byType(_PageBlockDynamoDbFieldPlugin.PageBlockDynamoDbFieldPlugin.type); const filteredItems = (0, _filter.filterItems)({ plugins, where: restWhere, items, fields }); const sortedItems = (0, _sort.sortItems)({ items: filteredItems, sort, fields }); return (0, _listResponse.createListResponse)({ items: await Promise.all(sortedItems.map(item => (0, _compression.decompress)(item))), limit: limit || 100000, totalCount: filteredItems.length, after: null }); }; const create = async params => { const { pageBlock } = params; const keys = { PK: (0, _keys.createPartitionKey)({ tenant: pageBlock.tenant, locale: pageBlock.locale, id: pageBlock.id }), SK: (0, _keys.createSortKey)(), GSI1_PK: (0, _keys.createGSIPartitionKey)({ tenant: pageBlock.tenant, locale: pageBlock.locale }), GSI1_SK: (0, _keys.createGSISortKey)({ blockCategory: pageBlock.blockCategory, id: pageBlock.id }) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...pageBlock, TYPE: createType(), ...keys, content: await (0, _compression.compress)(pageBlock.content) } }); /** * Always clear data loader cache when modifying the records. */ dataLoader.clear(); return pageBlock; } catch (ex) { throw new _error.default(ex.message || "Could not create page block.", ex.code || "PAGE_BLOCK_CREATE_ERROR", { keys }); } }; const update = async params => { const { original, pageBlock } = params; const keys = { PK: (0, _keys.createPartitionKey)({ tenant: original.tenant, locale: original.locale, id: pageBlock.id }), SK: (0, _keys.createSortKey)(), GSI1_PK: (0, _keys.createGSIPartitionKey)({ tenant: pageBlock.tenant, locale: pageBlock.locale }), GSI1_SK: (0, _keys.createGSISortKey)({ blockCategory: pageBlock.blockCategory, id: pageBlock.id }) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...pageBlock, TYPE: createType(), ...keys, content: await (0, _compression.compress)(pageBlock.content) } }); /** * Always clear data loader cache when modifying the records. */ dataLoader.clear(); return pageBlock; } catch (ex) { throw new _error.default(ex.message || "Could not update page block.", ex.code || "PAGE_BLOCK_UPDATE_ERROR", { keys, original, pageBlock }); } }; const deletePageBlock = async params => { const { pageBlock } = params; const keys = { PK: (0, _keys.createPartitionKey)({ tenant: pageBlock.tenant, locale: pageBlock.locale, id: pageBlock.id }), SK: (0, _keys.createSortKey)() }; try { await (0, _dbDynamodb.deleteItem)({ entity, keys }); /** * Always clear data loader cache when modifying the records. */ dataLoader.clear(); } catch (ex) { throw new _error.default(ex.message || "Could not delete page block.", ex.code || "PAGE_BLOCK_DELETE_ERROR", { keys, pageBlock }); } }; return { dataLoader, get, list, create, update, delete: deletePageBlock }; }; exports.createPageBlockStorageOperations = createPageBlockStorageOperations; //# sourceMappingURL=index.js.map