simple-state-model
v2.1.0
Published
Simple State Model — transport- and storage-agnostic core: config/state controllers, the RFC6902 mutation queue, and the provider interface
Readme
simple-state-model
Transport- and storage-agnostic core of the Simple State Model. It has no knowledge of Postgres, GraphQL, or the network — everything external plugs in through a provider.
Concepts
| Concept | What it is |
| --- | --- |
| ConfigController | Holds a value. Emits value and status. Set with setValue(). |
| StateController | A ConfigController whose value is changed with RFC6902 patches. Emits patch alongside value. |
| StateControllerMutationQueue | Tracks provisional vs. accepted values so a client can apply optimistic patches and roll them back. |
| ConfigModel / StateModel | Registry of named providers. load(providerID, options) returns a controller. |
| BaseConfigProvider / BaseStateProvider | The interface a storage backend implements: load(), has(), delete(). |
Install
npm install simple-state-modelUsage
import { StateModel } from "simple-state-model";
const stateModel = new StateModel({ logger: console });
// in-memory provider is registered as "inmemory" and is the default
const stateController = await stateModel.load(undefined, { id: "element-1" });
stateController.on("patch", (patch) => console.log("changed", patch));
stateController.patch([
{ op: "add", path: "/title", value: "Hello" }
]);
console.log(stateController.value); // { title: "Hello" }Controller modes
StateController runs in one of two modes:
AUTHORITATIVE(default) — patches are accepted the moment they are queued. Use this on a server, or anywhere this process owns the truth.CLIENT— patches stay provisional until something accepts them. Use this in a browser so a local edit shows immediately but can be rolled back if the server rejects it.
import { StateController } from "simple-state-model";
const stateController = new StateController({ mode: StateController.MODES.CLIENT });
const mutation = stateController.patch([{ op: "add", path: "/title", value: "Hello" }]);
mutation.accept(); // or mutation.reject({ error })Writing a provider
A provider maps a state ID to a controller instance and decides where the value lives.
import { BaseStateProvider } from "simple-state-model";
class MyStateProvider extends BaseStateProvider {
async load(options) {
const stateID = options.id;
// return a cached controller, or build one from your backing store
return new this.controllerClass({ id: stateID, value: await this.read(stateID) });
}
async has(options) { /* ... */ }
async delete(options) { /* ... */ }
}See simple-state-model-postgres for a full implementation.
Config permissions (optional)
ConfigPermissions gives the config providers you choose an owner per config (whoever created it), grants
of actions to users and roles, and access requests. Providers without a policy are left as they were.
import { ConfigPermissions, InMemoryPermissionsStore } from "simple-state-model";
const permissions = new ConfigPermissions({
store: new InMemoryPermissionsStore(), // or PostgresPermissionsStore
adminRoleIDs: ["admin"], // may do anything
policies: {
// owner-only until shared; edit implies read; only the owner and admins delete
workspaces: { label: "workspace", actions: ["read", "edit"] },
widgets: { label: "widget", actions: ["read", "edit"] }
}
});The rules, in order:
- Administrators and the owner may do anything.
- An action nobody has been granted follows the policy's
openByDefault: open to everyone, or closed to all but the owner. - Otherwise the caller needs a grant of the action, or of one that implies it, by name or by role.
Whoever holds an action may grant it on and take it away. A policy's carries names the configs a grant
also reaches (the widgets in a workspace, say), as far as the granter holds the action on them; taking
the grant away takes those too. A request for an action goes to the owner, or to holders the requester
names, and granting it grants the requester. ConfigPermissions emits access_request and
access_request_resolved so you can tell people.
Hand it to ConfigSubscriptionModel (simple-state-model-graphql-api) as permissions to enforce it on
reads, lists, saves, deletes and subscriptions. The rules themselves (permits, mayGrant, ...) are
exported on their own for things that are not configs.
Related packages
simple-state-model-postgres— Postgres-backed providersimple-state-model-graphql-api— GraphQL schema and resolverssimple-state-model-graphql-client— GraphQL client
