Creating

Toggle Example Tables

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

Countries
idnameiso2continent
76BrazilBRSouth America
156ChinaCNAsia
250FranceFREurope
554New ZealandNZOceania
566NigeriaNGAfrica
840United StatesUSNorth America
Cities
namecountry_id
Rio de Janeiro76
Beijing156
Paris250
Auckland554
Lagos556
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 getCities = 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.

500 Internal Server Error

The server was unable to encounter the situation it encountered.