@webiny/api-apw-scheduler-so-ddb
Version:
The DynamoDB storage operations for scheduler action feature in the Webiny APW API.
298 lines (296 loc) • 7.83 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createStorageOperations = 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 _listResponse = require("@webiny/db-dynamodb/utils/listResponse");
var _table = require("./definitions/table");
var _scheduleActionEntity = require("./definitions/scheduleActionEntity");
var _filter = require("@webiny/db-dynamodb/utils/filter");
var _ApwSchedulerScheduleActionDynamoDbFieldPlugin = require("./plugins/ApwSchedulerScheduleActionDynamoDbFieldPlugin");
var _plugins = require("@webiny/plugins");
var _fields = require("./definitions/fields");
var _filters = _interopRequireDefault(require("@webiny/db-dynamodb/plugins/filters"));
var _dbDynamodb = require("@webiny/db-dynamodb");
const createStorageOperations = params => {
const {
table: tableName,
documentClient,
attributes = {}
} = params;
const plugins = new _plugins.PluginsContainer([(0, _fields.createFields)(), (0, _filters.default)()]);
const table = (0, _table.createTable)({
table: tableName,
documentClient
});
const entity = (0, _scheduleActionEntity.createScheduleActionsEntity)({
entityName: "ApwScheduleAction",
table,
attributes
});
const PK = `APW#SA`;
function createPartitionKey({
tenant,
locale,
id
}) {
return `T#${tenant}#L#${locale}#${PK}#${id}`;
}
function createCurrentTaskPartitionKey({
tenant,
locale
}) {
return `T#${tenant}#L#${locale}#${PK}#CURRENT`;
}
function createType() {
return "apw.scheduleAction";
}
return {
async get(params) {
const {
where
} = params;
const keys = {
PK: createPartitionKey(where),
SK: "A"
};
try {
return await (0, _dbDynamodb.getClean)({
entity,
keys
});
} catch (ex) {
throw new _error.default(ex.message || "Could not load schedule action by given parameters.", ex.code || "SCHEDULE_ACTION_GET_ERROR", {
where
});
}
},
async list(params) {
const {
limit = 100,
sort = [],
where: initialWhere
} = params;
const queryAllParams = {
entity: entity,
partitionKey: PK,
options: {
beginsWith: initialWhere?.datetime_startsWith || undefined,
index: "GSI1",
limit: limit || undefined,
reverse: sort[0] === "datetime_DESC"
}
};
let results = [];
try {
results = await (0, _query.queryAll)(queryAllParams);
} catch (ex) {
throw new _error.default(ex.message || "Could not list schedule actions by given parameters.", ex.code || "SCHEDULE_ACTIONS_LIST_ERROR", {
partitionKey: queryAllParams.partitionKey,
options: queryAllParams.options
});
}
const where = {
...initialWhere
};
delete where.datetime_startsWith;
const apwSchedulerDynamoDbFields = plugins.byType(_ApwSchedulerScheduleActionDynamoDbFieldPlugin.ApwSchedulerScheduleActionDynamoDbFieldPlugin.type);
const filteredItems = (0, _filter.filterItems)({
plugins,
items: results,
where,
fields: apwSchedulerDynamoDbFields
});
return (0, _listResponse.createListResponse)({
items: (0, _cleanup.cleanupItems)(entity, filteredItems),
limit,
totalCount: filteredItems.length,
after: null
});
},
async create(params) {
const {
item
} = params;
const {
tenant,
locale,
id,
data
} = item;
const keys = {
PK: createPartitionKey({
tenant: tenant,
locale: locale,
id
}),
SK: "A",
GSI1_PK: PK,
GSI1_SK: data.datetime
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...item,
TYPE: createType(),
...keys
}
});
return item;
} catch (ex) {
throw new _error.default(ex.message || "Could not create schedule action item.", ex.code || "SCHEDULE_ACTION_CREATE_ERROR", {
keys,
params
});
}
},
async update(params) {
const {
item
} = params;
const {
tenant,
locale,
id,
data
} = item;
const keys = {
PK: createPartitionKey({
tenant: tenant,
locale: locale,
id: id
}),
SK: "A",
GSI1_PK: PK,
GSI1_SK: data.datetime
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...item,
TYPE: createType(),
...keys
}
});
return item;
} catch (ex) {
throw new _error.default(ex.message || "Could not update schedule action.", ex.code || "SCHEDULE_ACTION__UPDATE_ERROR", {
keys,
params
});
}
},
async delete(params) {
const {
tenant,
locale,
id
} = params;
const keys = {
PK: createPartitionKey({
tenant: tenant,
locale: locale,
id: id
}),
SK: "A"
};
try {
await (0, _dbDynamodb.deleteItem)({
entity,
keys
});
return true;
} catch (ex) {
throw new _error.default(ex.message || "Could not delete schedule action.", ex.code || "SCHEDULE_ACTION__DELETE_ERROR", {
keys,
params
});
}
},
async getCurrentTask(params) {
const {
where
} = params;
const keys = {
PK: createCurrentTaskPartitionKey(where),
SK: "A"
};
try {
return await (0, _dbDynamodb.getClean)({
entity,
keys
});
} catch (ex) {
throw new _error.default(ex.message || "Could not load current schedule action by given parameters.", ex.code || "CURRENT_SCHEDULE_ACTION_GET_ERROR", {
where
});
}
},
async updateCurrentTask(params) {
const {
item
} = params;
const {
tenant,
locale
} = item;
const PK = createCurrentTaskPartitionKey({
tenant: tenant,
locale: locale
});
const keys = {
PK: PK,
SK: "A"
};
try {
await (0, _dbDynamodb.put)({
entity,
item: {
...item,
TYPE: createType(),
...keys
}
});
return item;
} catch (ex) {
throw new _error.default(ex.message || "Could not update current schedule action.", ex.code || "CURRENT_SCHEDULE_ACTION__UPDATE_ERROR", {
keys,
params
});
}
},
async deleteCurrentTask(params) {
const {
tenant,
locale
} = params;
const keys = {
PK: createCurrentTaskPartitionKey({
tenant: tenant,
locale: locale
}),
SK: "A"
};
try {
await (0, _dbDynamodb.deleteItem)({
entity,
keys
});
return true;
} catch (ex) {
throw new _error.default(ex.message || "Could not delete current schedule action.", ex.code || "CURRENT_SCHEDULE_ACTION__DELETE_ERROR", {
keys,
params
});
}
}
};
};
exports.createStorageOperations = createStorageOperations;
//# sourceMappingURL=index.js.map