Skip to content

Repository files navigation

CircleCI codecov

go-rscsrv-mgo

The go-rscsrv-mgo is the MongoDB resource service that wraps globalsign/mgo library.

Dependencies

It depends on the lab259/go-rscsrv (and its dependencies, of course) itself and the globalsign/mgo library.

Installation

You may fetch the library directly using go get.

go get github.com/lab259/go-rscsrv-mgo

Usage

Applying configuration and starting service

// Create MgoService instance
var mgoService MgoService

// Applying configuration
err := mgoService.ApplyConfiguration(MgoServiceConfiguration{
    Addresses: []string{"localhost"},
	Username:  "username",
	Password:  "password",
	Database:  "my-db",
	PoolSize:  1,
    Timeout:   60,
})

if err != nil {
    panic(err)
}

// Starting service
err := mgoService.Start()

if err != nil {
    panic(err)
}

// Create a custom object
var object MyModel

// Executing something using a *mgo.Session
err := mgoService.RunWithSession(func(session *mgo.Session) error {
    // Retrieving an object from the MongoDB
    return session.DB("my-db").C("my-collection").FindId("my-object-id").One(&object)
})

if err != nil {
    panic(err)
}

id := object.Id // "my-object-id"

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages