sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Add option to produce private structs for query params

Open kazhuravlev opened this issue 3 years ago • 2 comments

We use sqlc to generate some code, and on top of that, we write others.

But unfortunately, generated code and structs are available by other packages. Also, different packages can see New and Queries. So in the next PR, I would like to add corresponding options.

kazhuravlev avatar Jul 20 '22 18:07 kazhuravlev

If the query structs are private, how are you calling the methods?

kyleconroy avatar Jul 23 '22 16:07 kyleconroy

Good point.

I try to explain what I am expecting.

storage/
- query.sql
- db.gen.go
- models.gen.go
- queries.gen.go
- public_api_of_my_service.go

I want to generate queries by sqlc to some packages, like storage. Also, this package will contain some hand-written code (public_api_of_my_service.go). When my clients consume storage functionality - they will use only methods in public_api_of_my_service.go.

So, they call some hand-write public method (like GetSomeObjectByID()), and this method will use code-generated code produced by sqlc.

kazhuravlev avatar Jul 23 '22 17:07 kazhuravlev

@kyleconroy, do you have any comments on that?

kazhuravlev avatar Aug 26 '22 23:08 kazhuravlev

By using an internal package you can get the same level of encapsulation. Take a look at the attached example, but if you put an internal package inside your storage package, only the storage package can access it.

example
├── go.mod
├── query.sql
├── sqlc.json
├── storage
│   ├── internal
│   │   └── db
│   │       ├── db.go
│   │       ├── models.go
│   │       └── query.sql.go
│   └── service.go
└── web
    └── main.go

example.zip

kyleconroy avatar Aug 30 '22 15:08 kyleconroy