feat: use D1 binding for local database
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
Clearity is a workspace, client, and contract management platform for managed office operations. It tracks clients, contacts, addresses, rooms, services, bookings, contracts, invoices, and workspace admins from a SvelteKit dashboard.
|
||||
|
||||
The app is built with SvelteKit, Svelte 5, shadcn-svelte, Superforms, Better Auth, Drizzle ORM, SQLite/libSQL for local development, Cloudflare D1 for production, and Cloudflare Workers.
|
||||
The app is built with SvelteKit, Svelte 5, shadcn-svelte, Superforms, Better Auth, Drizzle ORM, Cloudflare D1, and Cloudflare Workers.
|
||||
|
||||
## Development
|
||||
|
||||
@@ -15,7 +15,6 @@ pnpm install
|
||||
Create a local `.env` file:
|
||||
|
||||
```sh
|
||||
DATABASE_URL=file:local.db
|
||||
ORIGIN=http://localhost:5173
|
||||
BETTER_AUTH_SECRET=replace-with-a-long-random-secret
|
||||
```
|
||||
@@ -29,7 +28,7 @@ openssl rand -base64 32
|
||||
Apply database migrations:
|
||||
|
||||
```sh
|
||||
DATABASE_URL=file:local.db pnpm db:migrate
|
||||
pnpm db:migrate:local
|
||||
```
|
||||
|
||||
Start the development server:
|
||||
@@ -47,8 +46,8 @@ pnpm check # Wrangler types, SvelteKit sync, and svelte-check
|
||||
pnpm lint # Prettier check and ESLint
|
||||
pnpm format # Format the codebase
|
||||
pnpm db:generate # Generate Drizzle migrations after schema changes
|
||||
pnpm db:migrate # Apply Drizzle migrations to DATABASE_URL
|
||||
pnpm db:studio # Open Drizzle Studio
|
||||
pnpm db:migrate:local # Apply migrations to the local D1 database
|
||||
pnpm db:migrate:remote # Apply migrations to the remote D1 database
|
||||
pnpm gen # Regenerate Cloudflare Worker types
|
||||
```
|
||||
|
||||
@@ -56,40 +55,27 @@ pnpm gen # Regenerate Cloudflare Worker types
|
||||
|
||||
Required:
|
||||
|
||||
- `DATABASE_URL`: SQLite/libSQL connection URL for local development. Use `file:local.db`.
|
||||
- `ORIGIN`: Public app origin, for example `http://localhost:5173` locally or `https://clearity.example.com` in production.
|
||||
- `BETTER_AUTH_SECRET`: Secret used by Better Auth.
|
||||
- Cloudflare D1 binding `DB`, configured in `wrangler.jsonc`.
|
||||
|
||||
Production also requires the Cloudflare D1 binding named `DB`, configured in `wrangler.jsonc`. Do not set `DATABASE_URL` in production unless you intentionally want to use a hosted libSQL database instead of D1.
|
||||
Local development uses the same `DB` binding shape as production. SvelteKit's Cloudflare adapter populates `platform.env.DB` from `wrangler.jsonc` during dev and preview, and Wrangler stores local D1 data in its local state directory.
|
||||
|
||||
## Database
|
||||
|
||||
The application schema lives in `src/lib/server/db`. Drizzle migration files live in `drizzle`.
|
||||
|
||||
Runtime database selection is:
|
||||
|
||||
- If `DATABASE_URL` is set, Clearity uses the libSQL driver.
|
||||
- If `DATABASE_URL` is not set and the Cloudflare `DB` binding exists, Clearity uses Drizzle's Cloudflare D1 driver.
|
||||
|
||||
The fastest local database is a SQLite file:
|
||||
Clearity always uses Drizzle's Cloudflare D1 driver through the `DB` binding. Apply migrations to the local D1 database before starting the app:
|
||||
|
||||
```sh
|
||||
DATABASE_URL=file:local.db pnpm db:migrate
|
||||
pnpm db:migrate:local
|
||||
```
|
||||
|
||||
For schema changes:
|
||||
|
||||
```sh
|
||||
pnpm db:generate
|
||||
DATABASE_URL=file:local.db pnpm db:migrate
|
||||
```
|
||||
|
||||
To test against Wrangler's local D1 simulator instead, leave `DATABASE_URL` unset, apply migrations locally, and run the app through the Cloudflare Worker build:
|
||||
|
||||
```sh
|
||||
pnpm build
|
||||
pnpm wrangler d1 migrations apply clearity-production --local
|
||||
pnpm wrangler dev
|
||||
pnpm db:migrate:local
|
||||
```
|
||||
|
||||
## Production Deployment
|
||||
@@ -143,8 +129,6 @@ pnpm wrangler secret put ORIGIN
|
||||
pnpm wrangler secret put BETTER_AUTH_SECRET
|
||||
```
|
||||
|
||||
Production uses the `DB` binding by default. `DATABASE_URL` is only needed as a production secret if you intentionally deploy against a hosted libSQL database instead of D1.
|
||||
|
||||
Build and deploy:
|
||||
|
||||
```sh
|
||||
|
||||
Reference in New Issue
Block a user