Skip to content

wrapLiveQueryForMaterialization

wrapLiveQueryForMaterialization(sql, fields): string

Defined in: packages/client/src/live-rows-sql.ts:39

Make a live-read SQL statement SAFE TO MATERIALISE by giving every output column a UNIQUE explicit name.

fields is the ordered list of unique output aliases — one per output column, in the compiled SQL’s column order (Drizzle emits columns depth-first over the select’s field keys, which are unique by construction). The statement is wrapped in a derived table with a POSITIONAL column-alias-list:

SELECT * FROM (<sql>) "__pgx_live" ("<a0>", "<a1>", …)

A plain SELECT * FROM (<sql>) does NOT dedupe — the inner duplicate names survive — so the column-alias-list is what renames every output column POSITIONALLY to a distinct name. The wrapped query materialises cleanly, and its rows come back keyed by the aliases (so same-named source columns keep DISTINCT values). SELECT * from the aliased derived table is safe precisely because the alias list has already made the names unique.

Returns the SQL UNCHANGED when fields is absent or empty — the default path for callers (raw SQL strings, or non-colliding queries that pass no fields): they keep name-keyed rows exactly as before. Only a caller that supplies fields opts into alias-keyed rows.

string

readonly string[] | undefined

string