Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

#44 Update documentation for upsert

Angelico8e0f81debd66
diff --git a/web/docs/library/examples/patch.js b/web/docs/library/examples/patch.js
index 997e073ce9b..2c3fb62a697 100644
--- a/web/docs/library/examples/patch.js
+++ b/web/docs/library/examples/patch.js
@@ -30,7 +30,7 @@ const updateCityName = async () => {
try {
let cities = await supabase
.from('cities')
- .filter({'name', 'eq', 'Paris'})
+ .filter('name', 'eq', 'Paris')
.update({ name: 'Mordor' })
return cities
} catch (error) {
diff --git a/web/docs/library/examples/post.js b/web/docs/library/examples/post.js
index cb23c309327..ba21b5ee8cd 100644
--- a/web/docs/library/examples/post.js
+++ b/web/docs/library/examples/post.js
@@ -39,4 +39,27 @@ const createCities = async () => {
console.log('Error: ', error)
}
}
+`
+
+export const postUpsertJs = `
+import { createClient } from '@supabase/supabase-js'
+
+const supabase = createClient('https://world.supabase.co', '1a2b-3c4d-5e6f-7g8h')
+
+const createCities = async () => {
+ try {
+ let cities = await supabase
+ .from('cities')
+ .insert(
+ [
+ { name: 'The Shire', country_id: 554 },
+ { name: 'Rohan', country_id: 555 },
+ { name: 'City by the Bay', country_id:840}
+ ],
+ { upsert: true })
+ return cities
+ } catch (error) {
+ console.log('Error: ', error)
+ }
+}
`
\ No newline at end of file
diff --git a/web/docs/library/post.mdx b/web/docs/library/post.mdx
index ed2e85486cc..a341342bd87 100644
--- a/web/docs/library/post.mdx
+++ b/web/docs/library/post.mdx
@@ -10,7 +10,7 @@ import Collapsable from '../../src/components/Collapsable'
import CommonResponses from '../common/CommonResponses.mdx'
import FromFunction from '../common/FromFunction.mdx'
import CustomCodeBlock from '../../src/components/CustomCodeBlock'
-import { postSingleJs, postBulkJs } from './examples/post'
+import { postSingleJs, postBulkJs, postUpsertJs } from './examples/post'
<DummyData />
@@ -35,7 +35,7 @@ import { postSingleJs, postBulkJs } from './examples/post'
### Bulk create
<CustomCodeBlock
- header="Creates a multiple new cities."
+ header="Creates multiple new cities."
js={postBulkJs}
jsHighlight="{7-12}"
response={`{
@@ -49,6 +49,24 @@ import { postSingleJs, postBulkJs } from './examples/post'
}`}
/>
+### Upsert
+
+<CustomCodeBlock
+ header="Creates multiple new cities and updates existing ones."
+ js={postUpsertJs}
+ jsHighlight="{7-15}"
+ response={`{
+ "body": [
+ { "name": "The Shire", "country_id": 554},
+ { "name": "Rohan", "country_id": 555},
+ { "name": "City by the Bay", "country_id":840}
+ ],
+ "status": 201,
+ "statusCode": 201,
+ "statusText": "Created"
+}`}
+/>
+
## Reference
### `from()`
@@ -65,7 +83,15 @@ supabase
A single `object` or an array of rows of type `object` which contain information to be saved into the selected table.
#### `options: object?`
-@todo
+
+```js
+const options = {
+ upsert: false // Set to true to allow upserting.
+}
+```
+For `upsert`, if set to `true`, primary key columns would need to be included in the `data` parameter in order for an update to properly happen.
+Also, primary keys used must be natural, not surrogate.
+There are however, [workarounds](https://github.com/PostgREST/postgrest/issues/1118) for surrogate primary keys.
---