Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions public/__redirects
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@
/durable-objects/api/hibernatable-websockets-api/ /durable-objects/best-practices/websockets/ 301
/durable-objects/api/alarms-in-durable-objects/ /durable-objects/api/alarms/ 301
/durable-objects/api/websockets/ /durable-objects/best-practices/websockets/ 301
/durable-objects/api/sql-storage/ /durable-objects/api/storage-api/ 301
/durable-objects/api/sql-storage/ /durable-objects/api/sqlite-storage-api/ 301
/durable-objects/platform/data-location/ /durable-objects/reference/data-location/ 301
/durable-objects/platform/environments/ /durable-objects/reference/environments/ 301
/durable-objects/platform/troubleshooting/ /durable-objects/observability/troubleshooting/ 301
Expand All @@ -531,7 +531,7 @@
/durable-objects/reference/error-handling/ /durable-objects/best-practices/error-handling/ 301
/durable-objects/reference/troubleshooting/ /durable-objects/observability/troubleshooting/ 301
/durable-objects/reference/websockets/ /durable-objects/best-practices/websockets/ 301
/durable-objects/api/transactional-storage-api/ /durable-objects/api/storage-api/ 301
/durable-objects/api/transactional-storage-api/ /durable-objects/api/sqlite-storage-api/ 301
/durable-objects/platform/changelog/ /durable-objects/release-notes/ 301
/durable-objects/changelog/ /durable-objects/release-notes/ 301
/durable-objects/glossary/ /durable-objects/reference/glossary/ 301
Expand All @@ -549,6 +549,7 @@
/durable-objects/get-started/video-series/ /durable-objects/video-tutorials/ 301

/durable-objects/what-are-durable-objects/ /durable-objects/concepts/what-are-durable-objects/ 301
/durable-objects/api/storage-api /durable-objects/api/sqlite-storage-api/ 301

# email-routing
/email-routing/enable-email-routing/ /email-routing/get-started/enable-email-routing/ 301
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ date: 2025-04-07

