dao

package module
v0.0.0-...-b000cc0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2017 License: Apache-2.0 Imports: 8 Imported by: 0

README

dao

Data Access Object abstraction for easier MongoDb access

Documentation

Index

Constants

View Source
const (
	// Parameter keys
	KEY_DB_SERVER_ID = "id"
	KEY_DB_SERVER    = "server"
	//KEY_DB_PORT       = "port"
	KEY_DB_OPTIONS = "options"
	KEY_DB_MODE    = "mode"
	KEY_DB_LOG     = "log"
)

Variables

This section is empty.

Functions

func GetCollections

func GetCollections(servername, dbname string) (collnames []string, err error)

Lists all collections present.

func IsCollectionExists

func IsCollectionExists(servername, dbname, collname string) (exists bool, err error)

Checks if given collection exists.

func ServerConnect

func ServerConnect(params map[string]string) (err error)

Creates DB server connection. Provide map with the following values: "id": internal server ID, use "_" for default single server "server": ie "localhost" (not implemented) "port": optional port number "options": ie "connect=direct" "mode": ie "monotonic" "log": empty for no log, any string value will activate logging

func ServersDisconnect

func ServersDisconnect()

Gracefully closes servers connections.

Types

type CollIndexes

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

func BuildIndexes

func BuildIndexes(midxs ...mgo.Index) (idxs *CollIndexes)

Builds struct contaning an array of Mongo indexes. Indexes set to un-initialized.

func (*CollIndexes) Indexes

func (ci *CollIndexes) Indexes() []mgo.Index

type DAO

type DAO struct {
	Coll *mgo.Collection
	// contains filtered or unexported fields
}

func (*DAO) Close

func (dao *DAO) Close()

Closes session.

func (*DAO) CountAll

func (dao *DAO) CountAll() (n int, err error)

Counts all collections documents.

func (*DAO) DebugIndexes

func (dao *DAO) DebugIndexes()

Debug method that prints active indexes for given collection.

func (*DAO) Delete

func (dao *DAO) Delete(id bson.ObjectId) (err error)

Deletes single object from collection.

func (*DAO) DeleteAll

func (dao *DAO) DeleteAll(are, you, sure bool) (count int, err error)

Deletes all objects from collection.

func (*DAO) DeleteBy

func (dao *DAO) DeleteBy(kvals ...interface{}) (count int, err error)

Deletes object from collection by given criteria. Kvals is an array of key-value pairs like so: "name", "Joe", "age", 99, ...

func (*DAO) DeleteMany

func (dao *DAO) DeleteMany(ids []bson.ObjectId) (count int, err error)

Deletes many objects by IDs.

func (*DAO) DropCollection

func (dao *DAO) DropCollection(are, you, sure bool) (err error)

Drops collection. No turning back!

func (*DAO) EnsureIndexes

func (dao *DAO) EnsureIndexes()

Ensures multiple indexes. Error is suppressed yet reported.

func (*DAO) FindAs

func (dao *DAO) FindAs(obj interface{}, equals map[string]interface{}, fields ...string) (err error)

Finds an object from collection by given equals 'and' criteria. Obj must be a pointer to a struct.

func (*DAO) FindManyAs

func (dao *DAO) FindManyAs(objs interface{}, equals map[string]interface{}, fields ...string) (err error)

Finds many objects matching equals 'and' criteria. Objs must be a pointer to an empty array of structs.

func (*DAO) FindManyByIntervalAs

func (dao *DAO) FindManyByIntervalAs(objs interface{}, dateKey string, ps, pe time.Time, fields ...string) (err error)

Finds many objects matching dateKey to the specified time period. Note that interval is [ps, pe) meaning pe is EXCLUDED. Objs must be a pointer to an empty array of structs.

func (*DAO) FindManyByPeriodAs

func (dao *DAO) FindManyByPeriodAs(objs interface{}, psKey, peKey string, ps, pe time.Time, fields ...string) (err error)

Finds many objects matching 'ps' and 'pe' field keys to be inside the specified time period. Objs must be a pointer to an empty array of structs.

func (*DAO) GetAllAs

func (dao *DAO) GetAllAs(objs interface{}, fields ...string) (err error)

Gets all documents. Fields is an array of fields to be fetched. Objs must be a pointer to an empty array of structs.

func (*DAO) GetAllAsMap

func (dao *DAO) GetAllAsMap(fields ...string) (res []map[string]interface{}, err error)

Gets all documents. Fields is an array of fields to be fetched.

func (*DAO) GetAs

func (dao *DAO) GetAs(obj interface{}, id bson.ObjectId, fields ...string) (err error)

Gets document into provided object. Fields is an array of fields to be fetched.

