schema0-db-schema
Installation
SKILL.md
Database Schema
File Location
packages/db/src/
├── index.ts # Database client (createDb) and exports
├── schema/
│ ├── index.ts # Barrel re-exports (export * from "./{entity}")
│ └── [entity].ts # Table + all derived schemas (insert, select, update, form)
└── migrations/ # Auto-generated — never hand-write
7 Schemas Per Entity
Every entity file defines exactly 7 schemas:
- Table definition --
pgTable(...)withtext("id").primaryKey() - Insert schema --
createInsertSchema(table, overrides)with callback overrides for nullable columns - Select schema --
createSelectSchema(table, overrides)with callback overrides for nullable columns - Update schema --
selectSchema.partial().required({ id: true }) - Form schema --
insertSchema.omit({ id, createdAt, updatedAt }) - Edit form schema --
formSchema.partial()(NOid) - Router output schema --
z.object({...})matching DB return types
Key Rules
- Callback overrides for nullable columns:
colName: (schema) => schema.optional()in both insert and select schemas - Server-stamped fields (e.g.,
userId,organizationId,createdBy): keep.notNull()on the table but mark.optional()in the insert schema. The server fills them in from session context. Seereferences/schema-template.md> "Server-Stamped Fields". - NEVER use
.transform()-- breaks.omit()and.partial()chaining - NEVER use
serial()orbigintfor primary keys -- onlytext("id").primaryKey() - Router output: every column WITHOUT
.notNull()needs.nullable().optional() timestamp()returnsDateobjects -- usez.date()in router output, NOTz.string()- Form schemas belong in the entity file, not in
index.ts - Always use
createDb()per request -- never singleton
Column Types Quick Reference
id: text("id").primaryKey(),
name: text("name").notNull(),
email: varchar("email", { length: 255 }).unique(),
age: integer("age"),
price: decimal("price", { precision: 10, scale: 2 }),
active: boolean("active").default(true),
metadata: jsonb("metadata").$type<{ key: string }>(),
createdAt: timestamp("created_at").defaultNow().notNull(),
References
references/schema-template.md-- Full 7-schema code template with naming conventions, export, migration workflow, transform utilitiesreferences/column-types.md-- Column types reference, schema key rules, database client usage
Related skills
More from schema0/skills
schema0-dev
>-
23schema0-mobile
Mobile platform patterns — React Native / Expo, worker architecture, ORPC client, and navigation
17schema0-rls
Row-level security setup — RLS policies, authenticated database connections, and user-scoped data access
17schema0-ai
AI SDK integration with ORPC — chat streaming, prompt-response, tool calling, and provider configuration
17schema0-testing
Testing guide for web and mobile platforms — bun:test, Jest, PGlite, 3-layer validation, and test templates
17schema0-web-crud
Web frontend CRUD features — query collections, table columns, dialogs, forms, views, sidebar, and orchestration
17