-
Notifications
You must be signed in to change notification settings - Fork 0
Selection Methods
Christoph Herrmann edited this page Oct 17, 2019
·
4 revisions
The return value is result.rows of the pg result object.
const users = await sql.any(
'users',
{ name: 'name' }
)
// text: SELECT "*" FROM "users" WHERE "name" = $1
// values: ['name']const users = await sql.any(
'users',
['id', 'email', 'active'],
{ name: 'name' }
)
// text: SELECT "id", "email", "active" FROM "users" WHERE "name" = $1
// values: ['name']If several rows needs to be selected, e.g. some validated users:
const users = await sql.any(
sql`SELECT "*" FROM "users" WHERE validated = true`
)
// text: SELECT "*" FROM "users" WHERE validated = true
// values: []manyOrNone is an alias for any.
many will throw an error if there is not at least one row in the result.
If one row needs to be selected, e.g. a specific user:
const user = await sql.one(
sql`SELECT "*" FROM "users" WHERE "id" = 1`
)
// text: SELECT "*" FROM "users" WHERE "id" = 1
// values: []oneOrNone returns undefined instead of throwing an error if there is none row in the result.
table can also be given as an array with the schema. Otherwise if it's defined the defaultSchema option will be used.
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