Schema Generation
Esta página aún no está disponible en tu idioma.
Overview
Section titled “Overview”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 → PostgreSQLThis page covers every CLI command involved in that pipeline.
The Pipeline
Section titled “The Pipeline”1. Collections → Drizzle Schema
Section titled “1. Collections → Drizzle Schema”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.
2. Drizzle Schema → Migrations
Section titled “2. Drizzle Schema → Migrations”From the generated Drizzle schema, db generate diffs against the current database state and produces timestamped SQL migration files.
3. Migrations → PostgreSQL
Section titled “3. Migrations → PostgreSQL”The db migrate command applies pending migrations to your PostgreSQL database.
Commands
Section titled “Commands”rebase schema generate
Section titled “rebase schema generate”Generate a Drizzle ORM schema file from your collection definitions:
rebase schema generateWhat it does:
- Reads all collections from
config/collections/ - Generates
backend/src/schema.generated.tswith Drizzle table definitions, enums, and relations
Options:
| Flag | Description |
|---|---|
--collections, -c | Path to collections directory (default: config/collections/) |
--output, -o | Output path for the generated schema file |
--watch, -w | Watch for changes and regenerate automatically |
Watch mode is useful during development — edit a collection file and the schema regenerates instantly:
rebase schema generate --watchrebase schema introspect
Section titled “rebase schema introspect”Reverse-engineer collection definitions from an existing PostgreSQL database:
rebase schema introspectWhat 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:
| Flag | Description |
|---|---|
--output, -o | Output directory for generated collection files |
This is useful when adopting Rebase on an existing database — introspect first, then customize the generated collections.
rebase db push
Section titled “rebase db push”Push schema changes directly to the database without migration files:
rebase db pushWhat it does:
- Reads the generated Drizzle schema
- Applies changes directly to the database (CREATE, ALTER, DROP)
- Does not create migration files
rebase db generate
Section titled “rebase db generate”Generate SQL migration files from schema changes:
rebase db generateWhat 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.
rebase db migrate
Section titled “rebase db migrate”Run all pending migrations:
rebase db migrateWhat it does:
- Reads the
drizzle/directory for unapplied migrations - Applies them in order to the database
- Tracks which migrations have been applied
rebase db studio
Section titled “rebase db studio”Open Drizzle Studio to browse and edit your database visually:
rebase db studiorebase db branch
Section titled “rebase db branch”Database branching for parallel development:
rebase db branch create feature_authrebase db branch listrebase db branch delete feature_authrebase doctor
Section titled “rebase doctor”Detect three-way drift between your collection definitions, the generated Drizzle schema, and the live PostgreSQL database:
rebase doctorWhat 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.
rebase generate-sdk
Section titled “rebase generate-sdk”Generate a typed client SDK from your collection definitions:
rebase generate-sdkWhat it does:
- Reads collections from
config/collections/(supportsindex.tsbarrel exports or individual files) - Generates TypeScript types for all entities in
generated/sdk/ - Produces a
database.types.tsfile for use withcreateRebaseClient<Database>()
Options:
| Flag | Description |
|---|---|
--collections | Path to collections directory |
--output | Output 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 autocompleteconst { data } = await client.data.products.find();Development Workflow
Section titled “Development Workflow”The fast-iteration workflow for development:
# 1. Edit your collection in config/collections/# 2. Generate the Drizzle schemarebase schema generate
# 3. Push directly to dev databaserebase db pushProduction Workflow
Section titled “Production Workflow”The safe, reviewable workflow for production:
# 1. Edit your collection in config/collections/# 2. Generate the Drizzle schemarebase schema generate
# 3. Generate SQL migration filesrebase db generate
# 4. Review the generated SQL in drizzle/# 5. Commit the migration to version controlgit add drizzle/
# 6. Apply in productionrebase db migrateTroubleshooting
Section titled “Troubleshooting”| Symptom | Solution |
|---|---|
Could not detect an active database plugin | Install @rebasepro/server-postgresql in backend/package.json |
| Schema file not updating | Check the --collections path points to the right directory |
| Migration shows unexpected changes | Run rebase doctor to identify drift |
db push fails on production | Use db generate + db migrate instead |
Next Steps
Section titled “Next Steps”- Collections — Define your data model
- CLI Reference — All CLI commands
- Client SDK — Use the generated SDK