Gen follows the Configuration As Code practice to generate the DAO interface, here is the introduction to the configuration.
Configuration
You need to write the configuration as a runnable golang program, usually, the program will be organized in a sub-directory of your application.
// configuration.go — skeleton: adjust the model import and DSN for your project |
Run the above program, it will generate codes into directory ../dal, you can import the dal package in your application and use its interface to query data
gen.Config
type Config struct { |
Output Options
| Option Name | Description |
|---|---|
| OutPath | Output destination folder for the generated query code. There is no useful default: if left empty, output goes to the current working directory of the generator process, so set it explicitly |
| OutFile | Query code file name, default value: gen.go |
| ModelPkgPath | Where generated model code goes: a package name (placed next to OutPath, default model) or a directory path (when it contains a path separator, it is used as the model output dir) |
| WithUnitTest | Generate unit tests for the DAO package, default value: false |
NOTE:
g.Execute()(i.e.(*gen.Generator).Execute) runs the generator
and panics on failure (after printing the error). Useg.SetLogger(logger)
to install a custom logger.
Generate Struct Options
| Option Name | Description |
|---|---|
| FieldNullable | Generate pointer as field’s type if column is nullable in database |
| FieldCoverable | Generate pointer as field’s type if column has default value in database, to avoid zero-value issue, e.g: https://gorm.io/docs/create.html#Default-Values |
| FieldSignable | Use signable type as field’s type based on column’s data type in database |
| FieldWithIndexTag | Generate with gorm index tag, for example: gorm:"index:idx_name", default value: false |
| FieldWithTypeTag | Generate with gorm type tag, for example: gorm:"type:varchar(12)", default value: false |
Refer Database To Structs for more options
Generator Modes
| Tag Name | Description |
|---|---|
| gen.WithDefaultQuery | Generate global variable Q as DAO interface, then you can query data like: dal.Q.User.WithContext(ctx).First() |
| gen.WithQueryInterface | Generate query api interface instead of struct, usually used for mock testing |
| gen.WithGeneric | Generate code with generic and interface |
| gen.WithoutContext | Generate code without context constrain, then you can query data without passing context like: dal.User.First(); without this mode the DAO chain must start with WithContext, e.g: dal.User.WithContext(ctx).First() |
DAO Interface
Sample of the generated DAO query interface
type IUserDo interface { |
The Generated Query Struct
For each model, the generator emits a query struct with an unexported type
(the variable’s type is inferred; you never spell it out). You reach it through
the User field of the object returned by query.Use(db) — or through the
package-level query.User variable in gen.WithDefaultQuery mode. It carries
one typed field per column, plus a few helpers:
q := query.Use(db) |
Table/As return a copy bound to the new name/alias; the original u is
unaffected. The query type itself is unexported (constructed by the unexportednewUser), so always obtain it from query.Use(db) / query.SetDefault(db) /
the generated default variables and let Go infer the type. Note that the typed
fields live on the query struct itself — after u.WithContext(ctx) you are
holding the userDo chain, so keep referencing fields through u.
The Query Object
query.Use(db) returns a *query.Query that groups one DAO per model and adds
connection-level helpers; query.SetDefault(db) initializes the same object
package-globally (it has no return value) and is what backs the generated
package-level variables:
q := query.Use(db, gen.WithClauseChecker(myChecker)) // ...gen.DOOption since v0.3.28 |
Transactions are also started from the query object — see Transaction.
With gen.WithDefaultQuery the same helpers exist on the package-level query.Q
(and the generated package variables query.User, query.Bank, … are aliases into it).
Usage Example
The two examples below are application skeletons: add package main, replaceyour_project/dal with the import path of your generated DAO package, and use
your application’s DSN. The discarded result (_ = user) is illustrative;
consume it in your application after checking the query error.
- Use the global variable
Qifgen.WithDefaultQueryis enabled
import ( |
- Initialize DAO query interface
import ( |
Without gen.WithDefaultQuery, query.User-style package variables are not
generated — obtain DAOs from query.Use(db) instead.
For more usage details, please checkout