Data Model

Use the JavaScript Relational SDK for tables and views in the active schema, including CRUD, filters, JSON paths, ordering, pagination, joins, aggregates, and RPC.

Tables, Rows, and Columns

Call from(relation) to start an operation on a table or another relational object. The select(columns) method retrieves rows. Use * for all columns or a comma-separated projection for selected columns.

Use alias:column syntax to rename selected columns:

const { data, error } = await client
  .from("todos")
  .select("todo_id:id,todo_title:title,completed")
  .eq("completed", false);

Primary Keys and Foreign Keys

A primary key identifies each row with a unique, non-null value or combination of values. A foreign key relates values in one table to a primary or unique key in another table.

Define keys as database constraints when creating or altering tables. For details, see Integrity Constraints.

For upsert(), onConflict names a unique or primary-key column. Offline caching also requires an explicit declaration of each relation’s stable key columns; see Access Data Offline in JavaScript Applications.

Relationships Between Tables

Select related rows through the select() projection. Related tables use a left join by default. Add !inner to return only parent rows that have matching related rows.

const { data, error } = await client
  .from("users")
  .select("id,name,orders!inner(id,total,status)");

Use alias:relation syntax to rename a related table in the result. Use relation(count) to return its row count.

Views

The from(relation) method can start an operation on a view in the active schema. Select rows and columns from a view with the same query methods used for tables.

const { data, error } = await client
  .from("active_todos_view")
  .select("id,title,owner_id");