Implementation:173TECH Sayn DatabaseCreator
| Knowledge Sources | |
|---|---|
| Domains | Database, Factory_Pattern |
| Last Updated | 2026-10-17 00:00 GMT |
Overview
Concrete tool for creating database connection instances from credential configurations using a factory pattern that maps database types to adapter classes.
Description
The create function in sayn/database/creator.py is the factory for Database instances. It:
- Reads the
typefield from the credential settings - Looks up the corresponding adapter class in the
driversmapping - Extracts common parameters (
max_batch_rows,type) - Instantiates the adapter with connection name, settings, and common params
The create_dummy function creates an UnknownDb placeholder for missing credentials, allowing configuration to proceed even when a credential is not provided (useful for compile-only runs).
Supported database types:
postgresql→Postgresql(with COPY bulk loading)sqlite→Sqlite(for local/testing)mysql→Mysqlsnowflake→Snowflake(with bulk loading)redshift→Redshift(with S3 loading)bigquery→Bigquery(with native loading)
Usage
Used internally by get_connections() in sayn/core/settings.py to create Database instances from resolved credentials.
Code Reference
Source Location
- Repository: SAYN
- File:
sayn/database/creator.py - Lines: L1-42 (full module)
Signature
drivers = {
"postgresql": Postgresql,
"sqlite": Sqlite,
"mysql": Mysql,
"snowflake": Snowflake,
"redshift": Redshift,
"bigquery": Bigquery,
}
db_params = ("max_batch_rows", "type")
def create(name: str, name_in_settings: str, settings: dict) -> Database
def create_dummy(name: str) -> UnknownDb
Import
from sayn.database.creator import create, create_dummy
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | str | Yes Connection name from project.yaml required_credentials
| |
| name_in_settings | str | Yes Credential name from settings.yaml | |
| settings | dict | Yes Connection-specific parameters with type key
|
Outputs
| Name | Type | Description |
|---|---|---|
| create returns | Database Database adapter instance | |
| create_dummy returns | UnknownDb Placeholder for missing credentials |
Usage Examples
Creating Database Connections
from sayn.database.creator import create, create_dummy
# Create a PostgreSQL connection
pg_db = create(
name="warehouse",
name_in_settings="warehouse",
settings={
"type": "postgresql",
"host": "localhost",
"port": 5432,
"database": "analytics",
"user": "etl_user",
"password": "secret",
"max_batch_rows": 10000,
},
)
# pg_db is a Postgresql instance
# Create a SQLite connection
sqlite_db = create(
name="local",
name_in_settings="local",
settings={
"type": "sqlite",
"database": "./data.db",
},
)
# Create a dummy for missing credentials (compile-only)
dummy = create_dummy("missing_connection")
# dummy is an UnknownDb instance
Internal Usage by get_connections
# In sayn/core/settings.py
def get_connections(credentials):
out = dict()
for name, config in credentials.items():
if config is None:
out[name] = create_dummy(name)
elif config["type"] == "api":
out[name] = {k: v for k, v in config.items() if k != "type"}
else:
out[name] = create_db(name, name, deepcopy(config))
return Ok(out)