Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions examples/example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ app_name: Example Application
# Connection Configuration
# ----------------------
# Specifies how to connect to the data source. Supports various connection methods.
#
# RECOMMENDED: Use structured fields!
connect:
# Database connection string (DSN) with environment variable interpolation
dsn: "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:3306/${DB_NAME}?parseTime=true"
# If your database username or password includes characters that require URL encoding,
# you can specify them as separate options instead of embedding them directly in the DSN.
# Environment variables are expanded.
# For example, you might include:
# username: my_username
# password: my_secure_password
scheme: "mysql"
host: "${DB_HOST}"
port: "3306"
database: "${DB_NAME}"
user: "${DB_USER}"
password: "${DB_PASS}"
params:
parseTime: "true"
#
# This allows the connector to handle proper URL encoding during DSN construction.
# ALTERNATIVE: Use DSN with separate user/password fields
# connect:
# dsn: "mysql://${DB_HOST}:3306/${DB_NAME}?parseTime=true"
# user: "${DB_USER}"
# password: "${DB_PASS}"
#
# LEGACY: Complete DSN (not recommended if credentials contain special characters)
# connect:
# dsn: "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:3306/${DB_NAME}?parseTime=true"

# Resource Types
# -------------
Expand Down
28 changes: 23 additions & 5 deletions pkg/bsql/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,36 @@ func (c Config) HasActions() bool {
}

// DatabaseConfig contains settings required to connect to the database.
// You can specify either a complete DSN, or use structured fields, or a combination.
// Structured fields override corresponding parts of the DSN when both are provided.
type DatabaseConfig struct {
// DSN is the Database Source Name connection string used to establish the database connection.
// DSN is the Database Source Name connection string (optional if using structured fields).
// Supports environment variable expansion via ${VAR_NAME} syntax.
// Example: "postgres://${DB_HOST}:${DB_PORT}/${DB_DATABASE}?sslmode=disable"
DSN string `yaml:"dsn" json:"dsn"`

// These fields are not required if the DSN already includes the credentials.
// They should only be provided if the username or password contain characters that need URL encoding.
// Structured connection fields (optional, override DSN components when set)

// User is the database username used for authentication.
// Scheme is the database type (e.g., "postgres", "mysql", "sqlserver", "oracle", "hdb")
Scheme string `yaml:"scheme" json:"scheme"`

// Host is the database server hostname or IP address (may include port for some databases)
Host string `yaml:"host" json:"host"`

// Port is the database server port number
Port string `yaml:"port" json:"port"`

// Database is the name of the database to connect to
Database string `yaml:"database" json:"database"`

// User is the database username used for authentication
User string `yaml:"user" json:"user"`

// Password is the database password used for authentication.
// Password is the database password used for authentication
Password string `yaml:"password" json:"password"`

// Params contains additional connection parameters (e.g., {"sslmode": "disable", "timeout": "30s"})
Params map[string]string `yaml:"params" json:"params"`
}

// ResourceType defines configuration for a specific type of resource.
Expand Down
13 changes: 12 additions & 1 deletion pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,18 @@ func New(ctx context.Context, configFilePath string) (*Connector, error) {
}

func newConnector(ctx context.Context, c *bsql.Config) (*Connector, error) {
db, dbEngine, err := database.Connect(ctx, c.Connect.DSN, c.Connect.User, c.Connect.Password)
opts := database.ConnectOptions{
DSN: c.Connect.DSN,
Scheme: c.Connect.Scheme,
Host: c.Connect.Host,
Port: c.Connect.Port,
Database: c.Connect.Database,
User: c.Connect.User,
Password: c.Connect.Password,
Params: c.Connect.Params,
}

db, dbEngine, err := database.Connect(ctx, opts)
if err != nil {
return nil, err
}
Expand Down
Loading