How D1 work with Drizzle ORM?

I created a svelte repo using create cloudflare cli. I found
import { defineConfig } from 'drizzle-kit';
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');

export default defineConfig({
schema: './src/lib/server/db/schema.ts',

dbCredentials: {
url: process.env.DATABASE_URL
},

verbose: true,
strict: true,
dialect: 'sqlite'
});
import { defineConfig } from 'drizzle-kit';
if (!process.env.DATABASE_URL) throw new Error('DATABASE_URL is not set');

export default defineConfig({
schema: './src/lib/server/db/schema.ts',

dbCredentials: {
url: process.env.DATABASE_URL
},

verbose: true,
strict: true,
dialect: 'sqlite'
});
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
Sithu Khant
Sithu KhantOP2w ago
I mean like this in config file:
export default defineConfig({
out: `./src/lib/server/db/migrations`,
schema: "./src/lib/server/db/schemas",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
token: process.env.CLOUDFLARE_TOKEN!
},
verbose: true,
strict: true
});
export default defineConfig({
out: `./src/lib/server/db/migrations`,
schema: "./src/lib/server/db/schemas",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
token: process.env.CLOUDFLARE_TOKEN!
},
verbose: true,
strict: true
});
How can I keep both sqlite and d1 inside one config file?
Sithu Khant
Sithu KhantOP2w ago
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):
import "dotenv/config";
import { defineConfig } from "drizzle-kit";

const isProd = process.env.NODE_ENV === "production";

export default defineConfig({
out: `./src/lib/server/db/migrations-${isProd ? "prod" : "dev"}`,
schema: "./src/lib/server/db/schemas",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: isProd
? process.env.CLOUDFLARE_DATABASE_ID!
: process.env.CLOUDFLARE_DEV_DATABASE_ID!,
token: process.env.CLOUDFLARE_TOKEN!
},
verbose: true,
strict: true
});
import "dotenv/config";
import { defineConfig } from "drizzle-kit";

const isProd = process.env.NODE_ENV === "production";

export default defineConfig({
out: `./src/lib/server/db/migrations-${isProd ? "prod" : "dev"}`,
schema: "./src/lib/server/db/schemas",
dialect: "sqlite",
driver: "d1-http",
dbCredentials: {
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
databaseId: isProd
? process.env.CLOUDFLARE_DATABASE_ID!
: process.env.CLOUDFLARE_DEV_DATABASE_ID!,
token: process.env.CLOUDFLARE_TOKEN!
},
verbose: true,
strict: true
});
in my package.json file:
"db:generate": "cross-env NODE_ENV=development drizzle-kit generate",
"db:migrate": "cross-env NODE_ENV=development drizzle-kit migrate",
"db:studio": "cross-env NODE_ENV=development drizzle-kit studio",
"db:generate:prod": "cross-env NODE_ENV=production drizzle-kit generate",
"db:migrate:prod": "cross-env NODE_ENV=production drizzle-kit migrate",
"db:studio:prod": "cross-env NODE_ENV=production drizzle-kit studio"
"db:generate": "cross-env NODE_ENV=development drizzle-kit generate",
"db:migrate": "cross-env NODE_ENV=development drizzle-kit migrate",
"db:studio": "cross-env NODE_ENV=development drizzle-kit studio",
"db:generate:prod": "cross-env NODE_ENV=production drizzle-kit generate",
"db:migrate:prod": "cross-env NODE_ENV=production drizzle-kit migrate",
"db:studio:prod": "cross-env NODE_ENV=production drizzle-kit studio"
you have to install cross-env package:
npm i -D cross-env
npm i -D cross-env
I chose to use d1 for both dev and prod, so I would get realtime production database features.
boots
boots2w ago
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:
"db:migrate": "wrangler d1 migrations apply honc-d1-database --local",
"db:migrate:prod": "ENVIRONMENT=production drizzle-kit migrate",
"db:migrate": "wrangler d1 migrations apply honc-d1-database --local",
"db:migrate:prod": "ENVIRONMENT=production drizzle-kit migrate",
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 instead
GitHub
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.
Sithu Khant
Sithu KhantOP2w ago
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!
boots
boots2w ago
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

Did you find this page helpful?