qsmongodb

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvMongoUri      string = "MONGO_URI"
	EnvMongoDatabase string = "MONGO_DATABASE"
)

Variables

View Source
var (
	ErrMongoNoUri      = errors.New("no MongoDB URL found")
	ErrMongoNoDatabase = errors.New("no MongoDB database found")
)
View Source
var Module = fx.Options(
	fx.Provide(
		fx.Annotate(
			func(logger *zap.Logger, cfg *MongoDbConfigs, lc fx.Lifecycle) (*mongo.Client, *mongo.Database, error) {
				c, err := newMongoClient(cfg)
				if err != nil {
					return nil, nil, err
				}
				lc.Append(fx.Hook{
					OnStart: hookOnStart(logger, c),
					OnStop:  hookOnStop(logger, c),
				})
				db := newMongoDatabase(c, cfg)
				return c, db, nil
			},
			fx.ParamTags(
				`optional:"true"`,
			),
		),
	),
)

This module provides a mongo.Client and mongo.Database. It requires a MongoDbConfigs to work. For example:

var Module = fx.Options(
	mongo.ProvideMongoConfig(&mongo.EnvConfigType{})
)

In case a dependency is not found, it just skip

Functions

func ProvideMongoConfig

func ProvideMongoConfig(cfgs qsconfig.ConfigType[*MongoDbConfigs]) fx.Option

Validate, then provide the app with a MongoDB config.

To use this:

var Module = fx.Options(
	qsmongo.ProvideMongoConfig(&qsmongo.EnvConfigType{})
)

this code will provide a new default instance of a MongoDbConfigs that can be used to instantiate a mongo.Client.

in case you want to customize a MongoDbConfigs yourself, implements:

qsconfig.ConfigType[*wsmongo.MongoDbConfigs]

If you need custom dependencies, don't use this, instead, create a fx.Option yourself:

 fx.Provide(func (dep *Dependencies) (*qsmongo.MongoDbConfigs, error) {
		// Do something
		return &qsmongo.MongoDbConfigs{
			// Options here
		}
 })

func ValidateConfig

func ValidateConfig(opts *MongoDbConfigs) error

Validate configs for MongoDB client

Types

type EnvConfigType

type EnvConfigType struct{}

Requires these following environment variables

  • MONGO_DATABASE: The database name
  • MONGO_URI: URL of the MongoDB server. For example: mongodb://user:password@localhost:27017/?authSource=admin

func (*EnvConfigType) Config

func (t *EnvConfigType) Config() (*MongoDbConfigs, error)

type MongoDbConfigs

type MongoDbConfigs struct {
	URI      string
	Database string
}

func NewMongoConfig

func NewMongoConfig(uri string, db string) *MongoDbConfigs

Create a MongoDB credential holder

Jump to

Keyboard shortcuts

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