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

feat: Add JSON "codegen" output #1565

Merged
merged 4 commits into from
Apr 29, 2022
Merged

feat: Add JSON "codegen" output #1565

merged 4 commits into from
Apr 29, 2022

Conversation

kyleconroy
Copy link
Collaborator

@kyleconroy kyleconroy commented Apr 23, 2022

Inspired by @skabbes here #1470 (comment)

Add a json option to the codegen output. You'll need to use version 2 of the configuration file. You can configure the output path using the out option.

{
  "version": "2",
  "sql": [
    {
      "schema": "postgresql/schema.sql",
      "queries": "postgresql/query.sql",
      "engine": "postgresql",
      "gen": {
        "json": {
          "out": "gen",
          "indent": "  "
        }
      }
    }
  ]
}
sqlc generate --experimental
cat gen/codegen_request.json | gen-go-code
cat gen/codegen_request.json |  gen-ts-code
cat gen/codegen_request.json | wasm-runner ./python-gen.wasm
  • Add documentation for the JSON plugin

@skabbes
Copy link
Contributor

skabbes commented Apr 23, 2022

This is incredible! It is a testament to the architecture how trivial this was to set up.

At $COMPANY, we rely heavily on code generation for our Typescript and Go base models (~100 tables). Our architecture is basically this pipeline (collectively called "sync schema")

  • A Go program (readschema) that:
    • runs information_schema (and pg_*) queries against a dev (or prod!?) database
    • generates a json file
  • A Typescript program (writeSchema.ts) that:
    • reads the JSON, and runs them though handlebars templates
    • generates TableName_gen.ts and TableName_gen.go files
    • applies auto-formatting (go fmt, prettier, etc)

This change could completely replace readschema for us, and would allow any improvements there to instead be contributed to directly sqlc. Additionally, the code generators we have would be immediately be useful to others (sqlc -> Typescript) for example (the sqlc Go code generator is already light years ahead of ours though).

Anyway, that's a long winded -hell ya! And I also think this change will inspire someone (maybe me?) to just knock out this 2 year old issue. #296

@kyleconroy
Copy link
Collaborator Author

This change could completely replace readschema for us, and would allow any improvements there to instead be contributed to directly sqlc.

That's great to hear. Once this is merged, I'd love to hear if it's working out for you.

And I also think this change will inspire someone (maybe me?) to just knock out this 2 year old issue. #296

There shouldn't anything blocking you (or anyone else) on implementing codegen for another language. The internal codegen interface is now stable. You can use this PR as a template for adding others.

@kyleconroy kyleconroy merged commit b75b919 into main Apr 29, 2022
@kyleconroy kyleconroy deleted the kyle/json-codegen-output branch April 29, 2022 01:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants