From 336dea81471742c37a623814729826e390be7fec Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Thu, 10 Feb 2022 16:10:38 +0100 Subject: [PATCH] ensure multiple index via db interface --- setup.go | 14 +++++++++++++- types.go | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/setup.go b/setup.go index 662fa1a..5a638af 100644 --- a/setup.go +++ b/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{} diff --git a/types.go b/types.go index 7b6a8b2..9096720 100644 --- a/types.go +++ b/types.go @@ -22,9 +22,9 @@ type Session interface { type Database interface { Session() Session - C(name string) Collection + // C(name string) Collection 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 CreateDocument(m ModelInterface) error