Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-348

Refactor Client Creation

      Remove the NewClientFromConnString function from the mongo package. This will remove the confusion around creating connection strings for users and the inability to remake a string version of that connection string form a connstring.ConnString instance.

       


      Currently there are 3 functions that can be used to create a new mongo.Client: NewClient, NewClientWithOptions, and NewClientFromConnString. While useful to be able to create a client with either a URI or a ConnString it complicates the API. For instance, if we wanted to allow users to also use ClientOptions and a ConnString we would need to add another function.

      The current way of using ClientOptions is to start with mongo.ClientOpt and build the options off of that.

      To help clean up the API, we should change how Clients are created.

      Have a single function, NewClient, that takes a ClientOptions struct. Add two new functions, WithURI and WithConnString, each returning a ClientOptions struct.

      The new API would work like this:

      opts := mongo.WithURI("mongodb://localhost:27017").AppName("foo").Dialer(&net.Dialer{})
      client, err := mongo.NewClient(opts)
      

      We could change the methods on ClientOptions to be With* to make it read better as well. Alternatively, we can just have two functions, NewClient and NewClientWithConnString, with the following signatures:

      func NewClient(uri string, opts *ClientOptions) (*Client, error)
      func NewClientWithConnString(cs connstring.ConnString, opts *ClientOptions) (*Client, error)
      

      But this means adding a custom dialer to the client would look like this:

      opts := mongo.ClientOptions.Dialer(&net.Dialer{})
      client, err := mongo.NewClient("mongodb://localhost:27017", opts)
      

            Assignee:
            isabella.siu@mongodb.com Isabella Siu (Inactive)
            Reporter:
            kris.brandow@mongodb.com Kristofer Brandow (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: