gen
Gen: Friendly & Safer GORM powered by Code Generation
Type safe data access code from your database schema
GORM Gen generates reusable DAO and query code from a schema or SQL templates, adding static typing on top of GORM.
What Gen generates
Gen is a code generation tool for Go that produces idiomatic, reusable data access code from a database schema and from interface based SQL templates. The README summarizes its goals as generating DAO APIs, offering a type safe query DSL with strong static typing including a generics mode, and following GORM conventions when mapping database tables to structs. Because it is built on top of GORM, the generated code uses the same plugins, dialect drivers, and ecosystem you would already have if you use GORM. That matters for teams that want safer queries without leaving the ORM they know. The type safety is the headline feature: instead of writing string based queries and hoping column names line up, you call methods on generated query structs where fields and conditions are checked at compile time. The overview also notes that database to struct mapping respects GORM tags and details like nullable, default, unsigned, index, and type. In practice this means the generated layer keeps your model definitions consistent with the table while giving you a fluent API to build queries. The documentation lives at the GORM Gen site, with the base GORM guides linked for background. The source lives in the go-gorm-gen repository on GitHub and is implemented in Go, with the license recorded as MIT in the project metadata.
Using the generated code
The quick start shows the typical flow. You write a small generator entry, often cmd/gen/main.go, that opens a database connection, creates a generator with an output path and a mode, points it at the schema, and calls Execute. After running go run ./cmd/gen, you get a query package you import elsewhere. If you enabled WithDefaultQuery, you call query.SetDefault(db) once at startup and then use query.User or similar directly, as the README demonstrates with a Where clause on Age greater than eighteen. The note says SetDefault only needs to run a single time, after which you address tables through the generated query object. If you prefer not to keep a global default, you skip WithDefaultQuery and instead call query.Use(db), then build queries from that handle. Both styles are shown so the choice is about whether a package level default fits your app. The generated code reads like ordinary Go, which is the point: the boilerplate of scanning rows and mapping columns is gone, and what remains is a typed API you can refactor against. The README links a folder of runnable examples for readers who want more complete patterns than the snippets provide. The source lives in the go-gorm-gen repository on GitHub and is implemented in Go, with the license recorded as MIT in the project metadata.
Common setups
Gen has one generator entry point, gen.NewGenerator with a Config, and the README describes the main knobs as what to generate, how the query API looks, and code style. The what axis covers generating from a database schema into models and query code, plus optional interface based SQL methods. The how axis is the Config.Mode flags: WithDefaultQuery, WithoutContext, WithQueryInterface, and WithGeneric. The style axis includes Config.UseAny, which emits any instead of interface{} in generated code, and the README notes that requires Go 1.18 or newer and also rewrites the literal text inside comments and string literals. Three setups are described. Setup A is the recommended baseline that maps the schema to a model plus query with nullable, coverable, and index tag options turned on. Setup B binds an interface with SQL comments and templates to a generated model so you get reusable typed methods from your own queries. Setup C changes the query API surface to a generics mode for stronger typing without being a separate workflow. The recommended project layout keeps generation isolated under internal/dal with model and query subpackages and a checked in generator entry under cmd/gen, which works well with go:generate and CI. A separate GenTool provides a CLI path for those who prefer not to write a generator program.
Editorial conclusion
The library is written in Go and released under the MIT license, and it builds on the existing GORM ecosystem of plugins and dialects.
Community notes