Skip to content

Quick Start

This walkthrough takes you from nothing to generated code.

1. Create a config file

Create db-gen.json in your project. The minimum is a connection, an output folder, a template, and type mappings:

{
  "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" },
    { "DatabaseTypes": ["*"], "MappedType": "object", "MappingFunction": "GetValue" }
  ]
}

Keep secrets out of source control

Put your ConnectionString in a local override file (local.db-gen.json) that you .gitignore. db-gen merges it over the main config automatically. See Configuration → local overrides.

The "*" mapping is a catch-all so an unmapped type doesn't stop generation.

2. Write a template

A trivial templates/dbcontext.gotmpl that lists the generated functions:

// Generated by db-gen {{.BuildInfo.Version}}
public partial class DbContext
{
{{- range .Functions}}
    // {{.Schema}}.{{.DbFunctionName}} -> {{.FunctionName}}
{{- end}}
}

See Templating for the full data model and helper functions.

3. Generate

db-gen generate --config db-gen.json

db-gen connects, reads your routines, applies mappings, and writes files into ./generated. It also prints any schema changes since the last run.

4. Add models and processors (optional)

Turn on the other core outputs:

{
  "GenerateModels": true,
  "GenerateProcessors": true,
  "ModelTemplate": "./templates/model.gotmpl",
  "ProcessorTemplate": "./templates/processor.gotmpl"
}

Where to go next