CRUD operations with supabase-js - Time & Space Complexity
When using supabase-js to create, read, update, or delete data, it's important to know how the time needed grows as you work with more data.
We want to understand how the number of operations changes when we handle more records.
Analyze the time complexity of the following operation sequence.
const { data, error } = await supabase
.from('items')
.select('*')
.eq('category', 'books')
.limit(n)
for (const item of data) {
await supabase
.from('items')
.update({ status: 'sold' })
.eq('id', item.id)
.single()
}
This code fetches a list of items in the 'books' category, then updates each one to mark it as sold.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: The update call inside the loop that runs once per item.
- How many times: Exactly n times, where n is the number of items fetched.
As you increase the number of items n, the number of update calls grows the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 1 select + 10 updates = 11 calls |
| 100 | 1 select + 100 updates = 101 calls |
| 1000 | 1 select + 1000 updates = 1001 calls |
Pattern observation: The total calls increase roughly in a straight line with the number of items.
Time Complexity: O(n)
This means the time grows directly in proportion to how many items you update.
[X] Wrong: "The update calls inside the loop happen all at once, so time stays the same no matter how many items."
[OK] Correct: Each update waits for the previous one to finish, so the total time adds up with more items.
Understanding how your code scales with more data shows you can write efficient cloud operations, a key skill for real projects.
"What if we updated all items in a single batch call instead of one by one? How would the time complexity change?"
Practice
insert() method do in supabase-js when working with a table?Solution
Step 1: Understand the purpose of
Theinsert()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, andselect()fetches data. Soinsert()must add data.Final Answer:
Adds new data rows to the table -> Option BQuick Check:
insert() = add data [OK]
- Confusing insert() with update()
- Thinking insert() deletes data
- Mixing insert() with select()
users using supabase-js?Solution
Step 1: Recall supabase-js query structure
The correct method chain starts withfrom('table')thenselect()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 AQuick Check:
from() then select() = correct syntax [OK]
- Swapping order of from() and select()
- Using get() instead of select()
- Using query() which is not a supabase-js method
const { data, error } = await supabase.from('products').select('id, name').eq('category', 'books');
console.log(data);Solution
Step 1: Understand the query filters and selects
Theselect('id, name')picks only those two fields. Theeq('category', 'books')filters rows where category equals 'books'.Step 2: Predict the output
The resultdatawill be an array of objects with onlyidandnamefor products in the 'books' category.Final Answer:
An array of products with category 'books' showing only id and name fields -> Option DQuick Check:
select + eq filters data correctly [OK]
- Thinking eq() is invalid
- Assuming select() returns all fields
- Expecting error due to method chaining
const { data, error } = await supabase.from('users').update({ age: 30 }).where('id', 5);Solution
Step 1: Check method chaining for update filters
supabase-js useseq()to filter rows, notwhere().Step 2: Confirm other parts
Updating numeric fields is allowed,select()is optional, and 'users' is a valid table name.Final Answer:
The methodwheredoes not exist; should useeqinstead -> Option CQuick Check:
Use eq() not where() for filtering [OK]
- Using where() instead of eq()
- Thinking update() needs select() always
- Assuming numeric fields can't be updated
orders table where the status is 'cancelled'. Which code snippet correctly performs this action and handles errors?Solution
Step 1: Identify correct delete syntax with filter
supabase-js usesdelete()followed byeq('column', value)to filter rows to delete.Step 2: Check error handling
The code checks iferrorexists 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 AQuick Check:
delete() + eq() + error check = correct [OK]
- Using remove() instead of delete()
- Using where() instead of eq()
- Not handling errors after delete
Ready to prove it?
Pass the test and this pattern moves unknown
