File tree Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Expand file tree Collapse file tree 2 files changed +51
-0
lines changed Original file line number Diff line number Diff line change
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
+ ` ` `
Original file line number Diff line number Diff line change @@ -66,6 +66,7 @@ code ever again.
66
66
howto/embedding.md
67
67
howto/overrides.md
68
68
howto/rename.md
69
+ howto/separate-models-file.md
69
70
70
71
.. toctree ::
71
72
:maxdepth: 3
You can’t perform that action at this time.
0 commit comments