Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
ede07bb49a
|
|||
|
6834a79601
|
|||
|
336dea8147
|
|||
|
f8f8cb2366
|
27
mgo.go
27
mgo.go
@@ -2,7 +2,6 @@ package mgocrud
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
mgo "gopkg.in/mgo.v2"
|
||||
@@ -14,7 +13,6 @@ type MgoConnection struct {
|
||||
}
|
||||
|
||||
func NewMgoConnection(dial string) (Connection, error) {
|
||||
fmt.Println("CONNECTION CREATE")
|
||||
connection, err := mgo.Dial(dial)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -22,7 +20,6 @@ func NewMgoConnection(dial string) (Connection, error) {
|
||||
connection.SetMode(mgo.Monotonic, true)
|
||||
c := &MgoConnection{connection: connection}
|
||||
runtime.SetFinalizer(c, func(c *MgoConnection) {
|
||||
fmt.Println("CONNECTION CLOSE FINALIZER")
|
||||
if !c.closed {
|
||||
c.Close()
|
||||
}
|
||||
@@ -31,7 +28,6 @@ func NewMgoConnection(dial string) (Connection, error) {
|
||||
}
|
||||
|
||||
func (c *MgoConnection) Close() {
|
||||
fmt.Println("CONNECTION CLOSE MANUALLY")
|
||||
if !c.closed {
|
||||
c.connection.Close()
|
||||
c.closed = true
|
||||
@@ -40,12 +36,16 @@ func (c *MgoConnection) Close() {
|
||||
}
|
||||
|
||||
type MgoSession struct {
|
||||
connection *MgoConnection
|
||||
session *mgo.Session
|
||||
closed bool
|
||||
}
|
||||
|
||||
func (s *MgoSession) Connection() Connection {
|
||||
return s.connection
|
||||
}
|
||||
|
||||
func (s *MgoSession) Close() {
|
||||
fmt.Println("SESSION CLOSE MANUALLY")
|
||||
if !s.closed {
|
||||
s.session.Close()
|
||||
s.closed = true
|
||||
@@ -54,10 +54,11 @@ func (s *MgoSession) Close() {
|
||||
}
|
||||
|
||||
func (c *MgoConnection) NewSession() Session {
|
||||
fmt.Println("SESSION CREATE")
|
||||
s := &MgoSession{session: c.connection.Copy()}
|
||||
s := &MgoSession{
|
||||
connection: c,
|
||||
session: c.connection.Copy(),
|
||||
}
|
||||
runtime.SetFinalizer(s, func(s *MgoSession) {
|
||||
fmt.Println("SESSION CLOSE FINALIZER")
|
||||
if !s.closed {
|
||||
s.Close()
|
||||
}
|
||||
@@ -75,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 {
|
||||
|
||||
14
setup.go
14
setup.go
@@ -9,10 +9,22 @@ import (
|
||||
)
|
||||
|
||||
// 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)
|
||||
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)
|
||||
|
||||
textFields := []string{}
|
||||
|
||||
7
types.go
7
types.go
@@ -16,16 +16,16 @@ type Connection interface {
|
||||
}
|
||||
|
||||
type Session interface {
|
||||
Connection() Connection
|
||||
Close()
|
||||
DB(name string) Database
|
||||
}
|
||||
|
||||
type Database interface {
|
||||
Session() Session
|
||||
C(name string) Collection
|
||||
// C(name string) Collection
|
||||
Name() string
|
||||
EnsureIndex(m ModelInterface) error
|
||||
ValidateObject(m ModelInterface, changes bson.M) error
|
||||
EnsureIndex(m ModelInterface, index ...Index) 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