golib: github.com/levenlabs/golib/mgoutil Index | Files

package mgoutil

import "github.com/levenlabs/golib/mgoutil"

Package mgoutil provides various methods for working with the mgo.v2 package, implementing various oft-used behaviors

Index

Package Files

mgoutil.go

func EnsureSession Uses

func EnsureSession(addr string) *mgo.Session

EnsureSession connections to the given addr with a 5 second timeout, sets the session to Safe, sets the mode to PrimaryPreferred, and returns the resulting mgo.Session. If it fails to connect it fatals.

func MustEnsureIndexes Uses

func MustEnsureIndexes(db *mgo.Database, indexes ...Index)

MustEnsureIndexes takes the given indexes and ensure's they are all in place. It will make the calls with Background:true. If any indexes fail a message will be logged and the process will exit

DEPRECATED: Use the MustEnsureIndexes method on SessionHelper instead. This will be removed at some point in the future

NOTE WHEN SWITCHING TO NEW METHOD: the new method does *not* automatically set Background: true like this one does, so be aware of that difference when switching over

type Index Uses

type Index struct {
    Name   string // human readable name for the index, only used for the log entry
    Keys   []string
    Coll   string
    Unique bool
    Sparse bool
}

Index is used in MustEnsureIndexes to convey all the information needed to ensure a background index

DEPRECATED: MustEnsureIndexes is deprecated, and therefore this type is as well. This will be removed at some point in the future

type SessionHelper Uses

type SessionHelper struct {
    Session *mgo.Session
    DB      string
    Coll    string
}

CollectionHelper is a simple helper type which makes it easy to call methods on a Database or Collection safely. It will handle the Copy'ing and Close'ing of the given Session automatically

func (SessionHelper) MustEnsureIndexes Uses

func (s SessionHelper) MustEnsureIndexes(indexes ...mgo.Index)

MustEnsureIndexes takes the given indexes and ensure's they are all in place. If any indexes fail a message will be logged and the process will exit

func (SessionHelper) WithColl Uses

func (s SessionHelper) WithColl(fn func(*mgo.Collection))

WithColl handles calls the given function on an instance of the collection named by the field in the SessionHelper. It will handle Copy'ing and Close'ing the Session object automatically.

func (SessionHelper) WithDB Uses

func (s SessionHelper) WithDB(fn func(*mgo.Database))

WithDB handles calls the given function on an instance of the database named by the field in the SessionHelper. It will handle Copy'ing and Close'ing the Session object automatically.

Package mgoutil imports 3 packages (graph) and is imported by 3 packages. Updated 2016-07-15. Refresh now. Tools for package owners.