package orm import ( "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "gorm.io/gorm/logger" "time" ) type MysqlConfig struct { Dsn string MaxIdleConns int MaxOpenConns int } func NewMysql(mysqlConf *MysqlConfig, logwriter logger.Writer) *gorm.DB { db, err := gorm.Open(mysql.New(mysql.Config{ DSN: mysqlConf.Dsn, // DSN data source name DefaultStringSize: 256, // default length for string type fields //DisableDatetimePrecision: true, // disable datetime precision; not supported by databases before MySQL 5.6 //DontSupportRenameIndex: true, // rename indexes by dropping and recreating them; databases before MySQL 5.7 and MariaDB do not support renaming indexes //DontSupportRenameColumn: true, // rename columns using `change`; databases before MySQL 8 and MariaDB do not support renaming columns //SkipInitializeWithVersion: false, // auto-configure based on the current MySQL version }), &gorm.Config{ DisableForeignKeyConstraintWhenMigrating: true, Logger: logger.New( logwriter, // io writer logger.Config{ SlowThreshold: time.Second, // Slow SQL threshold LogLevel: logger.Warn, // Log level IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger ParameterizedQueries: true, // Don't include params in the SQL log Colorful: true, }, ), }) if err != nil { fmt.Println(err) } sqlDB, err2 := db.DB() if err2 != nil { fmt.Println(err2) } // SetMaxIdleConns sets the maximum number of connections in the idle connection pool sqlDB.SetMaxIdleConns(mysqlConf.MaxIdleConns) // SetMaxOpenConns sets the maximum number of open database connections. sqlDB.SetMaxOpenConns(mysqlConf.MaxOpenConns) return db }