ensure multiple index via db interface
This commit is contained in:
parent
f8f8cb2366
commit
336dea8147
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{}
|
||||||
|
4
types.go
4
types.go
@ -22,9 +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
|
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
|
||||||
|
Loading…
Reference in New Issue
Block a user