Skip to content

Commit 5121a71

Browse files
committed
docs(howto): add how-to guide for separating models file
1 parent 638643f commit 5121a71

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

docs/howto/separate-models-file.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Separating models file
2+
3+
By default, sqlc uses a single package to place all the generated code. But you may want to separate
4+
the generated models file into another package for loose coupling purposes in your project.
5+
6+
To do this, you can use the following configuration:
7+
8+
```yaml
9+
version: "2"
10+
sql:
11+
- engine: "postgresql"
12+
queries: "queries.sql"
13+
schema: "schema.sql"
14+
gen:
15+
go:
16+
out: "internal/" # Base directory for the generated files. You can also just use "."
17+
sql_package: "pgx/v5"
18+
package: "sqlcrepo"
19+
output_batch_file_name: "db/sqlcrepo/batch.go"
20+
output_db_file_name: "db/sqlcrepo/db.go"
21+
output_querier_file_name: "db/sqlcrepo/querier.go"
22+
output_copyfrom_file_name: "db/sqlcrepo/copyfrom.go"
23+
output_query_files_directory: "db/sqlcrepo/"
24+
output_models_file_name: "business/entities/models.go"
25+
output_models_package: "entities"
26+
models_package_import_path: "example.com/project/module-path/internal/business/entities"
27+
```
28+
29+
This configuration will generate files in the `internal/db/sqlcrepo` directory with `sqlcrepo`
30+
package name, except for the models file which will be generated in the `internal/business/entities`
31+
directory. The generated models file will use the package name `entities` and it will be imported in
32+
the other generated files using the given
33+
`"example.com/project/module-path/internal/business/entities"` import path when needed.
34+
35+
The generated files will look like this:
36+
37+
```
38+
my-app/
39+
├── internal/
40+
│ ├── db/
41+
│ │ └── sqlcrepo/
42+
│ │ ├── db.go
43+
│ │ └── queries.sql.go
44+
│ └── business/
45+
│ └── entities/
46+
│ └── models.go
47+
├── queries.sql
48+
├── schema.sql
49+
└── sqlc.yaml
50+
```

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ code ever again.
6666
howto/embedding.md
6767
howto/overrides.md
6868
howto/rename.md
69+
howto/separate-models-file.md
6970

7071
.. toctree::
7172
:maxdepth: 3

0 commit comments

Comments
 (0)