Ability to call Drizzle-Kit push function via import
Apologies as I am a very new developer, so this may be a rather obvious problem.
I'm writing a multi-tenant SaaS application that uses schema seperation per user/company in the DB. When a user is created, I need to perform a drizzle-kit push to deploy the new schema. I was thinking it would be most elegant to integrate this into my user creation API endpoint, but am not sure how to actually execute these push functions as they seem to only be available via CLI. Can anyone shed some light?
SaaS application is hosted on vercel using vercel/postgres driver. Can move to pg driver if required.
2 Replies
Right now there isn't a way to use
pull
directly from code as far as I am aware. However, if you've run drizzle-kit generate
which creates the SQL migration files, you can then use the migrate
function to apply the migrations to a database.
I'm not sure it would apply for your use case (are you generating a new custom schema each time, or is it the same Drizzle schema just separated in the db?), but if you're always using the same Drizzle schema, that means you can just run drizzle-kit generate
once and then migrate()
should work perfectly when you need it.
If your use case is more complex than I'm assuming, I'd be happy to chat more about it to see where Drizzle could help you.
https://orm.drizzle.team/docs/migrationsDrizzle ORM - Migrations
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Thanks HEL, this seems like a good approach. I'm working on the implementation now. Seems like there are some limitations with regards to creating db relationships, and I've also just burned my db to the ground as I failed to specify the schemaName flag. But I'll keep you posted.
Alright so turns out, it doesn't seem there is actually a way to migrate to a schema outside of the default public one. The parameters for schemaName and schemaTable are to do with the drizzle-kit log that is written to the database when migrations are performed. There is no way to specify a schema name in the database connector and migrations are always performed against the public schema. That naming convention and the docs are super unclear on that. Is there actually no way to specify a target schema for migrations?