denodrivers / postgres

PostgreSQL driver for Deno
https://denodrivers.github.io/postgres
MIT License
608 stars 97 forks source link

Support Set as bind parameter #477

Open tv42 opened 8 months ago

tv42 commented 8 months ago

Is your feature request related to a problem? Please describe.

I tried to pass a Set as a bind parameter, expecting it to behave like Array. It was hard to debug since the query gave wrong results, not an error.

Describe the solution you'd like

Support Set just like Array: convert it to a Postgres array.

Additional context

Set:

$ deno eval 'import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; const client = new Client({}); await client.connect(); const odds = new Set([1,3,5]); const result = await client.queryObject`SELECT 3=ANY(${odds}) AS should_be_true`; console.log(result.rows);'
[ { should_be_true: false } ]

Array:

$ deno eval 'import { Client } from "https://deno.land/x/postgres@v0.19.3/mod.ts"; const client = new Client({}); await client.connect(); const odds = new Array([1,3,5]); const result = await client.queryObject`SELECT 3=ANY(${odds}) AS should_be_true`; console.log(result.rows);'
[ { should_be_true: true } ]
tv42 commented 8 months ago

This seems like a very simple change (but I haven't actually tested this):

https://github.com/denodrivers/postgres/blob/2606e50f02b525ac89012bbca9ed7ca863b215a9/query/encode.ts#L98-L99

add

  } else if (value instanceof Set) {
    return encodeArray(Array.from(value));