Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InitDefault ¶
InitDefault opens MySQL and stores it as the dbgorm default database.
Example ¶
ExampleInitDefault shows the default-instance pattern. Call InitDefault once at application startup; dbgorm.Default() returns the shared *gorm.DB for the process. Unlike sync.Once, SetDefault can be called again if the connection needs to be replaced.
package main
import (
"fmt"
dbgorm "github.com/phcp-tech/common-library-golang/dbgorm"
"github.com/phcp-tech/common-library-golang/dbgorm/mysql"
)
func main() {
err := mysql.InitDefault(&mysql.Config{
Host: "127.0.0.1",
Port: "13307",
Database: "mydb",
Username: "user",
Password: "pass",
})
fmt.Println(err == nil)
fmt.Println(dbgorm.Default() != nil)
}
Output:
func NewMySQL ¶
NewMySQL opens a MySQL-backed GORM database.
Example ¶
ExampleNewMySQL shows how to open a MySQL GORM database with default pool settings. GORM's MySQL driver pings the server during Open — a non-nil error is returned when the server is unreachable.
package main
import (
"fmt"
"github.com/phcp-tech/common-library-golang/dbgorm/mysql"
)
func main() {
_, err := mysql.NewMySQL(&mysql.Config{
Host: "127.0.0.1",
Port: "13307",
Database: "mydb",
Username: "user",
Password: "pass",
})
fmt.Println(err != nil) // true — server unreachable, GORM pings on Open
}
Output: true
Types ¶
type Config ¶
type Config struct {
// Required connection settings.
Host string
Port string
Database string
Username string
Password string
// gorm.Config fields for connection pool settings.
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime int
ConnMaxIdletime int
Logger *slog.Logger
}
Config contains MySQL connection settings.
Click to show internal directories.
Click to hide internal directories.