Databases
kyotsu.config.databases
¶
This module provides the models and mix-ins necessary for configuring and maintaining the connections to various databases across the application.
The configuration for different types of database connections are defined and standardized in this package, ensuring the consistency and maintainability of the codebase.
| MODULE | DESCRIPTION |
|---|---|
models |
Defines the models for various types of database connections used across the application. |
_types |
Contains inner misc types for mixins and models. |
When needing to configure a new database connection or reuse an existing common configuration across multiple services, these modules should be the first place to look.
kyotsu.config.databases.models
¶
This module contains base models for defining database connection settings.
Each model class in this module represents a different type of database connection.
All classes derived from DBConnection that use separate properties
(USERNAME, PASSWORD, HOST, PORT, DATABASE) to define a connection string (CONN_STR) use two main methods:
-
_assemble_conn_str_value(): an abstract method, which needs to be defined in subclasses. It is responsible for creating the actual connection string value. -
assemble_conn_str(): a model validator, which checks whetherCONN_STRisNone. If yes, it builds the connection string using_assemble_conn_str_value().
| CLASS | DESCRIPTION |
|---|---|
DBConnection |
Abstract class that can be inherited to represent different types of DB connections. |
AmqpConnection |
Represents an AMQP (Advanced Message Queuing Protocol) connection. |
OpenSearchNoCertConnection |
Represents an OpenSearch connection without requiring SSL certification. |
PostgresConnection |
Represents a connection to a Postgres database using either asyncpg or psycopg2 scheme. |
RedisConnection |
Represents a Redis database connection string with support for SSL. |
DBConnection
¶
Bases: BaseModel, Generic[ConnType], ABC
This abstract base model class represents a generic database connection.
It is designed to be inherited by subclasses that represent specific database connections.
Subclasses must implement the _assemble_conn_str_value method to generate the necessary connection string
representation for a specific database type.
| ATTRIBUTE | DESCRIPTION |
|---|---|
USERNAME |
The username for the database connection.
TYPE:
|
PASSWORD |
The password for the database connection.
TYPE:
|
HOST |
The host for the database connection.
TYPE:
|
PORT |
The port for the database connection.
TYPE:
|
DATABASE |
The specific database to connect to.
TYPE:
|
CONN_STR |
The connection string, assembled from the other properties.
TYPE:
|
Source code in src/kyotsu/config/databases/models.py
AmqpConnection
¶
Bases: DBConnection[AmqpDsn]
Represents an AMQP (Advanced Message Queuing Protocol) connection.
| ATTRIBUTE | DESCRIPTION |
|---|---|
USERNAME |
The username for this AMQP service.
TYPE:
|
PASSWORD |
The password for this AMQP service.
TYPE:
|
HOST |
The host of the AMQP service.
TYPE:
|
PORT |
[Optional] The port of the AMQP service. Default is 5672.
TYPE:
|
DATABASE |
[Optional] The database for this AMQP service. Default is "%2f", a placeholder for root level.
TYPE:
|
USE_SSL |
[Optional] If true, the AMQP service is assumed to use SSL. Default is True.
TYPE:
|
QUERY_PARAMS |
[Optional] Set of optional parameters for the connection, including backpressure detection, channel max, etc.
TYPE:
|
CONN_STR |
The connection string for the AMQP service, assembled from the other properties.
TYPE:
|
Source code in src/kyotsu/config/databases/models.py
OpenSearchNoCertConnection
¶
Bases: DBConnection[AnyHttpUrl]
Represents an OpenSearch connection without requiring SSL certification.
| ATTRIBUTE | DESCRIPTION |
|---|---|
SCHEME |
The scheme for this connection, either "http" or "https".
TYPE:
|
USERNAME |
The username for the OpenSearch service.
TYPE:
|
PASSWORD |
The password for the OpenSearch service.
TYPE:
|
HOST |
The host of the OpenSearch service.
TYPE:
|
PORT |
The port of the OpenSearch service.
TYPE:
|
TTL |
[Optional] The duration after which the connection times out. Default is 60.
TYPE:
|
MAX_RETRIES |
[Optional] The maximum number of times the request to the connection can be retried. Default is 10.
TYPE:
|
RETRY_ON_TIMEOUT |
[Optional] If true, retries occur on timed-out connections. Default is True.
TYPE:
|
CONN_DICT |
After being processed in
TYPE:
|
CONN_STR |
The connection string for the OpenSearch service, assembled from, assembled from the other properties.
TYPE:
|
Source code in src/kyotsu/config/databases/models.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
CONN_STR: Annotated[str, ConnType]
property
¶
The connection string, assembled from the other properties.
This attribute is computed by calling the _assemble_conn_str_value method.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The connection string, assembled from the other properties. |
CONN_DICT: OpenSearchConnectionDict
property
¶
Returns dict with OpenSearch connection parameters, including host, scheme, port, authentication, and SSL context.
| WARNS | DESCRIPTION |
|---|---|
ImportWarning
|
When opensearch-py is not installed, therefore the ssl_context can't be added. |
| RETURNS | DESCRIPTION |
|---|---|
OpenSearchConnectionDict
|
The connection dictionary.
TYPE:
|
PostgresConnection
¶
Bases: DBConnection[PostgresDsn]
Represents a connection to a Postgres database.
| ATTRIBUTE | DESCRIPTION |
|---|---|
USERNAME |
The username for the Postgres database.
TYPE:
|
PASSWORD |
The password for the Postgres database.
TYPE:
|
HOST |
The host of the Postgres database.
TYPE:
|
PORT |
[Optional] The port of the Postgres database. Default is 5432.
TYPE:
|
DATABASE |
The name of the Postgres database.
TYPE:
|
CONN_STR |
The connection string for the Postgres database, assembled from the other properties.
TYPE:
|
Source code in src/kyotsu/config/databases/models.py
RedisConnection
¶
Bases: DBConnection[RedisDsn]
Represents a Redis database connection string.
| ATTRIBUTE | DESCRIPTION |
|---|---|
PASSWORD |
The password for the Redis database.
TYPE:
|
HOST |
The host of the Redis database.
TYPE:
|
PORT |
[Optional] The port for the Redis database. Default is 6379.
TYPE:
|
DATABASE |
[Optional] The database index for the Redis database. Default is "0".
TYPE:
|
USE_SSL |
[Optional] If true, the Redis service is assumed to use SSL. Default is True.
TYPE:
|
CONN_STR |
The connection string for the Redis service, assembled from the other properties.
TYPE:
|