Documentation
¶
Index ¶
- Constants
- func SetDebug(debug bool)
- func SetLogger(logger logger)
- func SetSlowQueryTime(sec uint)
- type Collection
- func (c *Collection) Count(query interface{}) (n int, err error)
- func (c *Collection) Distinct(query, models interface{}, key string) error
- func (c *Collection) Drop() (err error)
- func (c *Collection) Find(query, model interface{}, sorts ...string) error
- func (c *Collection) FindAll(query, selector, models interface{}, skip, limit int, sorts ...string) error
- func (c *Collection) FindAllWithMarker(query, selector, models interface{}, marker Marker, limit int) (prev, next interface{}, err error)
- func (c *Collection) FindAllWithPagination(query, selector, models interface{}, skip, limit int, sorts ...string) (Paginater, error)
- func (c *Collection) FindByID(id string, model interface{}) error
- func (c *Collection) FindByObjectID(id bson.ObjectId, model interface{}) error
- func (c *Collection) Insert(models ...interface{}) error
- func (c *Collection) Invoke(fn func(*mgo.Collection) error) error
- func (c *Collection) Remove(selector interface{}) error
- func (c *Collection) RemoveAll(selector interface{}) error
- func (c *Collection) RemoveByID(id string) error
- func (c *Collection) RemoveByObjectID(id bson.ObjectId) error
- func (c *Collection) Update(selector, update interface{}) error
- func (c *Collection) UpdateAll(selector, update interface{}) error
- func (c *Collection) UpdateByObjectID(id bson.ObjectId, update interface{}) error
- func (c *Collection) UpdateSet(selector, update interface{}) error
- func (c *Collection) UpdateSetAll(selector, update interface{}) error
- func (c *Collection) UpdateSetByObjectID(id bson.ObjectId, update interface{}) error
- func (c *Collection) Upsert(selector, update interface{}) (info *mgo.ChangeInfo, err error)
- func (c *Collection) UpsertByObjectID(id bson.ObjectId, update interface{}) (info *mgo.ChangeInfo, err error)
- type Database
- func (d *Database) C(name string, indexes ...Index) *Collection
- func (d *Database) Clone() *Database
- func (d *Database) Close()
- func (d *Database) Copy() *Database
- func (d *Database) Drop() error
- func (d *Database) InitWithDialInfo(info *DialInfo) error
- func (d *Database) InitWithURL(url string) error
- func (d *Database) Session() *mgo.Session
- type DialInfo
- type Index
- type Marker
- type ModelError
- type PageItem
- type Paginater
Constants ¶
const ( // PageFirst is the first page. PageFirst = "first" // PageLast is the last page. PageLast = "last" // PageNext is the next page. PageNext = "next" // PagePrev is the previous page. PagePrev = "previous" )
Variables ¶
This section is empty.
Functions ¶
func SetLogger ¶
func SetLogger(logger logger)
SetLogger sets the logger so that mgobase can logger the important info.
func SetSlowQueryTime ¶
func SetSlowQueryTime(sec uint)
SetSlowQueryTime sets the max second that a query can be tolerated before it's considered as a slow query.
Types ¶
type Collection ¶
type Collection struct {
// contains filtered or unexported fields
}
Collection represents a mongo collection of db.
func (*Collection) Count ¶
func (c *Collection) Count(query interface{}) (n int, err error)
Count returns the total number of documents by query.
func (*Collection) Distinct ¶
func (c *Collection) Distinct(query, models interface{}, key string) error
Distinct unmarshals into result the list of distinct values for the given key.
func (*Collection) Find ¶
func (c *Collection) Find(query, model interface{}, sorts ...string) error
Find finds a single document by given query and sort conditions if exist.
func (*Collection) FindAll ¶
func (c *Collection) FindAll(query, selector, models interface{}, skip, limit int, sorts ...string) error
FindAll finds all documents match the query, skips the `skip` steps and returns the limited sorted results with projection fields.
`selector` is used for project fields if no nil.
`limit` will be concerned if it's greater than 0.
each elements of `sorts` should be nonempty string if the `sorts` are provided.
func (*Collection) FindAllWithMarker ¶
func (c *Collection) FindAllWithMarker(query, selector, models interface{}, marker Marker, limit int) (prev, next interface{}, err error)
FindAllWithMarker uses the query-based paging technology. The pagination will use the `marker` to decide how to retrieve the documents.
See `Marker` also.
func (*Collection) FindAllWithPagination ¶
func (c *Collection) FindAllWithPagination(query, selector, models interface{}, skip, limit int, sorts ...string) (Paginater, error)
FindAllWithPagination works just like FindAll, but it returns a paginater to indicate the informations about pagination.
func (*Collection) FindByID ¶
func (c *Collection) FindByID(id string, model interface{}) error
FindByID finds a single document by object id in string form.
func (*Collection) FindByObjectID ¶
func (c *Collection) FindByObjectID(id bson.ObjectId, model interface{}) error
FindByObjectID finds a single document by object id.
func (*Collection) Insert ¶
func (c *Collection) Insert(models ...interface{}) error
Insert inserts one or more documents.
func (*Collection) Invoke ¶
func (c *Collection) Invoke(fn func(*mgo.Collection) error) error
Invoke invokes a callback function with a session created from session factory.
func (*Collection) Remove ¶
func (c *Collection) Remove(selector interface{}) error
Remove removes a single document by query selector.
func (*Collection) RemoveAll ¶
func (c *Collection) RemoveAll(selector interface{}) error
RemoveAll removes all documents match the query selector.
func (*Collection) RemoveByID ¶
func (c *Collection) RemoveByID(id string) error
RemoveByID removes a single document by object id in string form.
func (*Collection) RemoveByObjectID ¶
func (c *Collection) RemoveByObjectID(id bson.ObjectId) error
RemoveByObjectID removes a single document by object id.
func (*Collection) Update ¶
func (c *Collection) Update(selector, update interface{}) error
Update updates a single document by query selector.
func (*Collection) UpdateAll ¶
func (c *Collection) UpdateAll(selector, update interface{}) error
UpdateAll updates all documents match the query selector.
func (*Collection) UpdateByObjectID ¶
func (c *Collection) UpdateByObjectID(id bson.ObjectId, update interface{}) error
UpdateByObjectID updates a single document by object id.
func (*Collection) UpdateSet ¶
func (c *Collection) UpdateSet(selector, update interface{}) error
UpdateSet updates a single document with $set operator by query selector.
func (*Collection) UpdateSetAll ¶
func (c *Collection) UpdateSetAll(selector, update interface{}) error
UpdateSetAll updates all documents match the query selector with $set operator.
func (*Collection) UpdateSetByObjectID ¶
func (c *Collection) UpdateSetByObjectID(id bson.ObjectId, update interface{}) error
UpdateSetByObjectID updates a single document with $set operator by object id.
func (*Collection) Upsert ¶
func (c *Collection) Upsert(selector, update interface{}) (info *mgo.ChangeInfo, err error)
Upsert upserts documents by query selector.
func (*Collection) UpsertByObjectID ¶
func (c *Collection) UpsertByObjectID(id bson.ObjectId, update interface{}) (info *mgo.ChangeInfo, err error)
UpsertByObjectID upserts documents by given object id.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database is the db you connect.
func NewDatabase ¶
func NewDatabase() *Database
NewDatabase returns a Database instance. You should initiate it by calling `InitWithURL` or `InitWithDialInfo`.
func (*Database) C ¶
func (d *Database) C(name string, indexes ...Index) *Collection
C creates a Collection which connects to a specific mongo collection with optional indexes.
func (*Database) Clone ¶
Clone clones a new db instance from this instance, but reuses the same session as the original database.
func (*Database) Copy ¶
Copy copies a new db instance from this instance.
See `mgo.Session.Copy` also.
func (*Database) InitWithDialInfo ¶
InitWithDialInfo initiates db instance by given DialInfo.
func (*Database) InitWithURL ¶
InitWithURL initiates db instance by given url. The additional options are `dial_timeout`(second), `sync_timeout`(second) and `mode`(strong|eventual|mono|monotonic).
See `mgo.Dial` also.
type DialInfo ¶
DialInfo is the wrapper of mgo.DialInfo.
func ParseDialInfo ¶
ParseDialInfo parse the mongo dsn url to `DialInfo`.
type Index ¶
type Index struct { Key []string // Index key fields; prefix name with dash (-) for descending order Unique bool // Prevent two documents from having the same index key Background bool // Build index in background and return immediately Sparse bool // Only index documents containing the Key fields // If ExpireAfter is defined the server will periodically delete // documents with indexed time.Time older than the provided delta. ExpireAfter time.Duration }
Index represents a index of collection.
type Marker ¶
type Marker interface { List(col *mgo.Collection, query, selector, models interface{}, limit int) (prev, next interface{}, err error) QueryStatement(baseQuery interface{}) interface{} SortField() string PrevNext(result interface{}) (prev, next interface{}) }
Marker is the field which is the query condition in range-based paging query. Normally, a collection which hopes to use marker pagination should always have a default marker field.
type ModelError ¶
type ModelError int
ModelError is the mgobase package level error type.
const ( // ErrInvalidID represents the id is not a valid object id. ErrInvalidID ModelError = iota + 1 // ErrNotFound represents the record retrieved not found. ErrNotFound // ErrDuplicateKey represents the document to be inserted or updated into db has conflicting value on a field. ErrDuplicateKey // ErrNotConnected represents can't not connect to db. ErrNotConnected )
func (ModelError) Error ¶
func (e ModelError) Error() string
type Paginater ¶
type Paginater interface { Page() int Limit() int TotalItems() int TotalPages() int HasPrev() bool PrevPage() int HasNext() bool NextPage() int IterRange(leftEdge, leftCurrent, rightCurrent, RightEdge int) []PageItem }
Paginater interface
func NewPaginater ¶
NewPaginater instances a paginater implements Paginater interface.