nedpals / supabase-go

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

Select on insert #23

Closed Shalankwa closed 11 months ago

Shalankwa commented 1 year ago

Hi, I'm curious how to get the ID of an inserted row.

For other SDK it looks like you perform a .Select() after the insert, but this doesn't look to be supported here.

Offical js SDK

const { data, error } = await supabase
  .from('countries')
  .insert({ id: 1, name: 'Denmark' })
  .select()

Expected - supabase-go supabase.DB .From("Table") .Insert(row) .Select() .Execute(&result)

whoiscarlo commented 11 months ago

Hopefully this illustrates how to:

func main() {

    // Or some other type of struct
    var results []struct {
        Id int64 `json:"id"`
    }

    ctx := context.Background()
    supabase := supa.CreateClient(supabase_url, supabase_key)

    // This will always return an array of results
    err := supabase.DB.From(tableName).Insert(row).ExecuteWithContext(ctx, &results)
    if err != nil {
        log.Fatal(err.Error())
    }

    log.Debugf("results: %#v", results)
    log.Info(results[0].Id)
}