Session
fieldz_kb.lpg.session
Pylpg session for fieldz_kb.
Provides a Session class that wraps a pylpg.Session and adds higher-level methods for saving and retrieving fieldz objects.
Classes:
| Name | Description |
|---|---|
Session |
Session for saving and retrieving fieldz objects via pylpg. |
Session
Session for saving and retrieving fieldz objects via pylpg.
Wraps a pylpg.Session internally. Use as a context manager.
Example
import fieldz_kb.lpg import fieldz_kb.lpg.backends.neo4j backend = fieldz_kb.lpg.backends.neo4j.Neo4jBackend(hostname="localhost") with fieldz_kb.lpg.session.Session(backend) as session: ... session.save_from_object(person)
Initialize the session with a pylpg backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
Backend
|
A pylpg backend instance (Neo4jBackend, FalkorDBBackend, etc.) |
required |
Methods:
| Name | Description |
|---|---|
__enter__ |
Enter the session context manager. |
__exit__ |
Exit the session context manager. |
delete_all |
Delete all nodes and relationships from the database. |
execute_query |
Execute a Cypher query against the database. |
execute_query_as_objects |
Execute a Cypher query and convert results to Python objects. |
reset_context |
Replace the conversion context with a fresh one. |
save_from_object |
Save a single object to the database. |
save_from_objects |
Save multiple objects to the database. |
Source code in src/fieldz_kb/lpg/session.py
__enter__
__enter__() -> Session
__exit__
__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: object) -> bool | None
Exit the session context manager.
delete_all
execute_query
Execute a Cypher query against the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Cypher query string. |
required |
params
|
dict | None
|
Optional query parameters. |
None
|
resolve_nodes
|
bool
|
When True, any driver node objects in the results
are hydrated into |
False
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
Query results as a list of dicts. |
Source code in src/fieldz_kb/lpg/session.py
execute_query_as_objects
execute_query_as_objects(query: str, params: dict | None = None, node_id_to_object: dict | None = None, object_key_to_node: dict | None = None) -> list[list[object]]
Execute a Cypher query and convert results to Python objects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The Cypher query string. |
required |
params
|
dict | None
|
Optional query parameters. |
None
|
node_id_to_object
|
dict | None
|
Optional cache mapping node database IDs to objects. |
None
|
object_key_to_node
|
dict | None
|
Optional cache mapping objects to the nodes they were converted from, populated in place. Pass it on to a subsequent save to update the existing nodes instead of creating duplicates. Only the objects a row returns directly are recorded, not the ones nested inside them, so a query must return whatever it needs seeded. Keys are the objects themselves, which matches "hash" integration mode only; a save under "id" mode keys on object ids and misses every entry. |
None
|
Returns:
| Type | Description |
|---|---|
list[list[object]]
|
A list of rows, where each row is a list of Python objects |
list[list[object]]
|
converted from Node instances in the query results. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If object_key_to_node is given and a converted object is not hashable. |
Source code in src/fieldz_kb/lpg/session.py
reset_context
Replace the conversion context with a fresh one.
Clears all cached node classes and type mappings while keeping the same database connection.
save_from_object
save_from_object(object_: object, integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None, object_key_to_node: dict | None = None) -> None
Save a single object to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object_
|
object
|
The object to save. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to handle duplicate objects ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from integration logic. |
None
|
object_key_to_node
|
dict | None
|
Optional cache mapping object keys to their nodes, updated in place. Pass the same dict across calls to integrate objects shared between them. Keys are the objects themselves under "hash" integration mode and their ids under "id" mode, so a shared cache is only valid while the mode is constant. |
None
|
Source code in src/fieldz_kb/lpg/session.py
save_from_objects
save_from_objects(objects: list[object], integration_mode: Literal['hash', 'id'] = 'id', exclude_from_integration: tuple[type, ...] | None = None, object_key_to_node: dict | None = None) -> None
Save multiple objects to the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
objects
|
list[object]
|
The objects to save. |
required |
integration_mode
|
Literal['hash', 'id']
|
How to handle duplicate objects ("hash" or "id"). |
'id'
|
exclude_from_integration
|
tuple[type, ...] | None
|
Types to exclude from integration logic. |
None
|
object_key_to_node
|
dict | None
|
Optional cache mapping object keys to their nodes, updated in place. Pass the same dict across calls to integrate objects shared between them. Keys are the objects themselves under "hash" integration mode and their ids under "id" mode, so a shared cache is only valid while the mode is constant. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a node is not a subclass of BaseNode. |