mongodb

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2024 License: MIT Imports: 5 Imported by: 0

README

mongodb

The mongodb module defines supercharged MongoDB Connector that adds some usefull functions to the native go.mongodb.org/mongo-driver/mongo client.

This project has been developped by the Aloe team and is now open source.

tests Go Reference

Overview

The mongodb connector supercharge the native go.mongodb.org/mongo-driver/mongo client.

The mongodb module provides:

  • A structure to hold configuration
  • A factory to create the connector
  • Addionnal functions
  • Infinite compatibility because it embed a native go.mongodb.org/mongo-driver/mongo client

Concepts

The module aims to ease the configuration and some operations to interact with a MongoDB server.

Usage

Configuration

The mongodb.Conf use YAML and Mapstructure tags, it's easy to load MongoDB config with configuration file in your project

type Conf struct {
	DB         string `mapstructure:"db" yaml:"db"`                         // Name of the database.
	Host       string `mapstructure:"host" yaml:"host"`                     // URL to reach the mongoDB server.
	Port       int    `mapstructure:"port,omitempty" yaml:"port,omitempty"` // Optionnal port, if set to 0 it won't be processed.
	Username   string `mapstructure:"username" yaml:"username"`             // Credential to authenticate to the db.
	Password   string `mapstructure:"password" yaml:"password"`             // Credential to authenticate to the db.
	AuthSource string `mapstructure:"auth_source" yaml:"auth_source"`       // Database to check authentication
	Timeout    int    `mapstructure:"timeout" yaml:"timeout"`               // Connection timeout in seconds
}
Create new connector

To create a new MongoDB Connector use this function with as configuration the structure mongodb.FactoryConnector(c mongodb.Conf) (*mongodb.Connector, error) and try connection with mongodb.Connector.TryConnection() err

var config := mongodb.Conf{
	DB:       "my_database",
	Host:     "localhost:27006",
	Username: "user",
	Password: "pass",
	AuthSource: "admin"
	Timeout:  10,
}

md, err = mongodb.FactoryConnector(config)
if err != nil {
	return fmt.Errorf("fail to init MongoDB connector: %w", err)
}

err = md.TryConnection()
if err != nil {
	return fmt.Errorf("fail to ping MongoDB: %w", err)
}

Test

To run test use:

  • make test

All environment variables present in the test/.env.example must be set in your test environment.

Contributing

This section will be added soon.

License

Mongodb module is released under the MIT license. See LICENSE.txt.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf

type Conf struct {
	DB         string `mapstructure:"db" yaml:"db"`                         // Name of the database.
	Host       string `mapstructure:"host" yaml:"host"`                     // URL to reach the mongoDB server.
	Port       int    `mapstructure:"port,omitempty" yaml:"port,omitempty"` // Optionnal port, if set to 0 it won't be processed.
	Username   string `mapstructure:"username" yaml:"username"`             // Credential to authenticate to the db.
	Password   string `mapstructure:"password" yaml:"password"`             // Credential to authenticate to the db.
	AuthSource string `mapstructure:"auth_source" yaml:"auth_source"`       // Database to check authentication
	Timeout    int    `mapstructure:"timeout" yaml:"timeout"`               // Connection timeout in seconds
}

Conf contains all information to connect to a MongoDB server.

type Connector

type Connector struct {
	*mongo.Client
	DB          string
	Collections map[string]*mongo.Collection
}

Connector is the connector used to communicate with MongoDB database server. It embeds a native mongo.Client so it can be used as is and is supercharged with additionnal methods.

func FactoryConnector

func FactoryConnector(c Conf) (*Connector, error)

FactoryConnector instanciates a new *Connector with the given params.

func (*Connector) Collection

func (con *Connector) Collection(collectionName string) *mongo.Collection

Collection returns the *mongo.Collection identified its name. If the specified collections doesn't exists on con.Collections map then add it.

func (*Connector) TryConnection

func (con *Connector) TryConnection() error

TryConnection tests ping, it end if the ping is a success or timeout.

Jump to

Keyboard shortcuts

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