function check_document_exists_by_uid
Queries a Neo4j database to check if a ControlledDocument with a specific UID exists and returns the document object if found.
/tf/active/vicechatdev/CDocs/FC_sync.py
81 - 108
simple
Purpose
This function provides a lookup mechanism to verify document existence in a Neo4j graph database by UID. It's used for validation before operations like updates or deletions, preventing duplicate document creation, and retrieving document metadata. The function handles database errors gracefully and returns None if the document doesn't exist or if an error occurs.
Source Code
def check_document_exists_by_uid(cdoc_uid: str) -> Optional[ControlledDocument]:
"""
Check if a document with the given UID exists in the Neo4j database.
Args:
cdoc_uid: Document UID to check
Returns:
ControlledDocument: Document object if found, None otherwise
"""
try:
result = db.run_query(
"""
MATCH (doc:ControlledDocument {UID: $cdoc_uid})
RETURN doc
LIMIT 1
""",
{"cdoc_uid": cdoc_uid}
)
if result and len(result) > 0:
doc = ControlledDocument(result[0]['doc'])
return doc
return None
except Exception as e:
logger.error(f"Error checking document existence by UID: {e}")
return None
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
cdoc_uid |
str | - | positional_or_keyword |
Parameter Details
cdoc_uid: String identifier representing the unique ID (UID) of the ControlledDocument to search for in the Neo4j database. This should be a valid document UID that matches the UID property of ControlledDocument nodes in the database.
Return Value
Type: Optional[ControlledDocument]
Returns a ControlledDocument object if a document with the specified UID is found in the database. Returns None if no document exists with that UID or if an error occurs during the database query. The ControlledDocument object is instantiated from the Neo4j node properties.
Dependencies
loggingtypingCDocs.db.db_operationsCDocs.models.document
Required Imports
from typing import Optional
from CDocs.db import db_operations as db
from CDocs.models.document import ControlledDocument
import logging
Usage Example
from typing import Optional
from CDocs.db import db_operations as db
from CDocs.models.document import ControlledDocument
import logging
logger = logging.getLogger(__name__)
# Check if a document exists
doc_uid = "DOC-12345"
document = check_document_exists_by_uid(doc_uid)
if document:
print(f"Document found: {document}")
# Proceed with operations on the document
else:
print(f"No document found with UID: {doc_uid}")
# Handle missing document case
Best Practices
- Always check if the returned value is None before accessing document properties to avoid AttributeError
- Use this function before attempting to update or delete documents to ensure they exist
- The function logs errors but returns None, so check logs if unexpected None returns occur
- The LIMIT 1 clause ensures only one result is returned even if multiple documents share the same UID (which shouldn't happen with proper constraints)
- Consider implementing database constraints to ensure UID uniqueness at the database level
- Handle the None return case explicitly in calling code to distinguish between 'not found' and 'error occurred' scenarios
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function check_document_exists_by_doc_number 75.1% similar
-
function get_document 72.1% similar
-
function get_document_versions_from_db 69.3% similar
-
function node_exists 67.2% similar
-
function get_document_versions_from_db_v5 67.0% similar