function get_dbo_flocktypes_by_id
Retrieves a single dbo_FlockTypes node from a Neo4j graph database by its unique ID using a Cypher query.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
811 - 818
simple
Purpose
This function queries a Neo4j database to fetch a specific dbo_FlockTypes node matching the provided ID. It's designed for retrieving individual flock type records from the database, returning the complete node if found or None if no matching node exists. This is useful for lookup operations, data validation, or retrieving specific flock type information in applications managing livestock or bird populations.
Source Code
def get_dbo_flocktypes_by_id(id):
"""Get a dbo_FlockTypes node by its ID"""
query = """
MATCH (n:dbo_FlockTypes {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 of the dbo_FlockTypes node to retrieve. Expected to be a value that matches the 'id' property of nodes in the Neo4j database. Type is not explicitly constrained but should match the data type used for IDs in the database (typically string or integer).
Return Value
Returns the first matching dbo_FlockTypes node as a dictionary-like object containing all properties of the node if found. Returns None if no node with the specified ID exists. The node object structure depends on the properties defined in the Neo4j database schema for dbo_FlockTypes nodes.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j is configured
# Example run_query 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()
from neo4j import GraphDatabase
# Retrieve a flock type by ID
flock_type = get_dbo_flocktypes_by_id('FT001')
if flock_type:
print(f"Found flock type: {flock_type}")
else:
print("Flock type not found")
# Example with integer ID
flock_type = get_dbo_flocktypes_by_id(123)
if flock_type:
print(f"Flock type properties: {flock_type['name']}, {flock_type['description']}")
Best Practices
- Ensure the run_query function properly handles database connections and closes them to prevent connection leaks
- Validate the ID parameter before passing it to the function to prevent injection attacks or invalid queries
- 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
- Consider adding type hints to the function signature for better code documentation and IDE support
- For production use, implement proper logging to track database queries and potential issues
- Consider caching results if the same IDs are frequently queried to reduce database load
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_flocktypes_by_uid 90.5% similar
-
function get_dbo_flocks_by_id 89.4% similar
-
function get_all_dbo_flocktypes 87.6% similar
-
function get_dbo_flocks_with_references_flocktypes_dbo_flocktypes 86.1% similar
-
function get_dbo_flocks_by_uid 81.6% similar