@webiny/api-page-builder-so-ddb
Version:
The DynamoDB storage operations Webiny Page Builder API.
189 lines (187 loc) • 4.75 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPageElementStorageOperations = void 0;
var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
var _error = _interopRequireDefault(require("@webiny/error"));
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 _PageElementDynamoDbFieldPlugin = require("../../plugins/definitions/PageElementDynamoDbFieldPlugin");
var _dbDynamodb = require("@webiny/db-dynamodb");
const createPartitionKey = params => {
const {
tenant,
locale
} = params;
return `T#${tenant}#L#${locale}#PB#PE`;
};
const createSortKey = params => {
const {
id
} = params;
return id;
};
const createType = () => {
return "pb.pageElement";
};
const createPageElementStorageOperations = ({
entity,
plugins
}) => {
const create = async params => {
const {
pageElement
} = params;
const keys = {
PK: createPartitionKey(pageElement),
SK: createSortKey(pageElement)
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...pageElement,
TYPE: createType(),
...keys
}
});
return pageElement;
} catch (ex) {
throw new _error.default(ex.message || "Could not create pageElement.", ex.code || "PAGE_ELEMENT_CREATE_ERROR", {
keys,
pageElement
});
}
};
const update = async params => {
const {
pageElement,
original
} = params;
const keys = {
PK: createPartitionKey(pageElement),
SK: createSortKey(pageElement)
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...pageElement,
TYPE: createType(),
...keys
}
});
return pageElement;
} catch (ex) {
throw new _error.default(ex.message || "Could not update pageElement.", ex.code || "PAGE_ELEMENT_UPDATE_ERROR", {
keys,
original,
pageElement
});
}
};
const deletePageElement = async params => {
const {
pageElement
} = params;
const keys = {
PK: createPartitionKey(pageElement),
SK: createSortKey(pageElement)
};
try {
await (0, _dbDynamodb.deleteItem)({
entity,
keys
});
} catch (ex) {
throw new _error.default(ex.message || "Could not delete pageElement.", ex.code || "PAGE_ELEMENT_DELETE_ERROR", {
keys,
pageElement
});
}
};
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 page element by given parameters.", ex.code || "PAGE_ELEMENT_GET_ERROR", {
where
});
}
};
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 results = [];
try {
results = await (0, _query.queryAll)(queryAllParams);
} catch (ex) {
throw new _error.default(ex.message || "Could not list page elements by given parameters.", ex.code || "PAGE_ELEMENTS_LIST_ERROR", {
partitionKey: queryAllParams.partitionKey,
options: queryAllParams.options
});
}
const fields = plugins.byType(_PageElementDynamoDbFieldPlugin.PageElementDynamoDbFieldPlugin.type);
const filteredItems = (0, _filter.filterItems)({
plugins,
where: restWhere,
items: results,
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
});
};
return {
get,
create,
update,
delete: deletePageElement,
list
};
};
exports.createPageElementStorageOperations = createPageElementStorageOperations;
//# sourceMappingURL=index.js.map