select columns from an insert
See original GitHub issueFeature request
Is your feature request related to a problem? Please describe.
I need a way to perform an upsert, and know if I performed an update or an insert afterwards. Postgrest has an open issue to support this via 200 and 201 status codes https://github.com/PostgREST/postgrest/issues/1070. For the time being though, I can infer this on the client side using the xmax
column. E.g.
POST http://<supabase-db-url>.supabase.co/rest/v1/items?select=id,xmax
If xmax === "0"
then the row was inserted, otherwise an update occurred. Unfortunately, there is no way to write this kind of query with the supabase client.
Describe the solution you’d like
const result = await supabase
.from('items')
.insert([row], { onConflict: 'unique_field', upsert: true })
.select('id,xmax')
if (result.data[0].xmax === "0") {
console.log('insert')
} else {
console.log('update')
}
Describe alternatives you’ve considered
write an rpc function to wrap this logic
Additional context
an issue on the PostgREST repo discussing possible solutions here: https://github.com/PostgREST/postgrest/issues/1683
selects on insert is valuable for multiple reasons outside of my outlined use case. Any performant client will be interested in returning the minimal rows necessary after an update/insert, rather than all of them all the time. I feel like I saw an issue or discussion around the default behavior around the RETURNING
clause but for the life of me I cannot find it.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (6 by maintainers)
Yes, I think it should be supported. I’ll move this to postgrest-js(which supabase-js uses underlyingly).
🎉 This issue has been resolved in version 0.23.0 🎉
The release is available on:
Your semantic-release bot 📦🚀