@seasketch/geoprocessing
Version:
Geoprocessing and reporting framework for SeaSketch 2.0
87 lines • 3.83 kB
JavaScript
import { RemovalPolicy } from "aws-cdk-lib";
import { AttributeType, BillingMode, Table } from "aws-cdk-lib/aws-dynamodb";
/**
* Create database tables
*/
export const createTables = (stack) => {
const tables = {
tasks: undefined,
estimates: undefined,
subscriptions: undefined,
};
if (stack.getProcessingFunctions().length > 0) {
tables.tasks = new Table(stack, `GpTasksTable`, {
partitionKey: { name: "id", type: AttributeType.STRING },
sortKey: { name: "service", type: AttributeType.STRING },
billingMode: BillingMode.PAY_PER_REQUEST,
tableName: `gp-${stack.props.projectName}-tasks`,
removalPolicy: RemovalPolicy.DESTROY,
});
tables.estimates = new Table(stack, `GpEstimatesTable`, {
partitionKey: {
name: "service",
type: AttributeType.STRING,
},
billingMode: BillingMode.PAY_PER_REQUEST,
tableName: `gp-${stack.props.projectName}-estimates`,
removalPolicy: RemovalPolicy.DESTROY,
});
}
if (stack.getAsyncFunctionMetas().length > 0) {
tables.subscriptions = new Table(stack, "GpSubscriptionsTable", {
partitionKey: {
name: "connectionId",
type: AttributeType.STRING,
},
billingMode: BillingMode.PAY_PER_REQUEST,
tableName: `gp-${stack.props.projectName}-subscriptions`,
removalPolicy: RemovalPolicy.DESTROY,
});
}
return tables;
};
/** Setup function access to tables */
export const setupTableFunctionAccess = (stack) => {
// sync
for (const syncFunctionWithMeta of stack.getSyncFunctionsWithMeta()) {
if (stack.tables.tasks) {
stack.tables.tasks.grantReadWriteData(syncFunctionWithMeta.func);
syncFunctionWithMeta.func.addEnvironment("TASKS_TABLE", stack.tables.tasks.tableName);
}
if (stack.tables.estimates) {
stack.tables.estimates.grantReadWriteData(syncFunctionWithMeta.func);
syncFunctionWithMeta.func.addEnvironment("ESTIMATES_TABLE", stack.tables.estimates.tableName);
}
}
// async
for (const asyncFunctionWithMeta of stack.getAsyncFunctionsWithMeta()) {
if (stack.tables.tasks) {
stack.tables.tasks.grantReadWriteData(asyncFunctionWithMeta.startFunc);
stack.tables.tasks.grantReadWriteData(asyncFunctionWithMeta.runFunc);
}
if (stack.tables.estimates) {
stack.tables.estimates.grantReadWriteData(asyncFunctionWithMeta.startFunc);
stack.tables.estimates.grantReadWriteData(asyncFunctionWithMeta.runFunc);
}
addAsyncEnv(stack, asyncFunctionWithMeta.startFunc);
addAsyncEnv(stack, asyncFunctionWithMeta.runFunc);
}
// socket
for (const socketFunction of Object.values(stack.projectFunctions.socketFunctions)) {
if (socketFunction && stack.tables.subscriptions) {
stack.tables.subscriptions.grantReadWriteData(socketFunction);
socketFunction.addEnvironment("SUBSCRIPTIONS_TABLE", stack.tables.subscriptions.tableName);
}
}
if (stack.projectFunctions.socketFunctions.send && stack.tables.estimates)
stack.tables.estimates.grantReadWriteData(stack.projectFunctions.socketFunctions.send);
};
const addAsyncEnv = (stack, func) => {
if (stack.tables.tasks)
func.addEnvironment("TASKS_TABLE", stack.tables.tasks.tableName);
if (stack.tables.estimates)
func.addEnvironment("ESTIMATES_TABLE", stack.tables.estimates.tableName);
if (stack.tables.subscriptions)
func.addEnvironment("SUBSCRIPTIONS_TABLE", stack.tables.subscriptions.tableName);
};
//# sourceMappingURL=dynamodb.js.map