Discover how to query and filter data in Supabase using our step-by-step JavaScript guide with clear code examples for eq, gt, and like filters.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set up your Supabase Account and Project
Step 2: Install Supabase Client
To query data from Supabase, you'll need to install the Supabase JavaScript client in your project.
First, make sure you have Node.js installed. Then, initialize a new Node.js project, if you haven't already:
npm init -y
Next, install the Supabase client library:
npm install @supabase/supabase-js
Step 3: Initialize the Supabase Client
After installation, you need to initialize the Supabase client using the URL and the public API key of your Supabase project.
const { createClient } = require('@supabase/supabase-js')
const supabaseUrl = 'https://xyzcompany.supabase.co'
const supabaseKey = 'public-anon-key'
const supabase = createClient(supabaseUrl, supabaseKey)
Replace 'https://xyzcompany.supabase.co'
and 'public-anon-key'
with your actual Supabase project URL and anonymous key found in your project's settings under "API".
Step 4: Query with Filters
Now, let's retrieve data with specific filters. Assume you have a table named customers
.
To fetch all entries from the customers
table:
async function getCustomers() {
let { data, error } = await supabase
.from('customers')
.select('*')
if (error) console.log('Error:', error)
else console.log('Data:', data)
}
getCustomers()
To filter the customers where the age
is greater than 30
:
async function getCustomersAgeOver30() {
let { data, error } = await supabase
.from('customers')
.select('*')
.gt('age', 30)
if (error) console.log('Error:', error)
else console.log('Data:', data)
}
getCustomersAgeOver30()
Step 5: Use Additional Filter Options
Supabase provides various methods for filtering data. Some commonly used filters are:
eq
: Equal toneq
: Not equal togte
: Greater than or equal tolte
: Less than or equal tolike
: Pattern matchingilike
: Case-insensitive pattern matchingHere's an example using some of these filters:
// Filter customers with first name 'John'
async function getCustomersNamedJohn() {
let { data, error } = await supabase
.from('customers')
.select('*')
.eq('first_name', 'John')
if (error) console.log('Error:', error)
else console.log('Data:', data)
}
getCustomersNamedJohn()
// Filter customers whose last name starts with 'Do'
async function getCustomersWithLastNameStartingWithDo() {
let { data, error } = await supabase
.from('customers')
.select('*')
.like('last_name', 'Do%')
if (error) console.log('Error:', error)
else console.log('Data:', data)
}
getCustomersWithLastNameStartingWithDo()
Step 6: Combine Multiple Filters
You can combine multiple filters to retrieve more specific data:
// Filter customers named 'John' with age greater than or equal to 25
async function getSpecificCustomers() {
let { data, error } = await supabase
.from('customers')
.select('*')
.eq('first_name', 'John')
.gte('age', 25)
if (error) console.log('Error:', error)
else console.log('Data:', data)
}
getSpecificCustomers()
Conclusion
By following the steps above, you can effectively query and filter data in your Supabase database using the JavaScript client. The flexibility of the query filters allows you to tailor your data requests precisely to your needs.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.