Skip to content

Schema Generation

Rebase uses a schema-as-code pipeline where your TypeScript collection definitions are the single source of truth. The CLI transforms them through a deterministic pipeline:

Collections (TypeScript) → Drizzle Schema → SQL Migrations → PostgreSQL

This page covers every CLI command involved in that pipeline.

Your collection definitions in config/collections/ describe tables, columns, types, relations, and enums. The schema generate command reads these and outputs a Drizzle ORM schema file.

From the generated Drizzle schema, db generate diffs against the current database state and produces timestamped SQL migration files.

The db migrate command applies pending migrations to your PostgreSQL database.

Generate a Drizzle ORM schema file from your collection definitions:

rebase schema generate

What it does:

  • Reads all collections from config/collections/
  • Generates backend/src/schema.generated.ts with Drizzle table definitions, enums, and relations

Options:

FlagDescription
--collections, -cPath to collections directory (default: config/collections/)
--output, -oOutput path for the generated schema file
--watch, -wWatch for changes and regenerate automatically

Watch mode is useful during development — edit a collection file and the schema regenerates instantly:

rebase schema generate --watch

Reverse-engineer collection definitions from an existing PostgreSQL database:

rebase schema introspect

What it does:

  • Connects to your database (using the connection string from your .env)
  • Inspects all tables, columns, types, and foreign keys
  • Generates collection definition files

Options:

FlagDescription
--output, -oOutput directory for generated collection files

This is useful when adopting Rebase on an existing database — introspect first, then customize the generated collections.

Push schema changes directly to the database without migration files:

rebase db push

What it does:

  • Reads the generated Drizzle schema
  • Applies changes directly to the database (CREATE, ALTER, DROP)
  • Does not create migration files

Generate SQL migration files from schema changes:

rebase db generate

What it does:

  • Compares the Drizzle schema against the current database state
  • Produces timestamped SQL migration files in the drizzle/ directory
  • Files can be reviewed, edited, and committed to version control

The generated migrations are plain SQL files — you can inspect and modify them before applying.

Run all pending migrations:

rebase db migrate

What it does:

  • Reads the drizzle/ directory for unapplied migrations
  • Applies them in order to the database
  • Tracks which migrations have been applied

Open Drizzle Studio to browse and edit your database visually:

rebase db studio

Database branching for parallel development:

rebase db branch create feature_auth
rebase db branch list
rebase db branch delete feature_auth

Detect three-way drift between your collection definitions, the generated Drizzle schema, and the live PostgreSQL database:

rebase doctor

What it checks:

  • Collections ↔ Generated schema — are they in sync?
  • Generated schema ↔ Database — are there unapplied changes?
  • Collections ↔ Database — is there any unexpected drift?

Run doctor whenever something feels out of sync. It pinpoints exactly where the mismatch is.

Generate a typed client SDK from your collection definitions:

rebase generate-sdk

What it does:

  • Reads collections from config/collections/ (supports index.ts barrel exports or individual files)
  • Generates TypeScript types for all entities in generated/sdk/
  • Produces a database.types.ts file for use with createRebaseClient<Database>()

Options:

FlagDescription
--collectionsPath to collections directory
--outputOutput directory for the SDK (default: generated/sdk/)

Usage after generation:

import { createRebaseClient } from "@rebasepro/client";
import type { Database } from "./generated/sdk/database.types";
const client = createRebaseClient<Database>({
baseUrl: "http://localhost:3001",
});
// Full type safety and autocomplete
const { data } = await client.data.products.find();

The fast-iteration workflow for development:

# 1. Edit your collection in config/collections/
# 2. Generate the Drizzle schema
rebase schema generate
# 3. Push directly to dev database
rebase db push

The safe, reviewable workflow for production:

# 1. Edit your collection in config/collections/
# 2. Generate the Drizzle schema
rebase schema generate
# 3. Generate SQL migration files
rebase db generate
# 4. Review the generated SQL in drizzle/
# 5. Commit the migration to version control
git add drizzle/
# 6. Apply in production
rebase db migrate
SymptomSolution
Could not detect an active database pluginInstall @rebasepro/server-postgresql in backend/package.json
Schema file not updatingCheck the --collections path points to the right directory
Migration shows unexpected changesRun rebase doctor to identify drift
db push fails on productionUse db generate + db migrate instead