Closed seeden closed 2 years ago
Problem is with sqlNOW() + INTERVAL '${maxExpireIn} HOURS'
which is not working correctly. Do you have any idea how to write it correctly?
The problem isn't a query with a '
character. '1 HOURS'
is just a string and kysely definitely supports strings. The problem in your query is that you've put a parameter inside a string, which will lead Postgres to not consider it as a parameter.
There is no way for Kysely to automatically "fix" this, since it would mean parsing the SQL around the parameter to figure out if it's inside a string. And that would mean having a full-blown Postgres SQL parser in javascript, which would be tens of megabytes of code.
INTERVAL
doesn't seem to accept bindings at all so you need to use a string literal:
const maxExpiresInHours = sql.literal(`${maxExpireIn} HOURS`)
db.selectFrom('verifyCodes')
.where('userId', '=', userId)
.where('name', '=', name)
.where('used', '=', false)
.where('expireAt', '>', sql`NOW() + INTERVAL ${maxExpiresInHours}`)
.selectAll('verifyCodes')
.executeTakeFirst()
https://koskimas.github.io/kysely/interfaces/Sql.html#literal
Side note, interval
doesn't accept bindings, but you can do ${binding}::interval
instead, which may be safer if the interval value is somehow coming from user input. Altho in this case literal should be fine if we know maxExpireIn
is a number.
I will get next error: bind message supplies 4 parameters, but prepared statement "" requires 3 When I try to use:
Stacktrace
Kysely version: 0.21.6