Skip to content

Latest commit

 

History

History
66 lines (56 loc) · 1.39 KB

README_OPEN.md

File metadata and controls

66 lines (56 loc) · 1.39 KB

Connecting

Go Reference

dbw has tested official support for SQLite and Postgres. You can also use it to connect to any database that has a Gorm V2 driver which has official support for: SQLite, Postgres, MySQL and SQL Server.

SQLite

import(
    "github.com/hashicorp/go-dbw"
)

func main() {
    db, err := dbw.Open(dbw.Sqlite, "dbw.db")    
}

Postgres

import(
    "github.com/hashicorp/go-dbw"
)

func main() {
    dsn := "postgresql://go_db:go_db@localhost:9920/go_db?sslmode=disable"
    db, err := dbw.Open(dbw.Postgres, dsn)    
}

Any gorm v2 driver or an existing connection

import(
    "database/sql"
    "github.com/hashicorp/go-dbw"
    "gorm.io/gorm"
)

func main() {
    dsn := "postgresql://go_db:go_db@localhost:9920/go_db?sslmode=disable"
    sqlDB, err := sql.Open("mysql", dsn)
    db, err := dbw.OpenWith(mysql.New(mysql.Config{
        Conn: sqlDB,
    }))
}

Connection Pooling

import(
    "github.com/hashicorp/go-dbw"
)

func main() {
    dsn := "postgresql://go_db:go_db@localhost:9920/go_db?sslmode=disable"
    db, err := dbw.Open(dbw.Postgres, dsn, 
        dbw.WithMaxConnections(20),
        dbw.WithMinConnections(2),
    )    
    sqlDB, err = db.SqlDB()
    sqlDB.SetConnMaxLifetime(time.Hour)
    sqlDB.SetMaxIdleConns(10)
}