Skip to content

Database configuration

config/database.py defines BootstrapDatabase as a frozen keyword-only dataclass extending Database. It reads Env.get(...) and constructs validated entities; this page documents the actual template.

A declared field does not imply runtime automatically applies it. Retained but unconsumed settings are explicitly identified. Configuration does not open connections or verify the server contains the requested database.

PropertyTypeEnvironmentDefaultEffect
defaultConnectionName | strDB_CONNECTIONsqliteDefault name normalized to lowercase; accepts only the six ConnectionName members.
connectionsConnections | dictNo single variableConnections(...) with six backendsConstructs and validates each entity. A dictionary becomes Connections(**connections).

An invalid default type raises TypeError; an unsupported name raises ValueError. For names such as reporting use provider registration, rather than DB_CONNECTION=reporting in this entity.

Fields are sqlite: SQLite | dict, mysql: MySQL | dict, pgsql: PGSQL | dict, oracle: Oracle | dict, sqlserver: SQLServer | dict and redshift: Redshift | dict. Omitted fields create new backend entities. Dictionaries are converted to their corresponding entity; other types raise TypeError. Unknown fields are not constructor arguments.

The template constructs all six entities even when only one is used. DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD and DB_PREFIX are shared by entries reading them. DB_CHARSET applies only to the active MySQL, PostgreSQL or SQL Server driver; other entries retain their default charset. Configure entities explicitly for distinct connections.

