2022-02-09 21:57:31 +01:00
|
|
|
package mgocrud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"gopkg.in/mgo.v2/bson"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrNotFound = errors.New("not found")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Connection interface {
|
|
|
|
Close()
|
|
|
|
NewSession() Session
|
|
|
|
}
|
|
|
|
|
|
|
|
type Session interface {
|
|
|
|
Close()
|
|
|
|
DB(name string) Database
|
|
|
|
}
|
|
|
|
|
|
|
|
type Database interface {
|
|
|
|
Session() Session
|
2022-02-10 16:10:38 +01:00
|
|
|
// C(name string) Collection
|
2022-02-09 21:57:31 +01:00
|
|
|
Name() string
|
2022-02-10 16:10:38 +01:00
|
|
|
EnsureIndex(m ModelInterface, index ...Index) error
|
2022-02-09 21:57:31 +01:00
|
|
|
ValidateObject(m ModelInterface, changes bson.M) error
|
|
|
|
ReadDocument(m ModelInterface, selector bson.M) error
|
|
|
|
CreateDocument(m ModelInterface) error
|
|
|
|
ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) error
|
|
|
|
ReadCollectionCount(m ModelInterface, filter bson.M) (count int, err error)
|
|
|
|
UpdateDocument(m ModelInterface, changes bson.M) error
|
|
|
|
UpsertDocument(m ModelInterface, changes bson.M) error
|
|
|
|
DeleteDocument(m ModelInterface) error
|
|
|
|
DeleteDocuments(m ModelInterface, filter bson.M) (removed int, err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Collection interface {
|
|
|
|
Insert(docs ...interface{}) error
|
|
|
|
UpdateId(id interface{}, update interface{}) error
|
|
|
|
RemoveId(id interface{}) error
|
|
|
|
Upsert(selector interface{}, update interface{}) (ChangeInfo, error)
|
|
|
|
RemoveAll(filter interface{}) (ChangeInfo, error)
|
|
|
|
FindId(id interface{}) Query
|
|
|
|
Find(query interface{}) Query
|
|
|
|
EnsureIndex(index Index) error
|
|
|
|
Pipe(pipeline interface{}) Pipe
|
|
|
|
}
|
|
|
|
|
|
|
|
type ChangeInfo interface {
|
|
|
|
Matched() int
|
|
|
|
Removed() int
|
|
|
|
Updated() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Query interface {
|
|
|
|
Select(selector interface{}) Query
|
|
|
|
One(result interface{}) error
|
|
|
|
Sort(fields ...string) Query
|
|
|
|
Skip(n int) Query
|
|
|
|
Limit(n int) Query
|
|
|
|
All(result interface{}) error
|
|
|
|
Count() (int, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Index interface{}
|
|
|
|
|
|
|
|
type Pipe interface {
|
|
|
|
All(result interface{}) error
|
|
|
|
}
|