- Notifications
You must be signed in to change notification settings - Fork 0
Value(s)
Christoph Herrmann edited this page Oct 17, 2019 · 5 revisions
const values = ['email', 'name'] const result = await sql.query(sql` INSERT INTO users (email, name) VALUES (${sql.values(values)}) `) // text: INSERT INTO users (email, name) VALUES ($1, $2) // values: ['email', 'name']If the parameter is an object (e.g. a user) the values of the object will be used:
const user = { email: 'email', name: 'name' } const result = await sql.query(sql` INSERT INTO users (email, name) VALUES (${sql.values(user)}) `) // text: INSERT INTO users (email, name) VALUES ($1, $2) // values: ['email', 'name']In that object case it's also possible to give a list of columns, which will be extracted:
const user = { email: 'email', name: 'name', active: true } const result = await sql.query(sql` INSERT INTO users (email, name) VALUES (${sql.values(user, { columns: ['email', 'name'] })}) `) // text: INSERT INTO users (email, name) VALUES ($1, $2) // values: ['email', 'name']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