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
|
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) -> 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
|
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. |
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) -> 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
|
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) -> 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
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If a node is not a subclass of BaseNode. |