Skip to content

Commit

Permalink
Update database
Browse files Browse the repository at this point in the history
Database now gets a gorm.Dialector instead of an interface
  • Loading branch information
Cypaaa authored Dec 2, 2023
1 parent 282fc84 commit 17e1049
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@ import (
"gorm.io/gorm"
)

// Driver interface, required to create a new database connection
// could be mysql, postgres, sqlite, etc... Or even custom ones
type Driver interface {
New() gorm.Dialector
Open(dns string) gorm.Dialector
}

// Database struct
type Database struct {
conn *gorm.DB
dns string
driver Driver
conn *gorm.DB
dialector gorm.Dialector
}

// NewDatabase creates a new database connection
func NewDatabase(driver Driver, dns string) (*Database, error) {
func NewDatabase(dialector gorm.Dialector) (*Database, error) {
db := &Database{
dns: dns,
driver: driver,
dialector: dialector,
}
err := db.Init()
return db, err
Expand All @@ -35,7 +26,7 @@ func (d *Database) Conn() *gorm.DB {

// Init initializes the database connection
func (d *Database) Init() error {
conn, err := gorm.Open(d.driver.Open(d.dns), &gorm.Config{})
conn, err := gorm.Open(d.dialector, &gorm.Config{})
if err != nil {
return err
}
Expand Down

0 comments on commit 17e1049

Please sign in to comment.