Files
authorizer/server/db/providers/arangodb/provider.go

114 lines
3.3 KiB
Go
Raw Normal View History

2022-01-21 12:18:07 +05:30
package arangodb
import (
"context"
"github.com/arangodb/go-driver"
arangoDriver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/http"
"github.com/authorizerdev/authorizer/server/db/models"
2022-05-29 17:22:46 +05:30
"github.com/authorizerdev/authorizer/server/memorystore"
2022-01-21 12:18:07 +05:30
)
type provider struct {
db arangoDriver.Database
}
// for this we need arangodb instance up and running
// for local testing we can use dockerized version of it
// docker run -p 8529:8529 -e ARANGO_ROOT_PASSWORD=root arangodb/arangodb:3.8.4
// NewProvider to initialize arangodb connection
func NewProvider() (*provider, error) {
ctx := context.Background()
2022-05-31 08:14:03 +05:30
dbURL := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseURL
2022-01-21 12:18:07 +05:30
conn, err := http.NewConnection(http.ConnectionConfig{
2022-05-29 17:22:46 +05:30
Endpoints: []string{dbURL},
2022-01-21 12:18:07 +05:30
})
if err != nil {
return nil, err
}
arangoClient, err := arangoDriver.NewClient(arangoDriver.ClientConfig{
Connection: conn,
})
if err != nil {
return nil, err
}
var arangodb driver.Database
2022-05-31 08:14:03 +05:30
dbName := memorystore.RequiredEnvStoreObj.GetRequiredEnv().DatabaseName
2022-05-29 17:22:46 +05:30
arangodb_exists, err := arangoClient.DatabaseExists(nil, dbName)
2022-01-21 12:18:07 +05:30
if arangodb_exists {
2022-05-29 17:22:46 +05:30
arangodb, err = arangoClient.Database(nil, dbName)
2022-01-21 12:18:07 +05:30
if err != nil {
return nil, err
}
} else {
2022-05-29 17:22:46 +05:30
arangodb, err = arangoClient.CreateDatabase(nil, dbName, nil)
2022-01-21 12:18:07 +05:30
if err != nil {
return nil, err
}
}
userCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.User)
2022-05-13 00:47:01 +05:30
if !userCollectionExists {
2022-01-21 12:18:07 +05:30
_, err = arangodb.CreateCollection(ctx, models.Collections.User, nil)
if err != nil {
2022-05-13 00:47:01 +05:30
return nil, err
2022-01-21 12:18:07 +05:30
}
}
userCollection, _ := arangodb.Collection(nil, models.Collections.User)
userCollection.EnsureHashIndex(ctx, []string{"email"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
userCollection.EnsureHashIndex(ctx, []string{"phone_number"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
verificationRequestCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.VerificationRequest)
2022-05-13 00:47:01 +05:30
if !verificationRequestCollectionExists {
2022-01-21 12:18:07 +05:30
_, err = arangodb.CreateCollection(ctx, models.Collections.VerificationRequest, nil)
if err != nil {
2022-05-13 00:47:01 +05:30
return nil, err
2022-01-21 12:18:07 +05:30
}
}
verificationRequestCollection, _ := arangodb.Collection(nil, models.Collections.VerificationRequest)
verificationRequestCollection.EnsureHashIndex(ctx, []string{"email", "identifier"}, &arangoDriver.EnsureHashIndexOptions{
Unique: true,
Sparse: true,
})
verificationRequestCollection.EnsureHashIndex(ctx, []string{"token"}, &arangoDriver.EnsureHashIndexOptions{
Sparse: true,
})
sessionCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.Session)
2022-05-13 00:47:01 +05:30
if !sessionCollectionExists {
2022-01-21 12:18:07 +05:30
_, err = arangodb.CreateCollection(ctx, models.Collections.Session, nil)
if err != nil {
2022-05-13 00:47:01 +05:30
return nil, err
2022-01-21 12:18:07 +05:30
}
}
sessionCollection, _ := arangodb.Collection(nil, models.Collections.Session)
sessionCollection.EnsureHashIndex(ctx, []string{"user_id"}, &arangoDriver.EnsureHashIndexOptions{
Sparse: true,
})
configCollectionExists, err := arangodb.CollectionExists(ctx, models.Collections.Env)
2022-05-13 00:47:01 +05:30
if !configCollectionExists {
2022-01-21 12:18:07 +05:30
_, err = arangodb.CreateCollection(ctx, models.Collections.Env, nil)
if err != nil {
2022-05-13 00:47:01 +05:30
return nil, err
2022-01-21 12:18:07 +05:30
}
}
return &provider{
db: arangodb,
}, err
}