-
I am using Drizzle and Cloudflare D1 in my project and I like Drizzle a lot. And while I was trying Drizzle Studio, I found it connected to live database instead of local dev environment. I'd like to know if there is a way to connect to D1 local instance? |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 45 replies
-
Gm @JH365, please check this excerpt of drizzle-kit code for hints :P // src/orm-extenstions/d1-driver/wrangler-client.ts
var wrangler_client_exports = {};
__export(wrangler_client_exports, {
execute: () => execute
});
async function execute(query, configPath, dbName) {
try {
const response = await $`NO_D1_WARNING=true wrangler d1 execute ${dbName} --config=${configPath} --command=${query} --json`;
return { results: JSON.parse(response.stdout)[0].results };
} catch (e) {
try {
await $`NO_D1_WARNING=true wrangler d1 execute ${dbName} --config=${configPath} --command=${query}`;
} catch (e1) {
throw new Error(e1.stdout);
}
throw e;
}
} |
Beta Was this translation helpful? Give feedback.
-
Is there a clear consensus on how this should be addressed now? :) |
Beta Was this translation helpful? Give feedback.
-
Came up with a similar temp solution {
"scripts": {
"db:studio": "cross-env LOCAL_DB_PATH=$(find .wrangler/state/v3/d1/miniflare-D1DatabaseObject -type f -name '*.sqlite' -print -quit) drizzle-kit studio"
}
} Instead of specifying the sqlite file path, just grabbing the path of the import type { Config } from 'drizzle-kit';
export default process.env.LOCAL_DB_PATH
? ({
schema: './src/db/schema.ts',
driver: 'better-sqlite',
dbCredentials: {
url: process.env.LOCAL_DB_PATH!
}
} satisfies Config)
: ({
schema: './src/db/schema.ts',
out: './drizzle',
driver: 'd1',
dbCredentials: {
wranglerConfigPath: 'wrangler.toml',
dbName: 'NAME'
}
} satisfies Config); Haven't been able to get it working with the remote D1 db though https://github.com/drizzle-team/drizzle-kit-mirror/issues/289 |
Beta Was this translation helpful? Give feedback.
-
Joining the thread to get it working on Windows. Create $env:DB_URL=(Get-ChildItem .wrangler\state\v3\d1\miniflare-D1DatabaseObject\*.sqlite).FullName
drizzle-kit studio
export default {
schema: './src/lib/server/schema.ts',
out: './migrations',
driver: 'better-sqlite',
dbCredentials: {
url: process.env.DB_URL!
}
} satisfies Config;
"scripts": {
"db:studio": "powershell -ExecutionPolicy Bypass -File start-studio.ps1",
}, Repeat the same steps for unix environment or use cross-env |
Beta Was this translation helpful? Give feedback.
-
Thanks for sharing the tips, y'all! Here's the solution I came up with piecing everything together. I'm overall pretty happy with it since I can use Drizzle Studio with my local db, the preview db (for pages previews), and the prod db. https://kevinkipp.com/blog/going-full-stack-on-astro-with-cloudflare-d1-and-drizzle/ |
Beta Was this translation helpful? Give feedback.
-
This is now available in both Drizzle Studio for local development and with our Chrome Extension 🎉 |
Beta Was this translation helpful? Give feedback.
-
For the ORM: The simplest and cleanest way would be an orm connector which uses binding when available (in production) and fallsback to rest API when not available. This way we could still connect to a remote db when developing and still get the best performance while in production. For KIT: |
Beta Was this translation helpful? Give feedback.
-
in case anyone else had troubles wiring things up from scratch (bc they didn't have a local D1 database present in npx wrangler d1 execute DB_NAME --local --command='SELECT 1' this created the necessary files in |
Beta Was this translation helpful? Give feedback.
-
So which would be the official way to setup thedrizzle.config.ts with local and production env? |
Beta Was this translation helpful? Give feedback.
-
Here's what works for me //drizzle.config.ts
import type { Config } from 'drizzle-kit';
import fs from "fs";
import path from "path";
const getLocalD1 = () => {
try {
const basePath = path.resolve('.wrangler');
const dbFile = fs
.readdirSync(basePath, { encoding: 'utf-8', recursive: true })
.find((f) => f.endsWith('.sqlite'));
if (!dbFile) {
throw new Error(`.sqlite file not found in ${basePath}`);
}
const url = path.resolve(basePath, dbFile);
return url;
} catch (err) {
console.log(`Error ${err}`);
}
}
const isProd = () => process.env.NODE_ENV === 'production'
const getCredentials = () => {
const prod = {
driver: 'd1-http',
dbCredentials: {
accountId: process.env.CLOUDFLARE_D1_ACCOUNT_ID,
databaseId: 'xxx',
token: process.env.CLOUDFLARE_D1_API_TOKEN
}
}
const dev = {
dbCredentials: {
url: getLocalD1()
}
}
return isProd() ? prod : dev
}
export default {
schema: './src/schemas/*.ts',
out: './drizzle',
dialect: "sqlite",
...getCredentials()
} satisfies Config; |
Beta Was this translation helpful? Give feedback.
This is now available in both Drizzle Studio for local development and with our Chrome Extension 🎉