Skip to content

Cookbook

Copy-paste recipes for common db-gen setups.

Minimal config

Generate just the DbContext call code, no models/processors:

{
  "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" }
  ]
}

Models + processors with nullable-aware types

{
  "GenerateModels": true,
  "GenerateProcessors": true,
  "ModelTemplate": "./templates/model.gotmpl",
  "ProcessorTemplate": "./templates/processor.gotmpl",
  "Mappings": [
    {
      "DatabaseTypes": ["int4"],
      "MappedType": "int",
      "MappingFunction": "GetInt32",
      "NullableReturnType": "int?",
      "NullableParameterType": "int?",
      "OptionalParameterType": "Optional<int>"
    }
  ]
}

See Type Mapping.

Select and override functions

Generate everything except a couple, rename one, and tweak a return column:

{
  "Generate": [
    {
      "Schema": "public",
      "AllFunctions": true,
      "Functions": {
        "internal_helper": false,
        "get_user_by_id": { "MappedName": "GetUser" },
        "get_report": {
          "Model": { "total": { "MappedType": "decimal", "IsNullable": true } }
        }
      }
    }
  ]
}

Context parameters

{
  "UseUserContext": true,
  "UserContextParameterName": "ctx",
  "UserContextType": "UserContext",
  "ContextParameterMappings": [
    { "ParameterNames": ["_user_id", "_userid"], "ContextPath": "User.UserId" },
    { "ParameterNames": ["created_by"], "ContextPath": "User.Username" }
  ]
}

See Context Mapping.

Generate TypeScript types alongside C

{
  "AdditionalGenerators": [
    {
      "Name": "TypeScript models",
      "Enabled": true,
      "Template": "./templates/typescript.gotmpl",
      "OutputFolder": "./generated/ts",
      "FileExtension": ".ts",
      "FileCase": "camelcase",
      "GenerationType": "per-routine",
      "CleanOutputFolder": true
    }
  ]
}

For a single aggregated file, set FileName and use "GenerationType": "single-file".

Copy targets (bulk COPY)

{
  "GenerateCopyTargets": true,
  "CopyTargetTemplate": "./templates/copy-pgx.gotmpl",
  "CopyTargets": [
    { "Schema": "stage", "Table": "data_to_process", "Format": "text", "NullString": "" }
  ],
  "ContextParameterMappings": [
    { "ParameterNames": ["created_by"], "ContextPath": "ctx.CreatedBy" },
    { "ParameterNames": ["job_run_id"], "ContextPath": "ctx.JobRunId" }
  ]
}

See Copy Targets.

Offline generation

db-gen routines --config db-gen.json   # writes db-gen-routines.json
# with "UseRoutinesFile": true in config
db-gen generate --config db-gen.json

See Offline & Change Detection.

Detect schema drift in CI

db-gen database-changes --config db-gen.json

Prints routine and copy-target changes since the last generation, without writing files.