Skip to content

D1

D1 is a SQLite SQL database that you query from a Worker. On this platform, each database is a local-primary SQLite file on the node running ocd.

For example, you can use D1 for:

  • Querying relational data from a Worker
  • Importing a schema and running SQL
  • Batching statements in one transaction
ts
export default {
  async fetch(request: Request, env: Env): Promise<Response> {
    const { results } = await env.DB.prepare(
      "SELECT * FROM Customers WHERE CompanyName = ?",
    ).bind("Bs Beverages").all();
    return Response.json(results);
  },
} satisfies ExportedHandler<{ DB: D1Database }>;

Bind an existing database in open-compute.json. Ordinary product bindings are { type, id, permissions? }:

json
{
  "name": "d1-app",
  "main": "src/index.ts",
  "bindings": {
    "DB": { "type": "d1_database", "id": "<d1-database-id>" }
  }
}

id is an existing database on this platform. Binding grammar: bindings. The CLI is oc / oc run / oc types.

Compatibility

TopicCloudflareopen-compute
Worker APID1 Worker APISame: prepare / bind / run / all / first / raw / exec / batch, sessions, opaque bookmarks, prepared-statement / result / meta
TopologyHosted D1 with read replicasLocal primary SQLite on the node running ocd
Read replicasAvailableNot provided
Region routingAvailableNot provided
served_by geographyRegion / colo metadataNot provided; served_by_* is not a geography product
BookmarksCross-replica causalityLocal ordering on the same database
rows_read / rows_writtenBilling countersLocal SQLite execution counts
dump()Rejected on hosted non-alphaRejected (D1_DUMP_ERROR)
REST / client.v4AvailableNot provided; use the Worker binding

Next