This is a generator for sqlc-generated Go code which uses TypeID.
Warning
This is a prototype and not meant for production use, expect rough edges. See open issues for known limitations.
To leverage compile-time enforcement, TypeID requires strict typing for every prefix's ID. On the other hand, sqlc supports overriding one data type (e.g. UUID) to another in one-to-one mapping. This is not enough for TypeID.
To make this work with TypeID, we need to generate overrides for every model field, assuming that each model has its own distinct prefix. Writing that by hand does not sound fun, so this generator is here to ease the process.
Define your config file:
package: # Co-locate this with models.go from sqlc.
name: db # => "package db"
path: db/id.go
models:
- name: User
prefix: usr
table: users
- name: Organization
prefix: org
table: organizations
$ go run go.husin.dev/typeid-sqlc-shim -config path/to/config.yaml
[INFO] db/id.go has been written
[INFO] Append the following to your sqlc.yaml:
overrides:
- column: users.id
go_type:
type: UserID
- column: organizations.id
go_type:
type: OrganizationID
[INFO] See https://docs.sqlc.dev/en/latest/howto/overrides.html#the-go-type-map for more information.