Add Plans, Entitlements and Feature Flags
Plans with features and usage limits, billing events applied to them, and flags with rules and percentage rollouts.
Install
npm i @dapplelight/signet-db drizzle-ormDeclare plans and create the service
import { entitlementTables } from "@dapplelight/signet-db/columns/sqlite" // or /columns/pg
import { createEntitlements, definePlans } from "@dapplelight/signet-db/entitlements"
import { db } from "./db"
export const { subjectPlans, entitlementOverrides, usageCounters } = entitlementTables()
// Declare plans lowest first.
export 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 } },
})
export const ent = createEntitlements(db, {
plans,
default: "free",
tables: { subjectPlans, entitlementOverrides, usageCounters },
periods: { seats: "total" }, // aiCredits resets monthly by default
billing: { plans: { plan_Nx1: "pro", price_9f: "team" }, graceMs: 3 * 86_400_000 },
})The db needs an audit table: every write goes through audited.
Check and spend
import { app } from "../app"
import { ent } from "../entitlements"
app.post("/api/export", async (c) => {
const orgId = c.get("orgId")
if (!(await ent.can(orgId, "sso"))) return c.json({ error: "Upgrade to use SSO" }, 403)
const spent = await ent.consume(orgId, "aiCredits", 5) // { ok, used, limit, remaining, period }
if (!spent.ok) return c.json({ error: "Out of credits" }, 402)
return c.json({ ok: true })
})consume is one conditional update, so concurrent requests cannot overspend.
Apply billing events
import { createPayments } from "@dapplelight/signet-payments"
import { ent } from "./entitlements"
import { gateways, store } from "./gateways"
const payments = createPayments({
gateways,
db: store,
onEvent: (event) => ent.applyBillingEvent(event),
})
// At checkout, link the subscription to the subject on its first event:
export async function link(event: Parameters<typeof ent.applyBillingEvent>[0], orgId: string) {
await ent.applyBillingEvent(event, { subject: orgId })
}Grant and override by hand
import { ent } from "./entitlements"
export async function upgrade(orgId: string, adminId: string, trialEnd: Date) {
const actor = { type: "user", id: adminId } as const
await ent.setPlan(orgId, "pro", { actor })
await ent.grant(orgId, "audit", { actor, expiresAt: trialEnd })
}Add feature flags
import { createFlags, defineFlags } from "@dapplelight/signet-db/flags"
import { ent } from "./entitlements"
import { flagOverrides } from "./db/schema"
import { db } from "./db"
const flags = defineFlags({
newEditor: {
default: false,
rules: [
{ when: { plan: ["team"] }, value: true },
{ when: { attributes: { country: ["IN", "LK"] } }, value: true },
],
rollout: { percent: 10, value: true },
},
})
export const ff = createFlags(db, flags, { table: flagOverrides, entitlements: ent })
export async function newEditorFor(userId: string, country: string) {
return await ff.get("newEditor", { id: userId, attributes: { country } }) // { value, reason: "rule", rule: 1 }
}First match wins: the subject's override, the everyone override, the rules in order, the rollout, the default.
Next
Take Payments
Checkout across Razorpay and Stripe with a verified webhook inbox, a ledger, store purchases and daily reconciliation.
Add Remote Paywalls and a Customer Center
Offerings you publish without an app release, a paywall per placement, self-serve cancel and refund, and anonymous-to-account merge.