# README
mysql
Golang mysql package based on GORM V2.
Install
go get gitlab.com/go-ecosystem/mysql
Usage
DB
See more at gorm.
package examples
import (
"fmt"
"gitlab.com/go-ecosystem/mysql"
"gorm.io/gorm/logger"
)
type Product struct {
// mysql base model
mysql.Model
Code string
Price uint
}
func ExampleRegister() {
cnf := mysql.NewConfig("root",
"123456",
"127.0.0.1",
"3306",
"test",
"utf8mb4",
logger.Error)
// register default db
mysql.Register(cnf)
cnf2 := mysql.NewConfig("root",
"123456",
"127.0.0.1",
"3306",
"test2",
"utf8mb4",
logger.Error)
// register another db with key "test2"
mysql.RegisterByKey(cnf2, "test2")
defer mysql.Close()
// default db
db := mysql.GetDB()
// Migrate the schema
db.AutoMigrate(&Product{})
// Create
db.Create(&Product{Code: "L1212", Price: 1000})
// Read
var product Product
db.First(&product, "code = ?", "L1212") // find product with code l1212
// Update - update product's price to 2000
db.Model(&product).Update("Price", 2000)
// Delete - delete product
db.Delete(&product)
fmt.Println("default db finish")
// db with key "test2"
db2 := mysql.GetDBByKey("test2")
// Migrate the schema
db2.AutoMigrate(&Product{})
// Create
db2.Create(&Product{Code: "L1212", Price: 1000})
// Read
db2.First(&product, "code = ?", "L1212") // find product with code l1212
// Update - update product's price to 2000
db2.Model(&product).Update("Price", 2000)
// Delete - delete product
db2.Delete(&product)
fmt.Println("db2 finish")
// Output: default db finish
// db2 finish
}
MockDB
mock usage see more at https://github.com/DATA-DOG/go-sqlmock/blob/master/examples/orders/orders_test.go
_, mock := mysql.MockDB()
# Functions
Close closes current db connection.
Deregister deregister default examples.
DeregisterByKey deregister examples by key.
GetDB get default db.
GetDBByKey get by with key.
MockDB mock default DB.
MockDBByKey mock DB by key.
NewConfig new config with default value.
Register register default examples.
RegisterByKey register examples by key.
SetLogger replace default logger.
WithConnMaxLifetime new connMaxLifetime option.
WithMaxIdleConns new maxIdleConns option.
WithMaxOpenConns new maxOpenConns option.
WithSingularTable new singularTable option.
WithTablePrefix new tablePrefix option.
# Structs
Config mysql configuration.
ConnectionConfig connection configuration.
Model base model.
# Interfaces
ConfigOption config option.