@@ -157,13 +157,19 @@ func NewAdapterByDB(client *mongo.Client, config *AdapterConfig) (persist.BatchA
157157 config .Timeout = defaultTimeout
158158 }
159159
160+ collection := client .Database (config .DatabaseName ).Collection (config .CollectionName )
161+
160162 a := & adapter {
161163 client : client ,
162- collection : client . Database ( config . DatabaseName ). Collection ( config . CollectionName ) ,
164+ collection : collection ,
163165 timeout : config .Timeout ,
164166 filtered : config .IsFiltered ,
165167 }
166168
169+ if err := a .prepareIndexes (); err != nil {
170+ return nil , err
171+ }
172+
167173 // Call the destructor when the object is released.
168174 runtime .SetFinalizer (a , finalizer )
169175
@@ -185,6 +191,14 @@ func (a *adapter) open(clientOption *options.ClientOptions, databaseName string,
185191 a .client = client
186192 a .collection = collection
187193
194+ if err = a .prepareIndexes (); err != nil {
195+ return err
196+ }
197+
198+ return nil
199+ }
200+
201+ func (a * adapter ) prepareIndexes () error {
188202 indexes := []string {"ptype" , "v0" , "v1" , "v2" , "v3" , "v4" , "v5" }
189203 keysDoc := bson.D {}
190204
@@ -195,7 +209,7 @@ func (a *adapter) open(clientOption *options.ClientOptions, databaseName string,
195209 keysDoc = append (keysDoc , keyDoc )
196210 }
197211
198- if _ , err = collection .Indexes ().CreateOne (
212+ if _ , err := a . collection .Indexes ().CreateOne (
199213 context .Background (),
200214 mongo.IndexModel {
201215 Keys : keysDoc ,
0 commit comments