Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions src/content/docs/durable-objects/get-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">

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

Your file should now look like:

<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">

```js
export class MyDurableObject extends DurableObject {
Expand Down Expand Up @@ -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:

<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
<Tabs syncKey="language"> <TabItem label="JavaScript" icon="seti:javascript">

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

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

By finishing this tutorial, you have successfully created, tested and deployed a Durable Object.
## Summary and final code

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 are 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<Response> {
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<Env>;
```

By finishing this tutorial, you have:

- Successfully created a Durable Object
- Called the Durable Object by invoking a [RPC method](/workers/runtime-apis/rpc/)
- Deployed the Durable Object globally

## Related resources

Expand Down
Loading