Skip to content

Table

Source: src/AWS/DynamoDB/Table.ts

An Amazon DynamoDB table with optional indexes, PITR, TTL, and stream-aware binding support.

Table owns the lifecycle of the physical table while the binding contract allows runtime-specific integrations such as Lambda table event sources to request stream configuration without forcing a circular input prop.

Basic Table

import * as DynamoDB from "alchemy/AWS/DynamoDB";
const table = yield* DynamoDB.Table("UsersTable", {
partitionKey: "pk",
attributes: {
pk: "S",
},
});

Table with Sort Key and TTL

const table = yield* DynamoDB.Table("SessionsTable", {
partitionKey: "userId",
sortKey: "sessionId",
attributes: {
userId: "S",
sessionId: "S",
expiresAt: "N",
},
timeToLiveSpecification: {
Enabled: true,
AttributeName: "expiresAt",
},
});

Table with Global Secondary Index

const table = yield* DynamoDB.Table("OrdersTable", {
partitionKey: "pk",
sortKey: "sk",
attributes: {
pk: "S",
sk: "S",
gsi1pk: "S",
gsi1sk: "S",
},
globalSecondaryIndexes: [{
IndexName: "GSI1",
KeySchema: [
{ AttributeName: "gsi1pk", KeyType: "HASH" },
{ AttributeName: "gsi1sk", KeyType: "RANGE" },
],
Projection: { ProjectionType: "ALL" },
}],
});

Bind DynamoDB operations in the init phase and use them in runtime handlers. Bindings inject the table name and grant scoped IAM permissions automatically.

// init
const getItem = yield* DynamoDB.GetItem.bind(table);
const putItem = yield* DynamoDB.PutItem.bind(table);
return {
fetch: Effect.gen(function* () {
// runtime
yield* putItem({
Item: { pk: { S: "user#123" }, name: { S: "Alice" } },
});
const result = yield* getItem({
Key: { pk: { S: "user#123" } },
});
return yield* HttpServerResponse.json(result.Item);
}),
};

Process change data capture events from a DynamoDB table using a Lambda event source mapping. The stream is enabled automatically through the binding contract.

// init
yield* DynamoDB.streams(table, {
StreamViewType: "NEW_AND_OLD_IMAGES",
}).process(
Effect.fn(function* (record) {
yield* Effect.log(`${record.eventName}: ${JSON.stringify(record.dynamodb)}`);
}),
);