first commit
This commit is contained in:
commit
3d986c4ee2
27 changed files with 597 additions and 0 deletions
39
helpers/database.go
Normal file
39
helpers/database.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package helpers
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"git.readonly.ch/bouzoure/popvaud-people/models"
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var database *gorm.DB
|
||||
var connected bool
|
||||
|
||||
func GetDatabase() (*gorm.DB, error) {
|
||||
if connected {
|
||||
return database, nil
|
||||
}
|
||||
|
||||
return connectDatabase()
|
||||
}
|
||||
|
||||
func connectDatabase() (*gorm.DB, error) {
|
||||
config, err := GetConfig()
|
||||
if err != nil {
|
||||
// TODO: Handle exception
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
database, err = gorm.Open(
|
||||
sqlite.Open(config.Database.Location),
|
||||
&gorm.Config{},
|
||||
)
|
||||
|
||||
database.AutoMigrate(&models.User{})
|
||||
|
||||
connected = true
|
||||
|
||||
return database, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue