supabase-go icon indicating copy to clipboard operation
supabase-go copied to clipboard

how to select with order by

Open jiayuxiaochaoren opened this issue 2 years ago • 2 comments

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

jiayuxiaochaoren avatar Jul 07 '23 09:07 jiayuxiaochaoren

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)
``

whoiscarlo avatar Oct 25 '23 07:10 whoiscarlo

So in conclusion, we can not use OrderBy, can we?

Yongtae723 avatar Sep 25 '24 01:09 Yongtae723