From 8f115ac97e63f99bafa840ca623556efa2bd965f Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Tue, 22 Apr 2025 17:49:02 +0100 Subject: [PATCH 1/2] Improving Getting started for DO. --- .../docs/durable-objects/get-started.mdx | 44 +++++++++++++++++-- 1 file changed, 40 insertions(+), 4 deletions(-) diff --git a/src/content/docs/durable-objects/get-started.mdx b/src/content/docs/durable-objects/get-started.mdx index 8365fb5e2debee8..a0600690f593a4c 100644 --- a/src/content/docs/durable-objects/get-started.mdx +++ b/src/content/docs/durable-objects/get-started.mdx @@ -72,7 +72,7 @@ If you do not use JavaScript or TypeScript, you will need a [shim](https://devel 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. - + ```js export class MyDurableObject extends DurableObject { @@ -97,7 +97,7 @@ Workers communicate with a Durable Object using [remote-procedure call](/workers Your file should now look like: - + ```js export class MyDurableObject extends DurableObject { @@ -151,7 +151,7 @@ A Worker is used to [access Durable Objects](/durable-objects/best-practices/cre To communicate with a Durable Object, the Worker's fetch handler should look like the following: - + ```js export default { @@ -258,7 +258,43 @@ Once deployed, you should be able to see your newly created Durable Object Worke Preview your Durable Object Worker at `..workers.dev`. -By finishing this tutorial, you have successfully created, tested and deployed a Durable Object. +## Summary and final code + +You final code should look like this: + +```ts title="index.ts" +import { DurableObject } from "cloudflare:workers"; +export class MyDurableObject extends DurableObject { + constructor(ctx: DurableObjectState, env: Env) { + // Required, as we're extending the base class. + super(ctx, env) + } + + async sayHello() { + let result = this.ctx.storage.sql + .exec("SELECT 'Hello, World!' as greeting") + .one(); + return result.greeting; + } +} +export default { + async fetch(request, env, ctx): Promise { + let id = env.MY_DURABLE_OBJECT.idFromName(new URL(request.url).pathname); + + let stub = env.MY_DURABLE_OBJECT.get(id); + + let greeting = await stub.sayHello(); + + return new Response(greeting); + }, +} satisfies ExportedHandler; +``` + +By finishing this tutorial, you have: + +- Successfully created a Durable Object +- Called the Durable Object by invoking a RPC method +- Deployed the Durable Object globally ## Related resources From e1d81d22c89df6554f254b85bf84b89ba8c2c8bb Mon Sep 17 00:00:00 2001 From: Jun Lee Date: Tue, 22 Apr 2025 18:11:54 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Maddy <130055405+Maddy-Cloudflare@users.noreply.github.com> --- src/content/docs/durable-objects/get-started.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/content/docs/durable-objects/get-started.mdx b/src/content/docs/durable-objects/get-started.mdx index a0600690f593a4c..1825da1ab383047 100644 --- a/src/content/docs/durable-objects/get-started.mdx +++ b/src/content/docs/durable-objects/get-started.mdx @@ -260,13 +260,13 @@ Preview your Durable Object Worker at `..workers.de ## Summary and final code -You final code should look like this: +Your final code should look like this: ```ts title="index.ts" import { DurableObject } from "cloudflare:workers"; export class MyDurableObject extends DurableObject { constructor(ctx: DurableObjectState, env: Env) { - // Required, as we're extending the base class. + // Required, as we are extending the base class. super(ctx, env) } @@ -293,7 +293,7 @@ export default { By finishing this tutorial, you have: - Successfully created a Durable Object -- Called the Durable Object by invoking a RPC method +- Called the Durable Object by invoking a [RPC method](/workers/runtime-apis/rpc/) - Deployed the Durable Object globally ## Related resources