A package for dynamic state tracking for Cloudflare's Durable Objects using SQLite.
# NPM
$ npm install --save diffable-objects
# Yarn
$ yarn add diffable-objects
# PNPM
$ pnpm add diffable-objects
# Bun
$ bun add diffable-objects
For complete examples see the examples directory.
Basic example of diffable-objects
.
import { DurableObject } from "cloudflare:workers";
export { diffable, state } from "diffable-objects";
export class SampleObject extends DurableObject {
// Within a durale object we can register a property to
// have its values automatically tracked and persisted.
#state = state(this.ctx, "state", { count: 0 });
increment() {
this.#state.count++;
}
}
// Currently requires wrangler@next
export class DecoratorObject extends DurableObject {
// You can also use decorators if you'd prefer a simpler
// (but more magic) syntax.
@diffable
#state = { count: 0 };
// Snapshot policies are configrable via an options object.
@diffable({ snapshotPolicy: "every-change" })
#stateWithOptions = { count: 0 };
increment() {
this.#state.count++;
this.#stateWithOptions.count++;
}
}
Distributed under the MIT License. See LICENSE for more information.