Property and typeVariableDefaultUsage and effect
driver: str—'sqlite'Fixed backend discriminator; a value differing from the entity raises ValueError.
url: str | NoneDB_URLNoneInformational SQLite URL. None derives sqlite:/// + database. Runtime ignores it and builds its URL from database.
database: strDB_DATABASE'database/database.sqlite'Server database name; file or :memory: for SQLite. Requires nonempty text.
prefix: strDB_PREFIX''Prefix for logical table names when compiling plans/schemas; does not modify raw SQL.
foreign_key_constraints: bool | str | SQLiteForeignKeyDB_FOREIGN_KEYS'OFF'ON/OFF, bool or SQLiteForeignKey; installs PRAGMA foreign_keys on every connection.
busy_timeout: int | NoneDB_BUSY_TIMEOUT5000PRAGMA busy_timeout milliseconds. Accepts non-bool int or None; runtime applies only values greater than zero.
journal_mode: str | SQLiteJournalModeDB_JOURNAL_MODE'DELETE'Normalized journal mode applied through PRAGMA journal_mode.
synchronous: str | SQLiteSynchronousDB_SYNCHRONOUS'NORMAL'Normalized level applied through PRAGMA synchronous.
Property and typeVariableDefaultUsage and effect
driver: str—'mysql'Fixed backend discriminator; a value differing from the entity raises ValueError.
host: strDB_HOST'127.0.0.1'Server address; requires nonempty text, except Redshift’s optional host.
port: intDB_PORT3306Port from 1 to 65535. PGSQL/Redshift also accept an ASCII decimal string; other backends require int and reject bool.
database: strDB_DATABASE'orionis'Server database name; file or :memory: for SQLite. Requires nonempty text.
username: strDB_USERNAME'root'Connection username; nonempty when required. Redshift permits None for connector/IAM configuration.
password: strDB_PASSWORD''Password; empty strings are allowed. Redshift also accepts None. Never expose in diagnostics.
unix_socket: str | NoneDB_SOCKET''Optional MySQL socket path; nonempty values are added to URL parameters.
charset: str | MySQLCharsetDB_CHARSET'utf8mb4'Charset normalized through the backend enum. MySQL applies SET NAMES and URL settings; PGSQL forwards client_encoding. SQLServer validates/retains it without forwarding to the driver.
collation: str | MySQLCollationDB_COLLATION'utf8mb4_unicode_ci'MySQL collation accepted by MySQLCollation; applied with SET NAMES per connection.
prefix: str | NoneDB_PREFIX''Prefix for logical table names when compiling plans/schemas; does not modify raw SQL.
prefix_indexes: boolDB_PREFIX_INDEXESTrueValidated retained boolean; current compiler/schema code does not read it when generating index names.
strict: boolDB_STRICTTrueTrue applies ONLY_FULL_GROUP_BY, STRICT_TRANS_TABLES, NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO and NO_ENGINE_SUBSTITUTION. False applies only NO_ENGINE_SUBSTITUTION.
engine: str | MySQLEngine | NoneDB_ENGINE'InnoDB'Validated against MySQLEngine or None. Retained field: runtime does not apply it as table ENGINE.
Property and typeVariableDefaultUsage and effect
driver: str—'pgsql'Fixed backend discriminator; a value differing from the entity raises ValueError.
host: strDB_HOST'127.0.0.1'Server address; requires nonempty text, except Redshift’s optional host.
port: str | intDB_PORT5432Port from 1 to 65535. PGSQL/Redshift also accept an ASCII decimal string; other backends require int and reject bool.
database: strDB_DATABASE'orionis'Server database name; file or :memory: for SQLite. Requires nonempty text.
username: strDB_USERNAME'postgres'Connection username; nonempty when required. Redshift permits None for connector/IAM configuration.
password: strDB_PASSWORD''Password; empty strings are allowed. Redshift also accepts None. Never expose in diagnostics.
charset: str | PGSQLCharsetDB_CHARSET'UTF8'Charset normalized through the backend enum. MySQL applies SET NAMES and URL settings; PGSQL forwards client_encoding. SQLServer validates/retains it without forwarding to the driver.
prefix: strDB_PREFIX''Prefix for logical table names when compiling plans/schemas; does not modify raw SQL.
prefix_indexes: boolDB_PREFIX_INDEXESTrueValidated retained boolean; current compiler/schema code does not read it when generating index names.
search_path: strDB_SEARCH_PATH'public'Nonempty text sent to PGSQL; the sync path translates it to options=-csearch_path=… with escaped spaces.
sslmode: str | PGSQLSSLModeDB_SSLMODE'prefer'TLS policy normalized through the corresponding enum. PGSQL accepts its six modes; Redshift only verify-ca/verify-full.
Property and typeVariableDefaultUsage and effect
driver: str—'oracle'Fixed backend discriminator; a value differing from the entity raises ValueError.
username: strDB_USERNAME'sys'Connection username; nonempty when required. Redshift permits None for connector/IAM configuration.
password: strDB_PASSWORD''Password; empty strings are allowed. Redshift also accepts None. Never expose in diagnostics.
host: strDB_HOST'localhost'Server address; requires nonempty text, except Redshift’s optional host.
port: intDB_PORT1521Port from 1 to 65535. PGSQL/Redshift also accept an ASCII decimal string; other backends require int and reject bool.
service_name: str | NoneDB_SERVICE_NAME'ORCL'Oracle service when SID/DSN/TNS is unused; supplying SID omits service_name from the URL.
sid: str | NoneDB_SIDNoneOptional Oracle SID, used as URL database component instead of service_name.
dsn: str | NoneDB_DSNNoneExplicit DSN; takes precedence over TNS and host/port/service/SID addressing.
tns_name: str | NoneDB_TNSNoneTNS alias used as connect_args.dsn when explicit DSN is absent. The connector must resolve it.
encoding: str | OracleEncodingDB_ENCODING'AL32UTF8'Oracle compatibility field validated against OracleEncoding; not passed to the current connector.
nencoding: str | OracleNencodingDB_NENCODING'AL16UTF16'Oracle compatibility field validated against OracleNencoding; not passed to the current connector.

Without DSN/TNS requires nonempty host, valid port and at least a service or SID. DSN/TNS bypass that addressing validation. Current encoding/nencoding choices appear in the enum catalog.

