Closed chauhankiran closed 11 months ago
Destructuring for the win 😉
const [user] = await sql`...`
// user.id
And if you don't want the temporary variable you can do destructuring in .then
like this:
return await sql`...`.then(([x]) => x)
And going further a utility function is so simple to have that increasing the surface api of this library is not worth it:
const first = ([x]) => x
const user = first(await sql`...`)
// or
const user = await sql`...`.then(first)
Consider the following query.
This will return a single user.
And there always be scenario where we're interested to fetch only single row than multiple row results. So, it there a way that we can only return just a single object (
{}
) instead array of object ([{}]
)? As of now, I need to write likeuser[0].id
instead ofuser.id
.