Row Operations
Use the JavaScript Relational SDK to read, filter, order, insert, update, delete, and upsert relational rows.
Use a relational project with a registered web app. The examples use an initialized client and a todos table that the app can read and modify.
Read Operations
Use select(columns) to retrieve rows. Chain filters, sorting, and pagination methods to refine the result.
Selecting Rows and Columns
Use * to select all columns or provide a comma-separated projection. Use alias:column syntax to assign aliases to selected columns.
const { data, error } = await client
.from("todos")
.select("todo_id:id,todo_title:title,completed")
.eq("completed", false);
Use single() to convert exactly one returned row from an array to an object. It returns null when the query returns zero or multiple rows.
Use maybeSingle() to convert one returned row to an object. It returns null when the query returns zero or multiple rows.
Filtering with Conditions
Filters are chainable. Multiple filters use logical AND unless you group them with or() or and().
const { data, error } = await client
.from("todos")
.select("id,title,priority,status")
.eq("status", "open")
.gte("priority", 2)
.ilike("title", "%sdk%")
.order("priority", { ascending: false });
Ordering and Pagination
Use order(column, options) to sort query results. Set ascending to false for descending order.
Use limit(count) to limit the maximum number of returned rows. Use range(from, to) to return an inclusive result range.
Range positions start at zero. The following example skips the first 20 rows in identifier order and returns up to the next 20 rows:
const { data, error } = await client
.from("todos")
.select("*")
.order("id")
.range(20, 39);
Querying Views
Use from(relation) to query a view. This example uses active_todos_view with id, title, and owner_id columns:
const { data, error } = await client
.from("active_todos_view")
.select("id,title,owner_id");
Write Operations
Use write operations to insert, update, delete, or upsert rows. Chain filters to limit the rows affected by updates and deletions.
The examples use 42 and 43 as sample identifiers. Replace them with suitable IDs from your data; updates and deletes need a matching row.
Inserting Rows
Use insert(values) to insert one row or an array of rows. Chain select() to return the inserted rows.
const { data, error } = await client
.from("todos")
.insert({
title: "Write documentation",
completed: false,
priority: 2,
})
.select("id,title,completed,priority");
Updating Rows
Use update(values) to change rows that match a filter. The following example marks the row with ID 42 as completed:
const { data, error } = await client
.from("todos")
.update({ completed: true })
.eq("id", 42);
Deleting Rows
Use delete() to delete rows that match chained filters. Use a filter to limit the deleted rows.
const { data, error } = await client
.from("todos")
.delete()
.eq("id", 42)
.select("id,title");
Upserting Rows
Use upsert(values, { onConflict }) to insert rows or update existing rows when a conflict occurs. onConflict identifies the unique or primary-key column.
const { data, error } = await client
.from("todos")
.upsert(
{ id: 42, title: "Update release documentation", completed: true },
{ onConflict: "id" }
)
.select("id,title,completed");
Bulk Inserts and Upserts
Pass an array of rows to insert(values) to perform a bulk insert:
const { data, error } = await client
.from("todos")
.insert([
{ title: "Plan release", completed: false },
{ title: "Publish release notes", completed: false },
])
.select("id,title");
Pass an array to upsert() to apply the same conflict-column selection to multiple rows:
const { data, error } = await client
.from("todos")
.upsert(
[
{ id: 42, title: "Plan release", completed: false },
{ id: 43, title: "Publish release notes", completed: false },
],
{ onConflict: "id" },
)
.select("id,title,completed");
Each row must satisfy the table’s constraints. Array input does not establish a transaction across separate SDK requests.
Returning Data from Write Operations
Chain select(columns) to a write operation to specify the columns returned in data. For example, chaining .select("id,title") to insert(values) returns those columns from the inserted rows.
Note: An update(...).select(...) response can return COMPLETED as "TRUE" or "FALSE" strings. Read the row with select() when you need Boolean values.