Property and typeVariableDefaultUsage and effect
driver: str—'sqlserver'Fixed backend discriminator; a value differing from the entity raises ValueError.
host: strDB_HOST'127.0.0.1'Server address; requires nonempty text, except Redshift’s optional host.
port: intDB_PORT1433Port from 1 to 65535. PGSQL/Redshift also accept an ASCII decimal string; other backends require int and reject bool.
database: strDB_DATABASE'orionis'Server database name; file or :memory: for SQLite. Requires nonempty text.
username: strDB_USERNAME'sa'Connection username; nonempty when required. Redshift permits None for connector/IAM configuration.
password: strDB_PASSWORD''Password; empty strings are allowed. Redshift also accepts None. Never expose in diagnostics.
charset: SQLServerCharset | strDB_CHARSET'UTF-8'Charset normalized through the backend enum. MySQL applies SET NAMES and URL settings; PGSQL forwards client_encoding. SQLServer validates/retains it without forwarding to the driver.
prefix: strDB_PREFIX''Prefix for logical table names when compiling plans/schemas; does not modify raw SQL.
prefix_indexes: boolDB_PREFIX_INDEXESTrueValidated retained boolean; current compiler/schema code does not read it when generating index names.
encrypt: bool | strDB_ENCRYPT'yes'bool or yes/no/true/false/on/off/1/0; normalizes to Encrypt=yes/no in the ODBC URL.
trust_server_certificate: boolDB_TRUST_SERVER_CERTIFICATETrueBoolean configuring TrustServerCertificate; True skips certificate-chain validation.
odbc_driver: strDB_ODBC_DRIVER'ODBC Driver 18 for SQL Server'Nonempty name of the system-installed ODBC driver; forwarded as URL driver.
Property and typeVariableDefaultUsage and effect
driver: str—'redshift'Fixed backend discriminator; a value differing from the entity raises ValueError.
host: str | NoneDB_HOST'127.0.0.1'Server address; requires nonempty text, except Redshift’s optional host.
port: str | intDB_PORT5439Port from 1 to 65535. PGSQL/Redshift also accept an ASCII decimal string; other backends require int and reject bool.
database: strDB_DATABASE'dev'Server database name; file or :memory: for SQLite. Requires nonempty text.
username: str | NoneDB_USERNAME'awsuser'Connection username; nonempty when required. Redshift permits None for connector/IAM configuration.
password: str | NoneDB_PASSWORD''Password; empty strings are allowed. Redshift also accepts None. Never expose in diagnostics.
prefix: strDB_PREFIX''Prefix for logical table names when compiling plans/schemas; does not modify raw SQL.
ssl: boolDB_REDSHIFT_SSLTrueBoolean enabling TLS in redshift-connector.
sslmode: str | RedshiftSSLModeDB_REDSHIFT_SSLMODE'verify-full'TLS policy normalized through the corresponding enum. PGSQL accepts its six modes; Redshift only verify-ca/verify-full.
timeout: int | NoneDB_REDSHIFT_TIMEOUT30Redshift socket timeout in seconds: int >= 1 or None for no configured socket timeout.
iam: boolDB_REDSHIFT_IAMFalseBoolean forwarded for IAM authentication; config itself does not fetch or store IAM credentials.
region: str | NoneDB_REDSHIFT_REGIONNoneOptional AWS region passed to the connector; nonempty text or None.
cluster_identifier: str | NoneDB_REDSHIFT_CLUSTER_IDENTIFIERNoneProvisioned cluster identifier for IAM settings; nonempty text or None.
db_user: str | NoneDB_REDSHIFT_DB_USERNoneDatabase user for IAM; nonempty text or None.
profile: str | NoneDB_REDSHIFT_PROFILENoneOptional AWS profile; omitted values leave credential resolution to the connector.
is_serverless: boolDB_REDSHIFT_IS_SERVERLESSFalseBoolean forwarded to select Serverless configuration.
serverless_work_group: str | NoneDB_REDSHIFT_SERVERLESS_WORK_GROUPNoneServerless workgroup name; nonempty text or None.
serverless_acct_id: str | NoneDB_REDSHIFT_SERVERLESS_ACCT_IDNoneServerless AWS account ID; nonempty text or None.

