@webiny/api-headless-cms-ddb
Version:
DynamoDB storage operations plugin for Headless CMS API.
150 lines (148 loc) • 3.66 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createModelsStorageOperations = void 0;
var _error = _interopRequireDefault(require("@webiny/error"));
var _get = require("@webiny/db-dynamodb/utils/get");
var _cleanup = require("@webiny/db-dynamodb/utils/cleanup");
var _query = require("@webiny/db-dynamodb/utils/query");
var _dbDynamodb = require("@webiny/db-dynamodb");
const createPartitionKey = params => {
const {
tenant,
locale
} = params;
if (!tenant) {
throw new _error.default(`Missing tenant variable when creating model partitionKey.`);
} else if (!locale) {
throw new _error.default(`Missing locale variable when creating model partitionKey.`);
}
return `T#${tenant}#L#${locale}#CMS#CM`;
};
const createSortKey = params => {
return params.modelId;
};
const createKeys = params => {
return {
PK: createPartitionKey(params),
SK: createSortKey(params)
};
};
const createType = () => {
return "cms.model";
};
const createModelsStorageOperations = params => {
const {
entity
} = params;
const create = async params => {
const {
model
} = params;
const keys = createKeys(model);
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...(0, _cleanup.cleanupItem)(entity, model),
...keys,
TYPE: createType()
}
});
return model;
} catch (ex) {
throw new _error.default(`Could not create CMS Content Model.`, "CREATE_MODEL_ERROR", {
error: ex,
model,
keys
});
}
};
const update = async params => {
const {
model
} = params;
const keys = createKeys(model);
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...(0, _cleanup.cleanupItem)(entity, model),
...keys,
TYPE: createType()
}
});
return model;
} catch (ex) {
throw new _error.default(ex.message || "Could not update model.", ex.code || "MODEL_UPDATE_ERROR", {
error: ex,
model,
keys
});
}
};
const deleteModel = async params => {
const {
model
} = params;
const keys = createKeys(model);
try {
await (0, _dbDynamodb.deleteItem)({
entity,
keys
});
return model;
} catch (ex) {
throw new _error.default(ex.message || "Could not delete model.", ex.code || "MODEL_DELETE_ERROR", {
error: ex,
model,
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 model.", ex.code || "MODEL_GET_ERROR", {
error: ex,
keys
});
}
};
const list = async params => {
const {
where
} = params;
const queryAllParams = {
entity,
partitionKey: createPartitionKey(where),
options: {
gte: " "
}
};
try {
return await (0, _query.queryAllClean)(queryAllParams);
} catch (ex) {
throw new _error.default(ex.message || "Could not list models.", ex.code || "MODEL_LIST_ERROR", {
error: ex,
partitionKey: queryAllParams.partitionKey
});
}
};
return {
create,
update,
delete: deleteModel,
get,
list
};
};
exports.createModelsStorageOperations = createModelsStorageOperations;
//# sourceMappingURL=index.js.map