The following examples demonstrate how to perform standard database operations using the main branch API.
// Select with filters
opts := &postgrest.SelectOptions{Count: "exact"}
response, err := client.
From("users").
Select("id, name, email", opts).
Eq("status", "active").
Limit(10, nil).
Order("name", &postgrest.OrderOptions{Ascending: true}).
Execute(context.Background())
// Insert a single row
insertOpts := &postgrest.InsertOptions{Count: "exact"}
response, err = client.
From("users").
Insert(map[string]interface{}{
"name": "John Doe",
"email": "john@example.com",
}, insertOpts).
Execute(context.Background())
// Update rows
updateOpts := &postgrest.UpdateOptions{Count: "exact"}
response, err = client.
From("users").
Update(map[string]interface{}{
"status": "inactive",
}, updateOpts).
Eq("id", 1).
Execute(context.Background())
// Delete rows
deleteOpts := &postgrest.DeleteOptions{Count: "exact"}
response, err = client.
From("users").
Delete(deleteOpts).
Eq("status", "inactive").
Execute(context.Background())
// Upsert (insert or update)
upsertOpts := &postgrest.UpsertOptions{
OnConflict: "email",
IgnoreDuplicates: false,
Count: "exact",
}
response, err = client.
From("users").
Upsert(map[string]interface{}{
"email": "john@example.com",
"name": "John Doe",
}, upsertOpts).
Execute(context.Background())