mysql
MySQL module for MinStack. Provides a GORM *gorm.DB and a binary(16) UUID type optimized for MySQL.
Installation
sh
go get github.com/go-minstack/mysqlUsage
go
func main() {
app := core.New(cli.Module(), mysql.Module())
app.Provide(NewApp)
app.Run()
}sh
MINSTACK_DB_URL="user:pass@tcp(localhost:3306)/dbname?parseTime=True" ./myappEntity models
gorm.Model — default
The standard GORM base model. Uses uint as the primary key and works with all drivers.
go
type User struct {
gorm.Model // ID uint, CreatedAt, UpdatedAt, DeletedAt
Name string
Email string
}mysql.UuidModel — optional
An alternative base model that stores UUIDs as binary(16) — efficient and index-friendly. The ID is auto-generated in Go via BeforeCreate.
go
type User struct {
mysql.UuidModel // ID mysql.UUID, CreatedAt, UpdatedAt, DeletedAt
Name string
Email string
}UUID type
MySQL doesn't have a native UUID column type. MinStack provides mysql.UUID which stores UUIDs as binary(16) — efficient and index-friendly.
go
type User struct {
ID mysql.UUID `gorm:"primaryKey"`
Name string
}
user := User{ID: mysql.NewUUID(), Name: "MinStack"}API
mysql.Module() fx.Option
Registers *gorm.DB into the DI container. Reads MINSTACK_DB_URL from the environment.
UUID
| Function | Description |
|---|---|
mysql.NewUUID() | Generate a new random UUID |
mysql.ParseUUID(s) | Parse a UUID string |
mysql.MustParseUUID(s) | Parse or panic |
(UUID).String() | Format as standard UUID string |
(UUID).IsZero() | Check if zero value |
Environment variables
| Variable | Description |
|---|---|
MINSTACK_DB_URL | MySQL DSN, e.g. user:pass@tcp(host:3306)/db?parseTime=True |