fin-check-api/db/db.go

57 lines
1.1 KiB
Go
Raw Normal View History

2024-08-01 23:02:55 +02:00
package db
import (
"errors"
"log"
"os"
"sync"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var udb *gorm.DB
var conMu sync.Mutex
var (
ERROR_DB_NOT_INIT = errors.New("Database connection is not initialized")
)
func Connect() *gorm.DB {
conMu.Lock()
defer conMu.Unlock()
if udb != nil {
return udb
}
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Error,
IgnoreRecordNotFoundError: true,
ParameterizedQueries: true,
Colorful: false,
},
)
gormDB, err := gorm.Open(sqlite.Open("gonuts.db"), &gorm.Config{
Logger: newLogger,
})
if err != nil {
log.Panic(err)
}
newUDB := gormDB
2024-08-03 07:05:46 +02:00
gormDB.AutoMigrate(&Card{})
2024-08-01 23:02:55 +02:00
gormDB.AutoMigrate(&Category{})
gormDB.AutoMigrate(&Item{})
gormDB.AutoMigrate(&ItemPrice{})
gormDB.AutoMigrate(&Payment{})
gormDB.AutoMigrate(&ItemBought{})
gormDB.AutoMigrate(&Income{})
gormDB.AutoMigrate(&Debt{})
2024-08-03 07:15:39 +02:00
gormDB.AutoMigrate(&Transfer{})
2024-08-02 22:37:05 +02:00
gormDB.AutoMigrate(&User{})
2024-08-01 23:02:55 +02:00
return newUDB
}