Schema — Define Schema

This section shows how to declare your schema with vibecode‑db’s DSL. You define tables and column kinds in one place; vibecode‑db produces a Zod bundle and TypeScript types from that definition. (Relations exist, but are covered in the next subsection.)


Column kinds (current surface)

1integer | varchar | boolean | timestamp | uuid | json | enum

Declare kinds via the col helper from @vibecode-db/client.


Minimal schema (tables + columns)

1// db/schema.ts
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
11  todos: vibecodeTable('todos', {
12    id: col.uuid(),
13    title: col.varchar(),
14    completed: col.boolean(),
15    created_at: col.timestamp(),
16    updated_at: col.timestamp(),
17    user_id: col.integer(), // FK source column; relation added later
18  }),
19})
20// Outputs → db.zodBundle (validators), db.relations (after you declare FKs)

Keep model and column names stable. Your SQLite migrations will use the same names when creating tables and indexes.


Add relations (pointer only)

  • Relations connect a source column to a target column via references(...).
  • You’ll add them in 4.2 Relations & Referential Integrity.
  • Once declared, db.relations becomes available and should be included in your DBSpec.

Hand‑off to DBSpec

Use the schema outputs to assemble your DBSpec and pass it to createClient.

1// db/spec.ts
2import type { DBSpec } from '@vibecode-db/client'
3import { db } from './schema'
4
5export const dbSpec: DBSpec<typeof db.zodBundle.shape> = {
6  schema: db.zodBundle,
7  relations: db.relations, // after you add relations
8  // seed?: { ... }, meta?: { ... }
9}

Summary

Define tables with vibecodeTable('<name>', { ... }) and choose column kinds with col.<kind>(). Keep names stable, add relations next, and wire schema (+ relations) into your DBSpec for a portable, adapter‑agnostic setup.