Usage-based billing database schema
Meters, usage events and rate cards that roll up into invoices.
6 tables, 6 relationships
Opens as a new diagram you can change. Free.
Tables
customers
- iduuid not null
- nametext not null
- emailtext not null
- created_attimestamptz not null
meters
- iduuid not null
- keytext not null
- unittext not null
- aggregationtext not null
- created_attimestamptz not null
rate_cards
- iduuid not null
- meter_id> metersuuid not null
- price_per_unit_centsnumeric(12,4) not null
- starts_attimestamptz not null
- ends_attimestamptz
- created_attimestamptz not null
usage_events
- iduuid not null
- customer_id> customersuuid not null
- meter_id> metersuuid not null
- quantitynumeric(14,4) not null
- occurred_attimestamptz not null
- idempotency_keytext not null
- created_attimestamptz not null
invoices
- iduuid not null
- customer_id> customersuuid not null
- period_startdate not null
- period_enddate not null
- total_centsinteger not null
- created_attimestamptz not null
invoice_lines
- iduuid not null
- invoice_id> invoicesuuid not null
- meter_id> metersuuid not null
- quantitynumeric(14,4) not null
- amount_centsinteger not null
- created_attimestamptz not null
As PostgreSQL
The same diagram exports to MySQL, MariaDB, SQLite, SQL Server and Oracle, and to Prisma, Drizzle, SQLAlchemy and Convex models.
CREATE TABLE "customers" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "invoice_lines" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"invoice_id" UUID NOT NULL,
"meter_id" UUID NOT NULL,
"quantity" NUMERIC(14,4) NOT NULL,
"amount_cents" INTEGER NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "invoices" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"customer_id" UUID NOT NULL,
"period_start" DATE NOT NULL,
"period_end" DATE NOT NULL,
"total_cents" INTEGER NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "meters" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"key" TEXT NOT NULL UNIQUE,
"unit" TEXT NOT NULL,
"aggregation" TEXT DEFAULT 'sum' NOT NULL,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "rate_cards" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"meter_id" UUID NOT NULL,
"price_per_unit_cents" NUMERIC(12,4) NOT NULL,
"starts_at" TIMESTAMPTZ NOT NULL,
"ends_at" TIMESTAMPTZ,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
CREATE TABLE "usage_events" (
"id" UUID DEFAULT gen_random_uuid() NOT NULL,
"customer_id" UUID NOT NULL,
"meter_id" UUID NOT NULL,
"quantity" NUMERIC(14,4) NOT NULL,
"occurred_at" TIMESTAMPTZ NOT NULL,
"idempotency_key" TEXT NOT NULL UNIQUE,
"created_at" TIMESTAMPTZ DEFAULT now() NOT NULL,
PRIMARY KEY ("id")
);
ALTER TABLE "rate_cards" ADD CONSTRAINT "fk_rate_cards_meter_id" FOREIGN KEY ("meter_id") REFERENCES "meters" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "usage_events" ADD CONSTRAINT "fk_usage_events_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "usage_events" ADD CONSTRAINT "fk_usage_events_meter_id" FOREIGN KEY ("meter_id") REFERENCES "meters" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "invoices" ADD CONSTRAINT "fk_invoices_customer_id" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "invoice_lines" ADD CONSTRAINT "fk_invoice_lines_invoice_id" FOREIGN KEY ("invoice_id") REFERENCES "invoices" ("id") ON DELETE CASCADE ON UPDATE NO ACTION;
ALTER TABLE "invoice_lines" ADD CONSTRAINT "fk_invoice_lines_meter_id" FOREIGN KEY ("meter_id") REFERENCES "meters" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION;More SaaS & accounts templates
- SaaS starterOrganisations, users and memberships for a multi-tenant app.
- Authentication & sessionsUsers with linked identities, sessions, password resets and MFA factors.
- Roles & permissionsRole-based access control: roles, permissions and who holds them.
- Subscription billingCustomers, plans and prices, subscriptions, invoices and payments.
- Feature flagsFlags per environment, targeting rules and per-user overrides.
- Audit logWho did what to which record, with before and after values.
- NotificationsNotification types, per-user preferences, an inbox and delivery attempts.
- API platformDeveloper apps with API keys, webhooks and usage records.
- Product analyticsUsers, sessions and tracked events with properties, plus saved funnels.
- Sessions and rate limits (DynamoDB)Sign-in sessions and per-key request counters, each removed by DynamoDB's TTL when it lapses.
Start with one diagram, free
One diagram with two branches, no card needed. Upgrade when your team needs more.