SQLite in Durable Objects is now generally available (GA) with 10GB SQLite database per Durable Object. Since the [public beta](https://blog.cloudflare.com/sqlite-in-durable-objects/) in September 2024, we've added feature parity and robustness for the SQLite storage backend compared to the preexisting key-value (KV) storage backend for Durable Objects.

SQLite-backed Durable Objects are recommended for all new Durable Object classes, using `new_sqlite_classes` [Wrangler configuration](/durable-objects/best-practices/access-durable-objects-storage/#create-sqlite-backed-durable-object-class). Only SQLite-backed Durable Objects have access to Storage API's [SQL](/durable-objects/api/storage-api/#sql-api) and [point-in-time recovery](/durable-objects/api/storage-api/#pitr-point-in-time-recovery-api) methods, which provide relational data modeling, SQL querying, and better data management.
SQLite-backed Durable Objects are recommended for all new Durable Object classes, using `new_sqlite_classes` [Wrangler configuration](/durable-objects/best-practices/access-durable-objects-storage/#create-sqlite-backed-durable-object-class). Only SQLite-backed Durable Objects have access to Storage API's [SQL](/durable-objects/api/sqlite-storage-api/#sql-api) and [point-in-time recovery](/durable-objects/api/sqlite-storage-api/#pitr-point-in-time-recovery-api) methods, which provide relational data modeling, SQL querying, and better data management.

```js
export class MyDurableObject extends DurableObject {
Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/agents/api-reference/schedule-tasks.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Calling `await this.schedule` returns a `Schedule`, which includes the task's ra

:::note[Maximum scheduled tasks]

Each task is mapped to a row in the Agent's underlying [SQLite database](/durable-objects/api/storage-api/), which means that each task can be up to 2 MB in size. The maximum number of tasks must be `(task_size * tasks) + all_other_state < maximum_database_size` (currently 1GB per Agent).
Each task is mapped to a row in the Agent's underlying [SQLite database](/durable-objects/api/sqlite-storage-api/), which means that each task can be up to 2 MB in size. The maximum number of tasks must be `(task_size * tasks) + all_other_state < maximum_database_size` (currently 1GB per Agent).

:::

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ Learn more about the zero-latency SQL storage that powers both Agents and Durabl

:::

The SQL API exposed to an Agent is similar to the one [within Durable Objects](/durable-objects/api/storage-api/#sql-api): Durable Object SQL methods available on `this.ctx.storage.sql`. You can use the same SQL queries with the Agent's database, create tables, and query data, just as you would with Durable Objects or [D1](/d1/).
The SQL API exposed to an Agent is similar to the one [within Durable Objects](/durable-objects/api/sqlite-storage-api/#sql-api): Durable Object SQL methods available on `this.ctx.storage.sql`. You can use the same SQL queries with the Agent's database, create tables, and query data, just as you would with Durable Objects or [D1](/d1/).

### Use Agent state as model context

Expand Down
2 changes: 1 addition & 1 deletion src/content/docs/durable-objects/api/alarms.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ class MyDurableObject extends DurableObject {

- Understand how to [use the Alarms API](/durable-objects/examples/alarms-api/) in an end-to-end example.
- Read the [Durable Objects alarms announcement blog post](https://blog.cloudflare.com/durable-objects-alarms/).
- Review the [Storage API](/durable-objects/api/storage-api/) documentation for Durable Objects.
- Review the [Storage API](/durable-objects/api/sqlite-storage-api/) documentation for Durable Objects.
63 changes: 63 additions & 0 deletions src/content/docs/durable-objects/api/legacy-kv-storage-api.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
title: KV-backed Durable Object Storage (Legacy)
pcx_content_type: concept
sidebar:
order: 7
---

import { Render, Type, MetaInfo, GlossaryTooltip, TypeScriptExample, Details } from "~/components";

:::note
This page documents the storage API for legacy KV-backed Durable Objects.

For the newer SQLite-backed Durable Object storage API, refer to [SQLite-backed Durable Object Storage](/durable-objects/api/sqlite-storage-api).
:::

<Render file="api-storage-introduction" product="durable-objects" />

<Render file="recommend-sqlite-do" product="durable-objects" />

## Access storage

Durable Objects gain access to Storage API via the `DurableObjectStorage` interface and accessed by the `DurableObjectState::storage` property. This is frequently accessed via `this.ctx.storage` with the `ctx` parameter passed to the Durable Object constructor.

The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.

<TypeScriptExample>
```ts
export class Counter extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}

async increment(): Promise<number> {
let value: number = (await this.ctx.storage.get("value")) || 0;
value += 1;
await this.ctx.storage.put("value", value);
return value;
}
}
```
</TypeScriptExample>

JavaScript is a single-threaded and event-driven programming language. This means that JavaScript runtimes, by default, allow requests to interleave with each other which can lead to concurrency bugs. The Durable Objects runtime uses a combination of <GlossaryTooltip term="input gate">input gates</GlossaryTooltip> and <GlossaryTooltip term="output gate">output gates</GlossaryTooltip> to avoid this type of concurrency bug when performing storage operations. Learn more in our [blog post](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/).

## Asynchronous KV API

KV-backed Durable Objects provide KV API methods which are asynchronous.

<Render file="api-async-kv-legacy" product="durable-objects" />

## Alarms

<Render file="api-storage-alarms" product="durable-objects" />

## Other

<Render file="api-storage-other-methods" product="durable-objects" />

## Related resources

- [Durable Objects: Easy, Fast, Correct Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/)
- [Zero-latency SQLite storage in every Durable Object blog](https://blog.cloudflare.com/sqlite-in-durable-objects/)
- [WebSockets API](/durable-objects/best-practices/websockets/)
210 changes: 210 additions & 0 deletions src/content/docs/durable-objects/api/sqlite-storage-api.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
---
title: SQLite-backed Durable Object Storage
pcx_content_type: concept
sidebar:
order: 6
---

import { Render, Type, MetaInfo, GlossaryTooltip, TypeScriptExample, Details } from "~/components";

:::note
This page documents the storage API for the newer SQLite-backed Durable Objects.

For the legacy KV-backed Durable Object storage API, see [KV-backed Durable Object Storage (Legacy)](/durable-objects/api/legacy-kv-storage-api/).
:::

<Render file="api-storage-introduction" product="durable-objects" />

<Render file="recommend-sqlite-do" product="durable-objects" />

<Render file="do-sqlite-storage-no-bill-note" product="durable-objects" />

## Access storage

Durable Objects gain access to Storage API via the `DurableObjectStorage` interface and accessed by the `DurableObjectState::storage` property. This is frequently accessed via `this.ctx.storage` with the `ctx` parameter passed to the Durable Object constructor.

The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.

<TypeScriptExample>
```ts
export class Counter extends DurableObject {
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
}

async increment(): Promise<number> {
let value: number = (await this.ctx.storage.get('value')) || 0;
value += 1;
await this.ctx.storage.put('value', value);
return value;
}

}

