File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
src/content/docs/durable-objects Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,39 @@ Move into your new directory:
6262cd 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
67100Before you create and access a Durable Object, its behavior must be defined by an ordinary exported JavaScript class.
You can’t perform that action at this time.
0 commit comments