feat: initial commit

This commit is contained in:
Björn Benouarets
2025-10-18 21:02:31 +02:00
parent 44ce70090e
commit 692f8a29c9
11 changed files with 177 additions and 0 deletions

47
database/conn.go Normal file
View 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{})
}