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
TekipepsOP17mo ago
In the return type, it also shows as array
No description
Ling Mai Jiānguǒ
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
TekipepsOP17mo ago
okay, the return type is not specified in the docs. made me think something was wrong
No description
Ling Mai Jiānguǒ
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
kbemaster17mo 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
TekipepsOP17mo ago
Ah i see
Ling Mai Jiānguǒ
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
TekipepsOP17mo ago
.returning() always returns an array regardless
Ling Mai Jiānguǒ
Yep
Tekipeps
TekipepsOP17mo ago
thanks

Did you find this page helpful?