Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
6834a79601
|
|||
|
336dea8147
|
|||
|
f8f8cb2366
|
|||
|
6a7a6135da
|
|||
|
709ca7f23b
|
27
mgo.go
27
mgo.go
@@ -2,12 +2,14 @@ package mgocrud
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
mgo "gopkg.in/mgo.v2"
|
mgo "gopkg.in/mgo.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MgoConnection struct {
|
type MgoConnection struct {
|
||||||
connection *mgo.Session
|
connection *mgo.Session
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMgoConnection(dial string) (Connection, error) {
|
func NewMgoConnection(dial string) (Connection, error) {
|
||||||
@@ -16,23 +18,44 @@ func NewMgoConnection(dial string) (Connection, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
connection.SetMode(mgo.Monotonic, true)
|
connection.SetMode(mgo.Monotonic, true)
|
||||||
return &MgoConnection{connection: connection}, nil
|
c := &MgoConnection{connection: connection}
|
||||||
|
runtime.SetFinalizer(c, func(c *MgoConnection) {
|
||||||
|
if !c.closed {
|
||||||
|
c.Close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MgoConnection) Close() {
|
func (c *MgoConnection) Close() {
|
||||||
|
if !c.closed {
|
||||||
c.connection.Close()
|
c.connection.Close()
|
||||||
|
c.closed = true
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(c, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
type MgoSession struct {
|
type MgoSession struct {
|
||||||
session *mgo.Session
|
session *mgo.Session
|
||||||
|
closed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MgoSession) Close() {
|
func (s *MgoSession) Close() {
|
||||||
|
if !s.closed {
|
||||||
s.session.Close()
|
s.session.Close()
|
||||||
|
s.closed = true
|
||||||
|
}
|
||||||
|
runtime.SetFinalizer(s, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *MgoConnection) NewSession() Session {
|
func (c *MgoConnection) NewSession() Session {
|
||||||
return &MgoSession{session: c.connection.Copy()}
|
s := &MgoSession{session: c.connection.Copy()}
|
||||||
|
runtime.SetFinalizer(s, func(s *MgoSession) {
|
||||||
|
if !s.closed {
|
||||||
|
s.Close()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
type MgoDatabase struct {
|
type MgoDatabase struct {
|
||||||
|
|||||||
14
setup.go
14
setup.go
@@ -9,10 +9,22 @@ 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 (db *MgoDatabase) EnsureIndex(m ModelInterface, index ...Index) error {
|
||||||
colName := GetCollectionName(m)
|
colName := GetCollectionName(m)
|
||||||
col := db.C(colName)
|
col := db.C(colName)
|
||||||
|
|
||||||
|
if len(index) > 0 {
|
||||||
|
// only ensure given index
|
||||||
|
for _, i := range index {
|
||||||
|
err := col.EnsureIndex(i)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensure index by struct fields
|
||||||
mType := reflect.TypeOf(m)
|
mType := reflect.TypeOf(m)
|
||||||
|
|
||||||
textFields := []string{}
|
textFields := []string{}
|
||||||
|
|||||||
5
types.go
5
types.go
@@ -22,10 +22,9 @@ type Session interface {
|
|||||||
|
|
||||||
type Database interface {
|
type Database interface {
|
||||||
Session() Session
|
Session() Session
|
||||||
C(name string) Collection
|
// C(name string) Collection
|
||||||
Name() string
|
Name() string
|
||||||
EnsureIndex(m ModelInterface) error
|
EnsureIndex(m ModelInterface, index ...Index) error
|
||||||
ValidateObject(m ModelInterface, changes bson.M) error
|
|
||||||
ReadDocument(m ModelInterface, selector bson.M) error
|
ReadDocument(m ModelInterface, selector bson.M) error
|
||||||
CreateDocument(m ModelInterface) error
|
CreateDocument(m ModelInterface) error
|
||||||
ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) error
|
ReadCollection(results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) error
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// 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 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",
|
||||||
|
|||||||
Reference in New Issue
Block a user