How do I make .returning() return only the inserted value instead of array

Here I have createResource to insert a single record. The result from .returning is an array. I am using neon postgres db
No description
10 Replies
Tekipeps
TekipepsOP5mo ago
In the return type, it also shows as array
No description
Sword Molester
Sword Molester5mo ago
It returns an array composed only of the records you've inserted in that query, no? If you know that there will be one record, since that is what you're inserting, just return createdResource[0]!
Tekipeps
TekipepsOP5mo ago
okay, the return type is not specified in the docs. made me think something was wrong
No description
Sword Molester
Sword Molester5mo ago
I see, values is meant to take in (spread) array inside, so it works as intended, though I agree that it might be not very clear at the first glance.
kbemaster
kbemaster5mo ago
I like to do the following, when I know I just return 1 element
const [insertedValue] = db.insert(table).values(someValue).returning()
const [insertedValue] = db.insert(table).values(someValue).returning()
Tekipeps
TekipepsOP5mo ago
Ah i see
Sword Molester
Sword Molester5mo ago
With some sqlite drivers, you can add .get() to the query to specify that it's single or no records returned, that's what I usually do.
Tekipeps
TekipepsOP5mo ago
.returning() always returns an array regardless
Sword Molester
Sword Molester5mo ago
Yep
Tekipeps
TekipepsOP5mo ago
thanks
Want results from more Discord servers?
Add your server