@webiny/api-headless-cms-ddb
Version:
DynamoDB storage operations plugin for Headless CMS API.
174 lines (172 loc) • 4.14 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createGroupsStorageOperations = void 0;
var _error = _interopRequireDefault(require("@webiny/error"));
var _get = require("@webiny/db-dynamodb/utils/get");
var _query = require("@webiny/db-dynamodb/utils/query");
var _filter = require("@webiny/db-dynamodb/utils/filter");
var _ValueFilterPlugin = require("@webiny/db-dynamodb/plugins/definitions/ValueFilterPlugin");
var _sort = require("@webiny/db-dynamodb/utils/sort");
var _dbDynamodb = require("@webiny/db-dynamodb");
const createPartitionKey = params => {
const {
tenant,
locale
} = params;
return `T#${tenant}#L#${locale}#CMS#CMG`;
};
const createSortKeys = params => {
const {
id
} = params;
return id;
};
const createKeys = params => {
return {
PK: createPartitionKey(params),
SK: createSortKeys(params)
};
};
const createType = () => {
return "cms.group";
};
const createGroupsStorageOperations = params => {
const {
entity,
plugins
} = params;
const filteringPlugins = plugins.byType(_ValueFilterPlugin.ValueFilterPlugin.type);
if (filteringPlugins.length === 0) {
throw new _error.default("DynamoDB filtering plugins not loaded.", "MISSING_DYNAMODB_FILTERING_PLUGINS");
}
const create = async params => {
const {
group
} = params;
const keys = createKeys(group);
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...group,
TYPE: createType(),
...keys
}
});
return group;
} catch (ex) {
throw new _error.default(ex.message || "Could not create group.", ex.code || "CREATE_GROUP_ERROR", {
error: ex,
group,
keys
});
}
};
const update = async params => {
const {
group
} = params;
const keys = createKeys(group);
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...group,
TYPE: createType(),
...keys
}
});
return group;
} catch (ex) {
throw new _error.default(ex.message || "Could not update group.", ex.code || "UPDATE_GROUP_ERROR", {
error: ex,
group,
keys
});
}
};
const deleteGroup = async params => {
const {
group
} = params;
const keys = createKeys(group);
try {
await (0, _dbDynamodb.deleteItem)({
entity,
keys
});
return group;
} catch (ex) {
throw new _error.default(ex.message || "Could not delete group.", ex.code || "DELETE_GROUP_ERROR", {
error: ex,
group,
keys
});
}
};
const get = async params => {
const keys = createKeys(params);
try {
return await (0, _get.getClean)({
entity,
keys
});
} catch (ex) {
throw new _error.default(ex.message || "Could not get group.", ex.code || "GET_GROUP_ERROR", {
error: ex,
...params,
keys
});
}
};
const list = async params => {
const {
sort,
where
} = params;
const queryAllParams = {
entity,
partitionKey: createPartitionKey(where),
options: {
gte: " "
}
};
let records = [];
try {
records = await (0, _query.queryAll)(queryAllParams);
} catch (ex) {
throw new _error.default(ex.message || "Could not list groups.", ex.code || "LIST_GROUP_ERROR", {
error: ex,
...params,
sort,
where
});
}
const filteredItems = (0, _filter.filterItems)({
items: records,
where,
fields: [],
plugins
});
if (!sort || sort.length === 0) {
return filteredItems;
}
return (0, _sort.sortItems)({
items: filteredItems,
sort,
fields: []
});
};
return {
create,
update,
delete: deleteGroup,
get,
list
};
};
exports.createGroupsStorageOperations = createGroupsStorageOperations;
//# sourceMappingURL=index.js.map