Skip to content

CLI Commands

db-gen <command> [flags]

Commands

Command Description
generate Connect to the database (or load a routines file), read routine metadata, apply filters/mappings, and generate code. Prints schema changes since the last generation.
routines [out] Read routines from the database and write their definitions to a JSON file (default ./db-gen-routines.json, or the out argument) for offline generation.
database-changes Compare the current database schema against the last generation and print what changed — without generating.
completion [bash\|zsh\|fish\|powershell] Print a shell-completion script.
version Print version and build information.
help [command] Print help for a command.

Flags

Available on the data commands (generate, routines, database-changes):

Flag Short Description
--config -s Path to the configuration file. If omitted, default locations are tried.
--connectionString -c PostgreSQL connection string. Overrides the value in config.
--debug -d Print debug logs and write debug files.

Examples

db-gen generate --config db-gen.json
db-gen generate -s db-gen.json -c "postgresql://user:pass@host:5432/db"
db-gen routines --config db-gen.json
# or to a specific path:
db-gen routines ./snapshot/routines.json --config db-gen.json
db-gen database-changes --config db-gen.json

Config file resolution

If --config is not given, db-gen tries, in order:

  1. ./db-gen.json
  2. ./db-gen/db-gen.json
  3. ./db-gen/config.json

Relative paths inside the config resolve against the config file's directory. See Configuration for local overrides.

How generate works

flowchart LR
    A[Load config + local override] --> B{Data source}
    B -->|Database| C[Connect to PostgreSQL]
    B -->|Offline| D[Load routines JSON]
    C --> E[Query information_schema]
    D --> E
    E --> F[Filter + map types]
    F --> G[Render templates]
    G --> H[Write output files]
    C --> I[Diff vs last generation] --> J[Print changes]