@@ -60,50 +60,31 @@ type ScoreAndTestsRow struct {
6060}
6161```
6262
63- #### Nested JSON objects and arrays
63+ #### JSON objects and arrays
6464
65- ` sqlc.jsonb_build_object."Name"(key, value, key, value, ...) ` builds a named
66- Go struct from an inline JSON shape — a typed wrapper around Postgres's
67- ` jsonb_build_object ` , so keys must be string literals. This is most useful
68- for pulling a one-to-many relationship into a single query as a slice
69- field, instead of running a separate query per parent row. It's ** pgx/v5
70- only** : ` sqlc generate ` fails with a clear error for any other
71- ` sql_package ` .
72-
73- ` "Name" ` is required and must be a double-quoted identifier in that
74- position, not an argument — Postgres parses this as a 3-part qualified
75- function name (` catalog.schema.name ` ), and sqlc reads the struct name
76- directly off of it.
77-
78- ``` sql
79- CREATE TABLE authors (
80- id bigserial PRIMARY KEY ,
81- name text NOT NULL
82- );
83-
84- CREATE TABLE books (
85- id bigserial PRIMARY KEY ,
86- author_id bigint NOT NULL REFERENCES authors (id),
87- title text NOT NULL
88- );
89- ```
65+ ` sqlc.jsonb_build_object."Name"(key, value, ...) ` returns a JSON-shaped
66+ result decoded straight into a named Go struct (or ` []struct ` inside
67+ ` ARRAY(...) ` ), useful for pulling a one-to-many relationship into a single
68+ query instead of one query per parent row. It's ** pgx/v5 only** — `sqlc
69+ generate` fails with a clear error for any other ` sql_package` . ` "Name"` is
70+ required and is read off the 3-part qualified function name Postgres parses
71+ (` catalog.schema.name ` ), not an argument; keys must be string literals.
9072
9173``` sql
9274-- name: GetAuthors :many
9375SELECT
94- sqlc . embed ( authors) ,
76+ authors . id ,
9577 ARRAY(
9678 SELECT sqlc .jsonb_build_object ." Book" (' id' , books .id , ' title' , books .title )
97- FROM books
98- WHERE books .author_id = authors .id
79+ FROM books WHERE books .author_id = authors .id
9980 ) AS books
10081FROM authors;
10182```
10283
10384``` go
10485type GetAuthorsRow struct {
105- Author Author
106- Books []Book
86+ ID int64
87+ Books []Book
10788}
10889
10990type Book struct {
@@ -112,82 +93,9 @@ type Book struct {
11293}
11394```
11495
115- No custom ` Scan ` /` Value ` methods, no wrapper type — ` Books ` is a plain
116- ` []Book ` ; pgx v5 decodes the ` jsonb[] ` column into it directly. A lone
117- ` sqlc.jsonb_build_object."Name"(...) ` (not wrapped in ` ARRAY(...) ` ) works
118- the same way and produces a plain struct field instead of a slice:
119-
120- ``` sql
121- -- name: GetAuthorSummary :one
122- SELECT sqlc .jsonb_build_object ." AuthorSummary" (' name' , name) AS summary FROM authors LIMIT 1 ;
123- ```
124-
125- ``` go
126- type GetAuthorSummaryRow struct {
127- Summary AuthorSummary
128- }
129- ```
130-
131- Two queries that use the same explicit name reuse the same Go type, as long
132- as their shapes match (this works even mixing scalar and ` ARRAY(...) ` uses
133- of the same name). A shape mismatch, or a name that collides with an
134- existing model/enum type, fails generation with an error instead of
135- emitting Go code that won't compile.
136-
137- ##### Overriding generated names
138-
139- The struct name and individual field names can be overridden via the
140- ` rename ` option:
141-
142- ``` json
143- {
144- "rename" : {
145- "Book" : " BookSummary" ,
146- "Book.id" : " BookID"
147- }
148- }
149- ```
150-
151- ` "Book" ` renames the type; ` "Book.id" ` renames just the ` id ` field within
152- it, without affecting other types that also have an ` id ` key. Use the plain
153- key (` "id" ` ) instead to rename that field everywhere.
154-
155- ##### Embedding a whole row as JSON
156-
157- Listing every column by hand is tedious when you just want the whole row.
158- ` sqlc.embed.jsonb(table) ` builds a JSON object from all of a table's columns,
159- the same way ` sqlc.embed(table) ` gives you the table's model — but as a
160- single JSON value, so it can be nested inside ` ARRAY(...) ` to return a slice
161- of rows from one query:
162-
163- ``` sql
164- -- name: GetAuthorsWithBooks :many
165- SELECT
166- authors .id ,
167- ARRAY(SELECT sqlc .embed .jsonb(books) FROM books WHERE books .author_id = authors .id ) AS books
168- FROM authors;
169- ```
170-
171- ``` go
172- type GetAuthorsWithBooksRow struct {
173- ID int64 ` json:"id"`
174- Books []Book ` json:"books"`
175- }
176-
177- type Book struct {
178- ID int64 ` json:"id"`
179- AuthorID int64 ` json:"author_id"`
180- Title string ` json:"title"`
181- }
182- ```
183-
184- The generated struct is named from the result alias — singularized for the
185- ` ARRAY(...) ` case (` AS books ` → ` Book ` ), or used as-is for a scalar
186- ` sqlc.embed.jsonb(table) AS author ` (→ ` Author ` ). Fields, types and JSON keys
187- come from the table's columns, so the object always decodes cleanly. Under
188- the hood the call is rewritten to ` to_jsonb(table) ` , which you'll see in
189- ` EXPLAIN ` output and logs.
190-
191- Like ` sqlc.jsonb_build_object ` , this is pgx/v5 only, and the generated name
192- must not collide with a model or another JSON type; use the ` rename ` option
193- if it does.
96+ The call is rewritten to Postgres's ` jsonb_build_object ` , which you'll see in
97+ ` EXPLAIN ` output. Two queries that use the same name share one Go type when
98+ their shapes match; a shape mismatch, or a name that collides with a model or
99+ another JSON type, fails generation. Names and fields can be overridden with
100+ ` rename ` : ` "Book" ` renames the type, ` "Book.id" ` just the ` id ` field within
101+ it.
0 commit comments