Skip to content

@xtandard/kit/storage

@xtandard/kit/storage — the neutral key/value storage contract (KvStorage and its optional watch / compareAndSwap capabilities) with its feature-detection guards, withWatch + pgListenNotify for composable change notifications, the escaping and canonical-JSON helpers the drivers share, and generic key-segment helpers. Drivers live on their own subpaths (@xtandard/kit/storage/redis, …) so importing this never loads a peer.

import { … } from "@xtandard/kit/storage";
Name Summary
assertIdentifier Throw unless name is a safe SQL identifier ([a-zA-Z_][a-zA-Z0-9_]*). Returns name so it can be used inline. what names the identifier in the error ("table", "column", …).
canonical A JSON-equivalent copy of value with object keys sorted recursively, so two structurally equal values serialise identically. JSON.stringify semantics apply first (undefined members are dropped, toJSON is honoured, non-finite numbers become null).
canonicalJson JSON.stringify of the canonical form: equal values ⇒ equal strings.
decodeSegment Reverse encodeSegment (also decodes plain encodeURIComponent output).
deepEqual Structural equality for JSON-shaped values: key order ignored, arrays ordered, and own properties only ({a: undefined} is not equal to {b: undefined}).
encodeSegment Escape a free-form key segment: encodeURIComponent, plus ~ → %7E (~ is the separator of sortableLeaf).
escapeGlob Escape a literal prefix for SQLite GLOB (case-sensitive and index-usable, unlike SQLite’s LIKE, which folds ASCII case). *, ? and [ are wrapped in a one-character class; ] outside a class is already literal.
escapeLike Escape a literal prefix for LIKE … ESCAPE '!'. ! (not \) because a backslash inside a string literal is itself an escape on MySQL, which makes ESCAPE '\' a syntax error there; ! means the same on every dialect.
escapeRedisGlob Escape a literal prefix for a Redis glob (SCAN … MATCH, PSUBSCRIBE).
escapeRegex Escape a literal string for embedding in a regular expression.
importPeer import(name) an optional peer, turning a module-not-found error into requirePeer’s message. Other errors (a broken install, a throwing module) propagate unchanged.
isCompareAndSwap Runtime feature-detection: does this storage implement compareAndSwap?
isTransactional Runtime feature-detection: does this storage implement transaction?
isWatchable Runtime feature-detection: does this storage implement watch?
lastSegment The trailing /-separated segment of key, decoded with decodeSegment.
padMillis Zero-pad epoch milliseconds (default 13 digits, enough until the year 2286) so the lexicographic order of the strings is the numeric order. Negative values clamp to 0 and fractions are truncated.
parseJsonValue Normalise a JSON column value: pg, pglite and Drizzle usually return json/jsonb already parsed; a string is parsed defensively (and returned as-is if it is not JSON).
parseSortableLeaf Parse a sortableLeaf (or a key ending in one) back into { ms, id }. Returns null when the trailing segment has no ~, the millis are not a number, or the id is empty. The id is returned verbatim (not decoded).
pgListenNotify A WatchSubscribe backed by Postgres LISTEN/NOTIFY. Pair it with withWatch for the postgres or drizzle-over-pg drivers. Your migration owns the trigger that pg_notifys channel with the changed key as payload; this only LISTENs.
requirePeer Throw a clear, actionable error for a missing optional peer. The message names the package to install (what the user needs) and the kit subpath that asked for it.
sortableLeaf A sortable leaf <padded millis>~<id>: listing a prefix and sorting the keys yields them in time order (ties broken by id). The id is used verbatim, so it must not contain /; encode free-form ids with encodeSegment first.
withWatch Wrap a storage so it implements WatchableKvStorage using subscribe as the change source. The wrapper delegates every other member to storage and only adds watch; keys outside the watched prefix are filtered out. A notification without a key is delivered as an update of the prefix itself (enough to trigger a refresh).
Name Summary
CompareAndSwapInput Input of CompareAndSwapKvStorage.compareAndSwap.
CompareAndSwapKvStorage Storage that supports optimistic concurrency via compare-and-swap.
KvStorage The minimal key/value contract every storage backend must satisfy.
PgNotificationClient A dedicated notification client for pgListenNotify, satisfied by a pg Client. A Pool won’t do (it rotates connections); use a Client you connect().
StorageChangeEvent A storage change event delivered to WatchableKvStorage.watch callbacks.
TransactionalKvStorage Storage that supports atomic multi-key transactions.
WatchableKvStorage Storage that can push change notifications (e.g. Redis keyspace events, fs.watch).
Name Summary
JsonObject A JSON object.
JsonValue A JSON-serialisable value.
WatchSubscribe A change source. Called with a notify callback: wire your source to invoke it (optionally with the changed key) on every change, and return an unsubscribe function (sync or async).
Name Summary
SAFE_IDENTIFIER Identifiers the drivers are willing to interpolate into DDL and queries unquoted.