Skip to content

Commit 48d6992

Browse files
Add ConnectDB() and MustConnectDB() functions to pgutils (#15)
1 parent c171596 commit 48d6992

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

pgutils/connector.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,23 @@ func OpenDB(conn *PostgresqlConnector) *sqlx.DB {
192192
sqlDB := sql.OpenDB(conn)
193193
return sqlx.NewDb(sqlDB, "postgres")
194194
}
195+
196+
// ConnectDB opens a connection using the connector and verifies it with a ping
197+
func ConnectDB(conn *PostgresqlConnector) (*sqlx.DB, error) {
198+
db := OpenDB(conn)
199+
if err := db.Ping(); err != nil {
200+
db.Close()
201+
return nil, err
202+
}
203+
return db, nil
204+
}
205+
206+
// MustConnectDB is like ConnectDB but panics on error
207+
func MustConnectDB(conn *PostgresqlConnector) *sqlx.DB {
208+
db, err := ConnectDB(conn)
209+
if err != nil {
210+
panic(err)
211+
}
212+
return db
213+
}
214+

0 commit comments

Comments
 (0)