otmongo

package
v0.13.0-beta3 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package otmongo provides mongo client with opentracing. For documentation about redis usage, see https://pkg.go.dev/go.mongodb.org/mongo-driver

Integration

package otmongo exports the configuration factoryIn the following format:

mongo:
  default:
    uri:

Add the mongo dependency to core:

var c *core.C = core.New()
c.Provide(otmongo.Providers())

Then you can invoke redis from the application.

c.Invoke(func(client *mongo.Client) {
	client.Connect(context.Background())
})

Sometimes there are valid reasons to connect to more than one mongo server. Inject otmongo.Maker to factory a *mongo.Client with a specific configuration entry.

c.Invoke(function(maker otmongo.Maker) {
	client, err := maker.Make("default")
	// do something with client
})
Example
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/DoNewsCode/core"
	"github.com/DoNewsCode/core/otmongo"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/readpref"
)

func main() {
	if os.Getenv("MONGO_ADDR") == "" {
		fmt.Println("set MONGO_ADDR to run this example")
		return
	}
	c := core.New()
	c.ProvideEssentials()
	c.Provide(otmongo.Providers())
	c.Invoke(func(mongo *mongo.Client) {
		err := mongo.Ping(context.Background(), readpref.Nearest())
		fmt.Println(err)
	})
}
Output:

<nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewMonitor

func NewMonitor(tracer opentracing.Tracer) *event.CommandMonitor

NewMonitor creates a new mongodb event CommandMonitor.

func Providers added in v0.2.0

func Providers(optionFunc ...ProvidersOptionFunc) di.Deps

Providers returns a set of dependency providers. It includes the Maker, the default mongo.Client and exported configs.

Depends On:
	log.Logger
	contract.ConfigAccessor
	MongoConfigInterceptor `optional:"true"`
	opentracing.Tracer     `optional:"true"`
Provides:
	Factory
	Maker
	*mongo.Client

Types

type Factory

type Factory = di.Factory[*mongo.Client]

Factory is a *di.Factory that creates *mongo.Client using a specific configuration entry.

type Maker

type Maker interface {
	Make(name string) (*mongo.Client, error)
}

Maker models Factory

type MongoConfigInterceptor added in v0.2.0

type MongoConfigInterceptor func(name string, clientOptions *options.ClientOptions)

MongoConfigInterceptor is an injection type hint that allows user to make last minute modification to mongo configuration. This is useful when some configuration cannot be easily expressed factoryIn a text form. For example, the options.ContextDialer.

type ProvidersOptionFunc added in v0.9.0

type ProvidersOptionFunc func(options *providersOption)

ProvidersOptionFunc is the type of functional providersOption for Providers. Use this type to change how Providers work.

func WithConfigInterceptor added in v0.9.0

func WithConfigInterceptor(interceptor MongoConfigInterceptor) ProvidersOptionFunc

WithConfigInterceptor instructs the Providers to accept the MongoConfigInterceptor so that users can change config during runtime. This can be useful when some dynamic computations on configs are required.

func WithReload added in v0.10.0

func WithReload(shouldReload bool) ProvidersOptionFunc

WithReload toggles whether the factory should reload cached instances upon OnReload event.

Jump to

Keyboard shortcuts

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