DEV.co
Open-Source Databases · xo

dbtpl

dbtpl is a command-line code generator for Go that introspects SQL database schemas and generates type-safe, idiomatic Go code. It supports PostgreSQL, MySQL, SQLite, Oracle, and MSSQL, and can produce models, schema scripts, JSON/YAML definitions, and Graphviz diagrams.

Source: GitHub — github.com/xo/dbtpl
3.9k
GitHub stars
334
Forks
Go
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryxo/dbtpl
Ownerxo
Primary languageGo
LicenseMIT — OSI-approved
Stars3.9k
Forks334
Open issues67
Latest releasev1.0.2 (2024-05-24)
Last updated2026-03-30
Sourcehttps://github.com/xo/dbtpl

What dbtpl is

dbtpl connects to databases via DSN, extracts schema metadata using SQL introspection queries, and applies Go templates to generate type definitions for tables, enums, stored procedures, and custom queries. It supports both schema-wide generation and query-specific code generation with optional type safety verification.

Quickstart

Get the dbtpl source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/xo/dbtpl.gitcd dbtpl# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

Rapid database-driven Go development

Quickly scaffold type-safe Go models and query types from existing or new database schemas, reducing boilerplate and ensuring Go types stay synchronized with schema changes.

Multi-database projects

Generate consistent, idiomatic Go code across PostgreSQL, MySQL, SQLite, Oracle, and MSSQL without writing dialect-specific code or maintaining multiple ORM configurations.

Custom query type generation

Parse ad-hoc SQL queries and automatically generate Go types for complex result sets with embedded parameter binding, improving type safety and reducing manual struct definition.

Implementation considerations

  • Generated code is claimed to be production-quality but requires manual review and integration; regeneration workflow must be documented and tested.
  • Schema introspection requires live database connectivity during code generation; secure credential management (DSN handling) is developer responsibility.
  • Enum and custom type support varies by database (PostgreSQL has best coverage; Oracle and MSSQL lack enum support)—verify feature matrix against your schema.
  • Customizable templates via `--src` flag allow branching from defaults, but maintaining parallel template versions across updates adds maintenance overhead.
  • Command-line flag complexity is high (60+ options for schema/query generation); pipeline integration requires careful parameter validation and testing.

When to avoid it — and what to weigh

  • Need non-Go language support — dbtpl explicitly supports Go only. Other languages are not currently planned, making this unsuitable for polyglot teams or non-Go backends.
  • Require a full ORM with runtime query builder — dbtpl generates types and schemas only; it does not provide query execution, relationship management, or migration tooling. You must write or integrate SQL and query logic separately.
  • Expect zero post-generation customization — README notes that dbtpl-generated code is not a 'silver bullet'—manual authoring of SQL and Go is still expected. Heavy schema changes require regeneration.
  • Working with unsupported databases — If your primary database is not PostgreSQL, MySQL, SQLite, Oracle, or MSSQL, dbtpl cannot introspect your schema.

License & commercial use

dbtpl is licensed under the MIT License, a permissive open-source license that permits commercial use, modification, and distribution with minimal restrictions (requires attribution and license notice).

MIT License permits commercial use without restriction. You may use dbtpl in proprietary projects, modify the source, and sell software built with generated code. No commercial licensing or fees required. Recommended practice: include MIT license notice in distributions.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceActive
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

dbtpl requires database credentials (DSN) for schema introspection; DSN handling is developer responsibility—secure storage, rotation, and access control must be enforced externally. Generated Go code contains no security-related features or protections; security of resulting application depends on developer-written SQL and business logic. No security audit, CVE history, or hardened-by-default settings documented.

Alternatives to consider

sqlc

Query-first code generator; type-safe query results from explicit SQL; narrower scope but tighter type safety; supports more databases and languages; active maintenance.

gorm

Full-featured ORM with code generation; includes query builder, hooks, migrations; heavier runtime footprint; different philosophy (ORM abstraction vs. code generation).

ent

Graph-based ORM with schema-first code generation; provides query builder and migrations; more opinionated; tighter coupling to ent ecosystem.

Software development agency

Build on dbtpl with DEV.co software developers

Evaluate dbtpl for your project by testing schema generation against your database. Review generated code quality, template customization options, and integration requirements before adopting into your CI/CD pipeline.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

dbtpl FAQ

Can dbtpl generate code for databases without an internet connection?
dbtpl requires live connectivity to the target database for schema introspection. Query mode can work with explicit SQL and introspection of related tables, but full schema introspection requires active database connection.
Does dbtpl support migration generation?
No. dbtpl can generate schema creation scripts (with `-t createdb`), but does not provide versioning, diffing, or migration tracking. You must integrate a separate migration tool (e.g., goose, migrate).
Can I use dbtpl with an existing codebase that already has Go models?
Yes, with caveats. dbtpl supports append mode (`-a` flag) and single-file output (`-S` flag), allowing incremental generation. However, manual conflict resolution is likely needed if models diverge from schema.
Is dbtpl production-ready?
README states generated code is 'production quality' but explicitly disclaims that dbtpl is not a 'silver bullet.' Manual code review, testing, and ongoing maintenance of generated code are mandatory.

Work with a software development agency

From first prototype to production, DEV.co delivers software development services around tools like dbtpl. Our software development agency staffs experienced software developers and web developers for custom software development, web development, integrations, and ongoing support across open-source databases and beyond.

Ready to streamline Go database development?

Evaluate dbtpl for your project by testing schema generation against your database. Review generated code quality, template customization options, and integration requirements before adopting into your CI/CD pipeline.