func (*DAO) GetAsMap

func (dao *DAO) GetAsMap(id bson.ObjectId, fields ...string) (obj map[string]interface{}, err error)

Gets document as a map. Fields is an array of fields to be fetched.

func (*DAO) GetManyAs

func (dao *DAO) GetManyAs(objs interface{}, ids []bson.ObjectId, fields ...string) (err error)

Gets many documents matched by IDs into provided array. Fields is an array of fields to be fetched. Objs must be a pointer to an empty array of structs.

func (*DAO) Open

func (dao *DAO) Open(servername, dbname, collname string, idxs *CollIndexes)

Opens new session with collection. Don't forget to close.

func (*DAO) Quirk_GetOneAs

func (dao *DAO) Quirk_GetOneAs(obj interface{}, fields ...string) (err error)

Gets random one.

func (*DAO) Save

func (dao *DAO) Save(forceID bool, objs ...interface{}) (err error)

Saves objecst(s). Force ID means generate ID before passing to Mongo. Useful if you need this ID right away. Method checks if ID already exists and doesn't overwrite it in this case. IMPORTANT: Force ID mode only supports pointers to structs. ObjectId must be a pointer too. XXX Still not 100% how objs... work. Better to save one obj at a time.

func (*DAO) SetStrongMode

func (dao *DAO) SetStrongMode()

Changes consistency to strong.

func (*DAO) Update

func (dao *DAO) Update(id bson.ObjectId, params map[string]interface{}) (err error)

Updates (set/unset) by map. Nil means don't touch.

For string values: Empty string means unset. Not empty string means set.

Objects passed as they are.

func (*DAO) Update_ArrayAdd

func (dao *DAO) Update_ArrayAdd(id bson.ObjectId, addTo string, addObj interface{}) (err error)

Adds addObj element to array addTo only if it is not already there.

func (*DAO) Update_ArrayAddMany

func (dao *DAO) Update_ArrayAddMany(id bson.ObjectId, addTo string, addObjs []interface{}) (err error)

Adds addObjs elements to addTo array only if they are not already there.

func (*DAO) Update_ArrayPull

func (dao *DAO) Update_ArrayPull(id bson.ObjectId, pullFrom string, pullObj interface{}) (err error)

Removes pullFrom array element that matches pullObj.

func (*DAO) Update_ArrayPullMany

func (dao *DAO) Update_ArrayPullMany(id bson.ObjectId, pullFrom string, pullObjs []interface{}) (err error)

Removes pullFrom array elements that matches pullObjs.

func (*DAO) Update_ArrayPush

func (dao *DAO) Update_ArrayPush(id bson.ObjectId, pushTo string, pushObj interface{}) (err error)

Adds pushObj element to array pushTo.

func (*DAO) Update_ArrayPushMany

func (dao *DAO) Update_ArrayPushMany(id bson.ObjectId, pushTo string, pushObjs []interface{}) (err error)

Adds pushObjs elements to array pushTo.

func (*DAO) Update_ArraysPullAddMany

func (dao *DAO) Update_ArraysPullAddMany(id bson.ObjectId, pullFrom string, pullObjs []interface{}, addTo string, addObjs []interface{}) (err error)

Removes pullObjs elements from pullFrom array. Adds addObjs elements to addTo array only if they are not already there.

func (*DAO) Update_ArraysPullPush

func (dao *DAO) Update_ArraysPullPush(id bson.ObjectId, pullFrom string, pullObj interface{}, pushTo string, pushObj interface{}) (err error)

Removes pullObj element from pullFrom array. Adds pushObj element to pushTo array.

func (*DAO) Update_ArraysPullPushMany

func (dao *DAO) Update_ArraysPullPushMany(id bson.ObjectId, pullFrom string, pullObjs []interface{}, pushTo string, pushObjs []interface{}) (err error)

Removes pullObjs elements from pullFrom array. Adds pushObjs elements to pushTo array.

func (*DAO) Update_Set

func (dao *DAO) Update_Set(id bson.ObjectId, key string, obj interface{}) (err error)

Sets single field to given value. Provide empty string to unset the field regardless of field's type.

func (*DAO) Upsert

func (dao *DAO) Upsert(selector, update interface{}) (err error)

Upserts.

type M

type M map[string]interface{}

func (M) Select

func (m M) Select(fs ...string) (q M)

func (M) Unselect

func (m M) Unselect(fs ...string) (q M)

type MS

type MS []M

type MongoLogger

type MongoLogger struct{}

Logger for Mgo

func (*MongoLogger) Output

func (l *MongoLogger) Output(calldepth int, s string) error

Mgo MongoLogger interface implementation.

Jump to

Keyboard shortcuts

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