Complete example defining two backends with independent credentials; other Connections entries retain default entities:

from dataclasses import dataclass, field
from orionis.environment import Env
from orionis.foundation.config.database import (
Connections, Database, PGSQL, SQLite,
)
@dataclass(frozen=True, kw_only=True)
class BootstrapDatabase(Database):
default: str = "pgsql"
connections: Connections = field(
default_factory=lambda: Connections(
sqlite=SQLite(
database="database/local.sqlite",
foreign_key_constraints=True,
),
pgsql=PGSQL(
host=Env.get("PRIMARY_DB_HOST", "127.0.0.1"),
port=5432,
database="orionis",
username=Env.get("PRIMARY_DB_USERNAME", "app_user"),
password=Env.get("PRIMARY_DB_PASSWORD", ""),
search_path="public",
sslmode="verify-full",
),
)
)

The example’s PRIMARY_DB_* variables are customizations of that file, not additional native environment variables. An application instance can also use app.withConfigDatabase(default=..., connections=...) with the same validatable structure.

Entities are frozen and accept keyword arguments. toDict() provides settings consumed by the manager and dialect helpers. Do not log the full dictionary: it contains passwords. Validation can raise TypeError/ValueError during startup; fixing types, names and enums differs from installing a driver or repairing connectivity.

Import these classes from orionis.foundation.config.database. Entities normalize supported options; arbitrary server-recognized strings are not necessarily accepted. Entries show MEMBER = value. PGSQLCollation is public, but PGSQL has no collation field.

MySQLEngine, OracleEncoding, OracleNencoding and SQLServerCharset imply no runtime effects beyond those stated for their properties.

SQLITE = 'sqlite', MYSQL = 'mysql', PGSQL = 'pgsql', ORACLE = 'oracle', SQLSERVER = 'sqlserver', REDSHIFT = 'redshift'.

ASCII = 'ascii', BIG5 = 'big5', CP1250 = 'cp1250', CP1251 = 'cp1251', CP1256 = 'cp1256', CP1257 = 'cp1257', CP850 = 'cp850', CP852 = 'cp852', CP866 = 'cp866', CP932 = 'cp932', EUCKR = 'euckr', GB18030 = 'gb18030', GB2312 = 'gb2312', GBK = 'gbk', GREEK = 'greek', HEBREW = 'hebrew', KOI8R = 'koi8r', KOI8U = 'koi8u', LATIN1 = 'latin1', LATIN2 = 'latin2', LATIN5 = 'latin5', LATIN7 = 'latin7', MACROMAN = 'macroman', SJIS = 'sjis', TIS620 = 'tis620', UJIS = 'ujis', UTF8 = 'utf8', UTF8MB3 = 'utf8mb3', UTF8MB4 = 'utf8mb4'.

UTF8_GENERAL_CI = 'utf8_general_ci', UTF8_UNICODE_CI = 'utf8_unicode_ci', UTF8_BIN = 'utf8_bin', UTF8MB4_GENERAL_CI = 'utf8mb4_general_ci', UTF8MB4_UNICODE_CI = 'utf8mb4_unicode_ci', UTF8MB4_BIN = 'utf8mb4_bin', LATIN1_SWEDISH_CI = 'latin1_swedish_ci', LATIN1_GENERAL_CI = 'latin1_general_ci', LATIN1_BIN = 'latin1_bin', ASCII_GENERAL_CI = 'ascii_general_ci', ASCII_BIN = 'ascii_bin', GB18030_CHINESE_CI = 'gb18030_chinese_ci', GB18030_BIN = 'gb18030_bin', GB18030_UNICODE_520_CI = 'gb18030_unicode_520_ci'.

