📖 Scenario: You are building a simple web app to manage a list of books using Supabase as your backend database. You will create, read, update, and delete book records using the supabase-js client library.
🎯 Goal: Build a basic set of CRUD operations using supabase-js to add, fetch, update, and delete books in the Supabase database.
📋 What You'll Learn
Use the supabase-js client to connect to the database
Create a new book record with exact fields
Fetch all books from the database
Update a book's title by its ID
Delete a book by its ID
💡 Why This Matters
🌍 Real World
Managing data records in a cloud database is a common task in web and mobile apps. Supabase-js provides an easy way to perform these operations with JavaScript.
💼 Career
Understanding CRUD operations with Supabase is useful for backend and full-stack developers working with modern cloud databases and serverless architectures.
Progress0 / 4 steps
1
Initialize Supabase client and create a new book
Create a variable called supabase by calling createClient with the URL 'https://xyzcompany.supabase.co' and the key 'public-anonymous-key'. Then insert a new book into the books table with title set to 'The Great Gatsby' and author set to 'F. Scott Fitzgerald' using supabase.from('books').insert().
Supabase
Hint
Use createClient to connect. Use insert with an array of objects to add a new book.
2
Fetch all books from the database
Create a variable called books and assign it the result of fetching all rows from the books table using supabase.from('books').select('*'). Use await to wait for the data.
Supabase
Hint
Use select('*') to get all columns. Destructure the data property as books.
3
Update a book's title by ID
Use supabase.from('books').update() to change the title to 'The Great Gatsby - Revised' for the book with id equal to 1. Use eq('id', 1) to filter the row. Use await to wait for the update.
Supabase
Hint
Use update with an object for new values, then eq to specify the row by id.
4
Delete a book by ID
Use supabase.from('books').delete() to remove the book with id equal to 1. Use eq('id', 1) to filter the row. Use await to wait for the deletion.
Supabase
Hint
Use delete() with eq('id', 1) to remove the specific book.
Practice
(1/5)
1. What does the insert() method do in supabase-js when working with a table?
easy
A. Deletes data rows from the table
B. Adds new data rows to the table
C. Updates existing data rows in the table
D. Fetches data rows from the table
Solution
Step 1: Understand the purpose of insert()
The insert() method is used to add new records to a database table.
Step 2: Compare with other CRUD methods
delete() removes data, update() changes data, and select() fetches data. So insert() must add data.
Final Answer:
Adds new data rows to the table -> Option B
Quick Check:
insert() = add data [OK]
Hint: Insert means add new data rows [OK]
Common Mistakes:
Confusing insert() with update()
Thinking insert() deletes data
Mixing insert() with select()
2. Which of the following is the correct syntax to select all rows from a table named users using supabase-js?
easy
A. supabase.from('users').select('*')
B. supabase.select('*').from('users')
C. supabase.from('users').get('*')
D. supabase.query('users').select('*')
Solution
Step 1: Recall supabase-js query structure
The correct method chain starts with from('table') then select() to specify columns.
Step 2: Check each option
supabase.from('users').select('*') matches the correct order and methods. The other options use incorrect method order or invalid methods.
Final Answer:
supabase.from('users').select('*') -> Option A
Quick Check:
from() then select() = correct syntax [OK]
Hint: Use from('table').select('*') to fetch all rows [OK]
C. The method where does not exist; should use eq instead
D. The table name 'users' is invalid
Solution
Step 1: Check method chaining for update filters
supabase-js uses eq() to filter rows, not where().
Step 2: Confirm other parts
Updating numeric fields is allowed, select() is optional, and 'users' is a valid table name.
Final Answer:
The method where does not exist; should use eq instead -> Option C
Quick Check:
Use eq() not where() for filtering [OK]
Hint: Use eq() to filter, not where() [OK]
Common Mistakes:
Using where() instead of eq()
Thinking update() needs select() always
Assuming numeric fields can't be updated
5. You want to delete all rows from the orders table where the status is 'cancelled'. Which code snippet correctly performs this action and handles errors?
hard
A. const { error } = await supabase.from('orders').delete().eq('status', 'cancelled'); if (error) console.error(error);
B. const { data } = await supabase.from('orders').remove().eq('status', 'cancelled'); if (data) console.log(data);
C. const { error } = await supabase.from('orders').delete('status', 'cancelled'); if (error) console.error(error);
D. const { error } = await supabase.from('orders').delete().where('status', 'cancelled'); if (error) console.error(error);
Solution
Step 1: Identify correct delete syntax with filter
supabase-js uses delete() followed by eq('column', value) to filter rows to delete.
Step 2: Check error handling
The code checks if error exists and logs it, which is best practice.
Final Answer:
const { error } = await supabase.from('orders').delete().eq('status', 'cancelled'); if (error) console.error(error); -> Option A
Quick Check:
delete() + eq() + error check = correct [OK]
Hint: Use delete().eq() and check error for safe deletion [OK]
Common Mistakes:
Using remove() instead of delete()
Using where() instead of eq()
Not handling errors after delete
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.