Documentation
¶
Overview ¶
Package mongodb provides tools for easy and quick access to mongoDB via Connector.
Index ¶
- func BuildDsn(config Config) string
- type CommonConnector
- func (c *CommonConnector) Close(ctx context.Context) error
- func (c *CommonConnector) Collection(database *mongo.Database, name string, opts ...*mongoOptions.CollectionOptions) (*mongo.Collection, error)
- func (c *CommonConnector) Database(name string) (*mongo.Database, error)
- func (c *CommonConnector) ListDatabaseNames(ctx context.Context, filter bson.D, opts ...*mongoOptions.ListDatabasesOptions) ([]string, error)
- func (c *CommonConnector) ListDatabases(ctx context.Context, filter bson.D, opts ...*mongoOptions.ListDatabasesOptions) (*mongo.ListDatabasesResult, error)
- func (c *CommonConnector) Ping(ctx context.Context, rp *readpref.ReadPref) error
- type Config
- type Connector
- type NilClientError
- type NilDatabaseError
- type Option
- func WithAuthSource(authSource string) Option
- func WithMaxConnectionIdleTime(timeout time.Duration) Option
- func WithMaxConnectionTimeout(timeout time.Duration) Option
- func WithMaxConnections(num uint64) Option
- func WithMaxPoolSize(num uint64) Option
- func WithMinPoolSize(num uint64) Option
- func WithPassword(password string) Option
- func WithUsername(username string) Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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.
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 ¶
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 ¶
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 ¶
WithAuthSource sets AuthSource for mongo.Client connection.
func WithMaxConnectionIdleTime ¶
WithMaxConnectionIdleTime sets maximum amount of time for connection idle lifetime.
func WithMaxConnectionTimeout ¶
WithMaxConnectionTimeout sets maximum amount of time for connection lifetime.
func WithMaxConnections ¶
WithMaxConnections sets maximum opened connections.
func WithMaxPoolSize ¶
WithMaxPoolSize specifies that maximum number of connections allowed in the driver's connection pool to each server.
func WithMinPoolSize ¶
WithMinPoolSize specifies that minimum number of connections allowed in the driver's connection pool to each server.
func WithPassword ¶
WithPassword sets password for mongo.Client connection.
func WithUsername ¶
WithUsername sets username for mongo.Client connection.