Query your database with the JavaScript SDK
The everyday flows of `aura.db`, the rules that refuse unsafe calls, and how to protect a table with row-level security.
1. Read rows
aura.from('todos') is the form used throughout this documentation (it is a shortcut for aura.db.from('todos'), the same call). Every call returns { data, error } and never throws, see client options and errors.
Filters chain: eq, neq, gt, gte, lt, lte, like, ilike, in, is, and the others listed in the database reference. For a page of rows, use range(from, to).
2. Insert, update and delete
A write returns nothing (data is null) unless you chain .select(): inserting 10,000 rows does not send 10,000 rows back. With .select() the rows come back, and a PATCH or POST that matched no row says why in meta.hint.
Two rules protect you from mistakes:
- A batch insert needs the same keys in every row. Rows with different keys return
400withAll object keys must match. Fill the missing keys withnull. update()anddelete()without a filter are refused by the client with the error codeUNFILTERED_MUTATION, before any request is sent. To change every row on purpose, run SQL from a server.
3. One row, many rows, counts
count: 'exact' counts every matching row. 'planned' and 'estimated' are faster on very large tables and approximate.
4. Protect a table
Row-level security is off on a new table. With the public key, anyone can read and write it. Enable it, then add the policies you need:
The database roles are aura_anon, aura_authenticated and aura_service_role. auth.uid() is NULL for the public key and a user id for a signed-in user. See Row-Level Security.
A service_role key bypasses RLS by design: keep it on your server.
5. SQL functions and rpc
Create a function from a server, then call it from anywhere with rpc():
grant_execute_tois required to call it. Without it, nobody can execute the function. A role that is not listed gets403 permission denied for function. Bothauthenticatedandaura_authenticatedare accepted.- A new function is callable after a few seconds. Until the API reloads its schema,
rpc()returns404 Could not find the function. - Creating functions with raw SQL (
CREATE FUNCTION) is refused: usecreateFunction.
6. Run SQL from a server
db.sql() needs a service_role key. It refuses a DELETE without a WHERE, and it does not read system catalogs such as pg_class or pg_roles. To list your tables, use admin.db.schema().