porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.54k stars 273 forks source link

Partial query build giving string output #945

Open arrafi-ahmed opened 2 months ago

arrafi-ahmed commented 2 months ago

Trying to build partial query while working with jsonb, but its printing string in inserted row:

exports.updateEntityLastCount = async ({ pairs, tournamentId }) => { const keys = Object.keys(pairs); const vals = Object.values(pairs).map( (value, index) => ((entity_last_count ->> ${keys[index]})::int + ${value})::text, ); const keyArray = sql${keys.join(", ")}; const valArray = sql${vals.join(", ")};

return sql UPDATE tournaments SET entity_last_count = entity_last_count || jsonb_object( ARRAY[${keyArray}], ARRAY[${valArray}]) WHERE id = ${tournamentId};; };

DB inserted row: {"group": 1, "match": 1, "phase": "((entity_last_count ->> phase)::int + 1)::text", "bracket": 1}