Skip to content

D1

Reference doc for the `sst.cloudflare.D1` component.

The D1 component lets you add a Cloudflare D1 database to your app.

Minimal example

const db = new sst.cloudflare.D1("MyDatabase");

You can link the db to a worker.

new sst.cloudflare.Worker("MyWorker", {
handler: "./index.ts",
link: [db],
url: true
});

Once linked, you can use the SDK to interact with the db.

index.ts
import { Resource } from "sst";
await Resource.MyDatabase.prepare(
"SELECT id FROM todo ORDER BY id DESC LIMIT 1",
).first();

Constructor

new D1(name, args?, opts?)

Parameters

D1Args

transform?

Type Object

Transform how this component creates its underlying resources.

transform.database?

Type D1DatabaseArgs | (args: D1DatabaseArgs => void)

Transform the D1 resource.

Properties

id

Type Output<string>

The generated id of the D1 database.

nodes

Type Object

The underlying resources this component creates.

nodes.database

Type D1Database

The Cloudflare D1 database.

SDK

The following are accessible through the SDK at runtime.

Bindings

when you link a D1 database, the database will be available to the worker and you can query it using the API methods documented here.

index.ts
import { Resource } from "sst";
await Resource.MyDatabase.prepare(
"SELECT id FROM todo ORDER BY id DESC LIMIT 1",
).first();