Schema

Your schema is the single source of truth for vibecode‑db. You declare tables and column kinds (and later, relations) once; vibecode‑db generates a Zod bundle for runtime validation and TypeScript types for compile‑time safety. Every adapter (Web/Expo/Supabase) reads from the same definition—so the API you write stays identical across environments.


What the schema gives you

  • db.zodBundle — Zod validators derived from your schema DSL
  • Type inference — typed results and inputs for queries/inserts/updates
  • db.relations — relation graph (when you declare foreign keys)
  • Adapter‑agnostic contract — the outputs feed your DBSpec

Mental model (no deep code here)

  1. Define schema with tables + column kinds (e.g., integer, varchar, boolean, timestamp, uuid, json, enum).
  2. Add relations by declaring foreign keys with references(...).
  3. Build DBSpec with { schema: db.zodBundle, relations: db.relations, seed?, meta? }.
  4. Create client with an adapter; the fluent query API is the same everywhere.

Detailed how‑to lives in the subsections: 4.1 Define Schema, 4.2 Relations & Referential Integrity, 4.3 Derived Types.


Tiny illustrative shape

1// schema.ts (illustrative only)
2import { defineSchema, vibecodeTable, col } from '@vibecode-db/client'
3
4export const db = defineSchema({
5  users: vibecodeTable('users', {
6    id: col.integer(),
7    name: col.varchar(),
8    email: col.varchar(),
9  }),
10  todos: vibecodeTable('todos', {
11    id: col.uuid(),
12    title: col.varchar(),
13    completed: col.boolean(),
14    created_at: col.timestamp(),
15    updated_at: col.timestamp(),
16    user_id: col.integer(), // FK column; relation declared in the next step
17  }),
18})
19// Outputs: db.zodBundle (validators), db.relations (after you declare FKs)

Where to next

  • 4.1 Define Schema — tables, column kinds, structure
  • 4.2 Relations & Referential Integrity — declare FKs and understand cardinalities
  • 4.3 Derived Types — how the schema drives typed inputs/outputs and projection shapes

Keep your schema focused and portable—one definition powering all adapters.