Skip to content

Commit

Permalink
feat: add possibility to enable sslmode for PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
arkadiuszos4chain committed Jan 30, 2024
1 parent 6d42561 commit 6af3a6b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
2 changes: 2 additions & 0 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
defaultPageSize = 20 // The default amount of results to return
defaultPostgreSQLHost = "localhost" // Default host for PostgreSQL
defaultPostgreSQLPort = "5432" // Default port for PostgreSQL
defaultPostgreSQLSslMode = "disable" // Default sslmode for PostgreSQL
defaultSQLiteFileName = "datastore.db" // Default database filename
defaultSQLiteSharing = true // Default value for "sharing" in loading a SQLite database
defaultTablePrefix = "x" // Default database prefix for table names (x_model)
Expand Down Expand Up @@ -93,6 +94,7 @@ type SQLConfig struct {
TimeZone string `json:"time_zone" mapstructure:"time_zone"` // timezone (IE: Asia/Shanghai)
TxTimeout time.Duration `json:"tx_timeout" mapstructure:"tx_timeout"` // 5*time.Second
User string `json:"user" mapstructure:"user"` // database username
SslMode string `json:"ssl_mode" mapstructure:"ssl_mode"` // ssl mode (for PostgreSQL) [disable|allow|prefer|require|verify-ca|verify-full]
}

// SQLiteConfig is the configuration for each SQLite connection
Expand Down
15 changes: 7 additions & 8 deletions sql.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datastore

import (
"fmt"
"log"
"os"
"time"
Expand Down Expand Up @@ -227,21 +228,16 @@ func postgreSQLDialector(config *SQLConfig) gorm.Dialector {
// Create the default PostgreSQL configuration
cfg := postgres.Config{
// DriverName: "nrpgx",
// todo: make all params customizable via config
DSN: "host=" + config.Host +
" user=" + config.User +
" password=" + config.Password +
" dbname=" + config.Name +
" port=" + config.Port +
" sslmode=disable TimeZone=" + config.TimeZone,
PreferSimpleProtocol: true, // turn to TRUE to disable implicit prepared statement usage
WithoutReturning: false,
}

// Do we have an existing connection
if config.ExistingConnection != nil {
cfg.DSN = ""
cfg.Conn = config.ExistingConnection
} else {
cfg.DSN = fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s",
config.Host, config.User, config.Password, config.Name, config.Port, config.SslMode, config.TimeZone)
}

return postgres.New(cfg)
Expand Down Expand Up @@ -405,5 +401,8 @@ func (s *SQLConfig) sqlDefaults(engine Engine) *SQLConfig {
if len(s.TimeZone) == 0 {
s.TimeZone = defaultTimeZone
}
if len(s.SslMode) == 0 {
s.SslMode = defaultPostgreSQLSslMode
}
return s
}

0 comments on commit 6af3a6b

Please sign in to comment.