feat: 添加 db(gorm) 支持
This commit is contained in:
48
database/db/new.go
Normal file
48
database/db/new.go
Normal file
@ -0,0 +1,48 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"github.com/glebarez/sqlite"
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var defaultSqlite = "data.db"
|
||||
|
||||
func New(opts ...OptionFn) (DB, error) {
|
||||
var (
|
||||
err error
|
||||
conf = &config{
|
||||
sqlite: &defaultSqlite,
|
||||
}
|
||||
tx *gorm.DB
|
||||
)
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(conf)
|
||||
}
|
||||
|
||||
if conf.mysql != nil {
|
||||
tx, err = gorm.Open(mysql.Open(*conf.mysql))
|
||||
goto CHECK
|
||||
}
|
||||
|
||||
if conf.pg != nil {
|
||||
tx, err = gorm.Open(postgres.Open(*conf.pg))
|
||||
goto CHECK
|
||||
}
|
||||
|
||||
tx, err = gorm.Open(sqlite.Open(*conf.sqlite))
|
||||
|
||||
CHECK:
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &db{tx: tx}, nil
|
||||
}
|
||||
|
||||
func Init(opts ...OptionFn) (err error) {
|
||||
Default, err = New(opts...)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user