Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewModule ¶
func NewModule(namespace string, opts ...ModuleOption) fx.Option
NewModule construct a new fx Module for mongodb, using configuration options Each mongo client will be named as <namespace>_<name> Also register a <namespace> group
Example ¶
package main import ( "github.com/lagolibs/mongofx" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/fx" ) func main() { configs := make(map[string]string, 2) configs["clienta"] = "mongodb://localhost:27017/dba" configs["clientb"] = "mongodb://localhost:27017/dbb" fx.New( mongofx.NewModule("mongo", mongofx.WithURIs(configs)), fx.Invoke( fx.Annotate(func(client *mongo.Client) {}, fx.ParamTags(`name:"mongo_clienta"`), ), ), ).Run() }
Output:
Example (SingleClient) ¶
package main import ( "github.com/lagolibs/mongofx" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/fx" ) func main() { configs := make(map[string]string, 2) configs["clienta"] = "mongodb://localhost:27017/dba" fx.New( mongofx.NewModule("mongo", mongofx.WithURIs(configs)), fx.Invoke( fx.Annotate(func(client *mongo.Client) {}, fx.ParamTags(`name:"mongo_clienta"`), ), ), ).Run() }
Output:
func NewSimpleModule ¶
NewSimpleModule construct a module contain single client. Does not register group namespace. The name of the mongo client is the same as the name space.
Example ¶
package main import ( "github.com/lagolibs/mongofx" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/fx" ) func main() { fx.New( mongofx.NewSimpleModule("mongo", "mongodb://localhost:27017"), fx.Invoke( fx.Annotate(func(client *mongo.Client) {}, fx.ParamTags(`name:"mongo"`), ), ), ).Run() }
Output:
Types ¶
type ModuleOption ¶
type ModuleOption func(conf *moduleConfig)
ModuleOption applies an option to moduleConfig
func WithClient ¶
func WithClient(name string, options *options.ClientOptions) ModuleOption
func WithConnectTimeout ¶
func WithConnectTimeout(totalTimeout time.Duration) ModuleOption
WithConnectTimeout set the timeout for client initialization. The timeout for Connect and Ping operations will be half of given totalTimeout for each.
Example ¶
package main import ( "github.com/lagolibs/mongofx" "go.mongodb.org/mongo-driver/mongo" "go.uber.org/fx" "time" ) func main() { configs := make(map[string]string, 2) configs["clienta"] = "mongodb://localhost:27017/dba" configs["clientb"] = "mongodb://localhost:27017/dbb" fx.New( mongofx.NewModule("mongo", mongofx.WithURIs(configs), mongofx.WithConnectTimeout(5*time.Second)), fx.Invoke( fx.Annotate(func(client *mongo.Client) {}, fx.ParamTags(`name:"mongo_clienta"`), ), ), ).Run() }
Output:
func WithURIs ¶
func WithURIs(uris map[string]string) ModuleOption
WithURIs create ModuleOption that parse a map of uris into moduleConfig. This help integrate with configuration library such as vipers