Skip to content

Commit

Permalink
Merge pull request #322 from maxekman/fix/add-create-index-for-mongodb
Browse files Browse the repository at this point in the history
Fix / Add CreateIndex method to the MongoDB repo
  • Loading branch information
maxekman committed Jun 7, 2021
2 parents 760d8c9 + 3515417 commit 7443821
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions repo/mongodb/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,15 @@ func (r *Repo) Collection(ctx context.Context, f func(context.Context, *mongo.Co
return nil
}

// CreateIndex creates an index for a field.
func (r *Repo) CreateIndex(ctx context.Context, field string) error {
index := mongo.IndexModel{Keys: bson.M{field: 1}}
if _, err := r.entities.Indexes().CreateOne(ctx, index); err != nil {
return fmt.Errorf("could not create index: %s", err)
}
return nil
}

// SetEntityFactory sets a factory function that creates concrete entity types.
func (r *Repo) SetEntityFactory(f func() eh.Entity) {
r.newEntity = f
Expand Down

0 comments on commit 7443821

Please sign in to comment.