mongo

package
v6.0.2 Latest Latest
Warning

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

Go to latest
Published: May 29, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package mongo extends the functionality of mongo by adding tracing, logging, and implementing Shutdown and HealthCheck hooks.

Index

Constants

View Source
const ModuleName = "MongoClient"

Variables

This section is empty.

Functions

func GetDefaultConfig

func GetDefaultConfig(moduleName string, t Tracer, logger log.Log) *options.ClientOptions

GetDefaultConfig returns the default MongoDB client options including connection settings, application name, pool size, compression, read preference, write concern, logging, and monitoring.

func NewCustomBsonRegistry

func NewCustomBsonRegistry() *bsoncodec.Registry

NewCustomBsonRegistry returns the custom BSON registry configured to handle decimal.Decimal types.

Types

type BaseMongoDocument

type BaseMongoDocument struct {
	CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` // Time when the document was created.
	UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` // Time when the document was last updated.
	CreatedBy *string    `json:"createdBy,omitempty" bson:"createdBy,omitempty"` // Identifier of the user who created the document.
	UpdatedBy *string    `json:"updatedBy,omitempty" bson:"updatedBy,omitempty"` // Identifier of the user who last updated the document.
}

BaseMongoDocument defines a basic structure for a MongoDB document.

func (*BaseMongoDocument) SetCreateParam

func (b *BaseMongoDocument) SetCreateParam(actionBy string)

SetCreateParam sets the creation parameters for the document.

func (*BaseMongoDocument) SetUpdateParam

func (b *BaseMongoDocument) SetUpdateParam(actionBy string)

SetUpdateParam sets the update parameters for the document.

type BaseMongoModel

type BaseMongoModel struct {
	ID                 *primitive.ObjectID `json:"-" bson:"_id,omitempty"` // Unique identifier of the MongoDB document.
	*BaseMongoDocument `bson:",inline"`    // Embedded BaseMongoDocument.
}

BaseMongoModel extends BaseMongoDocument with an ObjectId.

func (*BaseMongoModel) SetCreateParam

func (b *BaseMongoModel) SetCreateParam(actionBy string)

SetCreateParam sets the creation parameters for the model.

func (*BaseMongoModel) SetUpdateParam

func (b *BaseMongoModel) SetUpdateParam(actionBy string)

SetUpdateParam sets the update parameters for the model.

type Collection

type Collection struct {
	*mongo.Collection
	// contains filtered or unexported fields
}

Collection extends mongo.Collection by adding support for encoding and decoding decimal types.

type Database

type Database struct {
	*mongo.Database
	// contains filtered or unexported fields
}

Database extends mongo.Database by incorporating enhanced logging functionality.

func (*Database) Collection

func (d *Database) Collection(name string, opts ...*options.CollectionOptions) *Collection

Collection returns a Collection with a custom BSON registry and enhanced logging. It prepends the custom registry option to any other collection options provided.

type Mongo

type Mongo struct {
	*mongo.Client
	// contains filtered or unexported fields
}

Mongo enhances mongo.Client with default option settings and logging capabilities.

func New

func New(logger log.Log, opts ...*options.ClientOptions) (*Mongo, error)

New creates a new Mongo client with the provided client options and initializes the connection.

func NewWithDefaultOptions

func NewWithDefaultOptions(logger log.Log, t Tracer, opts ...*options.ClientOptions) (*Mongo, error)

NewWithDefaultOptions creates a new Mongo client with the default configuration options, incorporating additional options provided by the caller.

func (*Mongo) Database

func (m *Mongo) Database(name string, opts ...*options.DatabaseOptions) *Database

Database returns a wrapped mongo.Database with enhanced logging.

func (*Mongo) GetClient

func (m *Mongo) GetClient() *mongo.Client

GetClient returns the underlying mongo.Client.

func (*Mongo) GetLogger

func (m *Mongo) GetLogger() log.Log

GetLogger returns the logger associated with the Mongo client.

func (*Mongo) HealthCheck

func (m *Mongo) HealthCheck(ctx context.Context) error

HealthCheck performs a health check by pinging the MongoDB server.

func (*Mongo) Name

func (m *Mongo) Name(ctx context.Context) string

Name returns the module name.

func (*Mongo) SetModuleName

func (m *Mongo) SetModuleName(name string)

SetModuleName sets the module name and updates the logger.

func (*Mongo) Shutdown

func (m *Mongo) Shutdown(ctx context.Context) error

Shutdown gracefully closes the Mongo client connection.

type MongoLogger

type MongoLogger struct {
	// contains filtered or unexported fields
}

MongoLogger is used for logging MongoDB events.

func (*MongoLogger) Error

func (m *MongoLogger) Error(err error, message string, keysAndValues ...interface{})

Error logs error messages.

func (*MongoLogger) Info

func (m *MongoLogger) Info(level int, message string, keysAndValues ...interface{})

Info logs informational messages.

func (*MongoLogger) PoolEvent

func (m *MongoLogger) PoolEvent(e *event.PoolEvent)

PoolEvent logs MongoDB pool events.

type Tracer

type Tracer interface {
	MongoDB() *event.CommandMonitor
}

Tracer interface defines a method for obtaining a MongoDB command monitor.

Directories

Path Synopsis
Package csfle implements boilerplate code for creating a MongoDB client with Client-Side Field Level Encryption (CSFLE) capabilities.
Package csfle implements boilerplate code for creating a MongoDB client with Client-Side Field Level Encryption (CSFLE) capabilities.

Jump to

Keyboard shortcuts

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