fix database issues

This commit is contained in:
rogee
2023-02-07 00:14:30 +08:00
parent 47e8139f47
commit 032cfbc283
10 changed files with 129 additions and 102 deletions

View File

@@ -0,0 +1,33 @@
package database
import (
"atom/container"
"atom/providers/config"
"errors"
"log"
"gorm.io/gorm"
)
const (
DriverMySQL = "mysql"
DriverSQLite = "sqlite"
DriverPostgres = "postgres"
DriverSQLServer = "sqlserver"
)
func init() {
if err := container.Container.Provide(NewDatabase); err != nil {
log.Fatal(err)
}
}
func NewDatabase(config *config.Config) (*gorm.DB, error) {
switch config.Database.Driver {
case DriverMySQL:
return NewMySQL(config.Database.MySQL)
case DriverSQLite:
return NewSQLite(config.Database.SQLite)
}
return nil, errors.New("failed to connect to db")
}