feat(pgx-v5): Add static typing support for jsonb_build_object#4518
Open
m-ali-akbay wants to merge 3 commits into
Open
feat(pgx-v5): Add static typing support for jsonb_build_object#4518m-ali-akbay wants to merge 3 commits into
m-ali-akbay wants to merge 3 commits into
Conversation
Add two directives that return JSON-encoded results decoded directly into
Go structs by pgx/v5, including nested objects and arrays from a single query:
- sqlc.jsonb_build_object."Name"(key, value, ...) builds an arbitrary
named struct from key/value pairs.
- sqlc.embed.jsonb(table) builds a struct mirroring a whole row, as a
shorthand that composes inside ARRAY(...) for slices of rows.
Both are rewritten to real Postgres functions (jsonb_build_object / to_jsonb)
before analysis; pgx/v5 decodes the jsonb value into a plain struct (or slice)
with no Scan/Value plumbing.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
m-ali-akbay
force-pushed
the
pgx-v5-jsonb
branch
from
July 18, 2026 07:51
74d6bfc to
fe89bbd
Compare
m-ali-akbay
marked this pull request as ready for review
July 18, 2026 08:21
The process_plugin_sqlc_gen_json fixture serializes the full CodeGenRequest, so adding json_fields/json_name to Column changes its output. Only codegen_json had been regenerated; this test skips locally when the sqlc-gen-json binary isn't on PATH, so CI caught it. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a
sqlc.jsonb_build_object."Name"(key, value, ...)directive for returning JSON-shaped results that decode straight into a Go struct. It's handy for pulling a one-to-many relationship into a single query instead of running one query per parent row.gives you:
A few notes:
generatefails with a clear error for any othersql_package. pgx decodes thejsonbvalue into the struct (or[]structinsideARRAY(...)) directly, so there's noScan/Valueplumbing.sqlc.jsonb_build_object."Book"ascatalog.schema.name, so no new parser work.jsonb_build_objector anARRAY(...)of one.rename.jsonb_build_objectbefore analysis.Docs in
docs/howto/embedding.md; covered by unit tests plus asqlc_jsongolden fixture.