Skip to content

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/go-minstack/mysql

Usage ​

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" ./myapp

Entity 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 ​

FunctionDescription
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 ​

VariableDescription
MINSTACK_DB_URLMySQL DSN, e.g. user:pass@tcp(host:3306)/db?parseTime=True