Skip to content

Query data from Supabase database

Hieu Vu edited this page Apr 13, 2023 · 2 revisions

Query data from Supabase Before getting data from Supabase Postgrest, make sure you already set up Postgrest in your project and Supabase dashboard

1. Declare data transfer object (DTO)

Create a data class with @Serializable annotation to be able to parse data from Supabase. For each property, use @SerialName to specify name of the column you want to set it to the property. Remember to set all the properties, if not, it will throw an exception

For example:

@Serializable
data class ProductDto(
    @SerialName("productid")
    val productId: String,
    @SerialName("name")
    val name: String,
    @SerialName("description")
    val description: String,
    @SerialName("price")
    val price: Double,
    @SerialName("image")
    val image: String,
    @SerialName("category")
    val category: String,
    @SerialName("nutrition")
    val nutrition: String,
    @SerialName("_id")
    val _id: Int,
)

2. Make API call

    override suspend fun getProducts() {
        val result = supabasePostgrest["products"]
            .select().decodeList<ProductDto>()
       // Handle result data for next step
    }