43 lines
1 KiB
Markdown
43 lines
1 KiB
Markdown
# Database Helper - SQLite (Cloudflare D1)
|
|
|
|
This package contains database helpers and schema definitions for Cloudflare D1.
|
|
|
|
## Structure
|
|
|
|
- `src/db/` - Database source files
|
|
- `schema.ts` - Drizzle ORM schema definitions (SQLite)
|
|
- `db_index.ts` - D1 database initializer and client
|
|
- `types.ts` - Database types
|
|
- `seed.ts` - Database seeding script
|
|
- `porter.ts` - Data migration utilities
|
|
- `drizzle.config.ts` - Drizzle Kit configuration
|
|
|
|
## Environment Variables
|
|
|
|
Create a `.env` file with:
|
|
|
|
```
|
|
DATABASE_URL=file:./dev.db
|
|
```
|
|
|
|
## Initialization (Workers)
|
|
|
|
Use `initDb` with your D1 binding before calling helpers:
|
|
|
|
```typescript
|
|
import { initDb } from '@packages/db_helper_sqlite'
|
|
|
|
export default {
|
|
async fetch(request: Request, env: Env) {
|
|
initDb(env.DB)
|
|
// ... call helper methods
|
|
},
|
|
}
|
|
```
|
|
|
|
## Scripts
|
|
|
|
- `npm run migrate` - Generate new migration files (SQLite)
|
|
- `npm run db:push` - Push schema changes to database
|
|
- `npm run db:seed` - Run database seeding
|
|
- `npm run db:studio` - Open Drizzle Studio
|