2021-12-20 17:51:27 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/authorizerdev/authorizer/server/constants"
|
2022-01-17 06:02:13 +00:00
|
|
|
"github.com/authorizerdev/authorizer/server/envstore"
|
2021-12-20 17:51:27 +00:00
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/readpref"
|
|
|
|
)
|
|
|
|
|
|
|
|
func initMongodb() (*mongo.Database, error) {
|
2022-01-17 06:02:13 +00:00
|
|
|
mongodbOptions := options.Client().ApplyURI(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseURL).(string))
|
2021-12-20 17:51:27 +00:00
|
|
|
maxWait := time.Duration(5 * time.Second)
|
|
|
|
mongodbOptions.ConnectTimeout = &maxWait
|
|
|
|
mongoClient, err := mongo.NewClient(mongodbOptions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ctx, _ := context.WithTimeout(context.Background(), 30*time.Second)
|
|
|
|
err = mongoClient.Connect(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mongoClient.Ping(ctx, readpref.Primary())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-17 06:02:13 +00:00
|
|
|
mongodb := mongoClient.Database(envstore.EnvInMemoryStoreObj.GetEnvVariable(constants.EnvKeyDatabaseName).(string), options.Database())
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
mongodb.CreateCollection(ctx, Collections.User, options.CreateCollection())
|
|
|
|
userCollection := mongodb.Collection(Collections.User, options.Collection())
|
|
|
|
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
|
|
mongo.IndexModel{
|
|
|
|
Keys: bson.M{"email": 1},
|
|
|
|
Options: options.Index().SetUnique(true).SetSparse(true),
|
|
|
|
},
|
|
|
|
}, options.CreateIndexes())
|
2021-12-22 05:21:12 +00:00
|
|
|
userCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
|
|
mongo.IndexModel{
|
2021-12-22 10:01:45 +00:00
|
|
|
Keys: bson.M{"phone_number": 1},
|
|
|
|
Options: options.Index().SetUnique(true).SetSparse(true).SetPartialFilterExpression(map[string]interface{}{
|
|
|
|
"phone_number": map[string]string{"$type": "string"},
|
|
|
|
}),
|
2021-12-22 05:21:12 +00:00
|
|
|
},
|
|
|
|
}, options.CreateIndexes())
|
2021-12-20 17:51:27 +00:00
|
|
|
|
|
|
|
mongodb.CreateCollection(ctx, Collections.VerificationRequest, options.CreateCollection())
|
|
|
|
verificationRequestCollection := mongodb.Collection(Collections.VerificationRequest, options.Collection())
|
|
|
|
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
|
|
mongo.IndexModel{
|
|
|
|
Keys: bson.M{"email": 1, "identifier": 1},
|
|
|
|
Options: options.Index().SetUnique(true).SetSparse(true),
|
|
|
|
},
|
|
|
|
}, options.CreateIndexes())
|
|
|
|
verificationRequestCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
|
|
mongo.IndexModel{
|
|
|
|
Keys: bson.M{"token": 1},
|
|
|
|
Options: options.Index().SetSparse(true),
|
|
|
|
},
|
|
|
|
}, options.CreateIndexes())
|
|
|
|
|
|
|
|
mongodb.CreateCollection(ctx, Collections.Session, options.CreateCollection())
|
2021-12-23 05:01:52 +00:00
|
|
|
sessionCollection := mongodb.Collection(Collections.Session, options.Collection())
|
|
|
|
sessionCollection.Indexes().CreateMany(ctx, []mongo.IndexModel{
|
|
|
|
mongo.IndexModel{
|
|
|
|
Keys: bson.M{"user_id": 1},
|
|
|
|
Options: options.Index().SetSparse(true),
|
|
|
|
},
|
|
|
|
}, options.CreateIndexes())
|
2021-12-20 17:51:27 +00:00
|
|
|
|
2021-12-31 08:22:10 +00:00
|
|
|
mongodb.CreateCollection(ctx, Collections.Config, options.CreateCollection())
|
|
|
|
|
2021-12-20 17:51:27 +00:00
|
|
|
return mongodb, nil
|
|
|
|
}
|