Categorygithub.com/sk-pkg/mysql
modulepackage
1.1.3
Repository: https://github.com/sk-pkg/mysql.git
Documentation: pkg.go.dev

# README

Mysql

Mysql is a quick-use package encapsulated on top of GORM.

Installation

go get "github.com/sk-pkg/mysql"

Quick Start

import (
	"github.com/sk-pkg/mysql"
	"gorm.io/gorm"
	"log"
)

type Product struct {
	gorm.Model
	Code  string
	Price uint
}

func main() {
	cfg := mysql.Config{
		User:     "homestead",
		Password: "secret",
		Host:     "127.0.0.1:33060",
		DBName:   "mysql_test",
	}

	db, err := mysql.New(mysql.WithConfigs(cfg))
	if err != nil {
		log.Fatal("failed to connect database")
	}

	// Migrate schema
	db.AutoMigrate(&Product{})

	// Create
	db.Create(&Product{Code: "D42", Price: 100})

	// Read
	var product Product
	db.First(&product, 1)                 // Find by integer primary key
	db.First(&product, "code = ?", "D42") // Find record where code field value is D42

	// Update - Set product's price to 200
	db.Model(&product).Update("Price", 200)
	// Update - Update multiple fields
	db.Model(&product).Updates(Product{Price: 200, Code: "F42"}) // Only update non-zero value fields
	db.Model(&product).Updates(map[string]interface{}{"Price": 200, "Code": "F42"})

	// Delete - Delete product
	db.Delete(&product, 1)
}

Configurable Options

  1. Configs: Database connection configuration. If you want to create multiple instances, fill in multiple configurations. This is required.
mysql.WithConfigs(mysql.Config{
    User:     "homestead",
    Password: "secret",
    Host:     "127.0.0.1:33060",
    DBName:   "mysql_test",
})

Multiple databases:

mysql.WithConfigs(config1, config2, config3)
  1. gormConfig: You can customize GORM configuration. This is optional. Refer to GORM Configuration.
mysql.WithGormConfig(gorm.Config{})
  1. maxIdleConn: The maximum number of connections in the idle connection pool, with a default value of 10. This is optional.
mysql.WithMaxIdleConn(10)
  1. maxOpenConn: The maximum number of open database connections, with a default value of 50. This is optional.
mysql.WithMaxOpenConn(50)
  1. connMaxLifetime: The maximum lifetime of a reusable connection, with a default value of 3 hours. This is optional.
mysql.WithConnMaxLifetime(3 * time.Hour)

Available Methods

  1. New: Initialize a database connection instance.
cfg := mysql.Config{
		User:     "homestead",
		Password: "secret",
		Host:     "127.0.0.1:33060",
		DBName:   "mysql_test",
	}

	db, err := mysql.New(mysql.WithConfigs(cfg))
	if err != nil {
		log.Fatal("failed to connect database")
	}
  1. NewMulti: Batch initialize database connection instances. It will return a collection with database names as keys and *gorm.DB as values.
cfg1 := Config{
	User:     "homestead",
	Password: "secret",
	Host:     "127.0.0.1:33060",
	DBName:   "mysql_test1",
}

cfg2 := Config{
	User:     "homestead",
	Password: "secret",
	Host:     "127.0.0.1:33060",
	DBName:   "mysql_test2",
}
    
dbs, err := NewMulti(WithConfigs(cfg1, cfg2))
if err != nil {
	t.Fatal("failed to connect database", err)
}
        
mysql1 := dbs["mysql_test1"]
mysql2 := dbs["mysql_test2"]

Reference Document

GORM

# Packages

No description provided by the author

# Functions

New initializes and returns a single database connection instance.
NewLog creates and returns a new logger instance with the given options.
NewMulti initializes and returns multiple database connection instances.
WithConfigs returns an Option that sets the database configurations.
WithConnMaxLifetime returns an Option that sets the maximum lifetime of connections.
WithGormConfig returns an Option that sets the GORM configuration.
WithIgnoreRecordNotFoundError returns a LoggerOption that sets whether to ignore "record not found" errors.
WithLevel returns a LoggerOption that sets the log level for the logger.
WithMaxIdleConn returns an Option that sets the maximum number of idle connections.
WithMaxOpenConn returns an Option that sets the maximum number of open connections.
WithSlowThreshold returns a LoggerOption that sets the threshold for slow query logging.

# Structs

Config represents the configuration for a MySQL database connection.

# Type aliases

LoggerOption is a function type used to configure the logger.
Option is a function type used to apply configuration options.