Files
remark42/backend/app/store/engine/mongo/connection.go
T
UmputunandGitHub de013c7d5a feature/mongo (#165)
* WIP: start mongo engine

* WIP: mongo accessor and admin

* integrate mongo store to main

* disable mongo tests by default, only in CI

* connection with constructor

* add mongo buffered writer

* buffered mongo writer

* fix nil responses on an empty list from mongo

* missing mongo index for scores

* cancelable store

* add gridfs implementation of avatar store

* fix race on mongo session copy

* gridfs avatars without tmp files

* move avatar store

* minor comments and refactoring for avatar store

* merged from current master

* simplify gridfs reader

* lint: fix minor warns

* test mongo against env defined url

* pass MONGO_REMARK_TEST to docker and travis

* set dockerfile env for mongo test url

* increase connect timeout in mongo tests

* pass MONGO_REMARK_TEST to drone build

* add MONGO_REMARK_TEST to branch stage of drone

* mass mongo test url via build_args_from_env

* populate mongo IP to docker build hosts

* test env

* pass mongo ip via .mongo

* remove .mongo temp from git

* add .mongo -> env to linter step

* allow more time to autoflush writer test

* default mongo tests to "mongo" if not in env

* merge fresh master into

* add test for mongo cleanup

* msg for a failed test

* lazy fix for failed test

* add an ability to skip all mongo tests

* add backend dev instructions

* remove unused code from mongo server

* move mongo testing to connection_test

* restore testing.go

* lint: minor warns for testing code
2018-07-13 12:27:11 -05:00

68 lines
2.2 KiB
Go

package mongo
import (
"fmt"
"github.com/globalsign/mgo"
)
// sessionFn is a function for all With calls, terminator not supported
type sessionFn func(coll *mgo.Collection) error
// Connection allows to run request in separate session, closing automatically
type Connection struct {
server *Server
db, collection string
}
// NewConnection makes a connection for server
func NewConnection(server *Server, db string, collection string) *Connection {
return &Connection{server: server, db: db, collection: collection}
}
// WithCollection passes fun with mgo.Collection from session copy, closes it after done,
// uses Connection.DB and Connection.Collection
func (c *Connection) WithCollection(fun sessionFn) (err error) {
return c.WithCustomCollection(c.collection, fun)
}
// 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 (c *Connection) WithCustomCollection(collection string, fun sessionFn) (err error) {
db := c.server.dial.Database
if c.db != "" {
db = c.db
}
return c.WithCustomDbCollection(db, collection, fun)
}
// WithCustomDbCollection passed fun with mgo.Collection from session copy, closes it after done
// uses passed db and collection directly.
func (c *Connection) WithCustomDbCollection(db string, collection string, fun sessionFn) (err error) {
session := c.server.SessionCopy()
defer session.Close()
return fun(session.DB(db).C(collection))
}
// WithDB passes fun with mgo.Database from session copy, closes it after done
// uses Connection.DB or (if not defined) dial.Database
func (c *Connection) WithDB(fun func(dbase *mgo.Database) error) (err error) {
db := c.server.dial.Database
if c.db != "" {
db = c.db
}
return c.WithCustomDB(db, fun)
}
// WithCustomDB passes fun with mgo.Database from session copy, closes it after done
// uses passed db directly
func (c *Connection) WithCustomDB(db string, fun func(dbase *mgo.Database) error) (err error) {
session := c.server.SessionCopy()
defer session.Close()
return fun(session.DB(db))
}
func (c *Connection) String() string {
return fmt.Sprintf("mongo:%s, db:%s, collection:%s", c.server, c.db, c.collection)
}