Skip to content

Commit 4f221fd

Browse files
committed
Updating DO storage code example.
1 parent 4f9650c commit 4f221fd

File tree

1 file changed

+7
-19
lines changed

1 file changed

+7
-19
lines changed

src/content/docs/durable-objects/api/storage-api.mdx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,13 @@ While access to a Durable Object is single-threaded, request executions can stil
2525
The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.
2626

2727
```js
28-
export class Counter {
29-
constructor(ctx, env) {
30-
this.ctx = ctx;
31-
}
32-
33-
async fetch(request) {
34-
let url = new URL(request.url);
35-
36-
// retrieve data
37-
let value = (await this.ctx.storage.get("value")) || 0;
38-
39-
// increment counter and get a new value
40-
value += 1;
41-
42-
// store data
43-
await this.ctx.storage.put("value", value);
44-
45-
return new Response(value);
46-
}
28+
export class Counter extends DurableObject {
29+
async increment() {
30+
let value = (await this.ctx.storage.get("value")) || 0;
31+
value += 1;
32+
await this.ctx.storage.put("value", value);
33+
return value;
34+
}
4735
}
4836
```
4937

0 commit comments

Comments
 (0)