Converting SQL to JavaScript API
A guide from Supabase
Apps on Plentiful run on Supabase's open-source database, sign-in, file storage and live updates, so most of this guide applies. The differences: your app reaches its database through its own server — not Supabase's data API (supabase.from(…)) — there are no Edge Functions (your app's server does that work), and scheduled work uses Jobs. Where it says "Supabase dashboard", use your app's Dashboard. Ask the builder when in doubt.
Many common SQL queries can be written using the JavaScript API, provided by the SDK to wrap Data API calls. Below are a few examples of conversions between SQL and JavaScript patterns.
Select statement with basic clauses#
Select a set of columns from a single table with where, order by, and limit clauses.
1select first_name, last_name, team_id, age2from players3where age between 20 and 24 and team_id != 'STL'4order by last_name, first_name desc5limit 20;1const { data, error } = await supabase2 .from('players')3 .select('first_name,last_name,team_id,age')4 .gte('age', 20)5 .lte('age', 24)6 .not('team_id', 'eq', 'STL')7 .order('last_name', { ascending: true }) // or just .order('last_name')8 .order('first_name', { ascending: false })9 .limit(20)Select statement with complex Boolean logic clause#
Select all columns from a single table with a complex where clause: OR AND OR
1select *2from players3where ((team_id = 'CHN' or team_id is null) and (age > 35 or age is null));1const { data, error } = await supabase2 .from('players')3 .select() // or .select('*')4 .or('team_id.eq.CHN,team_id.is.null')5 .or('age.gt.35,age.is.null') // additional filters imply "AND"Select all columns from a single table with a complex where clause: AND OR AND
1select *2from players3where ((team_id = 'CHN' and age > 35) or (team_id != 'CHN' and age is not null));1const { data, error } = await supabase2 .from('players')3 .select() // or .select('*')4 .or('and(team_id.eq.CHN,age.gt.35),and(team_id.neq.CHN,.not.age.is.null)')