Created
March 15, 2021 05:35
-
-
Save Ja7ad/eedaac2f0bedb1cadbd1514c621b92d0 to your computer and use it in GitHub Desktop.
Gorm Singleton DBMS (SQLite, MySQL)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package db | |
import ( | |
"gorm.io/driver/mysql" | |
"gorm.io/driver/sqlite" | |
"gorm.io/gorm" | |
"os" | |
) | |
var DB *gorm.DB | |
var err error | |
func SqliteConnector() error { | |
DB, err = gorm.Open(sqlite.Open(os.Getenv("SQLITEPATH")), &gorm.Config{}) | |
if err != nil { | |
return err | |
} | |
// Migrate data to table | |
if err := DB.AutoMigrate(&Books{}); err != nil { | |
return err | |
} | |
return nil | |
} | |
func MysqlConnector() error { | |
DB, err = gorm.Open(mysql.New(mysql.Config{ | |
DSN: os.Getenv("MYSQLCONFIG"), | |
DefaultStringSize: 256, | |
DisableDatetimePrecision: true, | |
DontSupportRenameIndex: true, | |
DontSupportRenameColumn: true, | |
SkipInitializeWithVersion: false, | |
}), &gorm.Config{}) | |
if err != nil { | |
return err | |
} | |
// Migrate data to table | |
if err := DB.AutoMigrate(&Books{}); err != nil { | |
return err | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment