Database¶
aloha.db package - Database and middleware connection helpers.
Sync modules (blocking): from aloha.db import PostgresOperator, MySqlOperator, RedisOperator, ... from aloha.db.postgres import PostgresOperator from aloha.db.mysql import MySqlOperator from aloha.db.redis import RedisOperator from aloha.db.mongo import MongoOperator from aloha.db.elasticsearch import ElasticSearchOperator from aloha.db.kafka import KafkaOperator from aloha.db.sqlite import SqliteOperator from aloha.db.duckdb import DuckOperator from aloha.db.oracle import OracledbOperator
Async modules (non-blocking): from aloha.db import PostgresOperator as PostgresOperatorAio, ... from aloha.db.postgres_aio import PostgresOperator from aloha.db.mysql_aio import MySqlOperator from aloha.db.redis_aio import RedisOperator from aloha.db.mongo_aio import MongoOperator from aloha.db.elasticsearch_aio import ElasticSearchOperator from aloha.db.kafka_aio import KafkaOperator from aloha.db.sqlite_aio import SqliteOperator from aloha.db.duckdb_aio import DuckOperator from aloha.db.oracle_aio import OracledbOperator
Base utilities: from aloha.db.base import PasswordVault from aloha.db.base_aio import PasswordVault # async version
Usage example (sync): from aloha.db.postgres import PostgresOperator
op = PostgresOperator(db_config)
result = op.execute_query("SELECT * FROM users")
for row in result:
print(row)
Usage example (async): from aloha.db.postgres_aio import PostgresOperator
async def main():
op = PostgresOperator(db_config)
result = await op.execute_query("SELECT * FROM users")
async for row in op.execute_query_scalars("SELECT * FROM users"):
print(row)
await op.close()
import asyncio
asyncio.run(main())
from .base import PasswordVault
try: from .postgres import PostgresOperator except (ImportError, ModuleNotFoundError): pass
try: from .mysql import MySqlOperator except (ImportError, ModuleNotFoundError): pass
try: from .redis import RedisOperator except (ImportError, ModuleNotFoundError): pass
try: from .mongo import MongoOperator except (ImportError, ModuleNotFoundError): pass
try: from .elasticsearch import ElasticSearchOperator except (ImportError, ModuleNotFoundError): pass
try: from .kafka import ConsumedMessage, KafkaOperator except (ImportError, ModuleNotFoundError): pass
try: from .sqlite import SqliteOperator except (ImportError, ModuleNotFoundError): pass
try: from .duckdb import DuckOperator except (ImportError, ModuleNotFoundError): pass
try: from .oracle import OracledbOperator except (ImportError, ModuleNotFoundError): pass
Async modules (importable as aliases for easy switching)¶
from .base_aio import PasswordVault as PasswordVaultAio
try: from .postgres_aio import PostgresOperator as PostgresOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .mysql_aio import MySqlOperator as MySqlOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .redis_aio import RedisOperator as RedisOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .mongo_aio import MongoOperator as MongoOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .elasticsearch_aio import ElasticSearchOperator as ElasticSearchOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .kafka_aio import ConsumedMessage as ConsumedMessageAio from .kafka_aio import KafkaOperator as KafkaOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .sqlite_aio import SqliteOperator as SqliteOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .duckdb_aio import DuckOperator as DuckOperatorAio except (ImportError, ModuleNotFoundError): pass
try: from .oracle_aio import OracledbOperator as OracledbOperatorAio except (ImportError, ModuleNotFoundError): pass
all = ( "ConsumedMessage", "ConsumedMessageAio", "DuckOperator", "DuckOperatorAio", "ElasticSearchOperator", "ElasticSearchOperatorAio", "KafkaOperator", "KafkaOperatorAio", "MongoOperator", "MongoOperatorAio", "MySqlOperator", "MySqlOperatorAio", "OracledbOperator", "OracledbOperatorAio", "PasswordVault", "PasswordVaultAio", # Sync operators "PostgresOperator", # Async operators (aliased) "PostgresOperatorAio", "RedisOperator", "RedisOperatorAio", "SqliteOperator", "SqliteOperatorAio", )
PasswordVault
¶
Password vault manager that provides access to password vault implementations.
Caches vault instances for performance.
Source code in aloha/db/base.py
get_vault(vault_type=None, vault_config=None, **kwargs)
staticmethod
¶
Get a password vault instance.
Supports multiple vault types: - 'plain' or 'aes': AES-based vault (default fallback) - 'cyberark': CyberArk vault - Other/None: Dummy vault (plain text)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vault_type
|
str | None
|
Type of vault to use (overrides config) |
None
|
vault_config
|
dict | None
|
Vault configuration dictionary |
None
|
args
|
Additional arguments |
required | |
kwargs
|
Additional keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
BaseVault
|
Vault instance implementing BaseVault interface |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If CyberArk vault is requested but config is missing |
Source code in aloha/db/base.py
main()
¶
Command-line tool to decrypt passwords from config.
Usage: python -m aloha.db.base
Source code in aloha/db/base.py
DuckDB connection helpers.
DuckOperator
¶
Create and use a DuckDB connection through SQLAlchemy.
Source code in aloha/db/duckdb.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |
connection_str
property
¶
Return a human-readable connection string.
__init__(db_config, **kwargs)
¶
Build a DuckDB engine, creating the database file if necessary.
Source code in aloha/db/duckdb.py
execute_query(sql, *args, **kwargs)
¶
Execute a SQL statement and return the cursor result.
Source code in aloha/db/duckdb.py
ElasticSearchOperator
¶
Create and use an Elasticsearch client with optional index helpers.
Source code in aloha/db/elasticsearch.py
__init__(config, index_config=None)
¶
Build the client and optionally load the index configuration.
Source code in aloha/db/elasticsearch.py
build_index(index_name=None, index_config=None, raise_if_exist=False)
¶
Create the index if it does not already exist.
Source code in aloha/db/elasticsearch.py
insert(doc, index_name=None, index_type=None, id=None)
¶
msearch(body)
¶
put_mapping(index_name=None, index_type=None, index_config=None)
¶
Apply a mapping definition to the current index.
Source code in aloha/db/elasticsearch.py
search(query, index_name=None, index_type=None)
¶
Kafka connection helpers.
ConsumedMessage
dataclass
¶
Represents a message consumed from Kafka.
Source code in aloha/db/kafka.py
KafkaOperator
¶
Create Kafka admin, producer, and consumer clients.
Source code in aloha/db/kafka.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 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 234 235 236 237 238 239 240 241 242 243 | |
__enter__()
¶
__exit__(exc_type, exc_val, exc_tb)
¶
__init__(kafka_config)
¶
Parameter reference: https://github.com/edenhill/librdkafka/blob/master/CONFIGURATION.md
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kafka_config
|
host = [ {host: kafka_server_1, port: 9092} ] |
required |
Source code in aloha/db/kafka.py
admin_client(*args, **kwargs)
¶
close()
¶
commit(*args, **kwargs)
¶
consumer_generator(topics_subscribe, group_id=None, poll_timeout=1.0, *args, **kwargs)
¶
Yield decoded messages from the subscribed Kafka topics.
Source code in aloha/db/kafka.py
create_topic(topic, num_partitions=3, replication_factor=1, *args, **kwargs)
¶
Create a Kafka topic and wait for the broker response.
Source code in aloha/db/kafka.py
producer()
¶
Return a configured Kafka Producer.
producer_deliver(topic, generator, func_callback=None, *args, **kwargs)
¶
Stream messages from an iterator into a Kafka topic.
Source code in aloha/db/kafka.py
MongoDB connection helpers.
MongoOperator(config)
¶
Return a cached MongoDB operation wrapper for the given config.
Source code in aloha/db/mongo.py
MySQL connection helpers.
MySqlOperator
¶
Create and use a SQLAlchemy-backed MySQL connection.
Source code in aloha/db/mysql.py
connection_str
property
¶
Return a human-readable connection string.
__init__(db_config, **kwargs)
¶
Build a connection pool from the provided database config.
Source code in aloha/db/mysql.py
execute_query(sql, *args, **kwargs)
¶
Oracle DB connection helpers.
OracledbOperator
¶
Create and use a SQLAlchemy-backed Oracle connection.
Source code in aloha/db/oracle.py
connection_str
property
¶
Return a human-readable connection string.
__init__(db_config, **kwargs)
¶
Build an Oracle connection pool from the provided config.
Source code in aloha/db/oracle.py
execute_query(sql, *args, **kwargs)
¶
Execute a SQL statement and return the cursor result.
PostgreSQL connection helpers.
PostgresOperator
¶
Create and use a SQLAlchemy-backed PostgreSQL connection.
Source code in aloha/db/postgres.py
connection_str
property
¶
Return a human-readable connection string.
__init__(db_config, **kwargs)
¶
Build a PostgreSQL connection pool from the database config.
Source code in aloha/db/postgres.py
execute_query(sql, *args, **kwargs)
¶
Execute a SQL statement and return the cursor result.
Redis connection helpers.
RedisOperator
¶
Create Redis connections with version-checked redis-py.
Source code in aloha/db/redis.py
connection_cluster
property
¶
Return a Redis Cluster client.
connection_generic
property
¶
Return a standard Redis client.
__init__(config)
¶
Normalize Redis connection settings and build connection metadata.
Source code in aloha/db/redis.py
SQLite connection helpers.
SqliteOperator
¶
Create and use a SQLAlchemy-backed SQLite connection.
Source code in aloha/db/sqlite.py
connection_str
property
¶
Return the SQLAlchemy connection URL used by the engine.
__init__(db_config, **kwargs)
¶
Build a SQLite or SQLCipher engine from the provided config.