@dapplelight/signet-db
v0.3.1
Published
Drizzle helpers: sortable ids, money, encrypted fields, per-user scoping, audit trail, migration checks.
Maintainers
Readme
@dapplelight/signet-db
Drizzle helpers for the parts every app rewrites: sortable ids, timestamps, soft delete, money, encrypted fields, per-user scoping, an audit trail, per-user export and erasure, seed data and a migration safety check.
The core never imports a driver. Thin entry points connect it to one:
| Import | What |
|---|---|
| @dapplelight/signet-db | Core: scoped, audited, createCipher, money, newId, seed, exportUser, deleteUser, nextSequence, describeSchema, defineQuery, fromDrizzle, dbCache |
| @dapplelight/signet-db/columns/sqlite | Column helpers for SQLite and D1 tables |
| @dapplelight/signet-db/columns/pg | The same helpers for Postgres tables |
| @dapplelight/signet-db/d1 | createDb(env.DB), Cloudflare D1 |
| @dapplelight/signet-db/sqlite | createDb("app.db"), bun:sqlite |
| @dapplelight/signet-db/postgres | createDb(url \| postgres()), postgres-js (optional peer) |
| @dapplelight/signet-db/migrations | checkMigrationSql; the signet-db-check CLI |
| @dapplelight/signet-db/zod | toZod(table, { mode }) (zod is an optional peer) |
| @dapplelight/signet-db/agent | agentTools(db, { tables, userId }), tools for AI agents (needs zod) |
| @dapplelight/signet-db/entitlements | definePlans, createEntitlements, billing events, usage limits, entitlementTools |
| @dapplelight/signet-db/flags | defineFlags, evaluateFlag, createFlags |
| @dapplelight/signet-db/admin | defineAdmin(schema, { authorize }), an admin API and admin MCP tools (needs zod) |
| @dapplelight/signet-db/webhooks | createWebhooks: outbound webhooks per user or org, signed, retried through the outbox, with a delivery log |
| @dapplelight/signet-db/privacy | createPrivacy: DPDP consent records, withdrawal, export and erasure requests; privacyTools |
| @dapplelight/signet-db/analytics | createAnalyticsStore: events in your database, counts, uniques, series, breakdowns, funnels, retention, trends; analyticsTools |
| @dapplelight/signet-db/orgs | createOrgs: organisations, memberships, roles and permissions, custom roles, invites, per-org domain and theme |
| @dapplelight/signet-db/growth | createGrowth: waitlist, invites, referrals, onboarding, changelog, feedback; growthTools |
| @dapplelight/signet-db/workflows | defineWorkflow, createWorkflows: durable multi-step runs with sleeps, events, human approvals, retries, cancel |
| @dapplelight/signet-db/offerings | createOfferings: versioned offerings (store and web products mapped to plans) with a remote paywall, served per placement and audience; plansFromProducts |
| @dapplelight/signet-db/customers | createCustomers: the customer timeline across stores and web, anonymous-to-account identity merge, and the Customer Center (cancel, refund request, retention offer, support) |
Peers: drizzle-orm ^0.45, @dapplelight/signet-lib ^0.3, postgres only
for ./postgres and zod only for ./zod. Types flow from your table definitions: rows, inserts
and updates are InferSelectModel/InferInsertModel of the table you pass.
Published on npm as @dapplelight/signet-db.
Quick start
Cloudflare D1
import { createDb } from "@dapplelight/signet-db/d1"
import { scoped } from "@dapplelight/signet-db"
import * as schema from "./schema"
export default {
async fetch(req: Request, bindings: { DB: D1Database }) {
const db = createDb(bindings.DB, { schema }) // per request; no env at import time
const me = scoped(db, { userId: await userIdFrom(req) })
return Response.json(await me.select(schema.orders))
},
}Atomic groups (deleteUser) run as one D1 batch, since D1 has no BEGIN.
See examples/d1/worker.ts.
SQLite on Bun (Docker, VPS)
import { createDb } from "@dapplelight/signet-db/sqlite"
const db = createDb("/data/app.db", { schema })Runs on Bun (bun:sqlite). For better-sqlite3 on Node:
fromDrizzle(drizzle(client), { dialect: "sqlite", atomicity: "sync-transaction" }).
See examples/sqlite.ts.
Postgres (Supabase, Neon, RDS)
import postgres from "postgres"
import { createDb } from "@dapplelight/signet-db/postgres"
const db = createDb(postgres(url, { prepare: false }), { schema }) // prepare:false for Supabase's poolerSee examples/postgres.ts.
Any other Drizzle driver
import { fromDrizzle } from "@dapplelight/signet-db"
const db = fromDrizzle(drizzle(client), { dialect: "pg", atomicity: "transaction" })Columns
import { sqliteTable, text } from "drizzle-orm/sqlite-core"
import { id, timestamps, softDelete } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
export const orders = sqliteTable("orders", {
id: id(), // text primary key, newId() on insert
userId: text("user_id").notNull(),
item: text("item").notNull(),
...timestamps(), // createdAt, updatedAt (moves on every Drizzle update)
...softDelete(), // deletedAt
})newId() ids are 26 characters, sort by creation time as plain strings and
stay ordered within a millisecond. idTime(id) reads the time back.
Timestamps carry a SQL default, so migrations and raw inserts get them too.
Money
import { money } from "@dapplelight/signet-db/columns/sqlite"
import { moneyOf, addMoney, formatMoney } from "@dapplelight/signet-db"
const items = sqliteTable("items", { id: id(), ...money("price", { currency: "INR" }) })
// → price (integer minor units: paise), priceCurrency (default 'INR')
formatMoney(addMoney(moneyOf(row, "price"), { amount: 99, currency: "INR" })) // "₹1,234.56"Amounts are integers; a float or a different currency throws. Formatting turns
minor units into an exact decimal string before Intl sees it, so 2^53 paise
format without rounding. minorDigits("JPY") is 0, "BHD" is 3. Also
subtractMoney, sumMoney, compareMoney. Postgres stores amounts as
bigint, read as numbers.
Encrypted fields
import { encrypted } from "@dapplelight/signet-db/columns/sqlite"
import { createCipher } from "@dapplelight/signet-db"
const profiles = sqliteTable("profiles", { id: id(), userId: text("user_id").notNull(), pan: encrypted("pan") })
const db = createDb(bindings.DB, { cipher: createCipher({ keys: { k1: bindings.FIELD_KEY }, current: "k1" }) })
await scoped(db, { userId }).insert(profiles, { pan: "ABCDE1234F" }) // stored as enc:v1:k1:…AES-GCM through Web Crypto. Reads through scoped/audited/exportUser
decrypt; writes encrypt. A raw Drizzle write of plaintext to an encrypted()
column throws instead of storing it. Reads fail closed (via
@dapplelight/signet-lib/crypto): a value that does not decrypt comes back as
null, never as ciphertext, and onUndecrypted is called.
Rotation: the key id is stored with each value. Add a new key, make it
current, keep the old one in keys; onStaleKey fires when an old key was
used and cipher.rotate(value) re-encrypts. generateKey() makes a key.
Encrypted columns cannot be filtered on (random IVs). See examples/encrypted.ts.
Per-user scope and soft delete
const me = scoped(db, { userId: session.userId })
await me.insert(orders, { item: "Dosa" }) // userId set for you; passing another throws
await me.select(orders, eq(orders.item, "Dosa"), { orderBy: desc(orders.createdAt), limit: 20 })
await me.update(orders, { item: "Idli" }, eq(orders.id, id))
await me.delete(orders, eq(orders.id, id)) // sets deletedAt
await me.select(orders, undefined, { withDeleted: true })
await me.delete(orders, eq(orders.id, id), { hard: true })
db.orm.select().from(orders).where(me.where(orders, extra)) // the same filter, hand-built queryTables with a userId column get where user_id = ? on every read, update
and delete; tables with deletedAt hide deleted rows unless a call passes
withDeleted. A scoped update cannot change userId.
Audit trail
import { auditLog } from "@dapplelight/signet-db/columns/sqlite"
export const audit = auditLog() // audit_log table; add it to your migrations
const db = createDb(bindings.DB, { schema, audit })
const me = audited(scoped(db, { userId })) // actor: { type: "user", id: userId }
const bot = audited(db, { actor: { type: "system", id: "nightly-cleanup" } }) // unscoped; where requiredEach changed row gets an entry: actorType (user, agent or system),
actorId, action
(insert/update/delete/delete_user), tableName, rowId, before, after,
at. Encrypted fields are recorded as ciphertext. The change and its entry
are separate statements; on D1 they are not one transaction.
Migration safety check
signet-db-check drizzle/ # exit 1 on findings
signet-db-check $(git diff --name-only --diff-filter=A origin/main -- 'drizzle/*.sql') # new files only, in CIFlags DROP TABLE, DROP COLUMN, renames, and NOT NULL added without a
DEFAULT (including SET NOT NULL). drizzle-kit's SQLite table rebuilds are
not flagged. Accept a finding with -- signet-allow: drop-column (or all)
in the statement. checkMigrationSql(sql, file) is the same check as a function.
Seed data
const rows = seed(users, 50, { city: "Pune" }, { seed: 7 }) // or (i) => ({ … })
await db.orm.insert(users).values(rows)Values follow column types and names: Indian names, +91 mobile numbers,
6-digit PIN codes, cities, states, emails at example.com, amounts in minor
units. Same seed, same rows. Columns with defaults are left out.
Per-user export and delete
const tables = [orders, profiles, [users, users.id]] as const // [table, ownerColumn] when it is not userId
const data = await exportUser(db, userId, tables) // { orders: [...], profiles: [...], users: [...] }, decrypted
const counts = await deleteUser(db, userId, tables) // { orders: 3, profiles: 1, users: 1 }deleteUser hard-deletes in one atomic batch, in the order given (children
first if foreign keys do not cascade). With auditing on, the same batch writes
a delete_user entry holding the counts, not the deleted data.
Describe the schema
describeSchema(schema) // text, below
describeSchema(schema, { format: "json" }) // { tables: [{ name, columns, primaryKey, scoped, softDelete, relations }] }
// table orders
// id text primary key has default
// user_id text not null -- js: userId; owner; rows are per user
// total integer not null
// total_currency text not null has default -- js: totalCurrency; currency of total, which is in minor unitsText for an agent's or a reviewer's context; JSON for tools. Each column says whether it is the owner, the soft-delete marker, money or encrypted.
Tools for AI agents
import { agentTools } from "@dapplelight/signet-db/agent"
import { createMcpServer } from "@dapplelight/signet-server/mcp"
const tools = agentTools(db, { tables: [orders, notes], userId: session.userId, maxRows: 50 })
createMcpServer({ name: "app", version: "1.0.0", tools }) // or call tool.run(input) yourselfPer table: <table>_list, <table>_get, <table>_search (equals or
contains), and with allowWrites: true also <table>_insert and
<table>_update, which take dryRun: true to return the would-be change.
Guards that hold for every tool:
- Scoped to
userId; a table without auserIdcolumn is refused. - Soft-deleted rows hidden;
userIdand encrypted fields never returned (includeEncrypted: trueopts encrypted fields in). - At most
maxRows(100) rows per call; inputs are strict Zod objects, so unknown fields (such asuserId) are rejected withBAD_INPUT. - Writes audited as
{ type: "agent", id }(actorto name the agent).
Each tool is { name, description, annotations, input, inputSchema, run,
handler }: input and handler are what signet-server's MCP server
registers; inputSchema is JSON Schema for other runners. AGENTS.md is the
short guide for agents.
Admin API from the schema
import { adminTools, defineAdmin, roles } from "@dapplelight/signet-db/admin"
import * as schema from "./schema"
export const admin = defineAdmin(schema, {
authorize: roles(), // admin: all; staff: all but delete; readonly: list, get
tables: {
users: { search: ["name", "email"], readonly: ["email"], hidden: ["phone"] },
outbox: false, // leave a table out
},
})
const ctx = { actor: { type: "user", id: me.id }, role: me.role }
await admin.list(db, ctx, "users", { filter: { plan: { eq: "pro" } }, sort: "createdAt", limit: 20 })
await admin.update(db, ctx, "users", id, { name: "New name" })Every table in the schema is included unless set to false; config is by SQL
table name. Operations: meta, list, get, create, update, delete,
bulkDelete, action, and handle(db, ctx, request) for HTTP (mount it with
signet-server's adminRoutes). Defaults that hold without config:
- Deny by default. Each op calls
authorize(ctx, { table, op }), then again with therow(each listed row, the stored row, or the new one on create). Anything buttrue, or a throw, is403 FORBIDDEN.roles(map)readsctx.role; write your own for anything finer. - Secrets hidden.
encrypted()columns and names matching password, hash, token, secret, salt, api key or private key are never returned, sorted, filtered or written.hiddenadds more;revealopts one back in. Audit log snapshots are cut to the audited table's visible columns. - Soft delete. Rows with
deletedAtset are left out unless a list or get passeswithDeleted; delete setsdeletedAt(soft: truein the result). - Allow-listed queries.
sort,filterandsearchcolumns are fixed atdefineAdmintime (defaults: visible NOT NULL scalars, visible scalars, visible text). A request naming anything else is400 VALIDATION_FAILED, and SQL is built from Drizzle columns only. Filter operators:eq,ne,in,isNull,lt/lte/gt/gte(numbers, dates, text),containsandstartsWith(text, case-insensitive). - Stable cursors. Pages sort by the column plus the primary key, and the cursor compares against the anchor row in SQL, so ties and inserts never repeat or skip a row. A cursor for another sort is rejected.
- Audited writes. Writes go through
audited(db, { actor: ctx.actor }); the db needs an audit table. TheauditLog()table itself is list and get only. Inputs are stricttoZodschemas: unknown, hidden and read-only fields are rejected with field-leveldetails.
meta(ctx) returns, per table this caller may use, the columns (type, enum,
money currency, read-only, sortable, filterable, operators), relations, the
allowed ops and actions: what an admin UI or an agent renders from.
Actions add per-row operations: actions: { refund: { description, input?,
destructive?, run: ({ row, input, ctx, db, ops }) => ... } }, where ops
is the audited writer.
Admin tools for agents
import { requireApproval } from "@dapplelight/signet-server/mcp"
tools: async ({ c }) =>
adminTools(admin, { db: dbFor(c), ctx: await adminCtx(c), approve: requireApproval })admin_schema, and per table admin_<table>_list, _search, _get; with
approve, also _create, _update, _delete and one tool per action. Only
ops authorize allows the caller are listed, and each call is authorized
again. A write validates and authorizes first, then calls approve (which
asks the person), and writes only when it returns undefined. Writes are
audited as { type: "agent", id: ctx.actor.id } unless actor is given.
Zod schemas from tables
import { toZod } from "@dapplelight/signet-db/zod"
import { parseBody } from "@dapplelight/signet-server/http/validate"
const body = await parseBody(c, toZod(orders, { mode: "insert" }))
await scoped(db, { userId }).insert(orders, body)insert leaves out the defaulted primary key, createdAt, updatedAt,
deletedAt and userId (stripped if a client sends them; keep: ["userId"]
keeps one), and makes defaulted or nullable columns optional. update is the
same with every field optional; select is a full row. Dates accept ISO
strings, money amounts must be integer minor units, currency codes three
capital letters, encrypted columns plaintext strings. Generated from the
columns, no drizzle-zod.
Transactional outbox
import { outbox } from "@dapplelight/signet-db/columns/sqlite"
const db = createDb(bindings.DB, { schema, outbox: schema.outbox })
await db.batch([
(tx) => tx.insert(orders).values(order),
enqueue(db, "order.created", { orderId: order.id }), // same atomic write
])
await drainOutbox(db, queue, { batch: 50 }) // cron or waitUntilqueue is anything with send(message), such as signet-server's Queue
adapters; messages are { id, topic, payload }. Each row is leased before it
is sent, so concurrent drainers do not double-send; a failed send retries with
backoff (2^attempt s, capped at 1 h, or your backoff) until maxAttempts
(10), then the row is dead. A crash between send and mark resends: at least
once, so dedupe on id. In an interactive transaction:
await enqueue(db, topic, payload)(tx). pruneOutbox(db) deletes old sent rows.
Outbound webhooks
@dapplelight/signet-db/webhooks: endpoints registered per user or org, each
with its own whsec_ secret; Standard Webhooks signatures; retries with
backoff through the outbox; a delivery log; manual redelivery; endpoints
switched off after repeated failures.
import { webhookTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createWebhooks, WEBHOOK_TOPIC } from "@dapplelight/signet-db/webhooks"
import { drainOutbox } from "@dapplelight/signet-db"
export const { webhookEndpoints, webhookDeliveries } = webhookTables()
const webhooks = createWebhooks(db, { endpoints: webhookEndpoints, deliveries: webhookDeliveries })
const { endpoint, secret } = await webhooks.register({ owner: orgId, url, events: ["order.created"] }) // secret shown once
// Queued in the same atomic write as the change that caused it:
await db.batch([
(tx) => tx.insert(orders).values(order),
...(await webhooks.statements({ owner: orgId, type: "order.created", data: order })),
])
// From a cron: webhook rows go to webhooks.send, the rest to the app's queue.
await drainOutbox(db, { send: (m) => (m.topic === WEBHOOK_TOPIC ? webhooks.send(m) : queue.send(m)) })- Signature:
webhook-id(the event id),webhook-timestampandwebhook-signature: v1,<base64 HMAC-SHA256 of id.timestamp.body>, made bysignWebhookfrom@dapplelight/signet-lib/webhooks. Receivers callverifyWebhook(secret, headers, rawBody)and dedupe onwebhook-id, which is the same on every retry and redelivery. The body is{ type, timestamp, data }. - Retries: each delivery is one outbox row, so
drainOutbox's backoff andmaxAttemptsapply. Pass the samemaxAttemptstocreateWebhooksso the last attempt logs asfailed. A delivered row is never posted again, even when the at-least-once outbox hands it over twice. - Delivery log:
deliveries(owner, { endpointId, status, limit, before }), newest first: status (pending,retrying,delivered,failed,skipped), attempts, the response status, the first 2 KB of the response, the error and the duration. - Redeliver:
redeliver(owner, deliveryId)queues a new delivery with the same event id and body. - Disable: after
disableAfter(default 20) failed attempts in a row the endpoint is switched off and its queued deliveries areskipped.update(owner, id, { enabled: true })turns it back on. - Safety: urls must be
https:on a public host (no localhost, private ranges, IP literals or credentials) unlessallowPrivateUrls. Before every attempt the host is resolved (node:dns/promiseslookupwithall: trueon Bun and Node) and the delivery is refused if any address is private, loopback, link-local, CGNAT, unique-local IPv6 or IPv4-mapped; that logs as a failed attempt (Refused: <host> resolves to a private address) and counts towarddisableAfterlike any other failure. Redirects are never followed: a 3xx is a failed attempt. Each attempt times out aftertimeoutMs(10 s). With a db cipher, secrets are stored encrypted;rotateSecretreplaces one. - Runtimes without DNS: on Cloudflare Workers without
nodejs_compatthere is nothing to resolve with, so the check is skipped and the platform's outbound protections apply. PassresolveHost: (host) => Promise<string[]>(DNS over HTTPS, say) to enforce it there too.
Admin routes: webhookRoutes in @dapplelight/signet-server/http/webhooks.
Screen: WebhookDeliveryLog in @dapplelight/signet-ui/screens/api-screens.
Idempotency keys
import { idempotencyKeys } from "@dapplelight/signet-db/columns/sqlite"
const db = createDb(bindings.DB, { schema, idempotency: schema.idempotency })
const order = await withIdempotency(db, c.req.header("idempotency-key") ?? "", userId, () =>
scoped(db, { userId }).insert(orders, body),
)Runs the function once per (scope, key) and replays its stored JSON result on
retries for ttlMs (24 h). A duplicate that arrives while the first is still
running gets IDEMPOTENCY_IN_FLIGHT (answer 409); a claim left by a crash
expires after leaseMs (60 s). If the function throws, the key is released.
With a cipher on the db the stored result is encrypted, so encrypted() fields
the function returns are not left as plaintext in the table.
pruneIdempotency(db) deletes expired keys.
AI answer cache
A persistent cache for signet-ai's createExtractor, on a table in either
dialect, so paid answers survive restarts and are shared by every process.
// schema.ts
import { aiCache } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
export const aiAnswers = aiCache() // table "ai_cache"
// where the extractor is made
import { dbCache } from "@dapplelight/signet-db"
const ai = createExtractor({ providers, cache: dbCache(db, schema.aiAnswers) })Rows are keyed by signet-ai's hash of model, schema, instructions and input,
hold the entry as JSON and expire after the extractor's cacheTtlSeconds. An
expired or unreadable row is a miss (an expired one is deleted on read). With a
cipher on the db the entry is encrypted, since answers often repeat the
personal data they were given. pruneAiCache(db, table) deletes expired rows;
run it from a cron. Held in a variable before it reaches createExtractor,
type it: dbCache<CacheEntry>(db, table).
Named queries
const openOrders = (userId: string) =>
defineQuery({
key: ["orders", "open", userId],
run: (db: SignetDb) => scoped(db, { userId }).select(orders, isNull(orders.shippedAt)),
})
await openOrders(userId).run(db)Plain { key, run(db) } objects with inferred result and key types, usable
from handlers, jobs and tests; the key names the query for caching, logs or
devtools.
Plans and entitlements
import { entitlementTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createEntitlements, definePlans } from "@dapplelight/signet-db/entitlements"
export const { subjectPlans, entitlementOverrides, usageCounters } = entitlementTables()
const plans = definePlans({
free: { features: ["export"], limits: { seats: 1, aiCredits: 20 } },
pro: { features: ["export", "sso"], limits: { seats: 10, aiCredits: 1000 } },
team: { features: ["export", "sso", "audit"], limits: { seats: Infinity, aiCredits: 5000 } },
})
const ent = createEntitlements(db, {
plans,
default: "free",
tables: { subjectPlans, entitlementOverrides, usageCounters },
periods: { seats: "total" }, // aiCredits: "month" by default
billing: { plans: { plan_Nx1: "pro", price_9f: "team" }, graceMs: 3 * 86_400_000 },
})
await ent.can(orgId, "sso") // an undeclared feature is a type error, and throws at runtime
await ent.limit(orgId, "seats") // a limit a plan leaves out is 0; Infinity is unlimited
await ent.get(orgId) // { plan, storedPlan, status, graceUntil, features, limits, overrides }
await ent.consume(orgId, "aiCredits", 5) // { ok, used, limit, remaining, period }
await ent.usage(orgId) // this period, every limit
const actor = { type: "user", id: adminId }
await ent.setPlan(orgId, "pro", { actor })
await ent.grant(orgId, "audit", { actor, expiresAt: trialEnd }) // also revoke, setLimit (null = unlimited)
await ent.removeOverride(orgId, overrideId, { actor })A subject is any id: a user or an org. It has one stored row per source:
each subscription (App Store, Google Play, Stripe, Razorpay, ...) and
manual for setPlan. Sources roll up into one entitlement per plan, active
while any of its sources is. A source is active while it is active or
trialing (until currentPeriodEnd, plus graceMs if the period ends with
no event), cancelled with a future currentPeriodEnd, or past_due before
graceUntil. Declare plans lowest first: the plan in effect is the last
declared plan with an active entitlement, else default. So setPlan sets
the manual source, and a live subscription on a later plan still wins.
ent.get(subject) returns plan, currentPeriodEnd (the latest among that
plan's active sources), source (the one behind it) and entitlements
(every plan with its sources). Overrides apply on
top, newest unexpired per key. Every write goes through audited, so the db
needs an audit table; usage counters are not audited.
consume adds to the counter only if the result stays within the limit, in
one UPDATE … WHERE used + n <= limit, so concurrent requests cannot
overspend. Periods are UTC calendar months ("2026-11"), days, or "total".
Billing events
const payments = createPayments({
gateways,
db: store,
onEvent: (event) => ent.applyBillingEvent(event),
})
// At checkout, link the subscription to the subject on its first event:
await ent.applyBillingEvent(event, { subject: orgId }) // or billing.subjectOf(event), e.g. from notesApplies subscription.created, activated, charged, updated, paused,
resumed, past_due, cancelled and expired (and App Store refunds) to
the source row of the event's gateway and subscription id. The subject is
opts.subject, billing.subjectOf(event), the subject already stored for
the subscription, or for Google Play the subject of the linkedPurchaseToken.
A subscription belongs to one subject: an event or restore that names another
returns { applied: false, reason: "other_subject" }.
The gateway plan id is read by billing.planIdOf (default: Razorpay's
payload.subscription.entity.plan_id, Stripe's item price or plan id, then
the event's store productId) and mapped through billing.plans, trying
"<gateway>:<id>" before <id>. currentPeriodEnd comes from
billing.periodEndOf (default: Stripe current_period_end, Razorpay
current_end, Paddle current_billing_period.ends_at, Lemon Squeezy
ends_at else renews_at; a web gateway's subscription.cancelled ends
access now, except Lemon Squeezy's, which lasts until ends_at). For created and updated, whose type does
not say whether the subscription is live, the status comes from the payload
via billing.statusOf (default: Stripe's data.object.status, Paddle's
data.status, Razorpay's entity status), falling back to the stored one.
Each subscription is its own source, so the old one lapsing after a
cancel-and-recreate upgrade does not touch the new one. Each
event applies once (withIdempotency,
so the db needs an idempotency table), and an event older than the stored
state returns { applied: false, reason: "stale" }: a late cancelled cannot
undo a newer upgrade. no_subject and unknown_plan are not stored as done,
so a retry after linking the subject or fixing the map applies.
App Store, Google Play and restore
const ent = createEntitlements(db, {
plans, default: "free", tables,
billing: {
plans: {
price_9f: "pro", // Stripe
"appstore:pro.monthly": "pro", // App Store product id
"googleplay:pro": "pro", // Google Play subscription product id
},
// First event before the app claimed the purchase: look the owner up in signet-payments.
subjectOf: async (e) =>
(await store.findLinked(e.gateway, { orderId: (e as { orderId?: string }).orderId }))?.userId,
},
})
// Restore purchases: re-verify on the store, claim, re-attach.
const { restored, refused } = await payments.restorePurchases({ userId, purchases })
for (const r of restored)
if (r.subscription) await ent.applySubscription(r.subscription, { subject: userId })App Store and Google Play events are read with storeStateOf, from the
store's own state, not the event type:
| Store state | Source | Access |
|---|---|---|
| App Store status 1 (auto-renew on or off) / Play ACTIVE, CANCELED | active | until currentPeriodEnd (expiresDate / expiryTime) |
| App Store status 4 (Billing Grace Period) / Play IN_GRACE_PERIOD | past_due | until gracePeriodExpiresDate / expiryTime |
| App Store status 3 (billing retry) / Play ON_HOLD | past_due | none |
| App Store status 2, 5 (expired, refunded or revoked) / Play EXPIRED, revoked | expired | none |
| Play PAUSED | paused | none |
applySubscription(sub, { subject }) applies a subscription read live (a
signet-payments Subscription), so a restore also fixes a missed webhook. A
cancelled web subscription ends access now, except Lemon Squeezy's, which keeps
access until its currentPeriodEnd (ends_at).
Offerings, customers and the Customer Center
RevenueCat-style monetisation on top of plans and entitlements, in your own
database. Tables: monetisationTables() from /columns/sqlite or /columns/pg
(offerings, placements, customerIdentities, customerEvents,
customerRequests, chartAudiences).
Offerings and remote paywalls
import { monetisationTables } from "@dapplelight/signet-db/columns/sqlite"
import { createOfferings, plansFromProducts } from "@dapplelight/signet-db/offerings"
export const { offerings, placements, customerIdentities, customerEvents, customerRequests } =
monetisationTables()
const products = [
{ id: "pro.monthly", store: "appstore", plan: "pro", interval: "month" },
{ id: "pro_monthly", store: "googleplay", plan: "pro", interval: "month" },
{ id: "price_pro_m", store: "web", plan: "pro", interval: "month", price: { amount: 49900, currency: "INR" } },
] as const
const off = createOfferings(db, { tables: { offerings, placements }, plans })
const ent = createEntitlements(db, { plans, default: "free", tables, billing: { plans: plansFromProducts(products) } })
await off.publish("default", {
products,
paywall: { title: "Go pro", plans: ["pro"], highlight: "pro", badge: "Best value", trial: "7 days free", cta: "Start trial" },
}, { actor }) // version 1; the next publish is 2
await off.setPlacement("onboarding", {
offering: "default", // latest version; `version: 1` pins (rollback)
overrides: [{ when: { country: ["IN"], platform: ["web"] }, offering: "india" }],
}, { actor })
await off.current("onboarding", { country: "IN", platform: "web", attributes: { cohort: "beta" } })
// { key, version, products, paywall, placement, override: 0 }- A product maps a store product id (App Store, Google Play) or a web
gateway's plan or price id to a plan.
plansFromProductsturns them into entitlements'billing.planskeys ("appstore:<id>","googleplay:<id>", bare ids for web), so what the paywall sells is what billing grants. publishvalidates (every plan declared, no product twice, integer prices, a highlight the offering sells) and writes an immutable version; the row id iskey@version, so concurrent publishes never share a number.- A placement serves its offering's latest version unless pinned. Overrides
match
country,platformandattributes(a list matches any value), first match wins, with the same matcher as feature flags. Changing either needs no app release: the app asks for the placement each time. - Admin-editable:
publish,setPlacement,versions,listandplacements, or signet-server'smonetisationAdminRoutes. IndefineAdmin, listofferingsas read-only so versions stay immutable. - The paywall config is copy, not logic: the purchase still goes through your
checkout (
payments.checkouton the web; StoreKit or Play Billing, thenverifyPurchase, in the app). signet-ui'sRemotePaywallrenders it.
Customer timeline
import { createCustomers } from "@dapplelight/signet-db/customers"
const customers = createCustomers(db, {
tables: { ...entitlementTables, customerIdentities, customerEvents, customerRequests },
entitlements: ent,
payments: store, // signet-payments store: payments, ledger, merge
})
createPayments({
...,
onEvent: async (e) => {
const subject = await customers.resolve(ownerOf(e)) // the app's user id or anonymous id
await ent.applyBillingEvent(e, { subject })
await customers.record(e, { subject })
},
})
await customers.timeline(userId) // { entries: [{ kind, gateway, amount, ... }], totals: [{ currency, paid, refunded, approximate }] }record puts each event on its customer's timeline once (keyed by gateway
and event id), as purchase, renewal, refund, cancellation,
uncancellation, billing_issue, grace_period (App Store status 4, Play
IN_GRACE_PERIOD), expiration, paused or dispute. Without a subject it
uses the owner of the event's subscription or payment. timeline adds the
events of the customer's payments from the payments store and totals per
currency from the ledger (App Store prices are marked approximate).
Identity merge
const anon = await customers.anonymousId({ orgId }) // "anon_<uuid>", before sign-in
// Buy as anon: App Store appAccountToken = anon.slice(5); verifyPurchase({ userId: anon })
const r = await customers.merge({ anonymousId: anon, userId, orgId })
// { merged: true, already: false, moved: { subscriptions, overrides, events, requests, payments } }Moves the anonymous id's subscriptions (not a manual plan), grants,
timeline, requests and, with payments, its payments (reassignUser) to the
account. Rows move, never copy, so nothing is granted twice. The claim is one
conditional UPDATE: of concurrent merges into different accounts exactly one
wins (already_merged for the rest); repeating a merge into the same account
returns already: true and finishes anything a crash left half done.
Refused: an id anonymousId did not issue (not_anonymous, so two real
accounts never merge), the same id, a target that is itself anonymous, another
org (other_org), or, with isMember, a user outside the org. Anonymous ids
carry 122 random bits: whoever holds one can claim its purchases, so keep it
on the device. After a merge, resolve(anon) is the account, so pass events
tagged with the old id through it.
Customer Center
const customers = createCustomers(db, {
...,
notify: (n) => notifier.notify(teamId, "customer.request", { kind: n.kind, text: n.text }),
cancel: (s) => payments.gateway(s.gateway).cancelSubscription(s.subscriptionId, { atPeriodEnd: true }),
retention: { offer: { kind: "discount", percent: 50, months: 3 }, apply: ({ source }) => applyCoupon(source) },
googlePlayPackage: "com.example.app",
})
await customers.overview(userId) // plan, status, currentPeriodEnd, renews, source, manageUrl, canCancel, retention
await customers.cancel(userId) // web: cancelled at the gateway; store: { cancelled: false, reason: "store", manageUrl }
await customers.acceptRetention(userId) // once per customer; applied through `retention.apply`
await customers.requestRefund(userId, { gateway, paymentId, reason }) // once per payment; the team is notified
await customers.contactSupport(userId, { message, email, topic })
await customers.requests({ status: "open" }); await customers.setRequestStatus(id, "done", { actor })- Stores cannot be cancelled by a server:
manageUrlis the App Store's subscriptions page, or Google Play's for the product and package. App Store refund requests also returnstoreUrl(Apple decides those). - A refund request is only for the customer's own payment (payments store,
else their timeline); anything else is
NOT_FOUND. It is recorded beforenotifyruns, and a request whose notice failed is sent again on retry. - The retention offer is shown to a live web subscription that never took
one; pass
eligibleto change that (store offers belong to the store). - A web cancel is recorded, so
renewsandcanCancelturn false while the gateway still reports the subscription active until its period end. - signet-ui's
CustomerCenterrenders it; signet-server'smonetisationRoutesserves it.
Offers
Free trials, intro offers and win-back offers ride on offering products, so
they change with a publish like the rest of the paywall:
await off.publish("default", {
products: [{
id: "pro.monthly", store: "appstore", plan: "pro", interval: "month",
offers: [
{ id: "trial7", type: "free_trial", days: 7 }, // never subscribed, once
{ id: "winback50", type: "win_back", price: { amount: 249, currency: "USD" }, days: 30,
eligibility: { lapsedDays: 30 } }, // lapsed 30+ days, once
],
}],
}, { actor })
const offering = await customers.withOffers(userId, await off.current("onboarding", audience))
// each product's `offers` cut to what this customer may see now, best first
await customers.redeemOffer(userId, offering, "winback50")
// { redeemed: true, offer, product } | { redeemed: false, reason: "not_eligible" | "already_used" | "no_such_offer" }- Eligibility (
offerEligible(offer, history), a pure function): never while a subscription is live;neverSubscribed(the default for free trials and intro offers) means no subscription or trial before on any plan;lapsedDays: n(required for win-back) means the last subscription ended at leastndays ago; every offer is once per customer unlessonce: false, and a free trial is also spent by any earlier trial on the timeline. redeemOfferclaims the offer before you apply it (a web coupon, a trial at checkout): one row per customer and offer, so of concurrent claims one wins. Store offers are still the store's to grant (App Store intro eligibility, Play offer tags); the filter decides what the paywall shows.- signet-server's
GET /offerings/:placementreturns the filtered offering; signet-ui'sRemotePaywallshows the first offer and passes it toonPurchase.
Promotional entitlements
await customers.grantPromotional(userId, { plan: "pro", days: 30, note: "Beta tester" }, { actor })
// or { expiresAt }; also ent.grantPromotional(subject, plan, { actor, expiresAt })
await customers.endPromotional(userId, grantId, { actor }) // early; onceA promotional grant is its own entitlement source (gateway promotional): it
rolls up with subscriptions, a paid plan declared later still wins, and it
lapses exactly at its expiry with no job to run and no grace period. The
timeline shows promotional when granted and expiration when it lapses or
is ended. It is not a paid source: the Customer Center does not offer to
cancel it, and charts leave it out.
Apple refund requests (CONSUMPTION_REQUEST)
When a customer asks Apple for a refund, Apple sends CONSUMPTION_REQUEST
and wants your consumption information within 12 hours, only for a customer
who consented to share it.
const customers = createCustomers(db, {
...,
consumption: {
consented: (subject) => privacy.has(subject, "apple_refund_info"), // required; no consent = no answer
refundPreference: (ctx) => undefined, // optional: DECLINE | GRANT_FULL | GRANT_PRORATED
},
})
onEvent: async (e) => {
await customers.record(e) // on the timeline as refund_request
const r = await customers.consumptionInfo(e)
if (r.respond) await appstoreGateway.sendConsumptionInfo(r.transactionId, r.body)
}The body is Apple's v2 ConsumptionRequest, built from the customer's data:
customerConsented: true, deliveryStatus DELIVERED when the purchase is
on their timeline (else UNDELIVERED_OTHER), consumptionPercentage as the
share of the paid period used (0 unless delivered, left out with
GRANT_PRORATED), and sampleContentProvided when they had a free trial.
Every field has a hook to override. respond: false says why
(no_consent, no_subject, not_configured, not_consumption_request).
Subscription charts
import { chartsCsv, createCharts } from "@dapplelight/signet-db/charts"
const charts = createCharts(db, {
tables: { customerEvents, chartAudiences, offerings }, // products from every offering version
fx: { base: "USD", rates: { INR: 0.012, EUR: 1.08 } }, // optional: totals in one currency
})
const r = await charts.metrics({ from: "2026-10-01", to: "2026-10-31", store: "appstore", country: "IN" })
// r.days[i]: activeSubscribers, activeTrials, mrr, mrrTotal, newRevenue, renewalRevenue, refunds,
// refundCount, newSubscriptions, trialsStarted, trialConversions, churned, impressions, conversions
// r.summary: the same on the last day or summed, plus trialConversion, churnRate, paywallConversion
const a = await charts.saveAudience("India App Store", { store: "appstore", country: "IN" }, { actor })
await charts.metrics({ from, to, audience: a.id })
await charts.csv({ from, to }) // one row per day; or chartsCsv(r)
await customers.logImpression(userId, { offering: "default", version: 3, country: "IN" }) // paywall viewsOne GROUP BY on SQLite, D1 or Postgres buckets the timeline per subscription,
kind and UTC day; the rows are then replayed per subscription. Money stays per
currency in minor units; mrrTotal is set only when fx has a rate for
every currency in it. App Store amounts are list prices, so such results say
approximate. How the timeline reads: the first paid charge is new revenue
and later ones renewals; each charge covers one interval of its product
(unknown products a month) and MRR is that charge per month (a year / 12;
events without an amount use the product's display price); a refund or
expiration ends coverage that day, and coverage that ends without a renewal is
churn; a trial (a zero-amount or App Store FREE_TRIAL purchase) lasts the
product's free-trial days (else trialDays, 7) and converts when paid
within it; a start is a paywall conversion when the customer saw an offering
within attributionDays (7) before, and that offering is what the offering
filter matches. A customer's country is the latest one recorded
(record(e, { country }) or the impression's). Ranges are at most 731 days.
signet-server's monetisationAdminRoutes({ charts }) serves it and signet-ui's
SubscriptionCharts draws it.
Web-to-app funnels
@dapplelight/signet-db/funnels: a hosted web checkout that sells an
offering's web products, then unlocks the purchase in the app. Tables:
funnelTables() (in ./columns/sqlite and ./columns/pg) plus
customerEvents from monetisationTables().
Funnels and checkout
import { createFunnels, funnelSteps } from "@dapplelight/signet-db/funnels"
const funnels = createFunnels(db, {
tables: { ...funnelTables(), customerEvents },
funnels: [{
key: "spring", placement: "web", title: "Sleep better",
questions: [{ id: "goal", title: "Your goal?", options: [{ value: "sleep", label: "Sleep" }] }],
}],
discounts: [{ code: "SPRING25", percentOff: 25, expiresAt: "2027-04-01", maxRedemptions: 500, plans: ["pro"] }],
offerings, customers, entitlements, // entitlements' billing.plans from plansFromProducts
checkout: (input) => payments.checkout(input), // signet-payments
track: (e) => sink.write([e]), // any signet-server AnalyticsSink
onPaid: async (s) => s.email && sendMail(s.email, (await redemptionLink(opts, s.id)).link),
})
createPayments({ ..., onEvent: async (e) => {
await entitlements.applyBillingEvent(e); await customers.record(e); await funnels.applyEvent(e)
} })page(key, { code })is what the hosted page shows: the funnel, the placement's priced web products, and the URL's code priced per product.start(key, { productId, answers, code, email, visitor })checks the product is a priced web product of the offering and the answers fit the questions, prices the discount and takes one use, creates a new anonymous customer (customers.anonymousId), and asks the gateway for exactly that amount under the session id (fs_<uuid>) as idempotency key and reference. The client never sends a price.applyEvent(e): a verifiedpayment.capturedfor a funnel checkout, for the amount and currency priced, grants the plan to the anonymous customer (one interval from the capture; lifetime foronce) throughentitlements.applySubscription, puts the discount on the customer timeline (kinddiscount) and marks the sessionpaid, once. A different amount isamount_mismatchand grants nothing.refund.processedends the access, on whichever account it was merged into. Passgrantto start a renewing gateway subscription instead.
URL discounts
A code arrives in the funnel URL (/f/spring?code=SPRING25) and is priced on
the server only: it must exist (case-insensitive), be unexpired, cover the
plan (plans) and the currency (amountOff), have uses left, and leave
something to charge. It is charged as a price override, so it works on every
gateway. maxRedemptions is held by one conditional UPDATE, so concurrent
checkouts never pass it; a checkout the gateway refused gives its use back.
An abandoned checkout holds its use for discountHoldMs (default 1 hour):
when a code looks used up, open sessions older than that are marked
expired and their uses freed. A payment captured after that still grants
the plan and takes a use again, so a limited code can go over by those late
payers only.
Redemption
const r = await funnels.redeem(sessionId, userId, { orgId })
// { redeemed: true, already: false, session } | { redeemed: false, reason }The session is claimed for one account with a conditional UPDATE, then
customers.merge moves the anonymous buyer's plan, payments and timeline to
it. Repeating it for the same account returns already: true and finishes a
merge a crash left half done; another account gets already_redeemed; of
two accounts racing, exactly one wins. A refused merge (other_org,
not_member) frees the claim. Before payment it is not_paid; after a
refund, refunded. The redemption token (signed, expiring) is
signet-server's: funnelRoutes checks it before calling this.
Funnel analytics
Every step is an event named funnel:<key>:<step>: one per question, then
plans, checkout, purchased, redeemed, with the browser's analytics
id so the purchase joins the visit. Drop-off:
analytics.funnel({ steps: funnelSteps(def) }). Analytics never fails a
purchase.
Feature flags
import { flagOverrides } from "@dapplelight/signet-db/columns/sqlite"
import { createFlags, defineFlags } from "@dapplelight/signet-db/flags"
const flags = defineFlags({
newEditor: {
default: false,
rules: [
{ when: { plan: ["team"] }, value: true },
{ when: { attributes: { country: ["IN", "LK"] } }, value: true },
],
rollout: { percent: 10, value: true },
},
})
const ff = createFlags(db, flags, { table: schema.flagOverrides, entitlements: ent })
await ff.get("newEditor", { id: userId, attributes: { country } }) // { value, reason: "rule", rule: 1 }
await ff.all({ id: userId })
await ff.set("newEditor", true, { subject: "*", actor }) // everyone; or one subject id
await ff.clear("newEditor", { subject: "*", actor })First match wins: the subject's override, the everyone ("*") override,
rules in order, the rollout, the default. The rollout bucket is FNV-1a of
salt:subjectId (salt defaults to the flag key) in 0–9999, so a subject keeps
its answer, raising the percent only adds subjects, and changing salt
reshuffles. evaluateFlag(key, def, subject, override?) is the same order as
a pure function. With entitlements, a subject without plan gets its plan
looked up. Overrides can expire and are audited. Every result says why.
Tools for agents
import { entitlementTools } from "@dapplelight/signet-db/entitlements"
import { approvalGate } from "@dapplelight/signet-server/mcp"
const tools = entitlementTools({
entitlements: ent,
flags: ff,
subject: orgId, // or allSubjects: true for an operator agent; tools then take `subject`
allowWrites: true, // default false: read tools only
actor: { type: "agent", id: "support-agent" },
approve: approvalGate,
})Read: get_plan, check_entitlement, usage, list_flags. Write (with
allowWrites): grant_feature, set_plan, set_flag. Writes always call
approve; without one they change nothing and return APPROVAL_REQUIRED.
approvalGate is signet-server's one approval step: it asks in the client,
or, with createMcpServer({ approvals }), opens a pending approval the
person decides in the app (see signet-server's "Approvals").
They are audited as actor. Give writes only to agents run by staff: on a
per-user agent the person approving is the user who benefits.
Experiments
A/B/n tests on top of feature flags. Tables: experimentTables() from
/columns/sqlite or /columns/pg (experiments, experimentExposures);
conversions are read from analyticsEvents().
import { analyticsEvents, experimentTables } from "@dapplelight/signet-db/columns/sqlite"
import { createExperiments } from "@dapplelight/signet-db/experiments"
export const { experiments, experimentExposures } = experimentTables()
const ex = createExperiments(db, { tables: { experiments, experimentExposures }, events: analyticsEvents() })
await ex.create("onboarding-copy", {
variants: [{ key: "control", weight: 50 }, { key: "short", weight: 50, value: { steps: 3 } }],
goal: "signup_completed", // an analytics event name
audience: { plan: ["free"] }, // optional: a flag rule's `when`
}, { actor })
await ex.start("onboarding-copy", { actor })
await ex.expose("onboarding-copy", { id: userId }) // { variant: "short", value: { steps: 3 }, reason: "assigned", first: true }
await ex.results("onboarding-copy")
// { control: "control", variants: [{ key, exposed, converted, rate, lift, pValue, significant }] }
await ex.rollout("onboarding-copy", "short", { actor }) // everyone gets "short" from now on- Assignment is sticky with no storage: the flags'
bucket(FNV-1a ofsalt:subjectId) laid over the cumulative weights. Variants, weights and salt can change only while a draft, so a subject never switches variant. assignreads the variant;exposealso records the first exposure (once per subject, only while running). Callexposewhere the person actually sees the variant, so the readout counts only them.- A conversion is the subject's goal event (
userIdoranonymousIdequal to the subject id) at or after their exposure.results(key, { goal })reads another event;alphadefaults to 0.05. - Each variant is compared with the control (the first variant) by a
two-sided two-proportion z-test. With many variants, lower
alpha. - Lifecycle:
draft→start→running⇄stop→rollout→rolled_out. Draft and stopped give the control (not_running); outside the audience is the control (not_eligible); rolled out gives the winner. A wrong move isCONFLICT, as one conditional UPDATE. - A variant's
valueis JSON the app reads. Pricing experiments put an offering key there ({ offering: "annual-first" }) for a placement to serve; see Pricing experiments. - signet-server's
experimentRoutes/experimentAdminRoutesserve it; signet-ui'sExperimentResultsis the admin readout andExperimentFormcreates and edits one.
Pricing experiments
A placement runs an experiment whose variants serve different offerings (or
pinned versions). Pass experiments to createOfferings, link the
experiment on the placement, and read it out with createPricing from
@dapplelight/signet-db/pricing. Placements gain an experiment column:
on an existing database, ALTER TABLE placements ADD COLUMN experiment text.
import { createPricing } from "@dapplelight/signet-db/pricing"
const ex = createExperiments(db, { tables: { experiments, experimentExposures } })
const offerings = createOfferings(db, { tables: { offerings: offeringsTable, placements }, plans, experiments: ex })
const pricing = createPricing(db, {
tables: { experimentExposures, customerEvents, customerIdentities },
offerings,
experiments: ex,
})
await ex.create("annual-first", {
variants: [
{ key: "control", weight: 50 }, // no value: the placement's own offering
{ key: "annual", weight: 50, value: { offering: "annual-first", version: 2 } },
],
goal: "subscribed",
}, { actor })
await offerings.setPlacement("onboarding", { offering: "default", experiment: "annual-first" }, { actor })
await ex.start("annual-first", { actor })
await offerings.current("onboarding", audience, subjectId)
// { key: "annual-first", version: 2, ..., experiment: { key: "annual-first", variant: "annual", reason: "assigned" } }
await pricing.readout("annual-first")
// { placements: ["onboarding"], variants: [{ key, offering, exposed, converted, rate, lift, pValue,
// significant, trials, trialConverted, trialConversion, revenue, revenuePerExposed, ltv }] }
await pricing.rollout("annual-first", "annual", { actor })
// the placement now serves annual-first v2 directly; the experiment is rolled_outcurrent(placement, audience, subjectId)assigns through the experiment (sticky, no storage) and records the exposure once, for everyone no audience override matched. With no subject id it serves the placement's own offering and records nothing. A variant whose offering is gone falls back to the placement's own.setPlacementchecks the experiment exists, is not rolled out, and that every variant's value is{ offering, version? }naming a published offering (or no value). It replaces the whole placement: leaving outexperimentunlinks it.- Attribution needs no extra writes: a subject's purchases are their
customer timeline events (
customers.record) at or after the exposure, followed through identity merges whencustomerIdentitiesis passed. Conversion is a trial or purchase; trial conversion is a paid charge after the trial; revenue is charges minus refunds, per currency in minor units, divided by everyone exposed. Conversion gets the same z-test asresults. ltvis an estimate, labelled so: the average charge times the expected number of payments,1 / churnwhere churn is subscriptions ended (expired or refunded) per paid charge, capped atmaxPayments(24).rolloutrolls the experiment out first, then points every placement running it at the winner's offering (the placement's own for a variant with none) and unlinks it; a retry after a partial failure is safe.- signet-server's
monetisationAdminRoutes({ pricing })serves the readout and rollout; signet-ui'sPricingExperimentResultsshows them.
Status page and maintenance
Tables: statusTables() (statusComponents, statusIncidents, maintenance).
import { statusTables } from "@dapplelight/signet-db/columns/sqlite"
import { createStatus } from "@dapplelight/signet-db/status"
export const { statusComponents, statusIncidents, maintenance } = statusTables()
const status = createStatus(db, { tables: { statusComponents, statusIncidents, maintenance } })
await status.setComponent("api", { name: "API", position: 1 }, { actor })
const inc = await status.openIncident(
{ title: "Slow sign-in", impact: "minor", components: ["api"], message: "Looking into it" },
{ actor },
)
await status.updateIncident(inc.id, { status: "resolved", message: "Fixed" }, { actor })
await status.setMaintenance({ on: true, message: "Upgrading the database", until }, { actor })
await status.summary() // { status, components, incidents, maintenance }- Component statuses (
operational,maintenance,degraded,partial_outage,major_outage) are set by hand. The overall status is the worst of them and of open incidents' impact (minordegraded,majorpartial outage,criticalmajor outage); maintenance mode shows unless something is worse. - Incident updates are appended, never edited;
resolvedcloses the incident, after which it cannot change.summarylists open incidents and those resolved in the last 7 days (dayschanges it). - Every write is audited.
maintenance()is what signet-server'smaintenanceModemiddleware reads. - signet-ui's
StatusPageis the public page andStatusAdminthe admin screen (maintenance switch, component statuses, incidents and updates).
Organisations and tenant scope
import { orgTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
export const { organizations, memberships, invitations, orgRoles } = orgTables()
export const projects = sqliteTable("projects", { id: id(), orgId: text("org_id").notNull(), name: text("name").notNull() })
import { createOrgs } from "@dapplelight/signet-db/orgs"
const orgs = createOrgs(db, { tables: { organizations, memberships, invitations, orgRoles } })
const org = await orgs.create({ name: "Acme", ownerId: userId }) // creator is the owner
await orgs.update(org.id, { domain: "app.acme.example", brandColor: "#12833c" }, { by: userId })
const { token } = await orgs.invite({ orgId: org.id, role: "member", invitedBy: userId, email })
await orgs.acceptInvite(token, { userId: other, email }) // { ok, membership, org } | { ok: false, reason }
await orgs.can(org.id, other, "members:invite") // false
await orgs.saveRole(org.id, "billing", ["billing:read"], { by: userId }) // a custom role in this org
const data = scoped(db, { orgId: org.id }) // org_id on every where and insert
const mine = scoped(db, { userId, orgId: org.id }) // both- Roles map to permission sets (
DEFAULT_ORG_ROLES: owner*, admin, member;defineRolesfor your own).members:*matches everymembers:permission. Custom roles live per org inorgRoles. - With
{ by }, a change is checked against the actor: they need the permission (ORG_PERMISSIONS) and cannot hand out a role with permissions they lack. An org always keeps one owner. - Invite tokens are 256 random bits shown once; only the SHA-256 is stored.
They expire (
inviteTtlMs, default 7 days), work once and can be revoked. byDomain(host)andbySlug(slug)resolve a tenant; signet-server'stenant()middleware uses them.- An org scope is deny-by-default: a table without
orgId(and without auserIdthe scope also fixes) throwsNOT_TENANT_TABLE; writes that name another org throwCROSS_ORG_WRITE. defineAdminwithctx.orgIdshows only tables withorgId, limits every op to that org and puts creates in it;tenant: truerequires an org.- With an audit table, every write is logged:
org.create,org.update,member.add,member.role,member.remove,member.leave,role.create,role.update,role.delete,invite.create,invite.accept,invite.revoke. Each entry has the actor (by, else the system), and before/after snapshots carryingorgId. Invite tokens and hashes are never logged. Without an audit table nothing is written. - Failures are
OrgError(aSignetDbErrorwith an HTTPstatus).
Growth: waitlist, invites, referrals, onboarding, changelog, feedback
import { growthTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createGrowth } from "@dapplelight/signet-db/growth"
export const { waitlist, invites, referralCodes, referrals, marks, feedback } = growthTables()
const growth = createGrowth(db, {
tables: { waitlist, invites, referralCodes, referrals, marks, feedback },
inviteTtlMs: 7 * 86_400_000, // the default
})
// Waitlist: idempotent, position among people still waiting
await growth.joinWaitlist(email, { source: "launch", referralCode }) // { email, position, joined }
await growth.waitlistStats() // { total, pending, invited }
await growth.markInvited([email]) // then send them an invite
// Invites: the token is returned once; only its SHA-256 is stored
const { invite, token } = await growth.createInvite({ invitedBy: userId, scope: orgId, role: "member", email })
await growth.acceptInvite(token, { userId, email }) // { ok: true, invite, already } | { ok: false, reason }
await growth.revokeInvite(invite.id)
// Referrals: one referrer per person, no self-referral, reward once
const code = await growth.referralCode(userId) // 8 Crockford base32 characters, stable
await growth.refer(code, newUserId) // at sign-up
const c = await growth.convertReferral(newUserId) // at first payment
if (c?.first) { /* reward c.referral.referrer */ await growth.markRewarded(newUserId) }
// Onboarding and "what's new"
await growth.completeStep(userId, "profile")
await growth.onboarding(userId, ["profile", "invite", "first-project"]) // { done, next, complete, percent }
await growth.unseenChangelog(userId, entries) // entries newer than the last markChangelogSeen
await growth.markChangelogSeen(userId)
// Feedback
await growth.sendFeedback({ subject: userId, kind: "bug", message, rating: 4, page: "/settings" })
await growth.listFeedback({ status: "new" })acceptInvite fails with not_found, expired, used (by someone else),
revoked or email_mismatch (the invite names an email and the caller's
does not match). The claim is one conditional UPDATE, so two people
cannot accept the same invite. What accepting grants (org membership, a role)
is the app's: read invite.scope and invite.role. marks is one timestamp
per subject and key; onboarding uses onboarding:<step> keys and the
changelog changelog:seen, and mark/marksOf are there for your own
(tours dismissed, tips seen). Feedback kinds default to
DEFAULT_FEEDBACK_KINDS (bug, idea, praise, other); pass feedbackKinds to
change them. These writes are the user's own actions and are not audited.
Tools for agents
import { growthTools } from "@dapplelight/signet-db/growth"
const tools = growthTools({ growth, allowWrites: true, invitedBy: staffId, approve })For an operator agent. Read: waitlist_stats, list_waitlist,
list_feedback, referral_stats, list_invites. Write (with
allowWrites): create_invite, revoke_invite, set_feedback_status, each
gated on approve like the entitlement tools.
DPDP: consent and data requests
import { privacyTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createPrivacy } from "@dapplelight/signet-db/privacy"
export const { consents, dataRequests } = privacyTables()
const privacy = createPrivacy(db, {
tables: { consents, dataRequests },
userTables: [notes, tags, [users, users.id]], // what export and erasure cover, children first
respondWithinMs: 30 * 86_400_000, // optional: sets dueAt; no default
})
await privacy.give(userId, "marketing-email", { noticeVersion: "2026-11-01", source: "signup" })
await privacy.has(userId, "marketing-email", { noticeVersion: "2026-11-01" })
await privacy.withdraw(userId, "marketing-email") // or withdraw(userId) for every purpose
await privacy.history(userId) // every record, withdrawn ones included
const req = await privacy.requestErasure(userId) // or requestExport; one open request per kind
await privacy.overdue() // pending past dueAt
await privacy.fulfil(req.id, { actor: { type: "user", id: staffId } })
await privacy.reject(req.id, { actor, reason: "Could not verify identity" })Consent records are append-only: each says which purpose, under which
notice version, how, and when it was given and withdrawn. Giving again under
the same notice returns the record in force; under a new notice it adds one.
fulfil on an export returns the data from exportUser (send it to the
person; it is not stored). On an erasure it runs deleteUser over
userTables (audited when the db has an audit table), withdraws every
consent, and stores the row counts. Consent and
