Database

更新时间:
复制 MD 格式

RDS Supabase is built on ApsaraDB RDS for PostgreSQL and offers standard PostgreSQL capabilities. It automatically generates Data APIs, including RESTful APIs, so you can perform create, read, update, and delete (CRUD) operations on your data immediately.

All examples use the Supabase JavaScript client library and follow the supabase.from('<table>').method() chaining pattern.

Basic operations

Retrieve data

select() performs a SELECT query on a table or view.

const { data, error } = await supabase
  .from('characters')
  .select()

Insert data

insert() adds one or more rows to a table. By default, inserted rows are not returned. To return the inserted rows, chain .select() after the call.

const { error } = await supabase
  .from('countries')
  .insert({ id: 1, name: 'Mordor' })

Update data

update() modifies existing rows in a table. By default, updated rows are not returned. To return the updated rows, chain .select() after the filter.

const { error } = await supabase
  .from('instruments')
  .update({ name: 'piano' })
  .eq('id', 1)

Upsert data

upsert() inserts a row if no conflict is found, or updates the existing row if one is found. Use the onConflict parameter to specify one or more columns to check for conflicts.

By default, upserted rows are not returned. To return the rows, chain .select() after the call.

// Insert or update based on primary key
const { data, error } = await supabase
  .from('instruments')
  .upsert({ id: 1, name: 'piano' })
  .select()

// Insert or update based on a specific column
const { data, error } = await supabase
  .from('users')
  .upsert({ id: 42, handle: 'saoirse', display_name: 'Saoirse' }, { onConflict: 'handle' })
  .select()

Delete data

delete() removes rows from a table that match the specified filter. By default, deleted rows are not returned. To return the deleted rows, chain .select() after the filter.

const response = await supabase
  .from('countries')
  .delete()
  .eq('id', 1)

References

  • To learn more about Supabase database features, see Database.

  • For the full Supabase JavaScript client library reference, see JavaScript.

  • To learn about the REST API for managing databases, see Postgres REST API.