UNPKG

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

Version:

The DynamoDB storage operations Webiny Page Builder API.

199 lines (197 loc) 4.68 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default; Object.defineProperty(exports, "__esModule", { value: true }); exports.createMenuStorageOperations = void 0; var _error = _interopRequireDefault(require("@webiny/error")); var _cleanup = require("@webiny/db-dynamodb/utils/cleanup"); var _query = require("@webiny/db-dynamodb/utils/query"); var _filter = require("@webiny/db-dynamodb/utils/filter"); var _sort = require("@webiny/db-dynamodb/utils/sort"); var _listResponse = require("@webiny/db-dynamodb/utils/listResponse"); var _MenuDynamoDbFieldPlugin = require("../../plugins/definitions/MenuDynamoDbFieldPlugin"); var _dbDynamodb = require("@webiny/db-dynamodb"); const createPartitionKey = params => { const { tenant, locale } = params; return `T#${tenant}#L#${locale}#PB#M`; }; const createSortKey = params => { const { slug } = params; return slug; }; const createType = () => { return "pb.menu"; }; const createMenuStorageOperations = ({ entity, plugins }) => { const get = async params => { const { where } = params; const keys = { PK: createPartitionKey(where), SK: createSortKey(where) }; try { return await (0, _dbDynamodb.getClean)({ entity, keys }); } catch (ex) { throw new _error.default(ex.message || "Could not load menu by given parameters.", ex.code || "MENU_GET_ERROR", { where }); } }; const create = async params => { const { menu } = params; const keys = { PK: createPartitionKey({ tenant: menu.tenant, locale: menu.locale }), SK: createSortKey(menu) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...menu, TYPE: createType(), ...keys } }); return menu; } catch (ex) { throw new _error.default(ex.message || "Could not create menu.", ex.code || "MENU_CREATE_ERROR", { keys, menu }); } }; const update = async params => { const { menu, original } = params; const keys = { PK: createPartitionKey({ tenant: menu.tenant, locale: menu.locale }), SK: createSortKey(menu) }; try { await (0, _dbDynamodb.put)({ entity, item: { ...menu, TYPE: createType(), ...keys } }); return menu; } catch (ex) { throw new _error.default(ex.message || "Could not update menu.", ex.code || "MENU_UPDATE_ERROR", { keys, original, menu }); } }; const list = async params => { const { where, sort, limit = 10 } = params; const { tenant, locale, ...restWhere } = where; const queryAllParams = { entity, partitionKey: createPartitionKey({ tenant, locale }), options: { limit: limit || undefined, gt: " " } }; let items = []; try { items = await (0, _query.queryAll)(queryAllParams); } catch (ex) { throw new _error.default(ex.message || "Could not list menus by given parameters.", ex.code || "MENUS_LIST_ERROR", { partitionKey: queryAllParams.partitionKey, options: queryAllParams.options }); } const fields = plugins.byType(_MenuDynamoDbFieldPlugin.MenuDynamoDbFieldPlugin.type); const filteredItems = (0, _filter.filterItems)({ plugins, where: restWhere, items, fields }).map(item => { return (0, _cleanup.cleanupItem)(entity, item); }); const sortedItems = (0, _sort.sortItems)({ items: filteredItems, sort, fields }); return (0, _listResponse.createListResponse)({ items: sortedItems, limit, totalCount: filteredItems.length, after: null }); }; const deleteMenu = async params => { const { menu } = params; const keys = { PK: createPartitionKey({ tenant: menu.tenant, locale: menu.locale }), SK: createSortKey(menu) }; try { await (0, _dbDynamodb.deleteItem)({ entity, keys }); return menu; } catch (ex) { throw new _error.default(ex.message || "Could not delete menu.", ex.code || "MENU_DELETE_ERROR", { keys, menu }); } }; return { get, create, update, list, delete: deleteMenu }; }; exports.createMenuStorageOperations = createMenuStorageOperations; //# sourceMappingURL=index.js.map