function create_dbo_texttranslations
Creates a new node labeled 'dbo_TextTranslations' in a Neo4j graph database with the specified properties and returns the created node.
/tf/active/vicechatdev/neo4j_schema/neo4j_python_snippets.py
1102 - 1111
simple
Purpose
This function provides a programmatic way to insert text translation records into a Neo4j graph database. It dynamically constructs a Cypher CREATE query based on the provided properties dictionary, executes the query using a helper function 'run_query', and returns the newly created node. This is useful for applications that need to store and manage text translations in a graph database structure, such as multilingual content management systems or translation services.
Source Code
def create_dbo_texttranslations(properties):
"""Create a new dbo_TextTranslations node"""
props_list = ', '.join([f"n.{prop} = ${prop}" for prop in properties.keys()])
query = f"""
CREATE (n:dbo_TextTranslations)
SET {props_list}
RETURN n
"""
result = run_query(query, properties)
return result[0] if result else None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
properties |
- | - | positional_or_keyword |
Parameter Details
properties: A dictionary containing key-value pairs representing the properties to be set on the new dbo_TextTranslations node. Keys should be valid property names (strings) and values can be any data type supported by Neo4j (strings, numbers, booleans, lists, etc.). Example: {'text_id': '123', 'language': 'en', 'translation': 'Hello World'}
Return Value
Returns the first node from the query result if the creation was successful, which is typically a dictionary-like object representing the created Neo4j node with all its properties. Returns None if the query execution fails or returns an empty result set. The returned node object can be used to access the properties of the newly created node.
Dependencies
neo4j
Required Imports
from neo4j import GraphDatabase
Usage Example
from neo4j import GraphDatabase
# Assuming run_query function is defined
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()
# Create a new text translation node
properties = {
'text_id': 'txt_001',
'language': 'es',
'original_text': 'Hello',
'translated_text': 'Hola',
'created_at': '2024-01-15'
}
node = create_dbo_texttranslations(properties)
if node:
print(f'Created node with properties: {node}')
else:
print('Failed to create node')
Best Practices
- Ensure the 'properties' dictionary contains only valid property names and values that are compatible with Neo4j data types
- Validate and sanitize input properties before passing them to this function to prevent injection attacks
- The function relies on parameterized queries which helps prevent Cypher injection, but property keys are directly interpolated into the query string - ensure property keys come from trusted sources
- Handle the None return value appropriately in calling code to detect creation failures
- Consider adding error handling and logging within this function or in the run_query helper
- Ensure proper database connection management in the run_query function (connection pooling, proper closing)
- Consider adding constraints or indexes on the dbo_TextTranslations label in Neo4j for better performance
- Be aware that this function does not check for duplicate nodes - consider adding uniqueness constraints if needed
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function get_all_dbo_texttranslations 74.5% similar
-
function get_dbo_texttranslations_by_id 74.5% similar
-
function create_dbo_tnv 74.4% similar
-
function create_dbo_treatments 71.6% similar
-
function get_dbo_texttranslations_by_uid 70.3% similar