database

package
v0.0.0-...-2d3cc3e Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2021 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bongo

type Bongo interface {
	Save(collectionName string, doc bongo.Document) error
	FindByID(collectionName string, id bson.ObjectId, doc interface{}) error
	Find(collectionName string, query interface{}) *bongo.ResultSet
	Delete(collectionName string, query bson.M) (*mgo.ChangeInfo, error)
}

Bongo represents the interface of Bongo's ODM so we can decouple our code from Bongo itself

type Connection

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

Connection holds bongo conn

func Connect

func Connect() *Connection

Connect will try to connect to mongodb using Bongo ODM

Example
conn := Connect()
fmt.Printf("%T", conn)
Output:

*database.Connection

func (*Connection) Delete

func (c *Connection) Delete(collectionName string, query bson.M) (*mgo.ChangeInfo, error)

Delete will remove documents from any given collection

Example
conn := setUp()
collectionName := "SomeCollection"
bongoDoc := &baseBongoDoc{
	SomeField: "SomeValue",
}
conn.Save(collectionName, bongoDoc)
changeInfo, _ := conn.Delete(collectionName, bson.M{"_id": bongoDoc.GetId()})
fmt.Println(changeInfo.Removed)
Output:

1

func (*Connection) Find

func (c *Connection) Find(collectionName string, query interface{}) *bongo.ResultSet

Find will return a *bongo.ResultSet containing all available routes for a single driver by its ObjectID

Example
conn := setUp()
collectionName := "SomeCollection"
bongoDoc := &baseBongoDoc{
	SomeField: "SomeValue",
}
conn.Save(collectionName, bongoDoc)
query := bson.M{"SomeField": "SomeValue"}
results := conn.Find(collectionName, query)
fmt.Printf("%T", results)
tearDown(collectionName)
Output:

*bongo.ResultSet

func (*Connection) FindByID

func (c *Connection) FindByID(collectionName string, id bson.ObjectId, doc interface{}) error

FindByID will retrieve a document by a given id

Example
conn := setUp()
collectionName := "SomeCollection"
bongoDoc := &baseBongoDoc{
	SomeField: "SomeValue",
}
conn.Save(collectionName, bongoDoc)
retrievedDoc := &baseBongoDoc{}
conn.FindByID(collectionName, bongoDoc.GetId(), retrievedDoc)
fmt.Println(retrievedDoc.SomeField)
tearDown(collectionName)
Output:

SomeValue

func (*Connection) Save

func (c *Connection) Save(collectionName string, doc bongo.Document) error

Save will persist a given bongo document in mongodb

Example
conn := setUp()
collectionName := "SomeCollection"
bongoDoc := &baseBongoDoc{
	SomeField: "SomeValue",
}
conn.Save(collectionName, bongoDoc)
fmt.Println(bongoDoc.SomeField)
tearDown(collectionName)
Output:

SomeValue

Jump to

Keyboard shortcuts

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