Current example was inspired by and created from twitch stream -> https://www.twitch.tv/videos/1704293186
In this stream @thdr tried to use Prisma + Kysely to setup automatic database migrations using sst
We have prepared simple MVP example of how to achieve both migrations and ORM functionality using just Drizzle ORM
Note: Current example just showing a possibility to use drizzle orm in serverless environment using AWS Lambda/Custom Resources/etc. In next versions of those examples we can setup Custom resources, by examples how SST Team already did here
We used create-sst bootstrap script
npx create-sst
Add drizzle dependencies to your project using pnpm
cd packages/core
pnpm i drizzle-orm@latest
pnpm i drizzle-kit -D
In this repo you may found same project structure as create-sst
script will generate. Just few things were added:
Current file contain drizzle AwsDataApi
connection setup + drizzle migrations setup. You can check more about drizzle-orm and drizzle-kit usage
import { RDSDataClient } from "@aws-sdk/client-rds-data";
import { drizzle } from "drizzle-orm/aws-data-api/pg";
import { migrate as mig } from "drizzle-orm/postgres-js/migrator";
import { RDS } from "sst/node/rds";
const rdsClient = new RDSDataClient({});
export const db = drizzle(rdsClient, {
database: RDS.rds.defaultDatabaseName,
secretArn: RDS.rds.secretArn,
resourceArn: RDS.rds.clusterArn,
});
export const migrate = async (path: string) => {
return mig(db, { migrationsFolder: path });
};
Current file contains basic table schema definition, that can be used in further queries
import { integer, pgTable, text } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: integer("id").primaryKey(),
name: text("name"),
});
- We have added 2 Lambda Functions
lambda.ts
-> check, that query to newly created database was invoked without errorsmigrator.ts
-> lambda, that currently invoked using API, to simulate migration process from lambda code
- Install dependencies
pnpm install
- Run the migration script in core package
cd packages/core
pnpm run migrate
- Run sst deploy or sst dev from the root of the project
pnpm run deploy
pnpm run dev
- From outputs use ApiGateway base url to invoke lambda functions. You can also run these endpoints from the SST console.
/migrate
- to simulate migration process/
- to check if query is working as expected
Core package has npm script migrate
. By running this script drizzle-kit
will generate new sql file in output folder, that was chosen in cli params
For more information you could check drizzle-kit docs and drizzle-orm docs