```
</TypeScriptExample>

JavaScript is a single-threaded and event-driven programming language. This means that JavaScript runtimes, by default, allow requests to interleave with each other which can lead to concurrency bugs. The Durable Objects runtime uses a combination of <GlossaryTooltip term="input gate">input gates</GlossaryTooltip> and <GlossaryTooltip term="output gate">output gates</GlossaryTooltip> to avoid this type of concurrency bug when performing storage operations. Learn more in our [blog post](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/).

## SQL API

The `SqlStorage` interface encapsulates methods that modify the SQLite database embedded within a Durable Object. The `SqlStorage` interface is accessible via the [`sql` property](/durable-objects/api/sqlite-storage-api/#sql) of `DurableObjectStorage` class.

For example, using `sql.exec()` a user can create a table and insert rows.

```ts
import { DurableObject } from "cloudflare:workers";

export class MyDurableObject extends DurableObject {
sql: SqlStorage;
constructor(ctx: DurableObjectState, env: Env) {
super(ctx, env);
this.sql = ctx.storage.sql;

this.sql.exec(`
CREATE TABLE IF NOT EXISTS artist(
artistid INTEGER PRIMARY KEY,
artistname TEXT
);
INSERT INTO artist (artistid, artistname) VALUES
(123, 'Alice'),
(456, 'Bob'),
(789, 'Charlie');
`);
}
}
```

- SQL API methods accessed with `ctx.storage.sql` are only allowed on [Durable Object classes with SQLite storage backend](/durable-objects/best-practices/access-durable-objects-storage/#create-sqlite-backed-durable-object-class) and will return an error if called on Durable Object classes with a KV-storage backend.
- When writing data, every row update of an index counts as an additional row. However, indexes may be beneficial for read-heavy use cases. Refer to [Index for SQLite Durable Objects](/durable-objects/best-practices/access-durable-objects-storage/#index-for-sqlite-durable-objects).
- Writing data to [SQLite virtual tables](https://www.sqlite.org/vtab.html) also counts towards rows written.

<Render file="d1-do-supported-sqlite-extensions" product="d1" params={{ product: "Durable Objects support" }}/>

### `exec`

<code>exec(query: <Type text="string" />, ...bindings: <Type text="any[]" />)</code>: <Type text="SqlStorageCursor" />

#### Parameters

- `query`: <Type text ='string' />
- The SQL query string to be executed. `query` can contain `?` placeholders for parameter bindings. Multiple SQL statements, separated with a semicolon, can be executed in the `query`. With multiple SQL statements, any parameter bindings are applied to the last SQL statement in the `query`, and the returned cursor is only for the last SQL statement.
- `...bindings`: <Type text='any[]' /> <MetaInfo text='Optional' />
- Optional variable number of arguments that correspond to the `?` placeholders in `query`.

#### Returns

A cursor (`SqlStorageCursor`) to iterate over query row results as objects. `SqlStorageCursor` is a JavaScript [Iterable](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol), which supports iteration using `for (let row of cursor)`. `SqlStorageCursor` is also a JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol), which supports iteration using `cursor.next()`.

`SqlStorageCursor` supports the following methods:

- `next()`
- Returns an object representing the next value of the cursor. The returned object has `done` and `value` properties adhering to the JavaScript [Iterator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol). `done` is set to `false` when a next value is present, and `value` is set to the next row object in the query result. `done` is set to `true` when the entire cursor is consumed, and no `value` is set.
- `toArray()`
- Iterates through remaining cursor value(s) and returns an array of returned row objects.
- `one()`
- Returns a row object if query result has exactly one row. If query result has zero rows or more than one row, `one()` throws an exception.
- `raw()`: <Type text='Iterator' />
- Returns an Iterator over the same query results, with each row as an array of column values (with no column names) rather than an object.
- Returned Iterator supports `next()` and `toArray()` methods above.
- Returned cursor and `raw()` iterator iterate over the same query results and can be combined. For example:

```ts
let cursor = this.sql.exec("SELECT * FROM artist ORDER BY artistname ASC;");
let rawResult = cursor.raw().next();

if (!rawResult.done) {
console.log(rawResult.value); // prints [ 123, 'Alice' ]
} else {
// query returned zero results
}

