nedpals / supabase-go

Unofficial Supabase client library for Go.
https://pkg.go.dev/github.com/nedpals/supabase-go
MIT License
362 stars 69 forks source link

how to select with order by #21

Open jiayuxiaochaoren opened 12 months ago

jiayuxiaochaoren commented 12 months ago

I want to get latest record,but i don't know how to query to meet such needs.

whoiscarlo commented 8 months ago

So there is a way to do this. If you look at this file on line 343:

func (b *SelectRequestBuilder) OrderBy(column, direction string) *SelectRequestBuilder {
    b.params.Set("order", column+"."+direction)
    return b
}

The way it would be used is:

var results []interface{}
client := supa.CreateClient(supabase_url, supabase_key)
err := client.DB.From(tableName).Select("*").OrderBy("name", "asc").ExecuteWithContext(ctx, &results)

log.Info("results", results)

However, this addition hasn't been added to the latest tags/ releases of postgres-go so you currently can't do it until @nedpals adds the new tag.


I did push this PR though, so that the function would work this way since the order parameter is just a binary function at the end of the day.

If accepted the function would work like this:


var results []interface{}
client := supa.CreateClient(supabase_url, supabase_key)
// true for ascend
// false for descend
err := client.DB.From(tableName).Select("*").OrderBy("name", true).ExecuteWithContext(ctx, &results)

log.Info("results", results)
``