Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/db.go
Julien Riou e7c18d048e
chore: Disable low-level database error messages
Signed-off-by: Julien Riou <julien@riou.xyz>
2022-09-02 08:49:19 +02:00

30 lines
834 B
Go

package main
import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var gconfig = &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
}
// NewDatabaseFromConfig creates a database object from configuration structure
func NewDatabaseFromConfig(config DatabaseConfig) (*gorm.DB, error) {
switch config.Type {
case "postgres":
return gorm.Open(postgres.New(postgres.Config{DSN: config.DSN}), gconfig)
case "mysql":
return gorm.Open(mysql.New(mysql.Config{DSN: config.DSN}), gconfig)
default:
return gorm.Open(sqlite.Open(config.DSN), gconfig)
}
}
// NewDatabaseFromFile creates a database object from path to file (passed as argument)
func NewDatabaseFromFile(path string) (*gorm.DB, error) {
return gorm.Open(sqlite.Open(path), gconfig)
}