github.com/zooyer/miskit@v1.0.71/micro/gorm.go (about)

     1  package micro
     2  
     3  import (
     4  	"github.com/glebarez/sqlite"
     5  	"gorm.io/driver/mysql"
     6  	"gorm.io/gorm"
     7  	"gorm.io/gorm/schema"
     8  )
     9  
    10  func OpenDB(name, dsn string, config interface{}) (*gorm.DB, error) {
    11  	var dial gorm.Dialector
    12  
    13  	switch name {
    14  	case "sqlite", "sqlite3":
    15  		dial = sqlite.Open(dsn)
    16  	case "mysql":
    17  		if conf, ok := config.(mysql.Config); ok {
    18  			dial = mysql.New(conf)
    19  		} else {
    20  			dial = mysql.Open(dsn)
    21  		}
    22  	}
    23  
    24  	return gorm.Open(dial, &gorm.Config{
    25  		NamingStrategy: schema.NamingStrategy{
    26  			SingularTable: true,
    27  		},
    28  	})
    29  }