Skip to content

Commit 44e5466

Browse files
committed
Actors library alpha changelog
1 parent c225c20 commit 44e5466

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
title: @cloudflare/actors library: SDK for Durable Objects in alpha
3+
description: @cloudflare/actors library with Durable Objects helpers and patterns.
4+
products:
5+
- durable-objects
6+
- workers
7+
date: 2025-06-25T06:00:00Z
8+
---
9+
10+
[@cloudflare/actors](https://www.npmjs.com/package/@cloudflare/actors) package is in alpha and provides a SDK for Durable Objects, in addition to Durable Objects [Workers API](https://developers.cloudflare.com/durable-objects/api/base/). "Actors" originates from the [actor programming model](/durable-objects/what-are-durable-objects/#actor-programming-model), which is a close analogy for Durable Objects.
11+
12+
For alpha, `/actors` library includes:
13+
14+
* Storage helpers for using embeddeded, per-object SQLite storage, including SQL schema migrations and query template literals
15+
* Alarm helpers for scheduling multiple alarms provided a date, delay in seconds, or cron expression
16+
* `Actor` class for using Durable Objects with a defined pattern
17+
18+
Storage and alarm helper methods can be combined with [any Javascript class](https://github.com/cloudflare/actors?tab=readme-ov-file#storage--alarms-with-durableobject-class) that defines your Durable Object, i.e, ones that extend `DurableObject` including the `Actor` class.
19+
20+
```js
21+
import { Storage } from "@cloudflare/actors/storage";
22+
23+
export class ChatRoom extends DurableObject<Env> {
24+
storage: Storage;
25+
26+
constructor(ctx: DurableObjectState, env: Env) {
27+
super(ctx, env)
28+
this.storage = new Storage(ctx.storage);
29+
this.storage.migrations = [{
30+
idMonotonicInc: 1,
31+
description: "Create users table",
32+
sql: "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY)"
33+
}]
34+
}
35+
async fetch(request: Request): Promise<Response> {
36+
// Run migrations before executing SQL query
37+
await this.storage.runMigrations();
38+
39+
// Query with SQL template
40+
let userId = new URL(request.url).searchParams.get("userId");
41+
const query = this.storage.sql`SELECT * FROM users WHERE id = ${userId};`
42+
return new Response(`${JSON.stringify(query)}`);
43+
}
44+
}
45+
```
46+
47+
`/actors` library introduces the `Actor` class pattern. `Actor` lets you access Durable Objects without writing the Worker that communicates with your Durable Object (the Worker is created for you). By default, requests are routed to a Durable Object named "default".
48+
49+
```js
50+
export class MyActor extends Actor<Env> {
51+
async fetch(request: Request): Promise<Response> {
52+
return new Response('Hello, World!')
53+
}
54+
}
55+
56+
export default handler(MyActor);
57+
```
58+
59+
You can [route](/durable-objects/get-started/#3-instantiate-and-communicate-with-a-durable-object) to different Durable Objects by name within your `Actor` class using [`nameFromRequest`](https://github.com/cloudflare/actors?tab=readme-ov-file#actor-with-custom-name).
60+
61+
```js
62+
export class MyActor extends Actor<Env> {
63+
static nameFromRequest(request: Request): string {
64+
let url = new URL(request.url);
65+
return url.searchParams.get("userId") ?? "foo";
66+
}
67+
68+
async fetch(request: Request): Promise<Response> {
69+
return new Response(`Actor identifier (Durable Object name): ${this.identifier}`);
70+
}
71+
}
72+
73+
export default handler(MyActor);
74+
```
75+
76+
For more examples, check out the library [README](https://github.com/cloudflare/actors?tab=readme-ov-file#getting-started). `/actors` library is a place for more helpers and built-in patterns, e.g. retry handling and websocket helpers, to reduce development overhead for common Durable Objects functionality. Please share feedback and what more you would like to see on our [Discord channel](https://discord.com/channels/595317990191398933/773219443911819284).

0 commit comments

Comments
 (0)