graflo.db.manager¶
Database connection manager for graph and source databases.
This module provides a connection manager for handling database connections to different database implementations (ArangoDB, Neo4j, PostgreSQL, etc.). It manages connection lifecycle and configuration.
Key Components
- ConnectionManager: Main class for managing database connections
- DBType: Enum for supported database types
The manager supports
- Target databases (ArangoDB, Neo4j, TigerGraph) - OUTPUT
- Source databases (PostgreSQL, MySQL, MongoDB, etc.) - INPUT
- Connection configuration
- Context manager interface
- Automatic connection cleanup
Example
from graflo.connections.onto import ArangoConfig config = ArangoConfig.from_env() with ConnectionManager(connection_config=config) as conn: ... # ArangoDB-specific AQL query (collection is ArangoDB terminology) ... conn.execute("FOR doc IN vertex_class RETURN doc")
ConnectionManager
¶
Manager for database connections (both graph and source databases).
This class manages database connections to different database implementations. It provides a context manager interface for safe connection handling and automatic cleanup.
Supports: - Target databases (OUTPUT): ArangoDB, Neo4j, TigerGraph - Source databases (INPUT): PostgreSQL, MySQL, MongoDB, etc.
Attributes:
| Name | Type | Description |
|---|---|---|
target_conn_mapping |
Mapping of target database types to connection classes |
|
config |
DBConfig
|
Connection configuration |
working_db |
Current working database name |
|
conn |
Active database connection |
Source code in graflo/db/manager.py
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 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 | |
__enter__()
¶
Enter the context manager.
Creates and returns a new database connection.
Returns:
| Name | Type | Description |
|---|---|---|
Connection |
Database connection instance |
Source code in graflo/db/manager.py
__exit__(exc_type, exc_value, exc_traceback)
¶
Exit the context manager.
Ensures the connection is properly closed when exiting the context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exc_type
|
Exception type if an exception occurred |
required | |
exc_value
|
Exception value if an exception occurred |
required | |
exc_traceback
|
Exception traceback if an exception occurred |
required |
Source code in graflo/db/manager.py
__init__(connection_config, **kwargs)
¶
Initialize the connection manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_config
|
DBConfig
|
Database connection configuration |
required |
**kwargs
|
Additional configuration parameters |
{}
|
Source code in graflo/db/manager.py
close()
¶
Close the database connection.
Closes the active connection and performs any necessary cleanup.
flavors_supporting(capability)
classmethod
¶
Database types whose connection class declares capability.
Source code in graflo/db/manager.py
graph_export_flavors()
classmethod
¶
open_read_connection(connection_config, *, require)
classmethod
¶
Open a connection for reading, gated on the capability actually needed.
Replaces open_graph_connection, which gated every read path on
supports_graph_export -- "can dump the whole graph". That is a far
stronger claim than "can describe itself" or "can answer a bounded
read", and asking for it is why introspection reached three of eight
backends. Callers now name the capability they use, so a backend that
can introspect but not export is no longer turned away.
The caller is responsible for closing the connection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_config
|
DBConfig
|
Which database to open. |
required |
require
|
ConnectionCapability
|
The capability the caller is about to exercise. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the flavor has no implementation, or does not declare require. |