Version: Next

Creating

Toggle Example Tables

We will be fictional "world" database in all of our examples.

Countries
idnameiso2population_range_millionsmain_exports
76BrazilBR( 205, 215 )[ beans, minerals ]
156ChinaCN( 1380, 1390 )[ computers, phones ]
250FranceFR( 60, 70 )[ cars, machines ]
554New ZealandNZ( 4, 6 )[ food, machines ]
556NigeriaNG( 185, 195 )[ oil, beans ]
840United StatesUS( 320, 330 )[ oil, cars, food ]
Cities
namecountry_id
Rio de Janeiro76
Beijing156
Paris250
Auckland554
Lagos556
Los Angeles840
San Francisco840

Create a record

Creates a new city.
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 }
])
return cities
} catch (error) {
console.log('Error: ', error)
}
}

Bulk create

Creates multiple new cities.
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 },
])
return cities
} catch (error) {
console.log('Error: ', error)
}
}

Upsert

Creates multiple new cities and updates existing ones.
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)
}
}

Reference

from()

supabase
.from(tableName)

tableName: string

Name of the database table to be listened to. Using the wildcard "*" will lead to all tables to be listened to.

tableName: string

Name of the database table that will be read from.

tableName: string

Name of the database table where data will be saved.

tableName: string

Name of the database table where data will be updated.

tableName: string

Name of the database table where data will be deleted.


insert()

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?

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 for surrogate primary keys.


Responses

201 Created

Successful

400 Bad request

An invalid syntax or configuration was sent.

401 Unauthorized

Invalid credentials were provided.

404 Not found

Requested resource cannot be found.

406 Not acceptable

The response provided by the server does not match the list of acceptable values stated in the request's headers.

409 Violating foreign key constraint

Raised when you are trying to insert or update into a table with a foreign key which doesn't exist on the target foreign relationship.

500 Internal Server Error

The server was unable to encounter the situation it encountered.