AWS Aurora DSQL with Drizzle
In this example, we use Drizzle ORM with an Aurora DSQL cluster.
const cluster = new sst.aws.Dsql("MyCluster");And link it to a Lambda function.
new sst.aws.Function("MyApi", { handler: "src/api.handler", link: [cluster], url: true,});Push the Drizzle schema to the database.
sst shell -- bun run push.tsNow in the function we can connect to the cluster using Drizzle with the DSQL connector. Learn more about DSQL Node.js connectors.
import { drizzle } from "drizzle-orm/node-postgres";import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector";import { Resource } from "sst";
const pool = new AuroraDSQLPool({ host: Resource.MyCluster.endpoint, user: "admin",});
export const db = drizzle(pool, { schema });const cluster = new sst.aws.Dsql("MyCluster");
new sst.aws.Function("MyApi", { handler: "src/api.handler", link: [cluster], url: true,});import { db } from "./drizzle";import { todo } from "./todo.sql";import { APIGatewayProxyEventV2 } from "aws-lambda";
export const handler = async (evt: APIGatewayProxyEventV2) => { if (evt.requestContext.http.method === "GET") { const result = await db.select().from(todo).execute();
return { statusCode: 200, body: JSON.stringify(result, null, 2), }; }
if (evt.requestContext.http.method === "POST") { const result = await db .insert(todo) .values({ title: "Todo", description: crypto.randomUUID() }) .returning() .execute();
return { statusCode: 200, body: JSON.stringify(result), }; }};View the full example.