Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3a826ac216 | |||
|
ede07bb49a
|
|||
|
6834a79601
|
8
crud.go
8
crud.go
@@ -176,13 +176,15 @@ func (db *MgoDatabase) ReadCollection(results interface{}, filter bson.M, select
|
||||
}
|
||||
|
||||
if len(sort) > 0 {
|
||||
sortM := bson.M{}
|
||||
sortM := bson.D{}
|
||||
for _, s := range sort {
|
||||
if strings.HasPrefix(s, "-") {
|
||||
s = s[1:]
|
||||
sortM[s] = -1
|
||||
sortM = append(sortM, bson.DocElem{Name: s, Value: -1})
|
||||
// sortM[s] = -1
|
||||
} else {
|
||||
sortM[s] = 1
|
||||
sortM = append(sortM, bson.DocElem{Name: s, Value: 1})
|
||||
// sortM[s] = 1
|
||||
}
|
||||
}
|
||||
// spew.Dump(sortM)
|
||||
|
||||
24
mgo.go
24
mgo.go
@@ -36,8 +36,13 @@ func (c *MgoConnection) Close() {
|
||||
}
|
||||
|
||||
type MgoSession struct {
|
||||
session *mgo.Session
|
||||
closed bool
|
||||
connection *MgoConnection
|
||||
session *mgo.Session
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (s *MgoSession) Connection() Connection {
|
||||
return s.connection
|
||||
}
|
||||
|
||||
func (s *MgoSession) Close() {
|
||||
@@ -49,7 +54,10 @@ func (s *MgoSession) Close() {
|
||||
}
|
||||
|
||||
func (c *MgoConnection) NewSession() Session {
|
||||
s := &MgoSession{session: c.connection.Copy()}
|
||||
s := &MgoSession{
|
||||
connection: c,
|
||||
session: c.connection.Copy(),
|
||||
}
|
||||
runtime.SetFinalizer(s, func(s *MgoSession) {
|
||||
if !s.closed {
|
||||
s.Close()
|
||||
@@ -68,15 +76,23 @@ func (s *MgoSession) DB(name string) Database {
|
||||
}
|
||||
|
||||
type MgoCollection struct {
|
||||
database *MgoDatabase
|
||||
collection *mgo.Collection
|
||||
}
|
||||
|
||||
func (c *MgoCollection) DB() Database {
|
||||
return c.database
|
||||
}
|
||||
|
||||
func (db *MgoDatabase) Session() Session {
|
||||
return db.session
|
||||
}
|
||||
|
||||
func (db *MgoDatabase) C(name string) Collection {
|
||||
return &MgoCollection{collection: db.database.C(name)}
|
||||
return &MgoCollection{
|
||||
database: db,
|
||||
collection: db.database.C(name),
|
||||
}
|
||||
}
|
||||
|
||||
func (db *MgoDatabase) Name() string {
|
||||
|
||||
3
types.go
3
types.go
@@ -16,6 +16,7 @@ type Connection interface {
|
||||
}
|
||||
|
||||
type Session interface {
|
||||
Connection() Connection
|
||||
Close()
|
||||
DB(name string) Database
|
||||
}
|
||||
@@ -25,7 +26,6 @@ type Database interface {
|
||||
// C(name string) Collection
|
||||
Name() string
|
||||
EnsureIndex(m ModelInterface, index ...Index) 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
|
||||
@@ -37,6 +37,7 @@ type Database interface {
|
||||
}
|
||||
|
||||
type Collection interface {
|
||||
DB() Database
|
||||
Insert(docs ...interface{}) error
|
||||
UpdateId(id interface{}, update interface{}) error
|
||||
RemoveId(id interface{}) error
|
||||
|
||||
@@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
// ValidateObject validates object via validator tag and custom method
|
||||
func (db *MgoDatabase) ValidateObject(m ModelInterface, changes bson.M) error {
|
||||
func ValidateObject(db Database, m ModelInterface, changes bson.M) error {
|
||||
// first validate via struct tag
|
||||
validator := validator.New(&validator.Config{
|
||||
TagName: "validator",
|
||||
|
||||
Reference in New Issue
Block a user