mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
Add D1 Database (#335)
This commit is contained in:
@@ -3,6 +3,16 @@ import { defineConfig } from "drizzle-kit";
|
||||
export default defineConfig({
|
||||
schema: "./src/db/schema",
|
||||
out: "./src/db/migrations",
|
||||
{{#if (eq dbSetup "d1")}}
|
||||
// DOCS: https://orm.drizzle.team/docs/guides/d1-http-with-drizzle-kit
|
||||
dialect: "sqlite",
|
||||
driver: "d1-http",
|
||||
dbCredentials: {
|
||||
accountId: process.env.CLOUDFLARE_ACCOUNT_ID!,
|
||||
databaseId: process.env.CLOUDFLARE_DATABASE_ID!,
|
||||
token: process.env.CLOUDFLARE_D1_TOKEN!,
|
||||
},
|
||||
{{else}}
|
||||
dialect: "turso",
|
||||
dbCredentials: {
|
||||
url: process.env.DATABASE_URL || "",
|
||||
@@ -10,4 +20,5 @@ export default defineConfig({
|
||||
authToken: process.env.DATABASE_AUTH_TOKEN,
|
||||
{{/if}}
|
||||
},
|
||||
{{/if}}
|
||||
});
|
||||
|
||||
@@ -13,6 +13,12 @@ export const db = drizzle({ client });
|
||||
{{/if}}
|
||||
|
||||
{{#if (eq runtime "workers")}}
|
||||
{{#if (eq dbSetup "d1")}}
|
||||
import { drizzle } from "drizzle-orm/d1";
|
||||
import { env } from "cloudflare:workers";
|
||||
|
||||
export const db = drizzle(env.DB);
|
||||
{{else}}
|
||||
import { drizzle } from "drizzle-orm/libsql";
|
||||
import { env } from "cloudflare:workers";
|
||||
import { createClient } from "@libsql/client";
|
||||
@@ -26,3 +32,4 @@ const client = createClient({
|
||||
|
||||
export const db = drizzle({ client });
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
|
||||
@@ -5,14 +5,30 @@
|
||||
"compatibility_flags": ["nodejs_compat"],
|
||||
"vars": {
|
||||
"NODE_ENV": "production"
|
||||
// Non-sensitive environment variables (visible in dashboard)
|
||||
// "CORS_ORIGIN": "https://your-frontend-domain.com",
|
||||
// "BETTER_AUTH_URL": "https://your-worker-domain.workers.dev"
|
||||
// Add public environment variables here
|
||||
// Example: "CORS_ORIGIN": "https://your-domain.com"
|
||||
}
|
||||
// ⚠️ SENSITIVE DATA: Use `wrangler secret put` instead of adding here
|
||||
// Don't put these in "vars" - they'll be visible in the dashboard!
|
||||
// - DATABASE_URL
|
||||
// - DATABASE_AUTH_TOKEN
|
||||
// - GOOGLE_GENERATIVE_AI_API_KEY
|
||||
// - BETTER_AUTH_SECRET
|
||||
// For sensitive data, use:
|
||||
// wrangler secret put SECRET_NAME
|
||||
// Don't add secrets to "vars" - they're visible in the dashboard!
|
||||
|
||||
{{#if (eq dbSetup "d1")}},
|
||||
// To set up D1 database:
|
||||
// 1. Run: wrangler login
|
||||
// 2. Run: wrangler d1 create your-database-name
|
||||
// 3. Copy the output and paste below
|
||||
// Then run migrations:
|
||||
// bun db:generate
|
||||
// To apply migrations locally, run:
|
||||
// wrangler d1 migrations apply YOUR_DB_NAME --local
|
||||
"d1_databases": [
|
||||
{
|
||||
"binding": "DB",
|
||||
"database_name": "YOUR_DB_NAME",
|
||||
"database_id": "YOUR_DB_ID",
|
||||
"preview_database_id": "local-test-db",
|
||||
"migrations_dir": "./src/db/migrations"
|
||||
}
|
||||
]
|
||||
{{/if}}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user