Documentation ¶
Overview ¶
Package mongoutils helps with setting up the mongo db
Index ¶
- Constants
- func IsEmptyBSON(t interface{}) bool
- func IsWriteError(err error, code int) bool
- func LastMigrationID(ctx context.Context, coll *mongo.Collection, migrations ...Migration) (string, error)
- func New(ctx context.Context, uri, name string, migrations ...Migration) (client *mongo.Client, db *mongo.Database, err error)
- func NewFromEnv(ctx context.Context, migrations ...Migration) (*mongo.Client, *mongo.Database, error)
- func NewWithOptions(ctx context.Context, uri, name string, ...) (client *mongo.Client, db *mongo.Database, err error)
- func RunMigrations(ctx context.Context, db *mongo.Database, migrations ...Migration) error
- func Transaction(ctx context.Context, client *mongo.Client, functions ...TransactionFunction) (interface{}, error)
- type Migration
- type MigrationData
- type TransactionFunction
Constants ¶
const ErrDuplicate = 11000
ErrDuplicate is the mongo code returned when there is a duplicate key
const ErrInvalidDocument = 121
ErrInvalidDocument is returned when the document is invalid
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 ¶
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 ¶
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 ¶
Migration reprensents a database migration
func MigrationsToRun ¶
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 ¶
MigrationData stores informations about executed migrations
type TransactionFunction ¶
TransactionFunction is a function that is part of a transaction