function get_lims_samples_by_id
Retrieves a single LIMS_Samples node from a Neo4j graph database by its unique identifier.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
382 - 389
simple
Purpose
This function queries a Neo4j database to fetch a specific LIMS (Laboratory Information Management System) sample record using its ID. It's designed for retrieving individual sample data from a graph database where samples are stored as nodes with the label 'LIMS_Samples'. The function returns the first matching node or None if no match is found.
Source Code
def get_lims_samples_by_id(id):
"""Get a LIMS_Samples node by its ID"""
query = """
MATCH (n:LIMS_Samples {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 LIMS_Samples node to retrieve. Expected to be a string or integer that matches the 'id' property of a LIMS_Samples node in the Neo4j database. This parameter is used in a parameterized Cypher query to prevent injection attacks.
Return Value
Returns a single LIMS_Samples node object (typically a dictionary or Neo4j node object) if a matching record is found, or None if no record with the specified ID exists. The returned object contains all properties of the matched node. The function uses indexing (result[0]) to extract the first result from the query result list.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query function is defined and Neo4j connection is established
# Example run_query implementation:
from neo4j import GraphDatabase
driver = GraphDatabase.driver('bolt://localhost:7687', auth=('neo4j', 'password'))
def run_query(query, parameters=None):
with driver.session() as session:
result = session.run(query, parameters)
return [record['n'] for record in result]
# Using get_lims_samples_by_id
sample_id = 'SAMPLE_12345'
sample = get_lims_samples_by_id(sample_id)
if sample:
print(f'Found sample: {sample}')
else:
print(f'No sample found with ID: {sample_id}')
Best Practices
- Ensure the run_query function is properly implemented with error handling and connection management
- The function assumes run_query returns a list; verify this behavior matches your implementation
- Consider adding error handling for database connection failures or query execution errors
- The function returns None for non-existent IDs; ensure calling code handles this case appropriately
- Use parameterized queries (as done here with $id) to prevent Cypher injection attacks
- Consider adding type hints for better code documentation: def get_lims_samples_by_id(id: str) -> Optional[Dict]
- For production use, implement proper logging to track query execution and failures
- Consider adding validation to check if the id parameter is not None or empty before executing the query
- Ensure proper Neo4j driver cleanup and connection pooling in the run_query implementation
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_lims_sampletestresults_by_id 91.7% similar
-
function get_lims_samples_by_uid 90.4% similar
-
function get_lims_sampletypes_by_id 88.8% similar
-
function get_all_lims_samples 86.9% similar
-
function get_lims_sampletypetests_by_id 86.7% similar