Skip to content

Instantly share code, notes, and snippets.

@Ja7ad
Created March 15, 2021 05:35
Show Gist options
  • Save Ja7ad/eedaac2f0bedb1cadbd1514c621b92d0 to your computer and use it in GitHub Desktop.
Save Ja7ad/eedaac2f0bedb1cadbd1514c621b92d0 to your computer and use it in GitHub Desktop.
Gorm Singleton DBMS (SQLite, MySQL)
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