feat: ✨ add mysql driver
This commit is contained in:
parent
16b74bb80d
commit
5171ce7967
33
db/driver.go
33
db/driver.go
@ -2,7 +2,9 @@ package db
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"database/sql"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
_ "github.com/jackc/pgconn"
|
_ "github.com/jackc/pgconn"
|
||||||
_ "github.com/jackc/pgx/v5"
|
_ "github.com/jackc/pgx/v5"
|
||||||
@ -25,3 +27,34 @@ func NewPostgresPool(dataSource string) *pgxpool.Pool {
|
|||||||
slog.Info("connected to database")
|
slog.Info("connected to database")
|
||||||
return dbPool
|
return dbPool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const maxOpenDbConn = 10
|
||||||
|
const maxIdleDbConn = 5
|
||||||
|
const maxDbLifetime = time.Minute * 5
|
||||||
|
|
||||||
|
func NewMySQL(dataSource string) (*sql.DB, error) {
|
||||||
|
d, err := sql.Open("mysql", dataSource)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error connecting to database", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetMaxOpenConns(maxOpenDbConn)
|
||||||
|
d.SetMaxIdleConns(maxIdleDbConn)
|
||||||
|
d.SetConnMaxLifetime(maxDbLifetime)
|
||||||
|
|
||||||
|
err = testDB(d)
|
||||||
|
if err != nil {
|
||||||
|
slog.Error("error pinging database", "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return d, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDB(d *sql.DB) error {
|
||||||
|
err := d.Ping()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@ -19,3 +19,10 @@ func LogAndReturnError(err error, message string) error {
|
|||||||
slog.Error(message, "error", err.Error())
|
slog.Error(message, "error", err.Error())
|
||||||
return fmt.Errorf("%s: %w", message, err)
|
return fmt.Errorf("%s: %w", message, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetBoolFromString(s string) bool {
|
||||||
|
if s == "S" || s == "s" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user