CLI
The restura-cli package provides a command-line interface for working with a Restura schema outside of the server. It can regenerate TypeScript types from a schema file, diff a schema against a live Postgres database to produce the SQL needed to bring them in sync, reset the scratch database to match the schema, and generate the full SQL CREATE TABLE schema for bootstrapping a new database.
Prerequisites
Section titled “Prerequisites”@restura/core must be built before using the CLI:
pnpm --filter @restura/core buildInstall dependencies from the workspace root:
pnpm installTo make the restura command available globally:
cd apps/restura-clipnpm link --globalCommands
Section titled “Commands”types (alias: t)
Section titled “types (alias: t)”Regenerates api.d.ts, models.d.ts, and restura.d.ts from a Restura schema file.
# Uses restura.schema.json in the current directoryrestura types --output ./generated-typesrestura t -o ./generated-types
# Explicit schema pathrestura types --schema ./path/to/restura.schema.json --output ./generated-typesrestura t -s ./path/to/restura.schema.json -o ./generated-types| Flag | Alias | Description | Required |
|---|---|---|---|
--schema <path> | -s | Path to the restura.schema.json file | No (defaults to restura.schema.json in cwd) |
--output <dir> | -o | Output directory for generated .d.ts files | No (defaults to .) |
diff (alias: d)
Section titled “diff (alias: d)”Diffs a restura.schema.json against a live database and prints the SQL needed to bring the database in line with the schema. The diff engine introspects the live database directly via pg_catalog and information_schema — no scratch database is needed.
Output goes to stdout so it can be inspected, piped, or redirected as needed.
# Uses restura.schema.json in the current directoryRESTURA_DB_URL=postgresql://user:pass@localhost:5432/mydb restura diffRESTURA_DB_URL=postgresql://user:pass@localhost:5432/mydb restura d
# Explicit schema pathRESTURA_DB_URL=postgresql://user:pass@localhost:5432/mydb restura diff --schema ./path/to/restura.schema.jsonRESTURA_DB_URL=postgresql://user:pass@localhost:5432/mydb restura d -s ./path/to/restura.schema.json
# Redirect output to a fileRESTURA_DB_URL=... restura diff > migrations/001_changes.sql| Flag | Alias | Description | Required |
|---|---|---|---|
--schema <path> | -s | Path to the restura.schema.json file | No (defaults to restura.schema.json in cwd) |
Supported diff operations
Section titled “Supported diff operations”Table drops
DROP TABLE— suppresses redundant index, FK, and check constraint drops that would be implicit
Table creates
CREATE TABLEwith FK constraints and check constraints inlined in the statement- Tables are created in dependency order based on FK relationships
- Self-referencing FKs are inlined in the
CREATE TABLE - Circular FK references are handled by deferring one side to
ALTER TABLE ADD CONSTRAINT
Column changes
ADD COLUMN/DROP COLUMNALTER COLUMNfor nullability and default changesALTER COLUMN TYPEforVARCHARlength changes (adding, removing, tightening, or widening)ALTER COLUMN TYPEforDECIMALprecision and scale changes
Index changes
CREATE INDEX/DROP INDEX- Index is rebuilt (
DROP+CREATE) when columns, uniqueness, sort order, orWHEREclause changes
Foreign key changes
ALTER TABLE ADD CONSTRAINT ... FOREIGN KEYfor new FKs on existing tablesALTER TABLE DROP CONSTRAINTfor removed FKs
Check constraint changes
ALTER TABLE ADD CONSTRAINT ... CHECK/ALTER TABLE DROP CONSTRAINTwhen the expression changesENUMcolumn value sets are normalized and diffed against PostgresANY(ARRAY[...])syntax, so only actual value-set changes trigger a rebuild
reset-scratch (alias: rs)
Section titled “reset-scratch (alias: rs)”Drops and recreates the scratch database’s public schema, then rebuilds it from the schema file. This is the same operation the Restura UI performs when you click “Preview Schema” — it materializes the proposed schema into the scratch database so it can be compared against the live database.
The scratch database name is derived as {database}_scratch by default, or {database}_scratch_{suffix} when a suffix is provided. The suffix should match the scratchDatabaseSuffix value in your restura.config.
# Uses restura.schema.json in the current directoryrestura reset-scratchrestura rs
# With a suffix to match your restura.config scratchDatabaseSuffixrestura rs --suffix josh
# Explicit schema pathrestura reset-scratch --schema ./path/to/restura.schema.jsonrestura rs -s ./path/to/restura.schema.json --suffix josh| Flag | Alias | Description | Required |
|---|---|---|---|
--schema <path> | -s | Path to the restura.schema.json file | No (defaults to restura.schema.json in cwd) |
--suffix <suffix> | Scratch database suffix (matches scratchDatabaseSuffix in config) | No |
sql (alias: s)
Section titled “sql (alias: s)”Generates the full SQL CREATE TABLE schema from a restura.schema.json file. The output represents the complete database structure — useful for bootstrapping a new database or reviewing the schema as raw SQL.
Output goes to stdout so it can be inspected, piped, or redirected as needed.
# Uses restura.schema.json in the current directoryrestura sqlrestura s
# Explicit schema pathrestura sql --schema ./path/to/restura.schema.jsonrestura s -s ./path/to/restura.schema.json
# Redirect output to a filerestura sql > schema.sql| Flag | Alias | Description | Required |
|---|---|---|---|
--schema <path> | -s | Path to the restura.schema.json file | No (defaults to restura.schema.json in cwd) |
Building
Section titled “Building”Produces a single self-contained binary with no runtime dependency:
pnpm build