porsager / postgres

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

Dynamic queries not working properly #733

Open kronos-999 opened 8 months ago

kronos-999 commented 8 months ago

Hello, I am trying to implement query fragments as described in your dynamic query section. I have the following:

const results = await sql`
    SELECT
      *
    FROM
      schema.table
    WHERE
      shorts_availabilities && ARRAY[${body.timespan}]::date[]
    ${body.areas !== 0 ? sql`AND areas && ARRAY[${body.area}]::INTEGER[]` : sql``}
    `

I get a syntax error at "AND"

MikeBellika commented 8 months ago

I think you might need to change && to AND