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

builder API for connection options

Open fmoor opened this issue 4 years ago • 1 comments

I think it would be nice to change the options/configuration API by adding options "constructors", giving the options type a builder API and making the option's fields private. This would:

  • make it possible to distinguish between unset values and values set to 0 for things like MinConns
  • make it possible to check that a configuration is valid before trying to connect

Related Discussion

  • https://github.com/edgedb/edgedb/discussions/1683
  • https://github.com/edgedb/edgedb-go/pull/31#pullrequestreview-554812086

Proposed API

func Connect(context.Context, Options) (Pool, error)
func ConnectOne(context.Context, Options) (Conn, error)

// loads options from a credentials file
func Credentials(string) (Options, error)

// loads options from environment variables
func Environment() (Options, error)

// loads options from a dsn string
func DSN(string) (Options, error)

type Options {
    // appends a host
    AddHost(string) Options
    // sets the host replacing any existing hosts
    Host(string) Options
    User(string) Options
    Database(string) Options
    // etc. Other methods omitted for brevity

    // Err returns an error if any of the other methods were called with invalid values.
    Err() error
}

cfg := edgedb.Credentials("instance-name")
// or
cfg := edgedb.Environment()
// or
cfg := edgedb.DSN("edgedb://foobar")

cfg.ConnectTimeout(500 * time.Milliseconds)
cfg.MinConns(0)  // invalid value causes cfg to be unusable and Err() returns the related error
cfg.MaxConns(10)

// user could check for errors if they want
if cfg.Err() != nil {
    // do something about it
}

con, err := Connect(ctx, cfg)
if err != nil {
    // cfg.Err() is returned from Connect() if not nil
}

fmoor avatar Jan 25 '21 17:01 fmoor

cc @1st1 @tailhook

fmoor avatar Jan 25 '21 17:01 fmoor