function get_dbo_product_by_id
Retrieves a single dbo_Product node from a Neo4j graph database by its unique ID.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1006 - 1013
simple
Purpose
This function queries a Neo4j database to fetch a specific product node labeled as 'dbo_Product' using its ID property. It's designed for retrieving individual product records from a graph database, typically used in e-commerce or inventory management systems where products are stored as graph nodes. The function returns the first matching result or None if no product is found with the given ID.
Source Code
def get_dbo_product_by_id(id):
"""Get a dbo_Product node by its ID"""
query = """
MATCH (n:dbo_Product {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_Product node to retrieve. Expected to be a string or integer that matches the 'id' property of a dbo_Product 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 dbo_Product node if found, containing all properties of the matched node. Returns None if no product with the specified ID exists in the database. The exact structure depends on the properties stored in the dbo_Product nodes (e.g., name, price, description, etc.). The return value is the first element of the query result array.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query is defined elsewhere
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
product = get_dbo_product_by_id('PROD-12345')
if product:
print(f"Found product: {product}")
else:
print("Product not found")
# Example with integer ID
product = get_dbo_product_by_id(12345)
if product:
print(f"Product name: {product.get('name')}")
print(f"Product price: {product.get('price')}")
Best Practices
- Always check if the return value is None before accessing properties to avoid AttributeError
- Ensure the run_query function properly handles database connections and closes them to prevent connection leaks
- Use parameterized queries (as done here with $id) to prevent Cypher injection attacks
- Consider adding error handling for database connection failures or query execution errors
- Index the 'id' property on dbo_Product nodes in Neo4j for optimal query performance
- Validate the id parameter before passing it to the function to ensure it matches expected format
- Consider adding logging for debugging and monitoring database access patterns
- The function assumes run_query returns a list; ensure this contract is maintained
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_dbo_product_by_uid 87.7% similar
-
function get_all_dbo_product 79.5% similar
-
function get_dbo_tnv_by_id 76.2% similar
-
function get_dbo_concepts_by_id 73.8% similar
-
function get_dbo_usergroup_by_id 73.6% similar