Advanced Guide
Dealing with SQL
GoFr simplifies the process of connecting to SQL databases where one needs to add respective configs in .env, which allows connecting to different SQL dialects(MySQL, PostgreSQL, SQLite) without going into complexity of configuring connections.
With GoFr, connecting to different SQL databases is as straightforward as setting the DB_DIALECT environment variable to the respective dialect.
If DB_DIALECT is not set, GoFr does not initialize SQL and ctx.SQL is nil, so an app can guard on ctx.SQL == nil. GoFr logs an error at startup when DB_DIALECT is missing but DB_HOST or DB_NAME is set, since that usually means the dialect was forgotten.
Usage for PostgreSQL and MySQL
To connect with PostgreSQL, set DB_DIALECT to postgres. Similarly, To connect with MySQL, simply set DB_DIALECT to mysql.
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=root123
DB_NAME=test_db
DB_PORT=3306
DB_DIALECT=postgres
Usage for SQLite
To connect with SQLite, set DB_DIALECT to sqlite and DB_NAME to the name of your DB File. If the DB file already exists then it will be used otherwise a new one will be created.
DB_NAME=test.db
DB_DIALECT=sqlite
Setting Max open and Idle Connections
To set max open and idle connection for any MySQL, PostgreSQL, SQLite. Add the following configs in .env file.
DB_MAX_IDLE_CONNECTION=5 // Default 2
DB_MAX_OPEN_CONNECTION=5 // Default unlimited
Check out the example on how to add configuration for SQL in GoFr: Visit GitHub
Keeping query arguments out of logs and traces
At LOG_LEVEL=DEBUG, GoFr logs every query together with its arguments, which can include personal data or secrets. Set DB_REDACT_ARGS=true to replace each argument with *****, so the log still shows how many arguments a query had but not their values:
DB_REDACT_ARGS=true // Default false
DB_REDACT_ARGS applies to the SQL datasource GoFr creates from config. A connection passed to NewSQLFromDB is not redacted.
The query text itself is still logged and traced, so pass sensitive values as arguments (? / $1) rather than building them into the query string. Trace spans carry only the query text, never its arguments.
Related production guides
- Connection Pooling: Tune
DB_MAX_OPEN_CONNECTIONand friends for production load — sizing pools to match traffic and DB capacity.