-
Notifications
You must be signed in to change notification settings - Fork 0
Christoph Herrmann edited this page Oct 17, 2019
·
4 revisions
Sometimes it's needed to build queries dynamically depending of some conditions, e.g. select user by name:
const name = 'raa'
const page = 0
const users = await sql.query(sql`
SELECT email FROM users
${sql.if(name, sql`WHERE name LIKE ${`%${name}%`}`, sql.pagination(page))}
`)
// text:
// SELECT name, email FROM users
// WHERE name LIKE $1
// values: ['%raa%']or if no name is given, all users are selected supported with pagination:
const name = ''
const page = 0
const users = await sql.query(sql`
SELECT email FROM users
${sql.if(name, sql`WHERE name LIKE ${`%${name}%`}`, sql.pagination(page))}
`)
// text:
// SELECT name, email FROM users
// LIMIT 10 OFFSET 0
// values: []If the else case is omitted, nothing will be added in the falsy case.
Found a bug or missing a feature? -> Create a new Issue
Found a security issue? -> Look at the Security Policy
Having questions, want to give feedback or talk to me? -> E-Mail me sql-pg@sharaal.de