A tiny module for lazy me, to connect database.
This module uses a predefined set of environment variables to connect databases using database/sql
.
Just call Connect()
or ConnectX()
to establish database connections.
import (
"database/sql"
"os"
"github.com/eidng8/go-db"
"<your-package>/ent"
)
func getEntClient() *ent.Client {
return ent.NewClient(ent.Driver(entsql.OpenDB(db.ConnectX())))
}
REQUIRED and cannot be empty. Determines what kind of database to connect. Can be any driver supported by database/sql
, such as mysql
, sqlite3
, pgx
, etc. Remember to import proper driver module to your package.
REQUIRED for connections other than MySQL. A complete DSN string to be used to establish connection to database. When set, this variable is passed directly to sql.Open()
. For MySQL, if this variable is not set, variables below will be used to configure the connection.
These variables are used to configure the connection to MySQL, if DB_DSN
is not set.
REQUIRED and cannot be empty. Determines the username to connect to database.
REQUIRED and cannot be empty. Determines the password to connect to database.
REQUIRED and cannot be empty. Determines the host to connect to.
REQUIRED and cannot be empty. Determines the database name to connect to.
OPTIONAL and defaults to tcp
.
OPTIONAL and defaults to utf8mb4_unicode_ci
.
OPTIONAL and defaults to UTC
.
When using Connect()
or ConnectX()
with environment variables, some configurations are also set:
mysql.Config{
// .....
AllowCleartextPasswords: false,
AllowFallbackToPlaintext: false,
AllowNativePasswords: true,
AllowOldPasswords: false,
MultiStatements: true,
ParseTime: true,
// .....
}
Besides using Connect()
or ConnectX()
, ConnectMysql()
can also be called with mysql.Config
pointer:
cfg := mysql.Config{
// You detailed configuration .....
}
ConnectMysql(cfg)
No matter which function is used to establish the connection, the following 3 settings are also called:
db.SetMaxIdleConns(10)
db.SetMaxOpenConns(100)
db.SetConnMaxLifetime(time.Hour)