Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder API for connection options #75

Open
fmoor opened this issue Jan 25, 2021 · 1 comment
Open

builder API for connection options #75

fmoor opened this issue Jan 25, 2021 · 1 comment

Comments

@fmoor
Copy link
Member

fmoor commented Jan 25, 2021

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

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
Copy link
Member Author

fmoor commented Jan 25, 2021

cc @1st1 @tailhook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant