Skip to content

Commit 9ef2536

Browse files
authored
[DO] Get started to mention how to add to existing Worker (cloudflare#24552)
* Adding a note component to address adding DO to existing Worker * Adding required code block * Using bullets instead of numbers * Updating comment
1 parent 48ae5d7 commit 9ef2536

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/content/docs/durable-objects/get-started.mdx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,39 @@ Move into your new directory:
6262
cd durable-object-starter
6363
```
6464

65+
:::note[Adding a Durable Object to an existing Worker]
66+
67+
To add a Durable Object to an existing Worker, you need to:
68+
69+
- Modify the code of the existing Worker to include the following:
70+
71+
```ts
72+
export class MyDurableObject extends DurableObject<Env> {
73+
constructor(ctx: DurableObjectState, env: Env) {
74+
// Required, as we're extending the base class.
75+
super(ctx, env)
76+
}
77+
78+
{/* Define your Durable Object methods here */}
79+
80+
}
81+
82+
export default {
83+
async fetch(request, env, ctx): Promise<Response> {
84+
const id:DurableObjectId = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
85+
86+
const stub = env.MY_DURABLE_OBJECT.get(id);
87+
88+
{/* Access your Durable Object methods here */}
89+
90+
},
91+
} satisfies ExportedHandler<Env>;
92+
```
93+
94+
- Update the Wrangler configuration file of your existing Worker to bind the Durable Object to the Worker.
95+
96+
:::
97+
6598
## 2. Write a Durable Object class using SQL API
6699

67100
Before you create and access a Durable Object, its behavior must be defined by an ordinary exported JavaScript class.

0 commit comments

Comments
 (0)