I am developing a simple Go
service to connect to a database for basic querying. I am using sqlc
to generate the Go
functions to interact with the DB. when switching the driver from lib/pq
to pgx/v5
now the types for the DB fields are pgtypes
instead of Go
types. Here's an example:
Instead of this:
type ListAccountsParams struct {
Owner string `json:"owner"`
Limit int32 `json:"limit"`
Offset int32 `json:"offset"`
}
I now get this:
type ListAccountsParams struct {
Owner pgtype.Text `json:"owner"`
Limit pgtype.Int4 `json:"limit"`
Offset pgtype.Int4 `json:"offset"`
}
However the only way I find of using pgtypes
is this one:
owner := pgtype.Text{
String: "Craigs List",
Valid: true,
}
Instead of just doing owner := "Craigs List"
. For numeric types is even more overkill, all implementations I find are like this:
pgtype.Numeric{
Int: big.NewInt(-543),
Exp: 3,
Status: pgtype.Present
}
Using sqlc
config file I can just override these types in favor of Go
standard types, but it makes no sense to me having to override a postgres text
type to string
and so on...
It seems to me that this is not the best way of using these types, it is counter intuitive for me. So my question is, am I doing it right? Is there a different way? Ultimately, is there a way to configure sqlc
to use Go
types instead of pgtypes
while still using the pgx/v5
driver?
pgtype
types manually in your code and it means you would need to convert the Go types to the correspondingpgtype
types before passing them to the generated functions, and vice versa when retrieving values from the database! – Dylan