AgentStack
Browse Sign in
Browse Why AgentStack Sell Docs
Sign in
SKILL verified MIT Self-run

Sqlc Jsonb Joins

skill-winens-agent-skills-sqlc-jsonb-joins · by winens

Compose relational JOIN results into Go model types using PostgreSQL to_jsonb() with sqlc. Use whenever a sqlc project has JOIN queries producing flat row structs and you want typed model composition instead — accessing results as row.User.Email, row.Order.Item rather than flat fields. Triggers on: sqlc JOIN queries, sqlc.embed failures, 'type explosion' in sqlc, model type reuse, composing relat…

No reviews yet
0 installs
22 views
0.0% view→install

Install

$ agentstack add skill-winens-agent-skills-sqlc-jsonb-joins

✓ scanned · ✓ verified, works with Claude Code, Cursor, and more.

Security review

✓ Passed

No issues found. Passed automated security review. · v0.1.0 How review works →

  • Prompt-injection patterns
  • Secret / credential exfiltration
  • Dangerous shell & filesystem operations
  • Untrusted network calls
  • Known-malicious package signatures

What it can access

  • Network access No
  • Filesystem access No
  • Shell / process execution No
  • Environment & secrets No
  • Dynamic code execution No

From automated source analysis of v0.1.0. “Used” means the capability is present in the source — more access means more to trust, not that it’s unsafe.

View the full security report →

Verified badge

Passed review? Show it. Paste this badge into your README, it links to the public security report.

AgentStack Verified badge Links to your public security report.
[![AgentStack Verified](https://agentstack.voostack.com/badges/verified.svg)](https://agentstack.voostack.com/security/report/skill-winens-agent-skills-sqlc-jsonb-joins)

Reliability & compatibility

Security review passed
0 installs to date
no reviews yet
3mo ago

Declared compatibility

Claude CodeClaude Desktop

Compatibility is declared by the source manifest. End-to-end runtime verification is coming, see below.

Preview Execution monitoring

We're building live execution health for every listing: tool-call success rate, median latency, uptime, and last-checked timestamps, measured, not self-reported. It isn't live yet, so we don't show numbers we can't stand behind.

How agent discovery & health will work →
Are you the author of Sqlc Jsonb Joins? Claim this listing to set pricing, connect Stripe payouts, and keep 70% of every sale.
Sign up to claim

About

sqlc jsonb Composition

Problem

sqlc JOIN queries produce flat structs — every column from every table merged into one type. Each JOIN gets a unique struct, no model reuse, handlers copy fields one-by-one.

sqlc.embed() seems like the fix but breaks when tables share column names (created_at, updated_at, role). Go won't compile two embedded structs with duplicate fields.

Solution

Return one to_jsonb() column per joined table. Unmarshal each into the corresponding model type.

The SQL pattern

-- name: ListOrdersWithCustomer :many
SELECT
    to_jsonb(o) AS "order",
    to_jsonb(c) AS "customer"
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.status = $1
ORDER BY o.created_at DESC;

What sqlc generates

type ListOrdersWithCustomerRow struct {
    Order    []byte `db:"order" json:"order"`
    Customer []byte `db:"customer" json:"customer"`
}

Unmarshal each column into the corresponding model:

var order repo.Order
json.Unmarshal(row.Order, &order)

var customer repo.Customer
json.Unmarshal(row.Customer, &customer)

Why not the alternatives

sqlc.embed() — breaks when joined tables share column names (created_at, updated_at). Also can't embed a nil struct for nullable joins.

jsonb_build_object(...) combined — returns one blob, needs an intermediate Go struct.

Flat rows — sqlc default. Every JOIN is a unique type with no reuse.

Three+ tables

One to_jsonb() per table. Scales linearly.

SELECT
    to_jsonb(o)  AS "order",
    to_jsonb(c)  AS "customer",
    to_jsonb(pm) AS "payment"
FROM orders o
JOIN customers c ON c.id = o.customer_id
JOIN payments pm ON pm.order_id = o.id
WHERE o.id = $1;

LEFT JOINs and NULL

to_jsonb() on the NULL side produces SQL NULL, which pgx scans as nil/empty []byte. json.Unmarshal handles this gracefully — returns an error and leaves the target as zero value. Just ignore it:

_ = json.Unmarshal(row.Payment, &payment)

Parent + children in one query

-- name: GetCustomerWithOrders :one
SELECT
    to_jsonb(c) AS customer,
    COALESCE(jsonb_agg(to_jsonb(o) ORDER BY o.created_at DESC)
        FILTER (WHERE o.id IS NOT NULL), '[]'::jsonb) AS orders
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id
WHERE c.id = $1
GROUP BY c.id;

COALESCE(..., '[]'::jsonb) handles zero children. FILTER (WHERE ...) excludes the NULL row from LEFT JOIN.

Gotcha: jsonb_agg with complex expressions sometimes makes sqlc generate interface{} instead of []byte. Handle with a type switch at the call site.

Selective columns

to_jsonb(t) serializes every column. Use jsonb_build_object to exclude sensitive data or reduce payload size:

SELECT
    to_jsonb(o) AS "order",
    jsonb_build_object('id', c.id, 'name', c.name, 'email', c.email) AS "customer"
FROM orders o
JOIN customers c ON c.id = o.customer_id

Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

Install and usage instructions live in the source repository linked above.

Reviews

No reviews yet, be the first.

Versions

  • v0.1.0 Imported from the upstream source.