Skip to content

Storage: SQL and document databases

Postgres (@xtandard/flags/storage/postgres)

Section titled “Postgres (@xtandard/flags/storage/postgres)”

Peer dep: pg (or pass any client exposing query(text, params) — including PGlite for a zero-infra embedded Postgres).

import { createPostgresStorage } from "@xtandard/flags/storage/postgres";
// Lazily creates a pg Pool:
const storage = createPostgresStorage({ connectionString: process.env.DATABASE_URL! });
// …or pass your own client / PGlite (great for tests, no server needed):
import { PGlite } from "@electric-sql/pglite";
const storage = createPostgresStorage({ client: new PGlite(), table: "xtandard_flags" });

Data lives in one table key text PRIMARY KEY, value jsonb (auto-created on first use). table defaults to xtandard_flags and is validated as a safe identifier.

Peer dep: drizzle-orm (supports 0.44/0.45 and the 1.0 beta — the factories return the dialect-agnostic DrizzleKvTable rather than a version-specific inferred type, so they stay portable across drizzle-orm versions). Use this instead of the Postgres adapter when you already use Drizzle and want the flags table to live in your schema + migrations (not auto-created via CREATE TABLE) and to reuse your existing connection (the adapter never opens or closes a pool). Works with Postgres, MySQL, and SQLite Drizzle databases — the upsert dialect is detected at runtime.

1. Declare the table with the matching factory and add it to your schema so drizzle-kit generates the migration:

// schema.ts — pick the factory for your dialect:
import { pgFlagsTable } from "@xtandard/flags/drizzle/pg"; // key text PK, value jsonb
// import { mysqlFlagsTable } from "@xtandard/flags/drizzle/mysql"; // key varchar PK, value json
// import { sqliteFlagsTable } from "@xtandard/flags/drizzle/sqlite"; // key text PK, value text(json)
export const flagsKv = pgFlagsTable("flags_kv");

Each factory takes optional extraColumns() / extraIndexes(table) (mirroring drizzle-audit), plus schema (pg) / keyLength (mysql).

On MySQL the key column is varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin: MySQL’s default collation (utf8mb4_0900_ai_ci) is case- and accent-insensitive, so keys like flags/Acme/… and flags/acme/… would be the same row. Tables created before this (with the default collation) need a one-off migration (adjust the length if you set keyLength):

ALTER TABLE flags_kv
MODIFY `key` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL;

If two keys already collided, the table holds only one of them (the last write); the ALTER doesn’t bring the other back.

utf8mb4_bin is a PAD SPACE collation: it ignores trailing spaces, so "flags/a " and "flags/a" are the same primary key. The adapter guards every operation: reads, removes and swaps match keys byte-exactly, and a write whose key differs from a stored one only by trailing spaces throws KeyCollisionError (exported from @xtandard/flags/storage/drizzle) instead of overwriting it. New tables can opt into a NO PAD collation with mysqlFlagsTable("flags_kv", { keyCollation: "utf8mb4_0900_bin" }) (MySQL 8.0+; "utf8mb4_nopad_bin" on MariaDB). Existing tables can migrate in place:

ALTER TABLE flags_kv
MODIFY `key` varchar(512) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin NOT NULL;

2. Build the storage over your existing db:

import { createDrizzleStorage } from "@xtandard/flags/storage/drizzle";
import { flagsKv } from "./schema.ts";
const storage = createDrizzleStorage({ db, table: flagsKv }); // `db` = your Drizzle instance

The Drizzle adapter has no built-in watch — change notifications are composable and adapter-agnostic. See Change notifications below to add watch from any source (Postgres LISTEN/NOTIFY, an ORM write-hook, Redis pub/sub, …). Without it, the runtime provider polls (refreshIntervalMs) — fine for the source plane.

Peer dep: mongodb.

import { createMongoStorage } from "@xtandard/flags/storage/mongodb";
const storage = createMongoStorage({
url: process.env.MONGO_URL!,
dbName: "xtandard_flags", // default
collectionName: "flags_kv", // default
});
// …or pass a connected MongoClient via { client }.

Documents are { _id: <key>, value: <any> }. No built-in watch (change streams require a replica set); the provider’s polling refresh covers updates, or wrap with withWatch driven by a change stream.

SQLite (@xtandard/flags/storage/sqlite) — Bun only

Section titled “SQLite (@xtandard/flags/storage/sqlite) — Bun only”

Zero dependencies, backed by bun:sqlite. Great for single-node deployments and local persistence; for multi-node runtimes prefer Redis/Postgres. Requires the Bun runtime (the module is external to the bundle and only resolves under Bun).

import { createSqliteStorage } from "@xtandard/flags/storage/sqlite";
const storage = createSqliteStorage({ path: "./flags.sqlite" }); // or ":memory:"
// …or pass an existing bun:sqlite Database via { database }.

Stored in one table key TEXT PRIMARY KEY, value TEXT (auto-created). table defaults to xtandard_flags. Standalone/CLI driver: SOURCE_STORAGE_DRIVER=sqlite with SOURCE_SQLITE_PATH (run the standalone/CLI under Bun).

libSQL / Turso (@xtandard/flags/storage/libsql)

Section titled “libSQL / Turso (@xtandard/flags/storage/libsql)”

The SQLite dialect, but against a remote, replicated, edge-distributed database — Turso / libSQL. Runs anywhere (Node, Bun, Deno, edge), unlike the Bun-only bun:sqlite adapter. Optional peer dependency: @libsql/client.

import { createLibsqlStorage } from "@xtandard/flags/storage/libsql";
// Turso (remote, with replicas at the edge):
const storage = createLibsqlStorage({
url: process.env.TURSO_DATABASE_URL!, // libsql://<db>-<org>.turso.io
authToken: process.env.TURSO_AUTH_TOKEN!,
});
// …or a local file / embedded replica / in-memory:
// createLibsqlStorage({ url: "file:flags.db" });
// createLibsqlStorage({ url: ":memory:" });
// …or pass a pre-constructed client via { client }.

One table key TEXT PRIMARY KEY, value TEXT (auto-created); table defaults to xtandard_flags. Connection is lazy and shared. Call storage.close() on shutdown. A great fit for runtime storage on the edge: snapshots replicate close to your workers, and the provider still serves last-known-good from memory if libSQL blips.