brunel/db/db.go

43 lines
1.0 KiB
Go
Raw Normal View History

2024-07-28 20:59:50 +02:00
package db
import (
"brunel/domain"
"log"
"os"
"time"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func New() (*gorm.DB, error) {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Silent, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
ParameterizedQueries: true, // Don't include params in the SQL log
Colorful: true, // Disable color
},
)
db, err := gorm.Open(sqlite.Open("pikabldr.db"), &gorm.Config{
SkipDefaultTransaction: true,
CreateBatchSize: 1999,
Logger: newLogger,
})
if err != nil {
panic("failed to connect database")
}
db.AutoMigrate(&domain.User{})
db.AutoMigrate(&domain.Session{})
db.AutoMigrate(&domain.PackageInfo{})
db.AutoMigrate(&domain.TimeContainer{})
db.AutoMigrate(&domain.SourcePackageDTO{})
return db, nil
}