🔍 Code Extractor

function get_reference_municipalities_by_id

Maturity: 39

Retrieves a single Reference_Municipalities node from a Neo4j graph database by matching its unique ID property.

File:
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
Lines:
616 - 623
Complexity:
simple

Purpose

This function queries a Neo4j database to fetch a specific municipality reference node using its ID. It's designed for retrieving individual municipality records from a graph database, typically used in applications that need to look up municipality information by a known identifier. The function returns the first matching node or None if no match is found.

Source Code

def get_reference_municipalities_by_id(id):
    """Get a Reference_Municipalities node by its ID"""
    query = """
    MATCH (n:Reference_Municipalities {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 Reference_Municipalities node to retrieve. Expected to be a string or integer that matches the 'id' property of a node in the Neo4j database. This parameter is used in a parameterized Cypher query to prevent injection attacks.

Return Value

Returns a single Reference_Municipalities node object (typically a dictionary or Neo4j node object) if a matching node is found, or None if no node with the specified ID exists. The returned node contains all properties associated with that Reference_Municipalities record in the database.

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=None):
    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 get a municipality by ID
municipality = get_reference_municipalities_by_id("12345")

if municipality:
    print(f"Found municipality: {municipality}")
else:
    print("Municipality not found")

Best Practices

  • Ensure the run_query function properly handles database connections and closes them after use to prevent connection leaks
  • Always check if the return value is None before accessing node properties to avoid AttributeError
  • 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
  • The function assumes run_query returns a list; ensure this contract is maintained
  • For production use, implement proper logging for debugging failed lookups
  • Consider adding type hints for better code documentation and IDE support

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_reference_municipalities_by_uid 91.9% similar

    Retrieves a single Reference_Municipalities node from a Neo4j graph database by matching its unique identifier (UID).

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_all_reference_municipalities 84.4% similar

    Queries a Neo4j graph database to retrieve Reference_Municipalities nodes with a configurable limit on the number of results returned.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function get_dbo_establishment_with_references_municipalities_reference_municipalities 74.7% similar

    Retrieves Reference_Municipalities nodes from a Neo4j graph database that are connected to a specific dbo_Establishment node via a REFERENCES_MUNICIPALITIES relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_reference_municipalities 74.4% similar

    Creates a new Reference_Municipalities node in a Neo4j graph database with the specified properties and returns the created node.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
  • function create_references_municipalities_relationship 64.1% similar

    Creates a REFERENCES_MUNICIPALITIES relationship in a Neo4j graph database between a dbo_Establishment node and a Reference_Municipalities node, with optional properties on the relationship.

    From: /tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
← Back to Browse