A sqlc plugin that generates type-safe Go database access code from SQL. Runs as a WASM plugin (recommended) or standalone binary.
version: '2'
plugins:
- name: golang
wasm:
url: https://github.com/vtuanjs/sqlc-gen-go/releases/download/v1.7.0/sqlc-gen-go.wasm
sha256: 996233111f91edd925266b9c52645b581fe8d16e751c412d9a59179b70f3cb90
sql:
- schema: schema.sql
queries: query.sql
engine: postgresql
codegen:
- plugin: golang
out: db
options:
package: db
sql_package: pgx/v5make all # produces bin/sqlc-gen-go and bin/sqlc-gen-go.wasmTo use a local build:
plugins:
- name: golang
wasm:
url: file:///path/to/bin/sqlc-gen-go.wasm
sha256: "" # optional since sqlc v1.24.0Two changes are required:
- Add a top-level
pluginsentry pointing to the WASM plugin. - Replace
gen.gowithcodegen, referencing the plugin by name. Move all options into theoptionsblock;outmoves up one level.
Before:
sql:
- engine: postgresql
gen:
go:
package: db
out: db
emit_json_tags: trueAfter:
plugins:
- name: golang
wasm:
url: https://github.com/vtuanjs/sqlc-gen-go/releases/download/v1.7.0/sqlc-gen-go.wasm
sha256: 996233111f91edd925266b9c52645b581fe8d16e751c412d9a59179b70f3cb90
sql:
- engine: postgresql
codegen:
- plugin: golang
out: db
options:
package: db
emit_json_tags: trueGlobal overrides/go move to options/<plugin-name>:
options:
golang:
rename:
id: "Identifier"
overrides:
- db_type: "timestamptz"
nullable: true
engine: postgresql
go_type:
import: "gopkg.in/guregu/null.v4"
package: "null"
type: "Time"Each SQL source file gets its own struct and interface instead of a shared Queries/Querier.
| SQL file | Struct | Interface |
|---|---|---|
users.sql |
UsersQueries |
UsersQuerier |
user_orders.sql |
UserOrdersQueries |
UserOrdersQuerier |
options:
emit_interface: true
emit_per_file_queries: true- Each
*.sql.gocontains its own struct, constructor, methods, and interface. db.goonly keepsDBTX;querier.gois not generated.- Incompatible with
emit_prepared_queries.
:one queries return nil, nil instead of nil, sql.ErrNoRows when no row is found.
options:
emit_err_nil_if_no_rows: trueInjects custom code at the start of every query method. Supports {{.MethodName}} and {{.StructName}} template variables.
options:
emit_tracing:
import: "go.opentelemetry.io/otel"
package: "otel"
code:
- "ctx, span := otel.Tracer(\"{{.StructName}}\").Start(ctx, \"{{.MethodName}}\")"
- "defer span.End()"| Field | Description |
|---|---|
import |
Import path of the tracing package |
package |
Package alias (if different from the last path segment) |
code |
Lines to inject; each is a Go template |
Adds a //go:generate directive to each *.sql.go file. $GOFILE expands to the current filename at generate time.
options:
go_generate: "mockgen -source=$GOFILE -destination=mock/$GOFILE -package=mock"Running go generate ./... produces a mock per SQL file:
| Source | Mock |
|---|---|
users.sql.go |
mock/users.sql.go |
orders.sql.go |
mock/orders.sql.go |