Skip to content

Commit ab2114d

Browse files
[DO] Improving Getting started for DO. (#21901)
* Improving Getting started for DO. * Apply suggestions from code review Co-authored-by: Maddy <[email protected]> --------- Co-authored-by: Maddy <[email protected]>
1 parent a824092 commit ab2114d

File tree

1 file changed

+40
-4
lines changed

1 file changed

+40
-4
lines changed

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel
7272

7373
Your `MyDurableObject` class will have a constructor with two parameters. The first parameter, `ctx`, passed to the class constructor contains state specific to the Durable Object, including methods for accessing storage. The second parameter, `env`, contains any bindings you have associated with the Worker when you uploaded it.
7474

75-
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
75+
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
7676

7777
```js
7878
export class MyDurableObject extends DurableObject {
@@ -97,7 +97,7 @@ Workers communicate with a Durable Object using [remote-procedure call](/workers
9797

9898
Your file should now look like:
9999

100-
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
100+
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
101101

102102
```js
103103
export class MyDurableObject extends DurableObject {
@@ -151,7 +151,7 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre
151151

152152
To communicate with a Durable Object, the Worker's fetch handler should look like the following:
153153

154-
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
154+
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">
155155

156156
```js
157157
export default {
@@ -258,7 +258,43 @@ Once deployed, you should be able to see your newly created Durable Object Worke
258258

259259
Preview your Durable Object Worker at `<YOUR_WORKER>.<YOUR_SUBDOMAIN>.workers.dev`.
260260

261-
By finishing this tutorial, you have successfully created, tested and deployed a Durable Object.
261+
## Summary and final code
262+
263+
Your final code should look like this:
264+
265+
```ts title="index.ts"
266+
import { DurableObject } from "cloudflare:workers";
267+
export class MyDurableObject extends DurableObject {
268+
constructor(ctx: DurableObjectState, env: Env) {
269+
// Required, as we are extending the base class.
270+
super(ctx, env)
271+
}
272+
273+
async sayHello() {
274+
let result = this.ctx.storage.sql
275+
.exec("SELECT 'Hello, World!' as greeting")
276+
.one();
277+
return result.greeting;
278+
}
279+
}
280+
export default {
281+
async fetch(request, env, ctx): Promise<Response> {
282+
let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname);
283+
284+
let stub = env.MY_DURABLE_OBJECT.get(id);
285+
286+
let greeting = await stub.sayHello();
287+
288+
return new Response(greeting);
289+
},
290+
} satisfies ExportedHandler<Env>;
291+
```
292+
293+
By finishing this tutorial, you have:
294+
295+
- Successfully created a Durable Object
296+
- Called the Durable Object by invoking a [RPC method](/workers/runtime-apis/rpc/)
297+
- Deployed the Durable Object globally
262298

263299
## Related resources
264300

0 commit comments

Comments
 (0)