pyconfigs/connections.py file allows you to define database connections for your Squirrels project using Python. This provides greater flexibility than the YAML-based configuration in squirrels.yml, enabling dynamic connection strings, conditional logic, and access to resource files if needed.
Connections can also be defined in the squirrels.yml file. Connections defined in
squirrels.yml are loaded first, then connections.py runs (if exists) and can add or override connections.File structure
Theconnections.py file must define a main function with the following signature:
pyconfigs/connections.py
The ConnectionsArgs object
Thesqrl argument is a ConnectionsArgs object that provides useful properties for building connection strings dynamically.
| Property | Type | Description |
|---|---|---|
project_path | str | Absolute path to the Squirrels project directory |
proj_vars | dict[str, Any] | Project variables from squirrels.yml |
env_vars | dict[str, str] | Environment variables from .env files and system |
Creating connections
Add connections by assigning ConnectionProperties objects to theconnections dictionary:
pyconfigs/connections.py
You can also store other values in the
connections dictionary that are not ConnectionProperties objects, as long as the key is not used as the database connection name for data models or data source parameters.This can be useful for storing artifacts (such as ML models or API clients) in memory at server startup time. These can then be accessed in the context.py file or Python data models.Connection types
Squirrels supports four connection types via ConnectionTypeEnum:| Type | Description | Placeholder support |
|---|---|---|
SQLALCHEMY | Standard SQLAlchemy connections (default) | Yes (:param_name) |
DUCKDB | Native DuckDB connections | Yes ($param_name) |
CONNECTORX | High-performance bulk data loading | No |
ADBC | Arrow Database Connectivity | No |
Examples
SQLite connection
pyconfigs/connections.py
PostgreSQL connection
pyconfigs/connections.py
DuckDB connection
pyconfigs/connections.py
Multiple connections with environment-based logic
pyconfigs/connections.py
High-performance connection with ConnectorX
pyconfigs/connections.py
Custom connection objects
You can also add custom connection objects (such as pre-configured database clients) to the dictionary:pyconfigs/connections.py
Related pages
- ConnectionsArgs - Properties available in the
sqrlobject argument - ConnectionProperties - Full reference for connection properties
- ConnectionTypeEnum - Available connection types for ConnectionProperties
- squirrels.yml connections - YAML-based connection configuration