mongofx

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 23, 2023 License: MIT Imports: 7 Imported by: 0

README

Mongo Fx

Fx Module for mongo client

Installation

Use Go modules to install mongofx.

go get -u github.com/lagolibs/mongofx

Usage

The recommended way to config mongo client is using uri options.

mongodb://[username:password@]host1[:port1][,...hostN[:portN]][/[defaultauthdb][?options]]

Example usage with multiple mongodb and viper:

mongodb.uris:
  clienta: mongodb://localhost:27017/dba
  clientb: mongodb://localhost:27017/dbb
package main

import (
	"github.com/lagolibs/mongofx"
	"github.com/samber/lo"
	"github.com/spf13/viper"
	"go.mongodb.org/mongo-driver/mongo"
	"go.uber.org/fx"
	"os"
)

func init() {
	viper.AddConfigPath(lo.Must(os.Getwd()))
	viper.SetConfigType("yaml")
	viper.SetConfigName("config")
	viper.AutomaticEnv()

	if err := viper.SafeWriteConfig(); err != nil {
		lo.Must0(viper.ReadInConfig())
	}
}

func main() {
	app := fx.New(
		mongofx.NewModule("mongo", mongofx.WithURIs(viper.GetStringMapString("mongodb.uris"))),
		fx.Invoke(fx.Annotate(func(client *mongo.Client, client2 *mongo.Client) {}, fx.ParamTags(`name:"mongo_clienta"`, `name:"mongo_clienb"`))),
	)

	app.Run()
}

See tests for more usage.

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

func NewSimpleModule(namespace string, uri string) fx.Option

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL