Skip to content

Commit 5879591

Browse files
pquernaggreer
andauthored
Improve DSN env vairable expansion (#92)
* Improve DSN env vairable expansion * Add failing test case. * improve query param handling * add new config syntax --------- Co-authored-by: Geoff Greer <geoff@greer.fm>
1 parent 6b042da commit 5879591

File tree

5 files changed

+1006
-31
lines changed

5 files changed

+1006
-31
lines changed

examples/example.yml

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,27 @@ app_name: Example Application
1010
# Connection Configuration
1111
# ----------------------
1212
# Specifies how to connect to the data source. Supports various connection methods.
13+
#
14+
# RECOMMENDED: Use structured fields!
1315
connect:
14-
# Database connection string (DSN) with environment variable interpolation
15-
dsn: "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:3306/${DB_NAME}?parseTime=true"
16-
# If your database username or password includes characters that require URL encoding,
17-
# you can specify them as separate options instead of embedding them directly in the DSN.
18-
# Environment variables are expanded.
19-
# For example, you might include:
20-
# username: my_username
21-
# password: my_secure_password
16+
scheme: "mysql"
17+
host: "${DB_HOST}"
18+
port: "3306"
19+
database: "${DB_NAME}"
20+
user: "${DB_USER}"
21+
password: "${DB_PASS}"
22+
params:
23+
parseTime: "true"
2224
#
23-
# This allows the connector to handle proper URL encoding during DSN construction.
25+
# ALTERNATIVE: Use DSN with separate user/password fields
26+
# connect:
27+
# dsn: "mysql://${DB_HOST}:3306/${DB_NAME}?parseTime=true"
28+
# user: "${DB_USER}"
29+
# password: "${DB_PASS}"
30+
#
31+
# LEGACY: Complete DSN (not recommended if credentials contain special characters)
32+
# connect:
33+
# dsn: "mysql://${DB_USER}:${DB_PASS}@${DB_HOST}:3306/${DB_NAME}?parseTime=true"
2434

2535
# Resource Types
2636
# -------------

pkg/bsql/config.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,36 @@ func (c Config) HasActions() bool {
3636
}
3737

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

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

46-
// User is the database username used for authentication.
49+
// Scheme is the database type (e.g., "postgres", "mysql", "sqlserver", "oracle", "hdb")
50+
Scheme string `yaml:"scheme" json:"scheme"`
51+
52+
// Host is the database server hostname or IP address (may include port for some databases)
53+
Host string `yaml:"host" json:"host"`
54+
55+
// Port is the database server port number
56+
Port string `yaml:"port" json:"port"`
57+
58+
// Database is the name of the database to connect to
59+
Database string `yaml:"database" json:"database"`
60+
61+
// User is the database username used for authentication
4762
User string `yaml:"user" json:"user"`
4863

49-
// Password is the database password used for authentication.
64+
// Password is the database password used for authentication
5065
Password string `yaml:"password" json:"password"`
66+
67+
// Params contains additional connection parameters (e.g., {"sslmode": "disable", "timeout": "30s"})
68+
Params map[string]string `yaml:"params" json:"params"`
5169
}
5270

5371
// ResourceType defines configuration for a specific type of resource.

pkg/connector/connector.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,18 @@ func New(ctx context.Context, configFilePath string) (*Connector, error) {
9494
}
9595

9696
func newConnector(ctx context.Context, c *bsql.Config) (*Connector, error) {
97-
db, dbEngine, err := database.Connect(ctx, c.Connect.DSN, c.Connect.User, c.Connect.Password)
97+
opts := database.ConnectOptions{
98+
DSN: c.Connect.DSN,
99+
Scheme: c.Connect.Scheme,
100+
Host: c.Connect.Host,
101+
Port: c.Connect.Port,
102+
Database: c.Connect.Database,
103+
User: c.Connect.User,
104+
Password: c.Connect.Password,
105+
Params: c.Connect.Params,
106+
}
107+
108+
db, dbEngine, err := database.Connect(ctx, opts)
98109
if err != nil {
99110
return nil, err
100111
}

0 commit comments

Comments
 (0)