Closed jauco closed 1 year ago
Yeah, I've been playing around with that myself, and there are some fixes needed to properly support it.
Oh wait.
Just do like this ;)
sql`
select *
from test
where (${ sql(['a', 'b']) }) > (${ 2 }, ${ 4 })
`
Well, that doesn’t work if you don’t know the amount of columns beforehand right?
I have an app where the user can sort by one ore more columns and then I use keyset based pagination.
Right, so that's order by and not where ;)
For that you could do like this:
const ordering = [{ column: 'age' }, { column: 'city', direction: 'desc' }]
const result = await sql`
select * from users
order by ${ ordering.map((x, i) =>
sql`${ i ? sql`,` : sql``} ${ sql(x.column) } ${ x.direction === 'desc' ? sql`desc` : sql`asc` } `
)}
`
The order by wasn't the problem. That works. The where
is needed for keyset pagination. (see: https://use-the-index-luke.com/no-offset ). I have an array of columns, I can order by them. But to do pagination I now also have an array of values for each of those columns to select the row to start from. I can provide the array of columns on the left hand side of the comparison query, but I can't insert the array of values on the righthand side.
Sorry, I got ya, didn't read properly ;)
I guess you could glean a solution from the order sample though ;)
const result = await sql`
select * from users
where (${ sql(['a', 'b']) }) > (${ [2, 4].map((x, i) => sql`${ i ? sql`,` : sql``}${ x }`) })
`
That works in the where query as well? Cool! Thanks!
It should, but didn't test 😋
Postgres has a nice shorthand for multi column greater than expressions:
Which is the same as saying:
I can't get this to work using postgres.js with dynamic columns and values:
This works:
This also works:
This also works:
This breaks:
with the following error: