How D1 work with Drizzle ORM?
I created a svelte repo using create cloudflare cli.
I found
for the drizzle config. Isn't only for local sqlite db, isn't? How can I config for production D1 database? I know I have to configure D1 in wrangler file. But there is no need to configure d1 database inside drizzle config file? How about generate and migrate?
6 Replies
I mean like this in config file:
How can I keep both sqlite and d1 inside one config file?
@Sithu Khant https://orm.drizzle.team/docs/connect-cloudflare-d1
yeah, I saw that.
I found Drizzle orm doesn't have option to handle both dev and prod database at the same time, we have to manually configure ourself.
So, this is what I did (maybe to someone who is looking for):
in my package.json file:
you have to install
cross-env
package:
I chose to use d1 for both dev and prod, so I would get realtime production database features.to throw another example in the mix here, this is what we do for our template Hono app with Drizzle + D1: https://github.com/fiberplane/create-honc-app/blob/main/templates/d1/drizzle.config.ts
it's a little less elegant, does a manual search for the local D1 database in
.wrangler
to find the correct file. which makes our migration scripts look like this:
then we rely on having separate .dev.vars
and .prod.vars
files.
i gotta say, i rather like your solution @Sithu Khant – might end up updating our templates to use something like yours insteadGitHub
create-honc-app/templates/d1/drizzle.config.ts at main · fiberplane...
Template for setting up a new HONC stack project. Contribute to fiberplane/create-honc-app development by creating an account on GitHub.
hey, I am glad that my method isn't wired, I thought it is wired and not the ideal way to do so. But I am now confident enough!
btw, I just found one of your work colleagues shared about your works on hono server showcase channel. That is awesome!
okay yeah one of my colleagues double checked bc i didn't have time to work on the templates this week — for note: our template handles a local D1 if you're interested in that. my colleague pointed out that your setup has a remote Dev database and a remote Prod database. still a nice solution you found, definitely seems like it works best for that case