Skip to content

Commit ac051db

Browse files
committed
Adding a TS version as well.
1 parent 4f221fd commit ac051db

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

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

Lines changed: 14 additions & 8 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,16 +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
27+
<TypeScriptExample>
28+
```ts
2829
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-
}
30+
constructor(ctx: DurableObjectState, env: Env) {
31+
super(ctx, env);
32+
}
33+
34+
async increment(): Promise < number > {
35+
let value: number = (await this.ctx.storage.get('value')) || 0;
36+
value += 1;
37+
await this.ctx.storage.put('value', value);
38+
return value;
39+
}
3540
}
3641
```
42+
</TypeScriptExample>
3743

3844
## Methods
3945

0 commit comments

Comments
 (0)