Database
Learn how to configure Cloudflare D1 database for your ShipTanStarter project
This guide covers database creation, initialization, and connecting to Cloudflare D1 database using Drizzle ORM.
Setup
Create and Initialize Local Database
The local D1 database can be automatically created when running the database initialization command. Run the following command to initialize the local database:
pnpm run db:migrate:localFor local development, you can use the following command to open Drizzle Studio for local database management:
pnpm run db:studio:localCreate and Initialize Remote Database
The remote D1 database needs to be created manually. Before creating it, you need to configure the Cloudflare API Token first.
(1) Configure Cloudflare API Token
Please complete the Cloudflare configuration first, ensuring the Token includes at least Account > D1 > Edit permission.
(2) Create Remote Database
You can create a remote D1 database via the Cloudflare Dashboard or Wrangler CLI:
Create a new D1 database:
pnpm wrangler d1 create your-database-nameSelect no when prompted after the command executes successfully, then copy the database_id from the command output.
- Log in to the Cloudflare Dashboard
- Go to Storage & Databases > D1 SQL Databases
- Click Create Database
- Enter a database name, click Create
- Once created, copy the Database ID from the database details page
After creation, update the database_id and database_name in your wrangler.jsonc file:
"d1_databases": [
{
"binding": "DB",
"database_name": "your-database-name", // Change to your database name
"database_id": "your-database-id", // Change to your database ID
"migrations_dir": "./src/db/migrations"
}
],Also add the database ID to your environment variables file:
CLOUDFLARE_DATABASE_ID=your-database-id(3) Initialize Remote Database
Run the following command to initialize the remote database:
pnpm run db:migrate:remoteRun the following command to view and manage the remote database:
pnpm run db:studio:remoteIf you are setting up your environment, you can now go back to the Environment Configuration and continue. The rest of this document can be read later.
Database Schema
The database schema is defined in the src/db/ directory:
src/db/auth.schema.ts- Better Auth schema (auto-generated)src/db/app.schema.ts- Application schema
The database includes the following tables:
- Users - User accounts and profiles
- Sessions - User authentication sessions
- Accounts - OAuth account links
- Verification - Email verification tokens
- API Keys - API key management
- Payments - Payment records and subscription tracking
- User Files - User uploaded file metadata
Database Commands
ShipTanStarter provides the following database management commands:
| Command | Description |
|---|---|
pnpm run db:generate | Generate Drizzle migration files |
pnpm run db:push | Push schema changes to database (use in development) |
pnpm run db:studio:local | Open Drizzle Studio (local database) |
pnpm run db:studio:remote | Open Drizzle Studio (remote database) |
pnpm run db:migrate:local | Apply migrations (local database) |
pnpm run db:migrate:remote | Apply migrations (remote database) |
pnpm run auth:schema:generate | Generate Better Auth schema |
References
Next Steps
Now that you understand how to set up a database in ShipTanStarter, you might want to explore these related features: