Database Options
The starter template supports two database configurations:- Local Development: SQLite with a local file (
file:./local.db) - Production/Remote: Turso (libSQL) with a remote database URL
DATABASE_URL environment variable.
Configuration
Drizzle Config
The database configuration is defined indrizzle.config.ts:
drizzle.config.ts
- Points to schema files in
./src/db/schema - Outputs migrations to
./src/db/migrations - Uses the
tursodialect (compatible with SQLite) - Reads the database URL from the
DATABASE_URLenvironment variable
Database Connection
The database client is initialized insrc/db/index.ts:
src/db/index.ts
Database Schema
The starter includes two schema files:Authentication Schema
Defined insrc/db/schema/auth.ts, this schema contains all tables required by Better Auth:
src/db/schema/auth.ts
Todo Schema
Defined insrc/db/schema/todo.ts, this is an example application schema:
src/db/schema/todo.ts
You can add more schema files in the
src/db/schema directory. Drizzle will automatically detect them based on the configuration.Setup Steps
Install Dependencies
First, install all required dependencies:This installs:
drizzle-orm- ORM librarydrizzle-kit- CLI tool for migrations@libsql/client- Database client for SQLite/Turso
Configure Environment
Set up your For local development, use the SQLite file URL format. For production, use your Turso database URL.See the Environment Variables guide for more details.
.env file with the database URL:.env
Push Schema to Database
Push your schema to the database without generating migration files:This command applies your schema directly to the database. It’s perfect for local development and rapid prototyping.
Local Development with Turso
For local development, you can use Turso’s local development server which provides a SQLite-compatible interface:turso dev --db-file local.db, which:
- Creates a local SQLite database file (
local.db) - Starts a local server that emulates Turso’s API
- Allows you to develop against a local database with the same interface as production
The local Turso server is optional. You can also use a direct SQLite file connection with
DATABASE_URL=file:./local.db.Database Management
Drizzle Studio
Drizzle Kit includes a visual database browser called Drizzle Studio:- Browse your database tables and data
- Run queries
- Edit records directly
- Inspect relationships
Available Scripts
All database-related scripts are defined inpackage.json:
| Command | Description |
|---|---|
pnpm db:push | Push schema changes directly to the database (development) |
pnpm db:generate | Generate migration files from schema changes |
pnpm db:migrate | Apply generated migrations to the database |
pnpm db:studio | Open Drizzle Studio visual database browser |
pnpm db:local | Start local Turso development server |
Production Setup
Using Turso
For production, we recommend using Turso, a distributed SQLite database:Create a Turso Account
Sign up at turso.tech and install the Turso CLI.
Schema Modifications
When you modify your database schema:- Update the schema files in
src/db/schema/ - For local development:
- For production (with versioned migrations):
Adding New Tables
To add a new table to your database:- Create a new file in
src/db/schema/(e.g.,posts.ts):
src/db/schema/posts.ts
- Push the changes to your database:
Troubleshooting
Database Connection Errors
If you encounter connection errors:- Verify your
DATABASE_URLis set correctly in.env - For local development, ensure the database file path is accessible
- For Turso, verify your auth token is valid
Schema Sync Issues
If your database schema is out of sync:- Delete your local database file:
rm local.db - Run
pnpm db:pushto recreate it
Migration Conflicts
If you have migration conflicts:- Review the generated migrations in
src/db/migrations/ - Manually adjust if needed
- Run
pnpm db:migrateto apply them