mongoutils

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 10 Imported by: 0

README

Mongo Utils

A set of utils to deal with the mongo client.

Features include:

  • handling migrations
  • a transaction function wrapper that compiles for with and without replica set

Documentation

Overview

Package mongoutils helps with setting up the mongo db

Index

Constants

View Source
const ErrDuplicate = 11000

ErrDuplicate is the mongo code returned when there is a duplicate key

View Source
const ErrInvalidDocument = 121

ErrInvalidDocument is returned when the document is invalid

View Source
const MigrationCollection = "migrations"

MigrationCollection is the name of the collection used for migrations

Variables

This section is empty.

Functions

func IsEmptyBSON

func IsEmptyBSON(t interface{}) bool

IsEmptyBSON returns true if marshaling to bson would return an empty value

func IsWriteError

func IsWriteError(err error, code int) bool

IsWriteError checks for an error code within the list of write errors

func LastMigrationID

func LastMigrationID(ctx context.Context, coll *mongo.Collection, migrations ...Migration) (string, error)

LastMigrationID returns the ID of the last executed migration

func New

func New(ctx context.Context, uri, name string, migrations ...Migration) (client *mongo.Client, db *mongo.Database, err error)

New connects a mongo client using provided values

func NewFromEnv

func NewFromEnv(ctx context.Context, migrations ...Migration) (*mongo.Client, *mongo.Database, error)

NewFromEnv connects a mongo client using the env var MONGO_URI and MONGO_DATABASE

func NewWithOptions added in v0.2.0

func NewWithOptions(
	ctx context.Context,
	uri,
	name string,
	opts func(*options.ClientOptions) *options.ClientOptions,
	migrations ...Migration) (client *mongo.Client, db *mongo.Database, err error)

NewWithOptions allows modifying the toptions

func RunMigrations

func RunMigrations(ctx context.Context, db *mongo.Database, migrations ...Migration) error

RunMigrations execute required migrations for the db

func Transaction

func Transaction(
	ctx context.Context,
	client *mongo.Client,
	functions ...TransactionFunction) (interface{}, error)

Transaction executes a transaction on a mongo replica set

Types

type Migration

type Migration interface {
	ID() string
	Migrate(context.Context, *mongo.Database) error
}

Migration reprensents a database migration

func MigrationsToRun

func MigrationsToRun(lastID string, migrations ...Migration) []Migration

MigrationsToRun finds which migration should be ran Current implementation is to find the id of the last migration in the array, and execute all subsequent ones. This is pretty naive...

type MigrationData

type MigrationData struct {
	MigrationID string
	Date        time.Time
}

MigrationData stores informations about executed migrations

type TransactionFunction

type TransactionFunction = func(ctx context.Context, data interface{}) (interface{}, error)

TransactionFunction is a function that is part of a transaction

Directories

Path Synopsis
Package tests provides utils function for testing
Package tests provides utils function for testing

Jump to

Keyboard shortcuts

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