Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 03b83d3fb9 |
22
crud.go
22
crud.go
@@ -7,6 +7,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,7 +28,7 @@ func (e *ErrorWithStack) Stack() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateDocument creates a document from specified model
|
// CreateDocument creates a document from specified model
|
||||||
func (db *MgoDatabase) CreateDocument(m ModelInterface) error {
|
func CreateDocument(db *mgo.Database, m ModelInterface) error {
|
||||||
m.PrepareInsert()
|
m.PrepareInsert()
|
||||||
|
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
@@ -37,7 +38,7 @@ func (db *MgoDatabase) CreateDocument(m ModelInterface) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadDocument gets one document via its id
|
// ReadDocument gets one document via its id
|
||||||
func (db *MgoDatabase) ReadDocument(m ModelInterface, selector bson.M) error {
|
func ReadDocument(db *mgo.Database, m ModelInterface, selector bson.M) error {
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
q := c.FindId(m.GetID())
|
q := c.FindId(m.GetID())
|
||||||
@@ -136,7 +137,7 @@ func idToObjectID(filter interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCollection gets the filtered collection of the model
|
// ReadCollection gets the filtered collection of the model
|
||||||
func (db *MgoDatabase) ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) {
|
func ReadCollection(db *mgo.Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err = &ErrorWithStack{Message: fmt.Sprintf("%v", r), StackTrace: string(debug.Stack())}
|
err = &ErrorWithStack{Message: fmt.Sprintf("%v", r), StackTrace: string(debug.Stack())}
|
||||||
@@ -211,7 +212,8 @@ func (db *MgoDatabase) ReadCollection(results interface{}, filter bson.M, select
|
|||||||
pipeline = pipelineModifier(pipeline)
|
pipeline = pipelineModifier(pipeline)
|
||||||
}
|
}
|
||||||
|
|
||||||
_err = c.Pipe(pipeline).All(results)
|
q := c.Pipe(pipeline).AllowDiskUse().Iter()
|
||||||
|
_err = q.All(results)
|
||||||
} else {
|
} else {
|
||||||
// search without pipe is faster
|
// search without pipe is faster
|
||||||
|
|
||||||
@@ -243,7 +245,7 @@ func (db *MgoDatabase) ReadCollection(results interface{}, filter bson.M, select
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCollectionCount gets the count of elements in filtered collection
|
// ReadCollectionCount gets the count of elements in filtered collection
|
||||||
func (db *MgoDatabase) ReadCollectionCount(m ModelInterface, filter bson.M) (count int, err error) {
|
func ReadCollectionCount(db *mgo.Database, m ModelInterface, filter bson.M) (count int, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err = fmt.Errorf("%v", r)
|
err = fmt.Errorf("%v", r)
|
||||||
@@ -255,7 +257,7 @@ func (db *MgoDatabase) ReadCollectionCount(m ModelInterface, filter bson.M) (cou
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDocument updates a document from specified model
|
// UpdateDocument updates a document from specified model
|
||||||
func (db *MgoDatabase) UpdateDocument(m ModelInterface, changes bson.M) error {
|
func UpdateDocument(db *mgo.Database, m ModelInterface, changes bson.M) error {
|
||||||
m.PrepareUpdate()
|
m.PrepareUpdate()
|
||||||
changes["updateTime"] = time.Now()
|
changes["updateTime"] = time.Now()
|
||||||
|
|
||||||
@@ -266,7 +268,7 @@ func (db *MgoDatabase) UpdateDocument(m ModelInterface, changes bson.M) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpsertDocument updates a document from specified model or inserts it, of not found
|
// UpsertDocument updates a document from specified model or inserts it, of not found
|
||||||
func (db *MgoDatabase) UpsertDocument(m ModelInterface, changes bson.M) error {
|
func UpsertDocument(db *mgo.Database, m ModelInterface, changes bson.M) error {
|
||||||
m.PrepareUpdate()
|
m.PrepareUpdate()
|
||||||
changes["updateTime"] = time.Now()
|
changes["updateTime"] = time.Now()
|
||||||
|
|
||||||
@@ -277,7 +279,7 @@ func (db *MgoDatabase) UpsertDocument(m ModelInterface, changes bson.M) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDocument deletes one document via its id
|
// DeleteDocument deletes one document via its id
|
||||||
func (db *MgoDatabase) DeleteDocument(m ModelInterface) error {
|
func DeleteDocument(db *mgo.Database, m ModelInterface) error {
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
err := c.RemoveId(m.GetID())
|
err := c.RemoveId(m.GetID())
|
||||||
@@ -286,12 +288,12 @@ func (db *MgoDatabase) DeleteDocument(m ModelInterface) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDocuments deletes documents found by filter
|
// DeleteDocuments deletes documents found by filter
|
||||||
func (db *MgoDatabase) DeleteDocuments(m ModelInterface, filter bson.M) (removed int, err error) {
|
func DeleteDocuments(db *mgo.Database, m ModelInterface, filter bson.M) (removed int, err error) {
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
info, err := c.RemoveAll(filter)
|
info, err := c.RemoveAll(filter)
|
||||||
if info != nil {
|
if info != nil {
|
||||||
removed = info.Removed()
|
removed = info.Removed
|
||||||
}
|
}
|
||||||
|
|
||||||
return removed, err
|
return removed, err
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module gitbase.de/gopackage/mgocrud/v2
|
module gitbase.de/gopackage/mgocrud
|
||||||
|
|
||||||
go 1.16
|
go 1.16
|
||||||
|
|
||||||
|
|||||||
19
lookup.go
19
lookup.go
@@ -4,11 +4,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Lookup extends results with data for inline structs
|
// Lookup extends results with data for inline structs
|
||||||
func (db *MgoDatabase) Lookup(structField string, results interface{}, selector bson.M) error {
|
func Lookup(db *mgo.Database, structField string, results interface{}, selector bson.M) error {
|
||||||
|
|
||||||
t := reflect.TypeOf(results)
|
t := reflect.TypeOf(results)
|
||||||
v := reflect.ValueOf(results)
|
v := reflect.ValueOf(results)
|
||||||
@@ -93,7 +94,7 @@ func (db *MgoDatabase) Lookup(structField string, results interface{}, selector
|
|||||||
// no entries to map
|
// no entries to map
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
sArr := make([]bson.M, lArr)
|
sArr := make([]bson.M, lArr, lArr)
|
||||||
aI := 0
|
aI := 0
|
||||||
for sID := range objectIDs {
|
for sID := range objectIDs {
|
||||||
sArr[aI] = bson.M{
|
sArr[aI] = bson.M{
|
||||||
@@ -122,7 +123,7 @@ func (db *MgoDatabase) Lookup(structField string, results interface{}, selector
|
|||||||
return fmt.Errorf("ID type in objects struct %+v is not bson.ObjectId", fieldType)
|
return fmt.Errorf("ID type in objects struct %+v is not bson.ObjectId", fieldType)
|
||||||
}
|
}
|
||||||
|
|
||||||
err := db.ReadCollection(objectResults, sQuery, selector, 0, 0, nil, nil)
|
err := ReadCollection(db, objectResults, sQuery, selector, 0, 0, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@@ -155,10 +156,14 @@ func (db *MgoDatabase) Lookup(structField string, results interface{}, selector
|
|||||||
object := objectIDs[objectID]
|
object := objectIDs[objectID]
|
||||||
|
|
||||||
field := elV.FieldByName(structField)
|
field := elV.FieldByName(structField)
|
||||||
if fieldIsPtr {
|
if object != nil && field.CanSet() {
|
||||||
field.Set(reflect.ValueOf(object))
|
objectVal := reflect.ValueOf(object)
|
||||||
} else {
|
|
||||||
field.Set(reflect.ValueOf(object).Elem())
|
if fieldIsPtr {
|
||||||
|
field.Set(objectVal)
|
||||||
|
} else {
|
||||||
|
field.Set(objectVal.Elem())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
217
mgo.go
217
mgo.go
@@ -1,217 +0,0 @@
|
|||||||
package mgocrud
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"runtime"
|
|
||||||
|
|
||||||
mgo "gopkg.in/mgo.v2"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MgoConnection struct {
|
|
||||||
connection *mgo.Session
|
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMgoConnection(dial string) (Connection, error) {
|
|
||||||
connection, err := mgo.Dial(dial)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
connection.SetMode(mgo.Monotonic, true)
|
|
||||||
c := &MgoConnection{connection: connection}
|
|
||||||
runtime.SetFinalizer(c, func(c *MgoConnection) {
|
|
||||||
if !c.closed {
|
|
||||||
c.Close()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoConnection) Close() {
|
|
||||||
if !c.closed {
|
|
||||||
c.connection.Close()
|
|
||||||
c.closed = true
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(c, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoSession struct {
|
|
||||||
session *mgo.Session
|
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *MgoSession) Close() {
|
|
||||||
if !s.closed {
|
|
||||||
s.session.Close()
|
|
||||||
s.closed = true
|
|
||||||
}
|
|
||||||
runtime.SetFinalizer(s, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoConnection) NewSession() Session {
|
|
||||||
s := &MgoSession{session: c.connection.Copy()}
|
|
||||||
runtime.SetFinalizer(s, func(s *MgoSession) {
|
|
||||||
if !s.closed {
|
|
||||||
s.Close()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoDatabase struct {
|
|
||||||
database *mgo.Database
|
|
||||||
session Session
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *MgoSession) DB(name string) Database {
|
|
||||||
return &MgoDatabase{database: s.session.DB(name), session: s}
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoCollection struct {
|
|
||||||
collection *mgo.Collection
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *MgoDatabase) Session() Session {
|
|
||||||
return db.session
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *MgoDatabase) C(name string) Collection {
|
|
||||||
return &MgoCollection{collection: db.database.C(name)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (db *MgoDatabase) Name() string {
|
|
||||||
return db.database.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) Insert(docs ...interface{}) error {
|
|
||||||
return c.collection.Insert(docs...)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) UpdateId(id interface{}, update interface{}) error {
|
|
||||||
return c.collection.UpdateId(id, update)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) RemoveId(id interface{}) error {
|
|
||||||
return c.collection.RemoveId(id)
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoChangeInfo struct {
|
|
||||||
changeInfo *mgo.ChangeInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ci *MgoChangeInfo) Matched() int {
|
|
||||||
return ci.changeInfo.Matched
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ci *MgoChangeInfo) Removed() int {
|
|
||||||
return ci.changeInfo.Removed
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ci *MgoChangeInfo) Updated() int {
|
|
||||||
return ci.changeInfo.Updated
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) Upsert(selector interface{}, update interface{}) (ChangeInfo, error) {
|
|
||||||
ci, err := c.collection.Upsert(selector, update)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &MgoChangeInfo{changeInfo: ci}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) RemoveAll(filter interface{}) (ChangeInfo, error) {
|
|
||||||
ci, err := c.collection.RemoveAll(filter)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &MgoChangeInfo{changeInfo: ci}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoQuery struct {
|
|
||||||
query *mgo.Query
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) FindId(id interface{}) Query {
|
|
||||||
return &MgoQuery{query: c.collection.FindId(id)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) Find(query interface{}) Query {
|
|
||||||
return &MgoQuery{query: c.collection.Find(query)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) Select(selector interface{}) Query {
|
|
||||||
q.query = q.query.Select(selector)
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) One(result interface{}) error {
|
|
||||||
err := q.query.One(result)
|
|
||||||
if err == mgo.ErrNotFound {
|
|
||||||
err = ErrNotFound
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) Sort(fields ...string) Query {
|
|
||||||
q.query = q.query.Sort(fields...)
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) Skip(n int) Query {
|
|
||||||
q.query = q.query.Skip(n)
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) Limit(n int) Query {
|
|
||||||
q.query = q.query.Limit(n)
|
|
||||||
return q
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) All(result interface{}) error {
|
|
||||||
err := q.query.All(result)
|
|
||||||
if err == mgo.ErrNotFound {
|
|
||||||
err = ErrNotFound
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *MgoQuery) Count() (int, error) {
|
|
||||||
c, err := q.query.Count()
|
|
||||||
if err == mgo.ErrNotFound {
|
|
||||||
err = ErrNotFound
|
|
||||||
}
|
|
||||||
return c, err
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoIndex struct {
|
|
||||||
index *mgo.Index
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewMgoIndex(index mgo.Index) Index {
|
|
||||||
return &MgoIndex{index: &index}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) EnsureIndex(index Index) error {
|
|
||||||
if i, ok := index.(*MgoIndex); ok {
|
|
||||||
if i != nil && i.index != nil {
|
|
||||||
return c.collection.EnsureIndex(*i.index)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errors.New("index parameter not initialized with mgo.Index")
|
|
||||||
}
|
|
||||||
|
|
||||||
type MgoPipe struct {
|
|
||||||
pipe *mgo.Pipe
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *MgoPipe) All(result interface{}) error {
|
|
||||||
err := p.pipe.All(result)
|
|
||||||
if err == mgo.ErrNotFound {
|
|
||||||
err = ErrNotFound
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *MgoCollection) Pipe(pipeline interface{}) Pipe {
|
|
||||||
return &MgoPipe{pipe: c.collection.Pipe(pipeline).AllowDiskUse()}
|
|
||||||
}
|
|
||||||
14
setup.go
14
setup.go
@@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// EnsureIndex ensured mongodb index reflecting model struct index tag
|
// EnsureIndex ensured mongodb index reflecting model struct index tag
|
||||||
func (db *MgoDatabase) EnsureIndex(m ModelInterface) error {
|
func EnsureIndex(db *mgo.Database, m ModelInterface) error {
|
||||||
colName := GetCollectionName(m)
|
colName := GetCollectionName(m)
|
||||||
col := db.C(colName)
|
col := db.C(colName)
|
||||||
|
|
||||||
@@ -57,14 +57,14 @@ func (db *MgoDatabase) EnsureIndex(m ModelInterface) error {
|
|||||||
case indexEl == "text":
|
case indexEl == "text":
|
||||||
textFields = append(textFields, "$text:"+fieldbase+bsonField)
|
textFields = append(textFields, "$text:"+fieldbase+bsonField)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("invalid index tag on collection %s.%s for field %s%s in model %+v", db.Name(), colName, fieldbase, bsonField, t)
|
return fmt.Errorf("invalid index tag on collection %s.%s for field %s%s in model %+v", db.Name, colName, fieldbase, bsonField, t)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
if len(index.Key) > 0 {
|
if len(index.Key) > 0 {
|
||||||
// fmt.Println(bsonField, index)
|
// fmt.Println(bsonField, index)
|
||||||
fmt.Printf("ensure index on collection %s.%s for field %s%s\n", db.Name(), colName, fieldbase, bsonField)
|
fmt.Printf("ensure index on collection %s.%s for field %s%s\n", db.Name, colName, fieldbase, bsonField)
|
||||||
err := col.EnsureIndex(NewMgoIndex(index))
|
err := col.EnsureIndex(index)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -93,13 +93,13 @@ func (db *MgoDatabase) EnsureIndex(m ModelInterface) error {
|
|||||||
|
|
||||||
if len(textFields) > 0 {
|
if len(textFields) > 0 {
|
||||||
// fmt.Println("$text", textFields)
|
// fmt.Println("$text", textFields)
|
||||||
fmt.Printf("ensure text index on collection %s.%s for fields %v\n", db.Name(), GetCollectionName(m), textFields)
|
fmt.Printf("ensure text index on collection %s.%s for fields %v\n", db.Name, GetCollectionName(m), textFields)
|
||||||
err := col.EnsureIndex(NewMgoIndex(mgo.Index{
|
err := col.EnsureIndex(mgo.Index{
|
||||||
Name: "textindex",
|
Name: "textindex",
|
||||||
Key: textFields,
|
Key: textFields,
|
||||||
DefaultLanguage: "german",
|
DefaultLanguage: "german",
|
||||||
Background: false,
|
Background: false,
|
||||||
}))
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
71
types.go
71
types.go
@@ -1,71 +0,0 @@
|
|||||||
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
|
|
||||||
C(name string) Collection
|
|
||||||
Name() string
|
|
||||||
EnsureIndex(m ModelInterface) error
|
|
||||||
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
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,12 @@ package mgocrud
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
validator "gopkg.in/go-playground/validator.v8"
|
validator "gopkg.in/go-playground/validator.v8"
|
||||||
|
mgo "gopkg.in/mgo.v2"
|
||||||
"gopkg.in/mgo.v2/bson"
|
"gopkg.in/mgo.v2/bson"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidateObject validates object via validator tag and custom method
|
// ValidateObject validates object via validator tag and custom method
|
||||||
func (db *MgoDatabase) ValidateObject(m ModelInterface, changes bson.M) error {
|
func ValidateObject(db *mgo.Database, m ModelInterface, changes bson.M) error {
|
||||||
// first validate via struct tag
|
// first validate via struct tag
|
||||||
validator := validator.New(&validator.Config{
|
validator := validator.New(&validator.Config{
|
||||||
TagName: "validator",
|
TagName: "validator",
|
||||||
@@ -18,7 +19,7 @@ func (db *MgoDatabase) ValidateObject(m ModelInterface, changes bson.M) error {
|
|||||||
|
|
||||||
// next execute custom model validator if exists
|
// next execute custom model validator if exists
|
||||||
if i, ok := m.(interface {
|
if i, ok := m.(interface {
|
||||||
Validate(db Database, changes bson.M) error
|
Validate(db *mgo.Database, changes bson.M) error
|
||||||
}); ok {
|
}); ok {
|
||||||
return i.Validate(db, changes)
|
return i.Validate(db, changes)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user