Files
supabase/apps/reference/_supabase_js/generated/insert.mdx
Bobbie Soedirgo a7a56eb800 docs(reference/v2/postgrest-js): update spec
* `curl -sS https://supabase.github.io/postgrest-js/spec.json >
spec/enrichments/tsdoc_v2/postgrest.json`
* `cd spec && make transform generate.js.v2 format`
2022-09-20 16:34:12 +08:00

243 lines
5.3 KiB
Plaintext

---
id: insert
title: 'Create data: insert()'
slug: /insert
custom_edit_url: https://github.com/supabase/supabase/edit/master/spec/supabase_js_v2_legacy.yml
---
import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'
Perform an INSERT into the table or view.
By default, inserted rows are not returned. To return it, chain the call
with `.select()`.
```js
const { data, error } = await supabase
.from('cities')
.insert([{ name: 'The Shire', country_id: 554 }])
```
## Parameters
<ul className="method-list-group">
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
values
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>Row</code> | <code>array</code>
</span>
</h4>
<div class="method-list-item-description">
The values to insert. Pass an object to insert a single row
or an array to insert multiple rows.
</div>
<ul className="method-list-group">
<h5 class="method-list-title method-list-title-isChild expanded">Properties</h5>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
<code>object</code>
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>object</code>
</span>
</h4>
<div class="method-list-item-description">
No description provided.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
Row
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>object</code>
</span>
</h4>
<div class="method-list-item-description">
No description provided.
</div>
</li>
</ul>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
options
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
<code>object</code>
</span>
</h4>
<div class="method-list-item-description">
Named parameters
</div>
<ul className="method-list-group">
<h5 class="method-list-title method-list-title-isChild expanded">Properties</h5>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
count
</span>
<span className="method-list-item-label-badge false">
optional
</span>
<span className="method-list-item-validation">
<code>exact</code> | <code>planned</code> | <code>estimated</code>
</span>
</h4>
<div class="method-list-item-description">
Count algorithm to use to count inserted rows.
`"exact"`: Exact but slow count algorithm. Performs a `COUNT(*)` under the
hood.
`"planned"`: Approximated but fast count algorithm. Uses the Postgres
statistics under the hood.
`"estimated"`: Uses exact count for low numbers and planned count for high
numbers.
</div>
<ul className="method-list-group">
<h5 class="method-list-title method-list-title-isChild expanded">Properties</h5>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
estimated
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
literal
</span>
</h4>
<div class="method-list-item-description">
No description provided.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
planned
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
literal
</span>
</h4>
<div class="method-list-item-description">
No description provided.
</div>
</li>
<li className="method-list-item">
<h4 className="method-list-item-label">
<span className="method-list-item-label-name">
exact
</span>
<span className="method-list-item-label-badge required">
required
</span>
<span className="method-list-item-validation">
literal
</span>
</h4>
<div class="method-list-item-description">
No description provided.
</div>
</li>
</ul>
</li>
</ul>
</li>
</ul>
## Examples
### Create a record
```js
const { data, error } = await supabase
.from('cities')
.insert([{ name: 'The Shire', country_id: 554 }])
```
### Create a record and return it
```js
const { data, error } = await supabase
.from('cities')
.insert([{ name: 'The Shire', country_id: 554 }])
.select()
```
### Bulk create
When running a bulk create, the operation is handled in a single transaction. If any of the inserts fail, all other operations are
rolled back.
```js
const { data, error } = await supabase.from('cities').insert([
{ name: 'The Shire', country_id: 554 },
{ name: 'Rohan', country_id: 555 },
])
```