Crash Courses · Basic
Database Queries
Query, insert, update, and delete data using the Supabase JavaScript client.
11 min read+150 XP on completionCert: Supabase Crash Course
Tap any word in the text below to start reading from there.
Database Queries
Supabase client wraps PostgreSQL queries in a chainable JavaScript API. Every operation returns { data, error }.
Select
const supabase = createClient()
// All rows
const { data: courses, error } = await supabase
.from('courses')
.select('*')
// Specific columns
const { data } = await supabase
.from('courses')
.select('id, title, xp, track')
// With filter
const { data } = await supabase
.from('courses')
.select('*')
.eq('track', 'crash')
.order('module', { ascending: true })
.limit(10)
// Single row
const { data: course, error } = await supabase
.from('courses')
.select('*')
.eq('id', 'cc-js-m01')
.single()Insert
const { data, error } = await supabase
.from('progress')
.insert({
user_id: userId,
course_id: courseId,
completed_at: new Date().toISOString(),
xp_earned: 150,
})
.select()
.single()
if (error) throw new Error(error.message)Update
const { error } = await supabase
.from('profiles')
.update({ display_name: newName, updated_at: new Date().toISOString() })
.eq('id', userId)
if (error) console.error('Update failed:', error.message)Delete
const { error } = await supabase
.from('progress')
.delete()
.eq('course_id', courseId)
.eq('user_id', userId)Error Handling Pattern
async function getCourse(id: string) {
const { data, error } = await supabase
.from('courses')
.select('*')
.eq('id', id)
.single()
if (error) {
if (error.code === 'PGRST116') return null // not found
throw new Error(`Database error: ${error.message}`)
}
return data
}Reading progress
0% read
In this module
.from()
.select()
.eq() / .filter()
.single()
0%