feat: initial commit
This commit is contained in:
47
database/conn.go
Normal file
47
database/conn.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/cluequest/go-sdk/config"
|
||||
"git.secnex.io/cluequest/go-sdk/models"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
var Conn Connection
|
||||
|
||||
func NewConnection(cfg config.DatabaseConfiguration) error {
|
||||
dsn := fmt.Sprintf(
|
||||
"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s",
|
||||
cfg.Host, cfg.User, cfg.Pass, cfg.DB, cfg.Port, cfg.SSL,
|
||||
)
|
||||
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("✅ Connection to database established!")
|
||||
Conn = Connection{DB: db}
|
||||
|
||||
// AutoMigration()
|
||||
err = Conn.AutoMigrate(&models.User{}, &models.Quiz{}, &models.Organization{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewConnectionFromEnv() error {
|
||||
return NewConnection(config.NewDatabaseConfigurationFromEnv())
|
||||
}
|
||||
|
||||
func AutoMigration() error {
|
||||
return Conn.AutoMigrate(&models.User{}, &models.Quiz{}, &models.Organization{})
|
||||
}
|
||||
Reference in New Issue
Block a user