function get_lims_tests_by_id
Retrieves a single LIMS_Tests node from a Neo4j graph database by its unique identifier.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
460 - 467
simple
Purpose
This function queries a Neo4j database to fetch a specific LIMS_Tests node using its ID. It's designed for Laboratory Information Management System (LIMS) data retrieval, allowing users to access test records stored in a graph database. The function returns the first matching node or None if no match is found.
Source Code
def get_lims_tests_by_id(id):
"""Get a LIMS_Tests node by its ID"""
query = """
MATCH (n:LIMS_Tests {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_Tests node to retrieve. Expected to be a string or integer that matches the 'id' property of a LIMS_Tests node in the Neo4j database. This parameter is used in a parameterized Cypher query to prevent injection attacks.
Return Value
Returns a dictionary-like object representing the LIMS_Tests node if found, containing all properties of the node. Returns None if no node with the specified ID exists. The exact structure depends on the properties stored in the LIMS_Tests node in the database.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
# Assuming run_query is defined and Neo4j connection is configured
from neo4j import GraphDatabase
# Define run_query helper 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
test_id = 'TEST-12345'
lims_test = get_lims_tests_by_id(test_id)
if lims_test:
print(f"Found test: {lims_test}")
else:
print(f"No test found with ID: {test_id}")
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
- 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, implement proper logging to track database queries and potential issues
- Consider adding type hints for better code documentation (e.g., def get_lims_tests_by_id(id: str) -> Optional[Dict])
- Validate the id parameter before querying to ensure it meets expected format requirements
- Consider implementing caching for frequently accessed LIMS_Tests nodes to reduce database load
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_lims_sampletypetests_by_id 90.0% similar
-
function get_lims_sampletestresults_by_id 89.9% similar
-
function get_lims_tests_by_uid 89.4% similar
-
function get_lims_tests4pathogens_by_id 89.2% similar
-
function get_lims_requests_by_id 86.3% similar