Configuration

Currently this project only support mysql driver.

Here is an example

package main

import (
"fmt"
"github.com/glitterlip/goeloquent"
)

var DB *goeloquent.DB

func init() {
DB = goeloquent.Open(map[string]goeloquent.DBConfig{
"default": {
Driver: "mysql",
Host: "127.0.0.1",
Port: "3306",
Database: "test",
Username: "root",
Password: "root",
Charset: "utf8mb4",
},
})
//or a dsn
DB.AddConfig("test", &goeloquent.DBConfig{
Driver: "mysql",
Dsn: "username:password@tcp(127.0.0.1:3306)/database",
})
DB.SetLogger(func(log goeloquent.Log) {
fmt.Println(log)
})
}
func main() {
m := make(map[string]interface{})
_, err := DB.Table("users").First(&m)
if err != nil {
fmt.Println(err)
}
fmt.Println(m)
}

A config with name default is required.

For Debug, you can use SetLogger func to trace sql.

goeloquent.Eloquent.SetLogger(func(log goeloquent.Log) {
fmt.Println(log)
})

Other configurations

More details see here

type DBConfig struct {
Driver string
//ReadHost []string
//WriteHost []string
Host string
Port string
Database string
Username string
Password string
Charset string
Prefix string //table prefix
ConnMaxLifetime int
ConnMaxIdleTime int
MaxIdleConns int
MaxOpenConns int
ParseTime bool //parseTime=true changes the output type of DATE and DATETIME values to time.Time instead of []byte / string The date or datetime like 0000-00-00 00:00:00 is converted into zero value of time.Time.
// mysql
Collation string
UnixSocket string
MultiStatements bool
Dsn string //if Dsn is set, we will use `sql.Open("mysql", Dsn)`
// pgsql
Sslmode string //not supported yet
TLS string //not supported yet
EnableLog bool //log query
}