graflo.db.conn¶
Abstract database connection interface for graph databases.
This module defines the abstract interface for database connections, providing a unified API for different graph database implementations. It includes methods for database management, graph structure operations, and data manipulation.
Key Components
- Connection: Abstract base class for database connections
- ConnectionType: Type variable for connection implementations
The connection interface supports
- Database/Graph creation and deletion
- Graph structure management (vertex types, edge types)
- Index definition
- Document operations (insert, update, fetch)
- Edge operations
- Aggregation queries
Database Organization Terminology
Different databases organize graph data differently:
-
ArangoDB:
- Database: Top-level container (like a schema)
- Collections: Container for vertices (vertex collections)
- Edge Collections: Container for edges
- Graph: Named graph that connects vertex and edge collections
-
Neo4j:
- Database: Top-level container
- Labels: Categories for nodes (equivalent to vertex types)
- Relationship Types: Types of relationships (equivalent to edge types)
- No explicit "graph" concept - all nodes/relationships are in the database
-
TigerGraph:
- Graph: Top-level container (functions like a database in ArangoDB)
- Vertex Types: Global vertex type definitions (can be shared across graphs)
- Edge Types: Global edge type definitions (can be shared across graphs)
- Vertex and edge types are associated with graphs
When using the Connection interface, the terms "vertex type" and "edge type" are used generically to refer to the appropriate concept in each database.
Example
class MyConnection(Connection): ... def create_database(self, name: str): ... # Implementation ... def execute(self, query, **kwargs): ... # Implementation
Connection
¶
Bases: ABC
Abstract base class for database connections.
This class defines the interface that all database connection implementations must follow. It provides methods for database/graph operations, graph structure management (vertex types, edge types), and data manipulation.
Note
All methods marked with @abc.abstractmethod must be implemented by concrete connection classes.
Source code in graflo/db/conn.py
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 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
__init__()
¶
aggregate(class_name, aggregation_function, discriminant=None, aggregated_field=None, filters=None)
abstractmethod
¶
Perform aggregation on a collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
Name of the collection |
required | |
aggregation_function
|
AggregationType
|
Type of aggregation to perform |
required |
discriminant
|
str | None
|
Field to group by |
None
|
aggregated_field
|
str | None
|
Field to aggregate |
None
|
filters
|
list | dict | None
|
Query filters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
dict |
Aggregation results |
Source code in graflo/db/conn.py
close()
abstractmethod
¶
create_database(name)
abstractmethod
¶
Create a new database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the database to create |
required |
define_edge_indices(edges)
abstractmethod
¶
Define indices for edge collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edges
|
list[Edge]
|
List of edge configurations containing index definitions |
required |
define_indexes(schema)
¶
Define indexes for vertices and edges in the schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
Schema containing vertex and edge configurations |
required |
Source code in graflo/db/conn.py
define_schema(schema)
abstractmethod
¶
Define collections based on the schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
Schema containing collection definitions |
required |
define_vertex_indices(vertex_config)
abstractmethod
¶
Define indices for vertex collections.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_config
|
VertexConfig
|
Vertex configuration containing index definitions |
required |
delete_database(name)
abstractmethod
¶
delete_graph_structure(vertex_types=(), graph_names=(), delete_all=False)
abstractmethod
¶
Delete graph structure (graphs, vertex types, edge types) from the database.
This method deletes graphs and their associated vertex/edge types. The exact behavior depends on the database implementation:
- ArangoDB: Deletes graphs and collections (vertex/edge collections)
- Neo4j: Deletes nodes from labels (vertex types) and relationships
- TigerGraph: Deletes graphs, vertex types, edge types, and jobs
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
vertex_types
|
Vertex type names to delete (database-specific interpretation) |
()
|
|
graph_names
|
Graph/database names to delete |
()
|
|
delete_all
|
If True, delete all graphs and their associated structures |
False
|
Source code in graflo/db/conn.py
execute(query, **kwargs)
abstractmethod
¶
Execute a database query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
Query to execute |
required | |
**kwargs
|
Additional query parameters |
{}
|
fetch_docs(class_name, filters, limit, return_keys, unset_keys, **kwargs)
abstractmethod
¶
Fetch documents from a vertex type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_name
|
Name of the vertex type (or collection/label in database-specific terms) |
required | |
filters
|
Query filters |
required | |
limit
|
Maximum number of documents to return |
required | |
return_keys
|
Keys to return |
required | |
unset_keys
|
Keys to unset |
required | |
**kwargs
|
Additional database-specific parameters (e.g., field_types for TigerGraph) |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Fetched documents |
Source code in graflo/db/conn.py
fetch_edges(from_type, from_id, edge_type=None, to_type=None, to_id=None, filters=None, limit=None, return_keys=None, unset_keys=None, **kwargs)
abstractmethod
¶
Fetch edges from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
from_type
|
str
|
Source vertex type |
required |
from_id
|
str
|
Source vertex ID (required) |
required |
edge_type
|
str | None
|
Optional edge type to filter by |
None
|
to_type
|
str | None
|
Optional target vertex type to filter by |
None
|
to_id
|
str | None
|
Optional target vertex ID to filter by |
None
|
filters
|
list | dict | None
|
Additional query filters |
None
|
limit
|
int | None
|
Maximum number of edges to return |
None
|
return_keys
|
list | None
|
Keys to return (projection) |
None
|
unset_keys
|
list | None
|
Keys to exclude (projection) |
None
|
**kwargs
|
Additional database-specific parameters |
{}
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
List of fetched edges |
Source code in graflo/db/conn.py
fetch_present_documents(batch, class_name, match_keys, keep_keys, flatten=False, filters=None)
abstractmethod
¶
Fetch documents that exist in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch of documents to check |
required | |
class_name
|
Name of the collection |
required | |
match_keys
|
Keys to match |
required | |
keep_keys
|
Keys to keep in result |
required | |
flatten
|
Whether to flatten the result |
False
|
|
filters
|
list | dict | None
|
Additional query filters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Documents that exist in the database |
Source code in graflo/db/conn.py
init_db(schema, clean_start)
abstractmethod
¶
Initialize the database with the given schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
Schema
|
Schema to initialize the database with |
required |
clean_start
|
Whether to clean existing data |
required |
insert_edges_batch(docs_edges, source_class, target_class, relation_name, collection_name, match_keys_source, match_keys_target, filter_uniques=True, uniq_weight_fields=None, uniq_weight_collections=None, upsert_option=False, head=None, **kwargs)
abstractmethod
¶
Insert a batch of edges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs_edges
|
Edge documents to insert |
required | |
source_class
|
Source vertex type/class |
required | |
target_class
|
Target vertex type/class |
required | |
relation_name
|
Name of the edge type/relation |
required | |
collection_name
|
Name of the edge type (database-specific: collection/relationship type) |
required | |
match_keys_source
|
Keys to match source vertices |
required | |
match_keys_target
|
Keys to match target vertices |
required | |
filter_uniques
|
Whether to filter unique edges |
True
|
|
uniq_weight_fields
|
Fields to consider for uniqueness |
None
|
|
uniq_weight_collections
|
Vertex/edge types to consider for uniqueness (database-specific) |
None
|
|
upsert_option
|
Whether to upsert existing edges |
False
|
|
head
|
Optional head document |
None
|
|
**kwargs
|
Additional insertion parameters |
{}
|
Source code in graflo/db/conn.py
insert_return_batch(docs, class_name)
abstractmethod
¶
Insert documents and return the inserted documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Documents to insert |
required | |
class_name
|
Name of the vertex type (or collection/label in database-specific terms) |
required |
Returns:
| Name | Type | Description |
|---|---|---|
list |
Inserted documents |
Source code in graflo/db/conn.py
keep_absent_documents(batch, class_name, match_keys, keep_keys, filters=None)
abstractmethod
¶
Keep documents that don't exist in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
Batch of documents to check |
required | |
class_name
|
Name of the collection |
required | |
match_keys
|
Keys to match |
required | |
keep_keys
|
Keys to keep in result |
required | |
filters
|
list | dict | None
|
Additional query filters |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
Documents that don't exist in the database |
Source code in graflo/db/conn.py
upsert_docs_batch(docs, class_name, match_keys, **kwargs)
abstractmethod
¶
Upsert a batch of documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
docs
|
Documents to upsert |
required | |
class_name
|
Name of the vertex type (or collection/label in database-specific terms) |
required | |
match_keys
|
Keys to match for upsert |
required | |
**kwargs
|
Additional upsert parameters |
{}
|