Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
754c6dd624
|
|||
|
6b367dbd7a
|
|||
|
98801edbc0
|
|||
|
7dd238342c
|
|||
|
115c288a0b
|
|||
|
702a05f405
|
16
crud.go
16
crud.go
@@ -27,7 +27,7 @@ func (e *ErrorWithStack) Stack() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CreateDocument creates a document from specified model
|
// CreateDocument creates a document from specified model
|
||||||
func CreateDocument(db *Database, m ModelInterface) error {
|
func (db *Database) CreateDocument(m ModelInterface) error {
|
||||||
m.PrepareInsert()
|
m.PrepareInsert()
|
||||||
|
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
@@ -37,7 +37,7 @@ func CreateDocument(db *Database, m ModelInterface) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadDocument gets one document via its id
|
// ReadDocument gets one document via its id
|
||||||
func ReadDocument(db *Database, m ModelInterface, selector bson.M) error {
|
func (db *Database) ReadDocument(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 +136,7 @@ func idToObjectID(filter interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCollection gets the filtered collection of the model
|
// ReadCollection gets the filtered collection of the model
|
||||||
func ReadCollection(db *Database, results interface{}, filter bson.M, selector bson.M, offset int, limit int, sort []string, pipelineModifier PipelineModifierFunction) (err error) {
|
func (db *Database) ReadCollection(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())}
|
||||||
@@ -243,7 +243,7 @@ func ReadCollection(db *Database, results interface{}, filter bson.M, selector b
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ReadCollectionCount gets the count of elements in filtered collection
|
// ReadCollectionCount gets the count of elements in filtered collection
|
||||||
func ReadCollectionCount(db *Database, m ModelInterface, filter bson.M) (count int, err error) {
|
func (db *Database) ReadCollectionCount(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 +255,7 @@ func ReadCollectionCount(db *Database, m ModelInterface, filter bson.M) (count i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDocument updates a document from specified model
|
// UpdateDocument updates a document from specified model
|
||||||
func UpdateDocument(db *Database, m ModelInterface, changes bson.M) error {
|
func (db *Database) UpdateDocument(m ModelInterface, changes bson.M) error {
|
||||||
m.PrepareUpdate()
|
m.PrepareUpdate()
|
||||||
changes["updateTime"] = time.Now()
|
changes["updateTime"] = time.Now()
|
||||||
|
|
||||||
@@ -266,7 +266,7 @@ func UpdateDocument(db *Database, 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 UpsertDocument(db *Database, m ModelInterface, changes bson.M) error {
|
func (db *Database) UpsertDocument(m ModelInterface, changes bson.M) error {
|
||||||
m.PrepareUpdate()
|
m.PrepareUpdate()
|
||||||
changes["updateTime"] = time.Now()
|
changes["updateTime"] = time.Now()
|
||||||
|
|
||||||
@@ -277,7 +277,7 @@ func UpsertDocument(db *Database, m ModelInterface, changes bson.M) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDocument deletes one document via its id
|
// DeleteDocument deletes one document via its id
|
||||||
func DeleteDocument(db *Database, m ModelInterface) error {
|
func (db *Database) DeleteDocument(m ModelInterface) error {
|
||||||
c := db.C(GetCollectionName(m))
|
c := db.C(GetCollectionName(m))
|
||||||
|
|
||||||
err := c.RemoveId(m.GetID())
|
err := c.RemoveId(m.GetID())
|
||||||
@@ -286,7 +286,7 @@ func DeleteDocument(db *Database, m ModelInterface) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDocuments deletes documents found by filter
|
// DeleteDocuments deletes documents found by filter
|
||||||
func DeleteDocuments(db *Database, m ModelInterface, filter bson.M) (removed int, err error) {
|
func (db *Database) DeleteDocuments(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)
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Lookup extends results with data for inline structs
|
// Lookup extends results with data for inline structs
|
||||||
func Lookup(db *Database, structField string, results interface{}, selector bson.M) error {
|
func (db *Database) Lookup(structField string, results interface{}, selector bson.M) error {
|
||||||
|
|
||||||
t := reflect.TypeOf(results)
|
t := reflect.TypeOf(results)
|
||||||
v := reflect.ValueOf(results)
|
v := reflect.ValueOf(results)
|
||||||
@@ -122,7 +122,7 @@ func Lookup(db *Database, structField string, results interface{}, selector bson
|
|||||||
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 := ReadCollection(db, objectResults, sQuery, selector, 0, 0, nil, nil)
|
err := db.ReadCollection(objectResults, sQuery, selector, 0, 0, nil, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|||||||
61
session.go
61
session.go
@@ -6,6 +6,27 @@ import (
|
|||||||
mgo "gopkg.in/mgo.v2"
|
mgo "gopkg.in/mgo.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrNotFound = errors.New("not found")
|
||||||
|
)
|
||||||
|
|
||||||
|
type Connection struct {
|
||||||
|
connection *mgo.Session
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConnection(dial string) (*Connection, error) {
|
||||||
|
connection, err := mgo.Dial(dial)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
connection.SetMode(mgo.Monotonic, true)
|
||||||
|
return &Connection{connection: connection}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Connection) Close() {
|
||||||
|
c.connection.Close()
|
||||||
|
}
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
session *mgo.Session
|
session *mgo.Session
|
||||||
}
|
}
|
||||||
@@ -14,27 +35,27 @@ func (s *Session) Close() {
|
|||||||
s.session.Close()
|
s.session.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSession(dial string) (*Session, error) {
|
func (c *Connection) NewSession() (*Session, error) {
|
||||||
session, err := mgo.Dial(dial)
|
return &Session{session: c.connection.Copy()}, nil
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
session.SetMode(mgo.Monotonic, true)
|
|
||||||
return &Session{session: session}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
database *mgo.Database
|
database *mgo.Database
|
||||||
|
session *Session
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) DB(name string) *Database {
|
func (s *Session) DB(name string) *Database {
|
||||||
return &Database{database: s.session.DB(name)}
|
return &Database{database: s.session.DB(name), session: s}
|
||||||
}
|
}
|
||||||
|
|
||||||
type Collection struct {
|
type Collection struct {
|
||||||
collection *mgo.Collection
|
collection *mgo.Collection
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *Database) Session() *Session {
|
||||||
|
return db.session
|
||||||
|
}
|
||||||
|
|
||||||
func (db *Database) C(name string) *Collection {
|
func (db *Database) C(name string) *Collection {
|
||||||
return &Collection{collection: db.database.C(name)}
|
return &Collection{collection: db.database.C(name)}
|
||||||
}
|
}
|
||||||
@@ -105,7 +126,11 @@ func (q *Query) Select(selector interface{}) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) One(result interface{}) error {
|
func (q *Query) One(result interface{}) error {
|
||||||
return q.query.One(result)
|
err := q.query.One(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) Sort(fields ...string) *Query {
|
func (q *Query) Sort(fields ...string) *Query {
|
||||||
@@ -124,11 +149,19 @@ func (q *Query) Limit(n int) *Query {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) All(result interface{}) error {
|
func (q *Query) All(result interface{}) error {
|
||||||
return q.query.All(result)
|
err := q.query.All(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Query) Count() (int, error) {
|
func (q *Query) Count() (int, error) {
|
||||||
return q.query.Count()
|
c, err := q.query.Count()
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return c, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type Index struct {
|
type Index struct {
|
||||||
@@ -151,7 +184,11 @@ type Pipe struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pipe) All(result interface{}) error {
|
func (p *Pipe) All(result interface{}) error {
|
||||||
return p.pipe.All(result)
|
err := p.pipe.All(result)
|
||||||
|
if err == mgo.ErrNotFound {
|
||||||
|
err = ErrNotFound
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
|
func (c *Collection) Pipe(pipeline interface{}) *Pipe {
|
||||||
|
|||||||
2
setup.go
2
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 EnsureIndex(db *Database, m ModelInterface) error {
|
func (db *Database) EnsureIndex(m ModelInterface) error {
|
||||||
colName := GetCollectionName(m)
|
colName := GetCollectionName(m)
|
||||||
col := db.C(colName)
|
col := db.C(colName)
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// ValidateObject validates object via validator tag and custom method
|
// ValidateObject validates object via validator tag and custom method
|
||||||
func ValidateObject(db *Database, m ModelInterface, changes bson.M) error {
|
func (db *Database) ValidateObject(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