Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Automatically Generating Code for Multiple Directories and Packages in sqlc #3722

Open
MatthewAraujo opened this issue Nov 25, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@MatthewAraujo
Copy link

What do you want to change?

Question:

Hi, I’m using sqlc in my project, and I have a modular service structure like this:

service/
├── auth/
├── customers/
├── products/
├── orders/

Each service has its own queries.sql file, and I would like the generated Go files (e.g., auth.sql.go, customers.sql.go, etc.) to be placed in their respective directories with the correct package name.

Currently, I need to create separate blocks in the sqlc.yml file for each service manually, like this:

- engine: "postgresql"
  schema: "./cmd/migrate/migrations/schema.sql"
  queries: "./service/auth/queries.sql"
  gen:
    go:
      package: auth
      out: "./service/auth"

This works, but it can get repetitive as the number of services grows.

My question is:
Does sqlc plan to support a way to automatically detect and generate code for multiple directories and packages based on the queries.sql file location? For example, if queries.sql is in service/customers/, the generated code would automatically go to service/customers/ with the customers package name.

If not, is this something that could be considered for future releases?

What database engines need to be changed?

No response

What programming language backends need to be changed?

No response

@MatthewAraujo MatthewAraujo added the enhancement New feature or request label Nov 25, 2024
@dcrystalj
Copy link

Its not that bad if you exploit yaml defaults. You just add "override" lines for each service.

sql:
  - &default
    engine: "postgresql"
    schema: "./db/schemas/auth.sql"
    queries: "./service/auth/query"
    gen:
      go: &go_defaults
        emit_json_tags: true
        json_tags_case_style: "camel"
        emit_interface: true
        emit_pointers_for_null_types: true
        package: "repository"
        out: "./service/auth/repository"
       ....
  - <<: *default
    schema: "./db/schemas/customers.sql"
    queries: "./service/customers/query"
    gen:
      go:
        <<: *go_defaults
        out: "./service/customers/repository"
  - <<: *default
    schema: "./db/schemas/products.sql"
    queries: "./service/products/query"
    gen:
      go:
        <<: *go_defaults
        out: "./service/products/repository"

But I agree it would be nice to just point to "service" directory.

@mmierzwa
Copy link

Its not that bad if you exploit yaml defaults. You just add "override" lines for each service.

This is true, but sqlc still generates models.go and db.go file with the same content for each directory/package, which is especially annoying in case described here (a single schema file as a source). I'd expect, in this situation, to be able to generate just one models.go and one db.go file for a project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants