mirror of
https://github.com/supabase/supabase.git
synced 2026-10-05 09:25:06 +03:00
103 lines
2.2 KiB
Plaintext
103 lines
2.2 KiB
Plaintext
---
|
|
id: post
|
|
title: 'Creating'
|
|
description: 'Add new records to your database with Supabase'
|
|
---
|
|
|
|
|
|
import DummyData from '../common/_DummyData.mdx'
|
|
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, postUpsertJs } from './examples/post'
|
|
|
|
|
|
<DummyData />
|
|
|
|
|
|
### Create a record
|
|
|
|
<CustomCodeBlock
|
|
header="Creates a new city."
|
|
js={postSingleJs}
|
|
jsHighlight="{7-11}"
|
|
response={`{
|
|
"body": [
|
|
{ "name": "The Shire", "country_id": 554}
|
|
],
|
|
"status": 201,
|
|
"statusCode": 201,
|
|
"statusText": "Created"
|
|
}`}
|
|
/>
|
|
|
|
### Bulk create
|
|
|
|
<CustomCodeBlock
|
|
header="Creates multiple new cities."
|
|
js={postBulkJs}
|
|
jsHighlight="{7-12}"
|
|
response={`{
|
|
"body": [
|
|
{ "name": "The Shire", "country_id": 554},
|
|
{ "name": "Rohan", "country_id": 555}
|
|
],
|
|
"status": 201,
|
|
"statusCode": 201,
|
|
"statusText": "Created"
|
|
}`}
|
|
/>
|
|
|
|
### 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()`
|
|
<FromFunction verb='post' />
|
|
|
|
### `insert()`
|
|
```js {3}
|
|
supabase
|
|
.from(tableName)
|
|
.insert(data, options?)
|
|
```
|
|
|
|
#### `data: { array | object }`
|
|
A single `object` or an array of rows of type `object` which contain information to be saved into the selected table.
|
|
|
|
#### `options: object?`
|
|
|
|
```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.
|
|
|
|
---
|
|
|
|
## Responses
|
|
|
|
#### 201 Created
|
|
Successful
|
|
|
|
<CommonResponses /> |