0Pricing
React Native Academy · Lesson

Querying the Database with the Supabase Client

Use the Supabase client to select, insert, update, and delete rows from a Postgres table, apply filters with eq and gte, and handle Row Level Security policies.

The Supabase Query Builder

The Supabase JS client exposes a query builder that wraps the PostgREST API, letting you build SQL-like queries in JavaScript. You start every query with supabase.from('table_name') and chain methods like .select(), .insert(), .update(), and .delete().

All queries return a Promise that resolves to { data, error }. Always check error before using data — if the query fails, data will be null.

const { data, error } = await supabase
  .from('posts')
  .select('*');

if (error) {
  console.error(error.message);
} else {
  console.log(data); // Array of post objects
}

Selecting Specific Columns

Instead of fetching all columns with select('*'), you can specify only the columns you need. This reduces data transfer and is especially important on mobile networks where bandwidth is limited.

You can also select columns from related tables using embedded relationships. Supabase infers the join from your foreign key definitions, so select('title, author:profiles(username)') performs a join and nests the profile data inside each post object.

// Select specific columns
const { data } = await supabase
  .from('posts')
  .select('id, title, created_at');

// Select with a join
const { data: postsWithAuthor } = await supabase
  .from('posts')
  .select('title, content, author:profiles(username, avatar_url)');

// Each item in postsWithAuthor looks like:
// { title: 'Hello', content: '...', author: { username: 'alice', avatar_url: '...' } }

All lessons in this course

  1. Setting Up Supabase Client in React Native
  2. Email and OAuth Authentication
  3. Querying the Database with the Supabase Client
  4. Real-Time Subscriptions
← Back to React Native Academy