Ever wondered how to set up drizzle with RDS deployed with SST? Probably not but here it is anyway.
Update: April 2024
This was written for SST 2.0, if you're working with SST Ion there's a better way!
Setup
Firstly, setup a drizzle folder with your endpoints, I went for /drizzle
.
Then define two files and a folder: index.ts
, schema.ts
, /migrations
.
Here's a bash command to do that if you like copying and pasting random snippets from the internet:
We now want to create an RDS client, a drizzle client and a wee migration function.
As we're using SST we have to bind RDS to our api routes:
which then allows us to do:
Now we can define a schema, in this case we're just going to define a table for users:
Migrations
Now to add this to our table we're gonna need to generate some migrations and apply them, we need to create a drizzle config file so we can use drizzle-kit
Note: you can put these files basically anywhere you like
Then just run pnpm drizzle-kit generate:pg
and your migrations folder should now populate. If it doesn't, try experimenting with relative/absolute filepaths in your drizzle config until it works.
Ok so now we write our migrator function. In this case, we'll be putting it in an ApiHandler that we can then hit to run the migrations. You could just use drizzle-kit migrate
but the issue is that we'd need to define our connection parameters in our drizzle config. In previous Drizzle setups I've created a migration function and added it to package.json
, but we need to have SST running or we can't bind the RDS resource to Drizzle so the recommended method is to just create a migration endpoint.
I've just dropped the migration function into the index.ts:
Now we simply write an endpoint that we can hit:
and add it to our sst.config.ts
Smash that with postman or whatever and you'll get a successful migration. You can then query your db, how exciting!!
Sources
This config was heavily inspired by the sst-drizzle-example which itself was inspired by a @thdxr twitch stream.