console.log(cursor.toArray()); // prints [{ artistid: 456, artistname: 'Bob' },{ artistid: 789, artistname: 'Charlie' }]
```

`SqlStorageCursor` has the following properties:

- `columnNames`: <Type text='string[]' />
- The column names of the query in the order they appear in each row array returned by the `raw` iterator.
- `rowsRead`: <Type text='number' />
- The number of rows read so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
- `rowsWritten`: <Type text='number' />
- The number of rows written so far as part of this SQL `query`. This may increase as you iterate the cursor. The final value is used for [SQL billing](/durable-objects/platform/pricing/#sqlite-storage-backend).
- Any numeric value in a column is affected by JavaScript's 52-bit precision for numbers. If you store a very large number (in `int64`), then retrieve the same value, the returned value may be less precise than your original number.

:::note[SQL transactions]
Note that `sql.exec()` cannot execute transaction-related statements like `BEGIN TRANSACTION` or `SAVEPOINT`. Instead, use the [`ctx.storage.transaction()`](/durable-objects/api/sqlite-storage-api/#transaction) or [`ctx.storage.transactionSync()`](/durable-objects/api/sqlite-storage-api/#transactionsync) APIs to start a transaction, and then execute SQL queries in your callback.
:::

#### Examples

<Render file="durable-objects-sql" product="durable-objects" />

### `databaseSize`

`databaseSize`: <Type text ='number' />

#### Returns

The current SQLite database size in bytes.

```ts
let size = ctx.storage.sql.databaseSize;
```

## PITR (Point In Time Recovery) API

For [SQLite-backed Durable Objects](/durable-objects/best-practices/access-durable-objects-storage/#create-sqlite-backed-durable-object-class), the following point-in-time-recovery (PITR) API methods are available to restore a Durable Object's embedded SQLite database to any point in time in the past 30 days. These methods apply to the entire SQLite database contents, including both the object's stored SQL data and stored key-value data using the key-value `put()` API. The PITR API is not supported in local development because a durable log of data changes is not stored locally.

The PITR API represents points in time using 'bookmarks'. A bookmark is a mostly alphanumeric string like `0000007b-0000b26e-00001538-0c3e87bb37b3db5cc52eedb93cd3b96b`. Bookmarks are designed to be lexically comparable: a bookmark representing an earlier point in time compares less than one representing a later point, using regular string comparison.

### `getCurrentBookmark`

<code>ctx.storage.getCurrentBookmark()</code>: <Type text="Promise<string>" />

- Returns a bookmark representing the current point in time in the object's history.

### `getBookmarkForTime`

<code>ctx.storage.getBookmarkForTime(timestamp: <Type text="number | Date" />)</code>: <Type text="Promise<string>" />

- Returns a bookmark representing approximately the given point in time, which must be within the last 30 days. If the timestamp is represented as a number, it is converted to a date as if using `new Date(timestamp)`.

### `onNextSessionRestoreBookmark`

<code>ctx.storage.onNextSessionRestoreBookmark(bookmark: <Type text="string" />)</code>: <Type text="Promise<string>" />

- Configures the Durable Object so that the next time it restarts, it should restore its storage to exactly match what the storage contained at the given bookmark. After calling this, the application should typically invoke `ctx.abort()` to restart the Durable Object, thus completing the point-in-time recovery.

This method returns a special bookmark representing the point in time immediately before the recovery takes place (even though that point in time is still technically in the future). Thus, after the recovery completes, it can be undone by performing a second recovery to this bookmark.

```ts
let now = new Date();
// restore to 2 days ago
let bookmark = ctx.storage.getBookmarkForTime(now - 2);
ctx.storage.onNextSessionRestoreBookmark(bookmark);
```

## Synchronous KV API

<Render file="api-sync-kv" product="durable-objects" />

## Alarms

<Render file="api-storage-alarms" product="durable-objects" />

## Other

<Render file="api-storage-other-methods" product="durable-objects" />

## Storage properties

### `sql`

`sql` is a readonly property of type `DurableObjectStorage` encapsulating the [SQL API](/durable-objects/api/sqlite-storage-api/#synchronous-sql-api).

## Related resources

- [Durable Objects: Easy, Fast, Correct Choose Three](https://blog.cloudflare.com/durable-objects-easy-fast-correct-choose-three/)
- [Zero-latency SQLite storage in every Durable Object blog](https://blog.cloudflare.com/sqlite-in-durable-objects/)
- [WebSockets API](/durable-objects/best-practices/websockets/)
Loading
Loading