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
TekipepsOP6mo ago
In the return type, it also shows as array
No description
Sword Molester
Sword Molester6mo 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
TekipepsOP6mo ago
okay, the return type is not specified in the docs. made me think something was wrong
No description
Sword Molester
Sword Molester6mo 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
kbemaster6mo 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
TekipepsOP6mo ago
Ah i see
Sword Molester
Sword Molester6mo 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
TekipepsOP6mo ago
.returning() always returns an array regardless
Sword Molester
Sword Molester6mo ago
Yep
Tekipeps
TekipepsOP6mo ago
thanks
Want results from more Discord servers?
Add your server