Skip to content

Commit

Permalink
v0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Lindfalk committed May 20, 2022
1 parent 9912f5e commit 1c6cb30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Do also check out the [dentech-floss/pagination](https://github.com/dentech-flos
## Install

```
go get github.com/dentech-floss/[email protected].1
go get github.com/dentech-floss/[email protected].2
```

## Usage
Expand Down Expand Up @@ -43,14 +43,12 @@ func main() {

orm := orm.NewMySqlOrm(
&orm.OrmConfig{
OnGCP: true,
DbName: "clinic",
DbUser: "some_user",
DbPassword: "some_pwd",
DbHost: "some_host",
DbPort: 3306, // not mandatory to provide, will default to 3306 if not provided
// this is optional, it defaults to false but shall be set to true when on Cloud Run.
// See https://cloud.google.com/sql/docs/mysql/connect-run#go
UseUnixSocket: true,
DbPort: 3306, // not mandatory, will default to 3306 if not provided
},
)

Expand Down Expand Up @@ -113,7 +111,7 @@ func (r *sqlRepository) FindClinicById(ctx context.Context, clinicId int32) (*mo

### Migration

You can get hold of the [Migrator Interface](https://gorm.io/docs/migration.html#Migrator-Interface) and the [Auto Migration](https://gorm.io/docs/migration.html#Auto-Migration) like this in order to handle schema migrations:
You can get hold of the [Migrator Interface](https://gorm.io/docs/migration.html#Migrator-Interface) and for example it's [Auto Migration](https://gorm.io/docs/migration.html#Auto-Migration) like this in order to handle schema migrations:

```go
package example
Expand All @@ -126,14 +124,15 @@ func main() {

orm := orm.NewMySqlOrm(
&orm.OrmConfig{
OnGCP: true,
DbName: "clinic",
DbUser: "some_user",
DbPassword: "some_pwd",
DbHost: "some_host",
},
)

if err := orm.GetMigrator().AutoMigrate(&model.Clinic{}); err != nil {
if err := orm.Migrator().AutoMigrate(&model.Clinic{}); err != nil {
panic(err)
}
}
Expand Down
23 changes: 8 additions & 15 deletions pkg/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ import (
)

var defaultDbPort = 3306
var defaultUseUnixSocket = false
var defaultMaxIdleConns = 100
var defaultMaxOpenConns = 100
var defaultConnMaxLifetimeMins = 15
var defaultMySqlLogger = logger.Discard.LogMode(logger.Silent) // rely on Opentelemetry
var defaultSQLiteLogger = logger.Default.LogMode(logger.Info)

type OrmConfig struct {
OnGCP bool
DbName string
DbUser string
DbPassword string
DbHost string
DbPort *int // defaults to 3306
UseUnixSocket *bool // defaults to false (set to true when on Cloud Run)
MaxIdleConns *int // default to 100
MaxOpenConns *int // default to 100
ConnMaxLifetimeMins *int // defaults to 15
DbPort *int // defaults to 3306
MaxIdleConns *int // default to 100
MaxOpenConns *int // default to 100
ConnMaxLifetimeMins *int // defaults to 15
Logger *logger.Interface
}

Expand All @@ -41,9 +40,6 @@ func (c *OrmConfig) setDefaults(
if c.DbPort == nil {
c.DbPort = &defaultDbPort
}
if c.UseUnixSocket == nil {
c.UseUnixSocket = &defaultUseUnixSocket
}
if c.MaxIdleConns == nil {
c.MaxIdleConns = &defaultMaxIdleConns
}
Expand Down Expand Up @@ -110,18 +106,15 @@ func newOrm(db *gorm.DB, config *OrmConfig) *Orm {
return &Orm{db}
}

func (o *Orm) GetMigrator() gorm.Migrator {
return o.Migrator()
}

// Create DB connection string based on the configuration given on creating the database object
func dsn(config *OrmConfig) string {
// When running on Cloud Run we need to connect using Unix Sockets.
// See https://cloud.google.com/sql/docs/mysql/connect-run#go
if *config.UseUnixSocket {
if config.OnGCP {
return unixDsn(config)
} else {
return tcpDsn(config)
}
return tcpDsn(config)
}

func unixDsn(config *OrmConfig) string {
Expand Down

0 comments on commit 1c6cb30

Please sign in to comment.