Skip to main content

@rtorcato/cf-common

Classes​

abstract BaseDurableObject​

Defined in: index.ts:59

A DurableObject base class whose this.storage is a TypedStorage over ctx.storage. It extends the runtime's DurableObject, so RPC methods, fetch, alarm and WebSocket handlers work as usual, and this.ctx and this.env are still there.

Example​

export class Counter extends BaseDurableObject<Env, { count: number }> {
async increment(): Promise<number> {
const count = (await this.storage.get('count', 0)) + 1
await this.storage.put('count', count)
return count
}
}

Extends​

  • DurableObject<Env>

Type Parameters​

Env​

Env = Cloudflare.Env

S​

S extends StorageSchema = StorageSchema

Constructors​

Constructor​

new BaseDurableObject<Env, S>(ctx, env): BaseDurableObject<Env, S>

Defined in: index.ts:65

Parameters​
ctx​

DurableObjectState

env​

Env

Returns​

BaseDurableObject<Env, S>

Overrides​

DurableObject<Env>.constructor

Properties​

storage​

protected readonly storage: TypedStorage<S>

Defined in: index.ts:63

Interfaces​

TypedStorage​

Defined in: index.ts:11

Key-checked access to a Durable Object's storage: each key only accepts and returns the value type its schema gives it. For list, transactions, alarms or SQL, use ctx.storage directly.

Type Parameters​

S​

S extends StorageSchema

Methods​

get()​
Call Signature​

get<K>(key): Promise<S[K] | undefined>

Defined in: index.ts:13

Read a key. Resolves to undefined if it has never been written.

Type Parameters​
K​

K extends string

Parameters​
key​

K

Returns​

Promise<S[K] | undefined>

Call Signature​

get<K>(key, fallback): Promise<S[K]>

Defined in: index.ts:15

Read a key, or fallback if it has never been written.

Type Parameters​
K​

K extends string

Parameters​
key​

K

fallback​

S[K]

Returns​

Promise<S[K]>

put()​

put<K>(key, value): Promise<void>

Defined in: index.ts:17

Write a key. The value is stored with structured clone, not JSON.

Type Parameters​
K​

K extends string

Parameters​
key​

K

value​

S[K]

Returns​

Promise<void>

delete()​

delete(key): Promise<boolean>

Defined in: index.ts:19

Delete a key. Resolves to true if it existed.

Parameters​
key​

keyof S & string

Returns​

Promise<boolean>

Type Aliases​

StorageSchema​

StorageSchema = Record<string, unknown>

Defined in: index.ts:4

Maps each storage key a Durable Object uses to the type of its value.

Functions​

typedStorage()​

typedStorage<S>(storage): TypedStorage<S>

Defined in: index.ts:31

Wrap a DurableObjectStorage so reads and writes are checked against a schema of key → value types.

Example​

const storage = typedStorage<{ count: number; owner: string }>(ctx.storage)
const count = await storage.get('count', 0)
await storage.put('count', count + 1)

Type Parameters​

S​

S extends StorageSchema

Parameters​

storage​

DurableObjectStorage

Returns​

TypedStorage<S>