function get_dbo_flocks_by_uid
Retrieves a single dbo_Flocks node from a Neo4j graph database by matching its unique identifier (UID).
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
859 - 866
simple
Purpose
This function queries a Neo4j database to find and return a specific dbo_Flocks node based on its UID property. It's designed for retrieving individual flock records from a graph database, likely part of a larger system managing biological or organizational flock data. The function returns the first matching node or None if no match is found.
Source Code
def get_dbo_flocks_by_uid(uid):
"""Get a dbo_Flocks node by its UID"""
query = """
MATCH (n:dbo_Flocks {UID: $uid})
RETURN n
"""
result = run_query(query, {"uid": uid})
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
uid |
- | - | positional_or_keyword |
Parameter Details
uid: The unique identifier (UID) of the dbo_Flocks node to retrieve. Expected to be a string or numeric value that matches the UID property stored in the Neo4j database. This should be a valid, existing UID for a dbo_Flocks node.
Return Value
Returns a single dbo_Flocks node object (typically a dictionary or Neo4j node object) if a matching UID is found, or None if no node with the specified UID exists. The returned node contains all properties associated with that dbo_Flocks record in the database. The function extracts the first element from the result list, assuming UIDs are unique.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j is configured
from neo4j import GraphDatabase
# Define run_query helper function (example implementation)
def run_query(query, parameters):
driver = GraphDatabase.driver("bolt://localhost:7687", auth=("neo4j", "password"))
with driver.session() as session:
result = session.run(query, parameters)
return [record["n"] for record in result]
driver.close()
# Use the function to retrieve a flock by UID
flock_uid = "FLOCK_12345"
flock_node = get_dbo_flocks_by_uid(flock_uid)
if flock_node:
print(f"Found flock: {flock_node}")
else:
print(f"No flock found with UID: {flock_uid}")
Best Practices
- Ensure the run_query function is properly defined and handles Neo4j connection management, including proper session cleanup
- Validate that the uid parameter is not None or empty before calling this function to avoid unnecessary database queries
- Consider adding error handling for database connection failures or query execution errors
- The function assumes UIDs are unique; ensure database constraints enforce UID uniqueness on dbo_Flocks nodes
- Consider adding type hints for better code documentation (e.g., def get_dbo_flocks_by_uid(uid: str) -> Optional[Dict])
- Be aware that this function returns only the first result; if multiple nodes share the same UID (which shouldn't happen), only one will be returned
- Consider implementing connection pooling in the run_query function for better performance in production environments
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_flocktypes_by_uid 91.3% similar
-
function get_dbo_flocks_by_id 90.2% similar
-
function get_dbo_interventionprotocolflocks_by_uid 82.7% similar
-
function get_dbo_flocktypes_by_id 81.6% similar
-
function get_dbo_usergroup_by_uid 78.0% similar