@webiny/api-page-builder-so-ddb
Version:
The DynamoDB storage operations Webiny Page Builder API.
148 lines (145 loc) • 3.47 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createSettingsStorageOperations = void 0;
var _get = require("@webiny/db-dynamodb/utils/get");
var _error = _interopRequireDefault(require("@webiny/error"));
var _dbDynamodb = require("@webiny/db-dynamodb");
/**
* Because it is a possibility that tenant and locale are set as false (for the global settings) we must take
* it in consideration and create the partition key for the global settings.
*/
const createPartitionKey = params => {
const {
tenant,
locale
} = params;
const parts = [];
if (tenant !== false) {
parts.push(`T#${tenant}`);
}
if (locale !== false) {
parts.push(`L#${locale}`);
}
parts.push("PB#SETTINGS");
return parts.join("#");
};
const createType = () => {
return "pb.settings";
};
const createSettingsStorageOperations = ({
entity
}) => {
const getDefaults = async () => {
const keys = {
PK: "PS#SETTINGS",
SK: "default"
};
try {
const result = await (0, _get.getClean)({
entity,
keys
});
if (!result) {
return null;
}
return {
websiteUrl: result.data.deliveryUrl,
websitePreviewUrl: result.data.appUrl
};
} catch (ex) {
throw new _error.default(ex.message || "Could not load default settings record.", ex.code || "DEFAULT_SETTINGS_GET_ERROR", {
keys
});
}
};
const get = async params => {
const {
where
} = params;
const keys = {
PK: createPartitionKey(where),
SK: "A"
};
try {
const result = await (0, _get.getClean)({
entity,
keys
});
return result?.data || null;
} catch (ex) {
throw new _error.default(ex.message || "Could not load settings record.", ex.code || "SETTINGS_GET_ERROR", {
keys
});
}
};
const create = async params => {
const {
settings
} = params;
const keys = {
PK: createPartitionKey(settings),
SK: "A"
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
data: settings,
TYPE: createType(),
...keys
}
});
return settings;
} catch (ex) {
throw new _error.default(ex.message || "Could not create settings record.", ex.code || "SETTINGS_CREATE_ERROR", {
settings,
keys
});
}
};
const update = async params => {
const {
original,
settings
} = params;
const keys = {
PK: createPartitionKey(settings),
SK: "A"
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
data: settings,
...keys,
TYPE: createType()
}
});
return settings;
} catch (ex) {
throw new _error.default(ex.message || "Could not update settings record.", ex.code || "SETTINGS_UPDATE_ERROR", {
original,
settings,
keys
});
}
};
/**
* We can simply return the partition key for this storage operations.
*/
const createCacheKey = params => {
return createPartitionKey(params);
};
return {
get,
getDefaults,
create,
update,
createCacheKey
};
};
exports.createSettingsStorageOperations = createSettingsStorageOperations;
//# sourceMappingURL=index.js.map