Skip to content

Commit f4baf7f

Browse files
authored
[DO] Storage example code update (#18865)
* Updating DO storage code example. * Adding a TS version as well. * Removing spaces for Promise<number>.
1 parent 14b6c6b commit f4baf7f

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

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

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sidebar:
55
order: 6
66
---
77

8-
import { Render, Type, MetaInfo, GlossaryTooltip } from "~/components";
8+
import { Render, Type, MetaInfo, GlossaryTooltip, TypeScriptExample } from "~/components";
99

1010
The Durable Object Storage API allows <GlossaryTooltip term="Durable Object">Durable Objects</GlossaryTooltip> to access transactional and strongly consistent storage. A Durable Object's attached storage is private to its unique instance and cannot be accessed by other objects.
1111

@@ -24,28 +24,22 @@ While access to a Durable Object is single-threaded, request executions can stil
2424

2525
The following code snippet shows you how to store and retrieve data using the Durable Object Storage API.
2626

27-
```js
28-
export class Counter {
29-
constructor(ctx, env) {
30-
this.ctx = ctx;
27+
<TypeScriptExample>
28+
```ts
29+
export class Counter extends DurableObject {
30+
constructor(ctx: DurableObjectState, env: Env) {
31+
super(ctx, env);
3132
}
3233

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
34+
async increment(): Promise<number> {
35+
let value: number = (await this.ctx.storage.get('value')) || 0;
4036
value += 1;
41-
42-
// store data
43-
await this.ctx.storage.put("value", value);
44-
45-
return new Response(value);
37+
await this.ctx.storage.put('value', value);
38+
return value;
4639
}
4740
}
4841
```
42+
</TypeScriptExample>
4943

5044
## Methods
5145

0 commit comments

Comments
 (0)