LogoShipTanStarter Docs

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:local

For local development, you can use the following command to open Drizzle Studio for local database management:

pnpm run db:studio:local

Create 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-name

Select no when prompted after the command executes successfully, then copy the database_id from the command output.

  1. Log in to the Cloudflare Dashboard
  2. Go to Storage & Databases > D1 SQL Databases
  3. Click Create Database
  4. Enter a database name, click Create
  5. 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:

wrangler.jsonc
"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:

.env
CLOUDFLARE_DATABASE_ID=your-database-id

(3) Initialize Remote Database

Run the following command to initialize the remote database:

pnpm run db:migrate:remote

Run the following command to view and manage the remote database:

pnpm run db:studio:remote

If 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:

CommandDescription
pnpm run db:generateGenerate Drizzle migration files
pnpm run db:pushPush schema changes to database (use in development)
pnpm run db:studio:localOpen Drizzle Studio (local database)
pnpm run db:studio:remoteOpen Drizzle Studio (remote database)
pnpm run db:migrate:localApply migrations (local database)
pnpm run db:migrate:remoteApply migrations (remote database)
pnpm run auth:schema:generateGenerate 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:

On this page