Skip to content

graflo.db.tigergraph.document_utils

Document helpers for TigerGraph REST upsert and fetch operations.

clean_document(doc)

Remove internal keys that shouldn't be stored in the database.

Removes keys starting with "_" except "_key".

Source code in graflo/db/tigergraph/document_utils.py
def clean_document(doc: dict[str, Any]) -> dict[str, Any]:
    """
    Remove internal keys that shouldn't be stored in the database.

    Removes keys starting with "_" except "_key".
    """
    return {k: v for k, v in doc.items() if not k.startswith("_") or k == "_key"}

extract_id(doc, match_keys)

Extract vertex ID from document based on match keys.

For composite keys, concatenates values with an underscore '_'. Prefers '_key' if present.

Source code in graflo/db/tigergraph/document_utils.py
def extract_id(
    doc: dict[str, Any] | None,
    match_keys: list[str] | tuple[str, ...],
) -> str | None:
    """
    Extract vertex ID from document based on match keys.

    For composite keys, concatenates values with an underscore '_'.
    Prefers '_key' if present.
    """
    if not doc:
        return None

    if "_key" in doc and doc["_key"]:
        return str(doc["_key"])

    if len(match_keys) > 1:
        try:
            id_parts = [str(doc[key]) for key in match_keys]
            return "_".join(id_parts)
        except KeyError:
            return None

    if len(match_keys) == 1:
        key = match_keys[0]
        if key in doc and doc[key] is not None:
            return str(doc[key])

    return None