sqlc-dev / sqlc

Generate type-safe code from SQL
https://sqlc.dev
MIT License
13.65k stars 813 forks source link

Config option to split generation paths #1951

Open taylow opened 2 years ago

taylow commented 2 years ago

What do you want to change?

I use SQLC to generate data storage model, repository interfaces, and their implementations from SQL files (obviously!), and absolutely love what the tool offers. I'm constantly recommending it.

I have recently started the transition towards Clean Architecture in the project I'm work on, using a layered approach to separate concerns (e.g. domain, application, infrastructure, presentation, etc.), and unfortunately have hit a small problem with the generated SQLC files.

The interface definition of repositories (Querier in SQLC) and DSOs should live in the application layer, whereas the implementations of the repositories should live in the infrastructure layer.

At the moment, SQLC generates all files into one folder determined by the path parameter of the config.

My proposal is to allow overrides for each of the generated files. This would fix my problem, as I could generate the Querier and models in application/repository, and the SQL query strings and implementations in infrastructure/repository.

I could wrap SQLC in a custom repository interface, but I really like the fact that the repository interface and models are generated from our SQL. My temporary solution is defining a new interface that embeds the Querier, but the direction of inversion is broken, as my application layer now points to the infrastructure layer, which is incorrect in Clean Architecture. I could also just suck it up and keep SQLC files in the application layer, but then we have infrastructure concerns in the application, which is also incorrect.

Any thoughts on this? If this is something that other want/see a use for, I can take a look at opening a PR (eventually). Any input is appreciated 🙏

What database engines need to be changed?

No response

What programming language backends need to be changed?

No response

taylow commented 2 years ago

Looks like there are two issues that want a similar feature for models (#835 and #1941), whereas this is about breaking out all generation paths

mpyw commented 1 year ago

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

vezzalinistefano commented 1 year ago

I'd like that too!

anhnmt commented 11 months ago

I hope this is done soon