database

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2019 License: NCSA Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound   = errors.New("Error: NOT_FOUND")
	ErrConnection = errors.New("Error: CONNECTION_FAILED")
	ErrUnknown    = errors.New("Error: UNKNOWN")
)
View Source
var ErrTypeMismatch = errors.New("Error: TYPE_MISMATCH")

Functions

func AddEntryToStats

func AddEntryToStats(stats map[string]interface{}, entry map[string]interface{}, fields []string) error

Updates the stats with the given entry

func ExtractTopLevel added in v0.4.0

func ExtractTopLevel(fields []string) []string

Remove everything in each field after, and including, the first '.'

func GetDefaultStats

func GetDefaultStats() map[string]interface{}

Returns a maps of default stats

func RemoveTopLevel added in v0.4.0

func RemoveTopLevel(fields []string) []string

Remove everything in each field before, and including, the first '.'

func UpdateStatsField

func UpdateStatsField(stats map[string]interface{}, key string, value interface{}) error

Types

type ChangeResults

type ChangeResults struct {
	Updated int
	Deleted int
}

Used to store information about the changes made by a database operation

type Database

type Database interface {
	Connect(host string) error
	Close()
	FindOne(collection_name string, query interface{}, result interface{}) error
	FindAll(collection_name string, query interface{}, result interface{}) error
	RemoveOne(collection_name string, query interface{}) error
	RemoveAll(collection_name string, query interface{}) (*ChangeResults, error)
	Insert(collection_name string, item interface{}) error
	Upsert(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
	Update(collection_name string, selector interface{}, update interface{}) error
	UpdateAll(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)
	DropDatabase() error
	GetStats(collection_name string, fields []string) (map[string]interface{}, error)
}

Database interface exposing the methods necessary to querying, inserting, updating, upserting, and removing records

func InitDatabase

func InitDatabase(host string, db_name string) (Database, error)

Initialize a connection to the given database

This function wraps a database specific initializion function This makes it simple to change the database used without rewriting code in the microservices

type MongoDatabase

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

MongoDatabase struct which implements the Database interface for a mongo database

func InitMongoDatabase

func InitMongoDatabase(host string, db_name string) (*MongoDatabase, error)

Initialize connection to mongo database

func (*MongoDatabase) Close added in v0.3.0

func (db *MongoDatabase) Close()

Close the global session to the given mongo database

func (*MongoDatabase) Connect

func (db *MongoDatabase) Connect(host string) error

Open a session to the given mongo database

func (*MongoDatabase) DropDatabase

func (db *MongoDatabase) DropDatabase() error

Drops the entire database

func (*MongoDatabase) FindAll

func (db *MongoDatabase) FindAll(collection_name string, query interface{}, result interface{}) error

Find all elements matching the given query parameters

func (*MongoDatabase) FindOne

func (db *MongoDatabase) FindOne(collection_name string, query interface{}, result interface{}) error

Find one element matching the given query parameters

func (*MongoDatabase) GetSession

func (db *MongoDatabase) GetSession() *mgo.Session

Returns a copy of the global session for use by a connection

func (*MongoDatabase) GetStats

func (db *MongoDatabase) GetStats(collection_name string, fields []string) (map[string]interface{}, error)

Returns a map of statistics for a given collection

func (*MongoDatabase) Insert

func (db *MongoDatabase) Insert(collection_name string, item interface{}) error

Insert the given item into the collection

func (*MongoDatabase) RemoveAll

func (db *MongoDatabase) RemoveAll(collection_name string, query interface{}) (*ChangeResults, error)

Remove all elements matching the given query parameters

func (*MongoDatabase) RemoveOne

func (db *MongoDatabase) RemoveOne(collection_name string, query interface{}) error

Remove one element matching the given query parameters

func (*MongoDatabase) Update

func (db *MongoDatabase) Update(collection_name string, selector interface{}, update interface{}) error

Finds an item based on the given selector and updates it with the data in update

func (*MongoDatabase) UpdateAll

func (db *MongoDatabase) UpdateAll(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)

Finds all items based on the given selector and updates them with the data in update

func (*MongoDatabase) Upsert

func (db *MongoDatabase) Upsert(collection_name string, selector interface{}, update interface{}) (*ChangeResults, error)

Upsert the given item into the collection i.e., if the item exists, it is updated with the given values, else a new item with those values is created.

type QuerySelector

type QuerySelector map[string]interface{}

An alias of a string -> interface{} map used for database queries and selectors

Jump to

Keyboard shortcuts

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