Skip to content

db-gen

A language-agnostic CLI that generates database-access code from PostgreSQL stored functions and procedures — consistent, customizable, and fully offline-capable.

db-gen connects to a PostgreSQL database, reads the metadata of your stored functions and procedures, and generates the boilerplate code that calls them — in whatever language and style your templates define. It is not an ORM: you keep writing SQL functions, and db-gen writes the typed call/mapping code around them.

Everything db-gen needs — configuration and templates — lives in your repository. The tool is a single self-contained executable with no runtime dependencies, no internet requirement, and no framework lock-in.


Feature highlights

  • Language-agnostic — output is driven entirely by Go templates you control; C#, Go, Elixir, TypeScript, anything.
  • DbContext / Models / Processors core generation, plus an extensible Additional Generators framework for any other output (TypeScript types, providers, WebSocket endpoints, …).
  • Three-tier type mapping — distinct types for base, nullable returns, nullable params, and optional (DEFAULT) params.
  • Context parameter injection — map _user_id, created_by, tenant_id, … to a context object instead of threading them through call sites.
  • Copy targets — generate bulk-COPY-into-staging-table code from a table's columns, language-agnostically.
  • Validation — reusable, strategy-agnostic parameter validation rules.
  • Schema change detection — see what changed in your routines and copy-target tables since the last generation.
  • Offline generation — export routine metadata to JSON and generate without a database connection.

Quick start

db-gen generate --config db-gen.json
{
  "ConnectionString": "postgresql://user:pass@localhost:5432/mydb",
  "OutputFolder": "./generated",
  "DbContextTemplate": "./templates/dbcontext.gotmpl",
  "GeneratedFileExtension": ".cs",
  "GeneratedFileCase": "pascalcase",
  "Generate": [{ "Schema": "public", "AllFunctions": true }],
  "Mappings": [
    { "DatabaseTypes": ["int4"], "MappedType": "int", "MappingFunction": "GetInt32" },
    { "DatabaseTypes": ["text"], "MappedType": "string", "MappingFunction": "GetString" }
  ]
}

See Getting Started → Quick Start for the full walkthrough.


Documentation map

  • Getting Started — what db-gen is, how to install it, and a first run.
  • Guide — configuration, templating, type mapping, context mapping, copy targets, validation, offline & change detection.
  • Reference — CLI commands and flags, every config key, the template data model.
  • Examples — copy-paste recipes for common setups.

Source of truth

This site documents the tool at github.com/KeenMate/db-gen. Config keys and template fields are validated against that source.