Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

D1

Cloudflare D1 is a distributed SQL database built on SQLite for Workers.

Define any number of D1 databases in your schema, and create Models to represent tables in those databases.

Defining a D1 Binding

To define a D1 environment binding, use the d1 block. Any number of bindings may be listed inside it:

d1 {
    MyDb
}

Wrangler Configuration

A Wrangler configuration will be generated for each D1 binding defined in the schema:

[[d1_databases]]
binding = "MyDb"
database_id = "replace_with_MyDb_id"
database_name = "replace_with_MyDb_name"
migrations_dir = "./migrations/MyDb"

Migrations

To generate SQL migration files for a specific binding, run the following command:

cloesce migrate --binding <binding> <migration-name>

Apply the generated migrations for a D1 database using the Wrangler CLI:

npx wrangler d1 migrations apply <binding-name>