Skip to content

feat(codogen): Added the ability to specify a separate package for models. #3379

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ The `gen` mapping supports the following keys:

- `package`:
- The package name to use for the generated code. Defaults to `out` basename.
- `package_models`:
- The package name to use for the generated code models. Defaults to `out` basename.
- `package_models_path_import`:
- The name of the model import path to be used when specifying imports in other created packages. By default, no, because all files are created in one package. For example, `github.com/path/internal `, as a result, the import will look like `github.com/path/internal/models `. The parameter should be used together with `package_models`.
- `out`:
- Output directory for generated code.
- `sql_package`:
Expand Down Expand Up @@ -183,6 +187,8 @@ The `gen` mapping supports the following keys:
- Customize the name of the models file. Defaults to `models.go`.
- `output_querier_file_name`:
- Customize the name of the querier file. Defaults to `querier.go`.
- `output_queries_path_name`:
- Customize the path to the generated queries. Defaults to ` `. At this point, you need to specify the same path as to the `db.go`.
- `output_copyfrom_file_name`:
- Customize the name of the copyfrom file. Defaults to `copyfrom.go`.
- `output_files_suffix`:
Expand Down
15 changes: 15 additions & 0 deletions examples/separate-package-model/postgresql/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module postgres-separate-package-model

go 1.21.6

require (
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.5.5
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
30 changes: 30 additions & 0 deletions examples/separate-package-model/postgresql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 17 additions & 0 deletions examples/separate-package-model/postgresql/models/order.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/separate-package-model/postgresql/models/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- +goose Up
-- +goose StatementBegin
CREATE SCHEMA auth;
CREATE EXTENSION "pgcrypto";
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP SCHEMA auth;
-- +goose StatementEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE "auth"."user" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"login" varchar NOT NULL,
"password" varchar NOT NULL,
"created_at" timestamptz NOT NULL,
PRIMARY KEY ("id")
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE "auth"."user";
-- +goose StatementEnd
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- +goose Up
-- +goose StatementBegin
CREATE SCHEMA content;
CREATE TABLE "content"."order" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"number" varchar NOT NULL,
"user_id" uuid NOT NULL,
"created_at" timestamptz NOT NULL,
PRIMARY KEY ("id")
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE "content"."order";
DROP SCHEMA content;
-- +goose StatementEnd
32 changes: 32 additions & 0 deletions examples/separate-package-model/postgresql/store/orderstore/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: GetOrder :one
SELECT * FROM "content"."order"
WHERE id = $1 LIMIT 1;

-- name: ListOrders :many
SELECT * FROM "content"."order"
ORDER BY number;

-- name: CreateOrder :one
INSERT INTO "content"."order" (
number, user_id, created_at
) VALUES (
$1, $2, $3
)
RETURNING *;

-- name: DeleteOrder :exec
DELETE FROM "content"."order"
WHERE id = $1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- name: GetUser :one
SELECT * FROM "auth"."user"
WHERE id = $1 LIMIT 1;

-- name: ListUsers :many
SELECT * FROM "auth"."user"
ORDER BY login;

-- name: CreateUser :one
INSERT INTO "auth"."user" (
login, password, created_at
) VALUES (
$1, $2, $3
)
RETURNING *;

-- name: DeleteUser :exec
DELETE FROM "auth"."user"
WHERE id = $1;
32 changes: 32 additions & 0 deletions examples/separate-package-model/postgresql/store/userstore/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading