mongo

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2019 License: MIT Imports: 14 Imported by: 27

README

Mongo Build Status Go Report Card Coverage Status

Provides helpers on top of mgo

Install and update

go get -u github.com/go-pkgz/mongo

Usage

  • Server represents mongo instance and provides session accessor. Application usually creates one server object and uses it for anything needed with this particular mongo host or replica set.

  • Connection encapsulates session and provides auto-closable wrapper. Each requests runs inside one of With* function makes new mongo session and closes on completion.

  • BufferedWriter implements buffered writer to mongo. Write method caching internally till it reached buffer size. Flush methods can be called manually at any time.

    m, err := NewServerWithURL("mongodb://127.0.0.1:27017/test?debug=true", 3*time.Second)
    if err != nil {
        panic("can't make mongo server")
    } 
    
    type testRecord struct {
    	Key1 string
    	Kay2 int
    }
    
    err = c.WithCollection(func(coll *mgo.Collection) error { // create session
        // insert 100 records
        for i := 0; i < 100; i++ {
            r := testRecord{
                Key1: fmt.Sprintf("key-%02d", i%5),
                Key2: i,
            }
            if e := coll.Insert(r); e != nil {
                return e
            }
        }
        return nil
    })
    

Dependencies

Testing

testing.go helps to create test for real mongo (not mocks)

  • mongo.MakeTestConnection creates mongo.Connection for url defined in env MONGO_TEST. If not defined mongodb://mongo:27017 used. By default it will use random connection with prefix test_ in test DB.
  • mongo.RemoveTestCollection - drops collection used by MakeTestConnection
  • mongo.RemoveTestCollections - drops user-defined collections from test DB

Documentation

Overview

Package mongo wraps mgo to provide easier way to construct mongo server (with auth). Connection provides With* func wrappers to run query with session copy

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoveTestCollection

func RemoveTestCollection(t *testing.T, c *Connection)

RemoveTestCollection removes all records and drop collection from connection

func RemoveTestCollections

func RemoveTestCollections(t *testing.T, c *Connection, collections ...string)

RemoveTestCollections clears passed collections

Types

type BufferedWriter

type BufferedWriter interface {
	Write(rec interface{}) error
	Flush() error
	Close() error
}

BufferedWriter defines interface for writes and flush

type BufferedWriterMgo

type BufferedWriterMgo struct {
	// contains filtered or unexported fields
}

BufferedWriterMgo collects records in local buffer and flushes them as filled. Thread safe by default using both DB and collection from provided connection. Collection can be customized by WithCollection method. Optional flush duration to save on interval

func NewBufferedWriter

func NewBufferedWriter(size int, connection *Connection) *BufferedWriterMgo

NewBufferedWriter makes batch writer for given size and connection

func (*BufferedWriterMgo) Close

func (bw *BufferedWriterMgo) Close() (err error)

Close flushes all in-fly records and terminates background auto-flusher

func (*BufferedWriterMgo) Flush

func (bw *BufferedWriterMgo) Flush() error

Flush writes everything left in buffer to mongo

func (*BufferedWriterMgo) WithAutoFlush

func (bw *BufferedWriterMgo) WithAutoFlush(duration time.Duration) *BufferedWriterMgo

WithAutoFlush sets auto flush duration

func (*BufferedWriterMgo) WithCollection

func (bw *BufferedWriterMgo) WithCollection(collection string) *BufferedWriterMgo

WithCollection sets custom collection to use with writer

func (*BufferedWriterMgo) Write

func (bw *BufferedWriterMgo) Write(rec interface{}) error

Write to buffer and, as filled, to mongo. If flushDuration defined check for automatic flush

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

Connection allows to run request in separate session, closing automatically

func MakeTestConnection

func MakeTestConnection(t *testing.T) (*Connection, error)

MakeTestConnection connects to MONGO_TEST url or "mongo" host (in no env) and returns new connection. collection name randomized on each call

func NewConnection

func NewConnection(server *Server, db string, collection string) *Connection

NewConnection makes a connection for server

func (*Connection) String

func (c *Connection) String() string

func (*Connection) WithCollection

func (c *Connection) WithCollection(fun sessionFn) (err error)

WithCollection passes fun with mgo.Collection from session copy, closes it after done, uses Connection.DB and Connection.Collection

func (*Connection) WithCustomCollection

func (c *Connection) WithCustomCollection(collection string, fun sessionFn) (err error)

WithCustomCollection passes fun with mgo.Collection from session copy, closes it after done uses Connection.DB or (if not defined) dial.Database, and user-defined collection

func (*Connection) WithCustomDB

func (c *Connection) WithCustomDB(db string, fun func(dbase *mgo.Database) error) (err error)

WithCustomDB passes fun with mgo.Database from session copy, closes it after done uses passed db directly

func (*Connection) WithCustomDbCollection

func (c *Connection) WithCustomDbCollection(db string, collection string, fun sessionFn) (err error)

WithCustomDbCollection passed fun with mgo.Collection from session copy, closes it after done uses passed db and collection directly.

func (*Connection) WithDB

func (c *Connection) WithDB(fun func(dbase *mgo.Database) error) (err error)

WithDB passes fun with mgo.Database from session copy, closes it after done uses Connection.DB or (if not defined) dial.Database

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server represents mongo instance and provides session accessor

func NewServer

func NewServer(dial mgo.DialInfo, params ServerParams) (res *Server, err error)

NewServer doing auth if passwd != "" and can delay to make sure local mongo is up

func NewServerWithURL

func NewServerWithURL(url string, timeout time.Duration) (res *Server, err error)

NewServerWithURL makes mongo server from url like mongodb://remark42:password@127.0.0.1:27017/test?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin

func (*Server) SessionCopy

func (m *Server) SessionCopy() *mgo.Session

SessionCopy returns copy of main session. Client should close it

func (*Server) String

func (m *Server) String() string

type ServerParams

type ServerParams struct {
	ConsistencyMode mgo.Mode
	Delay           int  // initial delay to give mongo server some time to start, in case if mongo part of the same compose
	Debug           bool // turn on mgo debug mode
	SSL             bool // enforce SSL connection
}

ServerParams optional set of parameters

Jump to

Keyboard shortcuts

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