Bob Gen for Postgres
Generates an ORM based on a postgres database schema
Usage
# With env variable
PSQL_DSN=postgres://user:pass@host:port/dbname go run github.com/stephenafamo/bob/gen/bobgen-psql@latest
# With configuration file
go run github.com/stephenafamo/bob/gen/bobgen-psql@latest -c ./config/bobgen.yaml
Driver Configuration
Link to general configuration and usage
The configuration for the postgres driver must all be prefixed by the driver name. You must use a configuration file or environment variables for configuring the database driver.
In the configuration file for postgresql for example you would do:
psql:
dsn: "postgres://user:pass@host:port/dbname"
When you use an environment variable it must also be prefixed by the driver name:
PSQL_DSN="postgres://user:pass@host:port/dbname"
Additionally if ssl mode is to be disabled (you will get connection failed error - unable to fetch table data: unable to load enums: pq: SSL is not enabled on the server), you can add sslmode to the dsn:
PSQL_DSN="postgres://user:pass@host:port/dbname?sslmode=disable"
The values that exist for the drivers:
| Name | Description | Default |
|---|---|---|
| driver | Driver to use for generating driver-specific code | github.com/lib/pq |
| dsn | URL to connect to | |
| schemas | Schemas find tables in | ["public"] |
| shared_schema | Schema to not include prefix in model | first value in "schemas" |
| uuid_pkg | UUID package to use (gofrs or google) | "gofrs" |
| queries | Folders containing sql query files | |
| only | Only generate these | |
| except | Skip generation for these | |
| concurrency | How many tables to fetch in parallel | 10 |
| column_order | Order of columns in generated models. "name" sorts alphabetically; "ordinal" preserves database column order | "ordinal" |
Driver-specific code
The driver configuration option enables Bob to generate code that is tailored to the specifics of the selected driver.
For Postgres, the supported drivers are:
- github.com/lib/pq (default)
- github.com/jackc/pgx/v5/stdlib (pgx through
database/sql) - github.com/jackc/pgx/v5 (native pgx, experimental, used with the
bob.Executorimplementations ingithub.com/stephenafamo/bob/drivers/pgx)
Bob leverages driver-specific code to perform precise error matching for generated error constants.
Arrays
One-dimensional array columns are generated as pq.StringArray, pq.Int32Array and the other lib/pq array types for primitive elements, and as pgtypes.Array[T] (or pgtypes.EnumArray[T] for arrays of enums) from github.com/stephenafamo/bob/types/pgtypes for everything else.
With the native pgx driver, every array column is generated as pgtypes.Array[T]. Native pgx hands sql.Scanner types its binary wire format, which the lib/pq array types cannot parse. pgtypes.Array[T] implements pgtype.ArraySetter and pgtype.ArrayGetter so pgx scans and encodes it directly, and it also decodes the binary format when wrapped in another sql.Scanner such as null.Val. A nil array is NULL, while an empty non-nil array is an empty array ({}).
When replacing the type of an array column for the native pgx driver, use pgtypes.Array[T] (for example pgtypes.Array[types.JSON[MyStruct]]) rather than a plain slice: a plain slice wrapped in null.Val cannot be scanned by native pgx, because null.Val is an sql.Scanner and receives the binary format.
Native pgx support is experimental. Some other column types, such as interval and tsvector, are generated as string and cannot yet be scanned from pgx's binary format; use a type replacement for them.
Only/Except:
The only and except configuration options can be used to specify which tables to include or exclude from code generation. You can either supply a list of table names or use regular expressions to match multiple tables.
Consider the example below:
psql:
only:
"/^foo/":
bar_baz:
This configuration only generates models for tables that start with foo and the table named bar_baz.
Alternatively, the following example excludes these tables from code generation rather than including them:
psql:
except:
"/^foo/":
bar_baz:
You may also exclude specific columns:
psql:
# Removes public.migrations table, the name column from the addresses table, and
# secret_col of any table from being generated. Foreign keys that reference tables
# or columns that are no longer generated may cause problems.
except:
public.migrations:
public.addresses:
- name
"*":
- secret_col