sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Export models to a different package

Open td0m opened this issue 4 years ago • 3 comments

Hi! Great work on this project!

I was wondering if it's currently possible to export Go model structs to a different folder package.

I think this could be a good feature for anyone who would like to make their code independent of the database backend but still use the automatically generated models.

For example:

internal/
  model/
    models.go
  db/
    postgres/
      db.go
      user.sql.go
      querier.go

This way you could e.g. also have a mongodb provider that uses these models, but without depending on the postgres package. I'm sure you'd agree that it would be odd for mongo to use postgres.User as a type.

Is this something that's possible to do? If not, would you possibly accept a PR that adds support for it? Or it it something you're not planning on supporting for now?

Thanks

td0m avatar Dec 17 '20 12:12 td0m

We just tried this out at our company, for context we are using go-migrate so we have a single folder with all of our database migrations. Without a feature like this the, otherwise excellent sqlc, isn't a viable option for us.

As it stands (unless I've missed some configuration?) each generated package has a models.go file containing all models from our entire database.

Having an option to decide where to generate the models file, and read from it on the subsequent generated packages OR being able to remove unused models would be a much needed feature.

I see this has been left untouched since 2020, is there anything I can do to help speed this along?

johan-lejdung avatar Feb 22 '23 12:02 johan-lejdung

https://github.com/kyleconroy/sqlc/issues/1941#issuecomment-1623408555

mpyw avatar Jul 06 '23 10:07 mpyw

+1

anhnmt avatar Jan 03 '24 08:01 anhnmt