diffable-objects
Version:
A package for dynamic state tracking for Cloudflare's Durable Objects using SQLite
80 lines (79 loc) • 2.42 kB
TypeScript
import type { DurableObject } from "cloudflare:workers";
import { type SnapshotPolicy } from "./index.js";
type FieldDecoratorFactoryReturn<T> = (value: T, metadata: {
kind: string;
name: string;
}) => FieldDecoratorReturn<T>;
type FieldDecoratorReturn<T> = (this: DurableObject, initialValue: T) => T;
type DiffableOpts = {
/**
* The name of the state, typically the name of the field.
*/
name?: string;
/**
* Diffable-objects will automatically snapshot the state perodically based on this policy to minimize
* the number of diffs that must be applied to restore the state when the Durable Object is restarted.
*/
snapshotPolicy?: SnapshotPolicy;
};
/**
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
*
* ```
* import { DurableObject } from "cloudflare:workers";
* import { diffable } from "diffable-objects";
*
* class Counter extends DurableObject {
* @diffable
* #state = { count: 0 };
*
* async fetch(request) {
* this.#state.count += 1;
* return new Response(`Count: ${this.#state.count}`);
* }
* }
* ```
*/
export declare function diffable(_: any, { kind, name }: {
kind: string;
name: string;
}): FieldDecoratorReturn<any>;
/**
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
*
* ```
* import { DurableObject } from "cloudflare:workers";
* import { diffable } from "diffable-objects";
*
* class Counter extends DurableObject {
* @diffable("counter")
* #state = { count: 0 };
*
* async fetch(request) {
* this.#state.count += 1;
* return new Response(`Count: ${this.#state.count}`);
* }
* }
* ```
*/
export declare function diffable(name?: string): FieldDecoratorFactoryReturn<any>;
/**
* Dynamically create a state object that persists changes to durable storage using Proxy and SQLite.
*
* ```
* import { DurableObject } from "cloudflare:workers";
* import { diffable } from "diffable-objects";
*
* class Counter extends DurableObject {
* @diffable({ name: "counter", snapshotPolicy: "never" })
* #state = { count: 0 };
*
* async fetch(request) {
* this.#state.count += 1;
* return new Response(`Count: ${this.#state.count}`);
* }
* }
* ```
*/
export declare function diffable(options: DiffableOpts): FieldDecoratorFactoryReturn<any>;
export {};