INNODB = 'InnoDB'.

AL32UTF8 = 'AL32UTF8'.

AL16UTF16 = 'AL16UTF16', UTF8 = 'UTF8'.

EUC_CN = 'EUC_CN', EUC_JP = 'EUC_JP', EUC_JIS_2004 = 'EUC_JIS_2004', EUC_KR = 'EUC_KR', EUC_TW = 'EUC_TW', ISO_8859_5 = 'ISO_8859_5', ISO_8859_6 = 'ISO_8859_6', ISO_8859_7 = 'ISO_8859_7', ISO_8859_8 = 'ISO_8859_8', KOI8R = 'KOI8R', KOI8U = 'KOI8U', LATIN1 = 'LATIN1', LATIN2 = 'LATIN2', LATIN3 = 'LATIN3', LATIN4 = 'LATIN4', LATIN5 = 'LATIN5', LATIN6 = 'LATIN6', LATIN7 = 'LATIN7', LATIN8 = 'LATIN8', LATIN9 = 'LATIN9', LATIN10 = 'LATIN10', UTF8 = 'UTF8', WIN866 = 'WIN866', WIN874 = 'WIN874', WIN1250 = 'WIN1250', WIN1251 = 'WIN1251', WIN1252 = 'WIN1252', WIN1253 = 'WIN1253', WIN1254 = 'WIN1254', WIN1255 = 'WIN1255', WIN1256 = 'WIN1256', WIN1257 = 'WIN1257', WIN1258 = 'WIN1258'.

C = 'C', POSIX = 'POSIX'.

DISABLE = 'disable', ALLOW = 'allow', PREFER = 'prefer', REQUIRE = 'require', VERIFY_CA = 'verify-ca', VERIFY_FULL = 'verify-full'.

VERIFY_CA = 'verify-ca', VERIFY_FULL = 'verify-full'.

ON = 'ON', OFF = 'OFF'.

DELETE = 'DELETE', TRUNCATE = 'TRUNCATE', PERSIST = 'PERSIST', MEMORY = 'MEMORY', WAL = 'WAL', OFF = 'OFF'.

FULL = 'FULL', NORMAL = 'NORMAL', EXTRA = 'EXTRA', OFF = 'OFF'.

UTF8 = 'UTF-8', CP437 = 'CP437', CP850 = 'CP850', CP874 = 'CP874', CP932 = 'CP932', CP936 = 'CP936', CP949 = 'CP949', CP950 = 'CP950', CP1250 = 'CP1250', CP1251 = 'CP1251', CP1252 = 'CP1252', CP1253 = 'CP1253', CP1254 = 'CP1254', CP1255 = 'CP1255', CP1256 = 'CP1256', CP1257 = 'CP1257', CP1258 = 'CP1258', ISO_8859_1 = 'ISO-8859-1', ISO_8859_2 = 'ISO-8859-2', ISO_8859_3 = 'ISO-8859-3', ISO_8859_4 = 'ISO-8859-4', ISO_8859_5 = 'ISO-8859-5', ISO_8859_6 = 'ISO-8859-6', ISO_8859_7 = 'ISO-8859-7', ISO_8859_8 = 'ISO-8859-8', ISO_8859_9 = 'ISO-8859-9', ISO_8859_13 = 'ISO-8859-13', ISO_8859_15 = 'ISO-8859-15'.

from orionis.foundation.config.database import (
MySQL, MySQLCharset, MySQLCollation, SQLite, SQLiteJournalMode,
)
mysql = MySQL(
charset=MySQLCharset.UTF8MB4,
collation=MySQLCollation.UTF8MB4_UNICODE_CI,
)
sqlite = SQLite(journal_mode=SQLiteJournalMode.WAL, foreign_key_constraints=True)

See drivers for installation and .env examples, and connections for additional names. This template has no native DB_POOL_* variables, generic overriding URL or global isolation option.