function get_dbo_establishment_by_id
Retrieves a single dbo_Establishment node from a Neo4j graph database by its unique ID.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
733 - 740
simple
Purpose
This function queries a Neo4j database to fetch a specific establishment node identified by its ID. It's designed for retrieving individual establishment records from a graph database, likely part of a larger application dealing with establishment data (businesses, organizations, etc.). The function returns the first matching node or None if no match is found.
Source Code
def get_dbo_establishment_by_id(id):
"""Get a dbo_Establishment node by its ID"""
query = """
MATCH (n:dbo_Establishment {id: $id})
RETURN n
"""
result = run_query(query, {"id": id})
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
id |
- | - | positional_or_keyword |
Parameter Details
id: The unique identifier for the dbo_Establishment node to retrieve. Expected to be a string or integer that matches the 'id' property of nodes labeled as 'dbo_Establishment' in the Neo4j database. This parameter is used directly in the Cypher query as a parameter to prevent injection attacks.
Return Value
Returns the first dbo_Establishment node object from the query results if found, or None if no matching node exists. The node object typically contains all properties associated with that establishment in the database. The return type is either a Neo4j node record (dictionary-like object) or None.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is already defined and Neo4j is configured
from neo4j import GraphDatabase
# Define or import run_query function (example implementation)
def run_query(query, params):
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
with driver.session() as session:
result = session.run(query, params)
return [record['n'] for record in result]
driver.close()
# Use the function
establishment_id = '12345'
establishment = get_dbo_establishment_by_id(establishment_id)
if establishment:
print(f'Found establishment: {establishment}')
else:
print('Establishment not found')
Best Practices
- Ensure the run_query function properly handles database connections and closes them to prevent connection leaks
- Always check if the return value is None before accessing properties to avoid AttributeError
- The function uses parameterized queries ($id) which is good practice for preventing Cypher injection attacks
- Consider adding error handling for database connection failures or query execution errors
- The function assumes run_query returns a list; ensure this contract is maintained
- For production use, consider adding logging to track query execution and failures
- If querying by ID frequently, ensure the 'id' property is indexed in Neo4j for optimal performance
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_establishment_by_uid 89.1% similar
-
function get_dbo_establishmentcycles_by_id 88.2% similar
-
function get_all_dbo_establishment 81.7% similar
-
function get_dbo_establishmentcycles_by_uid 80.1% similar
-
function get_dbo_establishmentcycles_with_references_establishment_dbo_establishment 77.5% similar