Some checks failed
ci/woodpecker/push/billing_fees Pipeline was successful
ci/woodpecker/push/bff Pipeline was successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/chain_gateway Pipeline was successful
ci/woodpecker/push/fx_ingestor Pipeline was successful
ci/woodpecker/push/fx_oracle Pipeline was successful
ci/woodpecker/push/frontend Pipeline was successful
ci/woodpecker/push/payments_orchestrator Pipeline was successful
ci/woodpecker/push/bump_version Pipeline failed
ci/woodpecker/push/nats Pipeline was successful
ci/woodpecker/push/ledger Pipeline was successful
ci/woodpecker/push/notification Pipeline was successful
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
package repositoryimp
|
|
|
|
import (
|
|
"context"
|
|
|
|
ri "github.com/tech/sendico/pkg/db/repository/index"
|
|
"github.com/tech/sendico/pkg/merrors"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
)
|
|
|
|
func (r *MongoRepository) CreateIndex(def *ri.Definition) error {
|
|
if r.collection == nil {
|
|
return merrors.NoData("data collection is not set")
|
|
}
|
|
if len(def.Keys) == 0 {
|
|
return merrors.InvalidArgument("Index definition has no keys", "index.keys")
|
|
}
|
|
|
|
// ----- build BSON keys --------------------------------------------------
|
|
keys := bson.D{}
|
|
for _, k := range def.Keys {
|
|
var value any
|
|
switch {
|
|
case k.Type != "":
|
|
value = k.Type // text, 2dsphere, …
|
|
case k.Sort == ri.Desc:
|
|
value = int8(-1)
|
|
default:
|
|
value = int8(1) // default to Asc
|
|
}
|
|
keys = append(keys, bson.E{Key: k.Field, Value: value})
|
|
}
|
|
|
|
opts := options.Index().
|
|
SetUnique(def.Unique)
|
|
if def.TTL != nil {
|
|
opts.SetExpireAfterSeconds(*def.TTL)
|
|
}
|
|
if def.Name != "" {
|
|
opts.SetName(def.Name)
|
|
}
|
|
|
|
_, err := r.collection.Indexes().CreateOne(
|
|
context.Background(),
|
|
mongo.IndexModel{Keys: keys, Options: opts},
|
|
)
|
|
return err
|
|
}
|