edgedb / edgedb-go

The official Go client library for EdgeDB
https://pkg.go.dev/github.com/edgedb/edgedb-go
Apache License 2.0
167 stars 11 forks source link

Expose a common interface between Client and Tx #302

Closed lsdch closed 3 months ago

lsdch commented 3 months ago

This PR introduces the Executor interface which exposes all querying functions that are common to both Client and Tx. Example use-case:

func CreateItem(db edgedb.Executor, name string) (item Item, err error) {
  query := `select (insert Item { name := $0 }) { name };`
  err = db.QuerySingle(context.Background(), query, &item, name)
  return 
}

// Using Client
client, _ := edgedb.CreateClient(ctx, edgedb.Options{})
CreateItem(client, "Item name")

// Using Tx
client.Tx(context.Background(), func(ctx context.Context, tx *edgedb.Tx) error {
  CreateItem(tx, "Item name")
  // Transaction may then be rolled back, e.g. if this is a unit-test
  return errors.New("Rollback")
})
edgedb-cla[bot] commented 3 months ago

All commit authors signed the Contributor License Agreement.
CLA signed

fmoor commented 3 months ago

You can run the linter locally with make lint & make gendocs-lint.

lsdch commented 3 months ago

Thanks! Linter seems to be happy now :slightly_smiling_face:

fmoor commented 3 months ago

This change was released in v0.16.0.