Skip to content

Storage: change notifications

Some adapters detect their own changes and implement watch; the rest need a change signal supplied. watch drives push-based snapshot refresh in the runtime provider — without it the provider polls every refreshIntervalMs (fine for the source plane, which is off the evaluation path).

Adapter Built-in watch? How
memory ✅ yes microtask callbacks
file ✅ yes fs.watch
redis ✅ yes keyspace notifications (notify-keyspace-events KEA)
postgres, drizzle, mongodb, sqlite, libsql, unstorage, cloudflare-kv ❌ no wrap with withWatch (below)

watch is orthogonal to which storage you use, so it’s a composable wrapper rather than an adapter option. withWatch(storage, subscribe) returns a WatchableFlagsStorage that delegates all reads/writes to storage and drives watch from whatever change source you provide.

withWatch does not detect writes by itself — it needs a real change signal you plug in (a Postgres NOTIFY, an ORM write-hook, a Redis message, a websocket, …). If a backend has no change feed to subscribe to (e.g. Cloudflare KV), there’s nothing to wire and it stays polling-only.

You can wrap an already-watchable adapter (memory/file/redis) — reads/writes still delegate — but the wrapper’s watch replaces the adapter’s built-in one (your source wins; there’s no double-subscription). Normally you’d only wrap adapters that lack watch; wrap a watchable one only to deliberately swap its signal (e.g. drive redis storage off your app’s own pub/sub instead of keyspace notifications).

import { withWatch } from "@xtandard/flags";
// Bring your own source — an ORM write-hook, Redis pub/sub, a websocket, an
// EventEmitter. Call notify(changedKey?) on each change; return an unsubscribe.
const storage = withWatch(base, (notify) => {
const sub = redis.subscribe("flag-changes", (msg) => notify(msg.key));
return () => sub.unsubscribe();
});

notify(key?) filters by the watched prefix; a call with no key is delivered as a change to the prefix itself (enough to trigger a refresh). Because the source is yours, this works for any dialect (MySQL/SQLite included) using whatever signal your stack already emits (e.g. an ORM’s onInsert/onUpdate/onDelete hooks).

pgListenNotify(client, channel?) returns a ready-made source. You own the trigger (added via your migrations — the adapters issue no DDL) that pg_notifys the channel with the changed key:

-- migration (yours): notify "xtandard_flags" with the key on every change
CREATE FUNCTION flags_kv_notify() RETURNS trigger AS $$
BEGIN
PERFORM pg_notify('xtandard_flags', COALESCE(NEW.key, OLD.key));
RETURN NULL;
END; $$ LANGUAGE plpgsql;
CREATE TRIGGER flags_kv_notify_trg
AFTER INSERT OR UPDATE OR DELETE ON flags_kv
FOR EACH ROW EXECUTE FUNCTION flags_kv_notify();
import { withWatch, pgListenNotify } from "@xtandard/flags";
import { Client } from "pg";
const listener = new Client({ connectionString: process.env.DATABASE_URL });
await listener.connect(); // a dedicated Client — a pooled connection won't do
const storage = withWatch(
createDrizzleStorage({ db, table: flagsKv }), // or createPostgresStorage(...)
pgListenNotify(listener, "xtandard_flags"),
);