Files
remark42/backend/vendor/github.com/go-pkgz/mongo
UmputunandGitHub dbd1d4069f feature/ext-mongo (#177)
* externalize mongo wrapper

* remove mongo env from drone

* export mongo test for coverage report
2018-07-22 22:46:25 -04:00
..
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00
2018-07-22 22:46:25 -04:00

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