database-schema
Skill2 starsChanged 6 months agoMITView on GitHub
Use when designing the Postgres schema or setting up Drizzle ORM — model users, organizations, memberships, and tenant-scoped tables with the right keys, indexes, timestamps, and migrations from day one.
3 other projects use a version of this.
Adopted by
Most recently taken up by
- SkillMedev/full-stack-web-devtheir own version, first seen Oct 5, 2026
- alinaqi/maggytheir own version, first seen Jun 12, 2026
- sunnypatneedi/claude-starter-kittheir own version, first seen Jan 23, 2026
Other versions3 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 3
- alinaqi/maggy★ 707same name
- sunnypatneedi/claude-starter-kit★ 11same name
- SkillMedev/full-stack-web-dev★ 1same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository14 other files it gives its agents.
- api-routes-and-validationSkill
- authenticationSkill
- authorization-rbacSkill
- background-jobsSkill
- data-access-layerSkill
- deployment-and-ciSkill
- environment-and-configSkill
- file-uploads-and-storageSkill
- multi-tenancySkill
- observability-and-errorsSkill
- payments-stripeSkill
- project-scaffoldingSkill
- subscription-billingSkill
- transactional-emailSkill
The file
We split this skill into the rules it gives an agent
1 rule. 0 are common, 1 is distinctive, and 0 are very specific to this project.
0
Common
Most of the 3 edited versions we read keep these.
1
Distinctive
This author's own choices. Worth a look.
0
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 75
Commit the generated SQL. Never hand-edit applied migrations.
“Common” is measured: kept by at least 6 in 10 of the 3 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Database Schema
Overview
Use Drizzle ORM + Postgres: schema is TypeScript, migrations are generated and version-controlled, and queries are fully typed. The single most important early decision for SaaS is the tenancy model — almost every table hangs off an organization. Model that now; retrofitting organization_id onto a live table is painful.
When to use
- Setting up the database layer of a new app.
- Adding any table — especially tenant-owned data.
- Migrating from an untyped query builder or raw SQL.
Core schema
// src/server/db/schema.ts
import { pgTable, uuid, text, timestamp, pgEnum, uniqueIndex, index } from "drizzle-orm/pg-core";
export const roleEnum = pgEnum("role", ["owner", "admin", "member"]);
export const users = pgTable("users", {
id: uuid("id").defaultRandom().primaryKey(),
email: text("email").notNull().unique(),
name: text("name"),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
});
export const organizations = pgTable("organizations", {
id: uuid("id").defaultRandom().primaryKey(),
name: text("name").notNull(),
slug: text("slug").notNull().unique(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
});
export const memberships = pgTable("memberships", {
id: uuid("id").defaultRandom().primaryKey(),
userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
orgId: uuid("org_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
role: roleEnum("role").notNull().default("member"),
}, (t) => ({
userOrg: uniqueIndex("memberships_user_org").on(t.userId, t.orgId),
}));
// Tenant-owned table: ALWAYS carry org_id + index it.
export const projects = pgTable("projects", {
id: uuid("id").defaultRandom().primaryKey(),
orgId: uuid("org_id").notNull().references(() => organizations.id, { onDelete: "cascade" }),
name: text("name").notNull(),
createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
}, (t) => ({
byOrg: index("projects_org_idx").on(t.orgId),
}));Rules of thumb
org_idon every tenant table, indexed, withonDelete: "cascade"from organizations.- UUID PKs (
defaultRandom) avoid enumeration and merge conflicts; usebigserialonly when you need ordered keys. timestamptzeverywhere withdefaultNow(). Store UTC; format in the UI.- Money as integer cents (or
numeric), never floats. - Index foreign keys and every column you filter/sort by. Postgres doesn't auto-index FKs.
Migrations
npx drizzle-kit generate # diff schema -> SQL migration
npx drizzle-kit migrate # apply to the databaseCommit the generated SQL. Never hand-edit applied migrations; add a new one.
Pitfalls
- Forgetting
org_id— the table becomes a cross-tenant leak waiting to happen. pushto production — use generated, reviewedmigrate;pushis for local prototyping.- Floats for money — rounding errors in billing. Use cents.
- No FK indexes — joins and cascades get slow as data grows.
- Naive timestamps —
timestampwithout timezone causes off-by-hours bugs across regions.
Hand-off
A typed, migratable schema. data-access-layer wraps it in tenant-scoped queries; multi-tenancy enforces isolation on top.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

