mongodb

package
v1.14.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2025 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Package mongodb provides tools for easy and quick access to mongoDB via Connector.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDsn

func BuildDsn(config Config) string

Types

type CommonConnector

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

CommonConnector is base connector to work with database.

func New

func New(
	ctx context.Context,
	dsn string,
	rp *readpref.ReadPref,
	opts ...Option,
) (*CommonConnector, error)

New is constructor of CommonConnector. Gets database Config and logging.Logger to create an instance.

func (*CommonConnector) Close

func (c *CommonConnector) Close(ctx context.Context) error

Close closes pool of connections.

func (*CommonConnector) Collection

func (c *CommonConnector) Collection(
	database *mongo.Database,
	name string,
	opts ...*mongoOptions.CollectionOptions,
) (*mongo.Collection, error)

Collection returns mongo.Collection for provided database.

func (*CommonConnector) Database

func (c *CommonConnector) Database(name string) (*mongo.Database, error)

Database returns mongo.Database.

func (*CommonConnector) ListDatabaseNames

func (c *CommonConnector) ListDatabaseNames(
	ctx context.Context,
	filter bson.D,
	opts ...*mongoOptions.ListDatabasesOptions,
) ([]string, error)

ListDatabaseNames returns all mongo.Database names within established connection with mongoDB.

func (*CommonConnector) ListDatabases

func (c *CommonConnector) ListDatabases(
	ctx context.Context,
	filter bson.D,
	opts ...*mongoOptions.ListDatabasesOptions,
) (*mongo.ListDatabasesResult, error)

ListDatabases returns all mongo.Database within established connection with mongoDB.

func (*CommonConnector) Ping

Ping checks if mongo.Client is connected.

type Config

type Config struct {
	Host string
	Port int
}

Config is a database config, on base of which new Connector is created.

type Connector

type Connector interface {
	Database(name string) (*mongo.Database, error)
	Collection(
		database *mongo.Database,
		name string,
		opts ...*mongoOptions.CollectionOptions,
	) (*mongo.Collection, error)
	Ping(ctx context.Context, rp *readpref.ReadPref) error
	ListDatabases(
		ctx context.Context,
		filter bson.D,
		opts ...*mongoOptions.ListDatabasesOptions,
	) (*mongo.ListDatabasesResult, error)
	ListDatabaseNames(
		ctx context.Context,
		filter bson.D,
		opts ...*mongoOptions.ListDatabasesOptions,
	) ([]string, error)
	Close(ctx context.Context) error
}

Connector interface is created for usage in external application according to "dependency inversion principle" of SOLID due to working via abstractions.

type NilClientError

type NilClientError struct {
	Message string
	BaseErr error
}

NilClientError is an error, representing no connection established with mongoDB.

func (NilClientError) Error

func (e NilClientError) Error() string

func (NilClientError) Unwrap

func (e NilClientError) Unwrap() error

type NilDatabaseError

type NilDatabaseError struct {
	Message string
	BaseErr error
}

NilDatabaseError is an error, representing no mongo.Database provided.

func (NilDatabaseError) Error

func (e NilDatabaseError) Error() string

func (NilDatabaseError) Unwrap

func (e NilDatabaseError) Unwrap() error

type Option

type Option func(options *options) error

Option represents golang functional option pattern func for mongo.Client configuration.

func WithAuthSource

func WithAuthSource(authSource string) Option

WithAuthSource sets AuthSource for mongo.Client connection.

func WithMaxConnectionIdleTime

func WithMaxConnectionIdleTime(timeout time.Duration) Option

WithMaxConnectionIdleTime sets maximum amount of time for connection idle lifetime.

func WithMaxConnectionTimeout

func WithMaxConnectionTimeout(timeout time.Duration) Option

WithMaxConnectionTimeout sets maximum amount of time for connection lifetime.

func WithMaxConnections

func WithMaxConnections(num uint64) Option

WithMaxConnections sets maximum opened connections.

func WithMaxPoolSize

func WithMaxPoolSize(num uint64) Option

WithMaxPoolSize specifies that maximum number of connections allowed in the driver's connection pool to each server.

func WithMinPoolSize

func WithMinPoolSize(num uint64) Option

WithMinPoolSize specifies that minimum number of connections allowed in the driver's connection pool to each server.

func WithPassword

func WithPassword(password string) Option

WithPassword sets password for mongo.Client connection.

func WithUsername

func WithUsername(username string) Option

WithUsername sets username for mongo.Client connection.

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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