repogen is a Golang Codegen that generates database query and mutation end-to-end with its model.
repogen describe our given tables from our database connection. Therefore, providing the database connection is compulsory.
To define the database connection credentials We could write the connection inside our .env
file. Another way to define our DB connection is directly using flag -creds
with DSN URL format.
Using go get command
$ go get github.com/sog01/repogen
This only supports Go 1.16 or higher.
Once We have successfully installed the repogen We can run it by typing repogen
following with the mandatory flags.
The easy way to run at the first time is by running this command below :
$ repogen -tables <database table separated by commas> -creds <DSN URL>
Or We can write this command using go generate.
The repogen will generate two directories that contains models and repositories. We could specify the directory name or packages using flags -modelDir
,-modelPackage
, and -repositoryPackage
. The interfaces of the generated files should look like this :
type Repository{{.Name}}Query interface {
Select{{.Name}}(fields ...{{.Name}}Field) Repository{{.Name}}Query
Exclude{{.Name}}(excludedFields ...{{.Name}}Field) Repository{{.Name}}Query
Filter{{.Name}}(filter Filter) Repository{{.Name}}Query
Pagination{{.Name}}(pagination Pagination) Repository{{.Name}}Query
OrderBy{{.Name}}(orderBy []Order) Repository{{.Name}}Query
Get{{.Name}}Count(ctx context.Context) (int, error)
Get{{.Name}}(ctx context.Context) (*{{.ModelPackage}}{{.Name}}, error)
Get{{.Name}}List(ctx context.Context) ({{.ModelPackage}}{{.Name}}List, error)
}
The implementation is also generated and ready to use inside our project.
module
: Define go mod nametables
: Define list of tables to generate (comma separated)creds
: Define db credentials with dsn formatenv
: Define env path that hold creds informationenvFile
: Define env filename that hold creds informationenvPrefix
: Define envPrefix that append on creds informationdestination
: Define destination of generated filemodelPackage
: Define model packagemodelDir
: Define model directory namerepositoryPackage
: Define repository packagequeryOnly
: Only generate the repository code