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 ¶
- func RemoveTestCollection(t *testing.T, c *Connection)
- func RemoveTestCollections(t *testing.T, c *Connection, collections ...string)
- type BufferedWriter
- type BufferedWriterMgo
- func (bw *BufferedWriterMgo) Close() (err error)
- func (bw *BufferedWriterMgo) Flush() error
- func (bw *BufferedWriterMgo) WithAutoFlush(duration time.Duration) *BufferedWriterMgo
- func (bw *BufferedWriterMgo) WithCollection(collection string) *BufferedWriterMgo
- func (bw *BufferedWriterMgo) Write(rec interface{}) error
- type Connection
- func (c *Connection) String() string
- func (c *Connection) WithCollection(fun sessionFn) (err error)
- func (c *Connection) WithCustomCollection(collection string, fun sessionFn) (err error)
- func (c *Connection) WithCustomDB(db string, fun func(dbase *mgo.Database) error) (err error)
- func (c *Connection) WithCustomDbCollection(db string, collection string, fun sessionFn) (err error)
- func (c *Connection) WithDB(fun func(dbase *mgo.Database) error) (err error)
- type Server
- type ServerParams
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 ¶
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 ¶
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.
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 ¶
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 ¶
SessionCopy returns copy of main session. Client should close it