@@ -230,6 +230,7 @@ def add_relationship(
230230 CREATE (a)-[:{ ":" .join (self ._clean_labels ([predicate .name ]))} {{ Description: '{ safe_desc } ' }}]->(b)
231231 RETURN a, b
232232 """
233+ self .ensure_database (brain_id )
233234 result = self .driver .execute_query (cypher_query , database_ = brain_id )
234235 return result
235236
@@ -248,6 +249,7 @@ def search_graph(self, nodes: list[Node], brain_id: str) -> list[Node]:
248249 queries .append (query )
249250
250251 cypher_query = " UNION " .join (queries )
252+ self .ensure_database (brain_id )
251253 result = self .driver .execute_query (cypher_query , database_ = brain_id )
252254 return result
253255
@@ -260,6 +262,7 @@ def node_text_search(self, text: str, brain_id: str) -> list[Node]:
260262 WHERE toLower(n.Name) CONTAINS toLower('{ text } ')
261263 RETURN n
262264 """
265+ self .ensure_database (brain_id )
263266 result = self .driver .execute_query (cypher_query , database_ = brain_id )
264267 return [
265268 Node (
@@ -315,6 +318,7 @@ def get_nodes_by_uuid(
315318 if with_relationships :
316319 cypher_query += ", r, m.uuid as m_uuid, m.name as m_name, labels(m) as m_labels, m.description as m_description, properties(m) as m_properties"
317320
321+ self .ensure_database (brain_id )
318322 result = self .driver .execute_query (cypher_query , database_ = brain_id )
319323
320324 if with_relationships :
@@ -362,6 +366,7 @@ def get_graph_entities(self, brain_id: str) -> list[str]:
362366 MATCH (n)
363367 RETURN DISTINCT labels(n) as labels
364368 """
369+ self .ensure_database (brain_id )
365370 result = self .driver .execute_query (cypher_query , database_ = brain_id )
366371 return [label for record in result .records for label in record ["labels" ]]
367372
@@ -373,6 +378,7 @@ def get_graph_relationships(self, brain_id: str) -> list[str]:
373378 CALL db.relationshipTypes() YIELD relationshipType
374379 RETURN relationshipType
375380 """
381+ self .ensure_database (brain_id )
376382 result = self .driver .execute_query (cypher_query , database_ = brain_id )
377383 return [record ["relationshipType" ] for record in result .records ]
378384
@@ -384,6 +390,7 @@ def get_by_uuid(self, uuid: str, brain_id: str) -> Node:
384390 MATCH (n) WHERE n.uuid = '{ uuid } '
385391 RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties
386392 """
393+ self .ensure_database (brain_id )
387394 result = self .driver .execute_query (cypher_query , database_ = brain_id )
388395 if not result .records or len (result .records ) == 0 :
389396 return None
@@ -403,6 +410,7 @@ def get_by_uuids(self, uuids: list[str], brain_id: str) -> list[Node]:
403410 MATCH (n) WHERE n.uuid IN ["{ '","' .join (uuids )} "]
404411 RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties
405412 """
413+ self .ensure_database (brain_id )
406414 result = self .driver .execute_query (cypher_query , database_ = brain_id )
407415 return [
408416 Node (
@@ -438,6 +446,7 @@ def get_by_identification_params(
438446 MATCH (n{ (":" + ":" .join (self ._clean_labels (entity_types ))) if entity_types else "" } ) { ("WHERE " + " AND " .join (where_clauses )) if where_clauses else "" }
439447 RETURN n.uuid as uuid, n.name as name, labels(n) as labels, n.description as description, properties(n) as properties
440448 """
449+ self .ensure_database (brain_id )
441450 result = self .driver .execute_query (cypher_query , database_ = brain_id )
442451 if not result .records or len (result .records ) == 0 :
443452 return None
@@ -457,6 +466,7 @@ def get_graph_property_keys(self, brain_id: str) -> list[str]:
457466 CALL db.propertyKeys() YIELD propertyKey
458467 RETURN propertyKey
459468 """
469+ self .ensure_database (brain_id )
460470 result = self .driver .execute_query (cypher_query , database_ = brain_id )
461471 return [record ["propertyKey" ] for record in result .records ]
462472
@@ -474,6 +484,7 @@ def get_neighbors(
474484 CASE WHEN startNode(r2) = c THEN 'out' ELSE 'in' END AS direction,
475485 c.uuid AS c_uuid, c.name AS c_name, labels(c) AS c_labels, c.description AS c_description, properties(c) AS c_properties
476486 """
487+ self .ensure_database (brain_id )
477488 result = self .driver .execute_query (cypher_query , database_ = brain_id )
478489
479490 neighbors = []
@@ -538,6 +549,7 @@ def get_neighbor_node_tuples(
538549 m.uuid as m_uuid, m.name as m_name, labels(m) as m_labels,
539550 m.description as m_description, properties(m) as m_properties, r as rel
540551 """
552+ self .ensure_database (brain_id )
541553 result = self .driver .execute_query (cypher_query , database_ = brain_id )
542554
543555 if not result .records :
@@ -632,6 +644,7 @@ def get_connected_nodes(
632644 n.uuid as n_uuid, n.name as n_name, labels(n) as n_labels, n.description as n_description, properties(n) as n_properties,
633645 CASE WHEN startNode(r) = n THEN 'out' ELSE 'in' END AS direction
634646 """
647+ self .ensure_database (brain_id )
635648 result = self .driver .execute_query (cypher_query , database_ = brain_id )
636649 return [
637650 (
@@ -727,6 +740,7 @@ def search_relationships(
727740 { "WHERE " + " AND " .join (filters ) if filters else "" }
728741 RETURN count(r) AS total
729742 """
743+ self .ensure_database (brain_id )
730744 result = self .driver .execute_query (cypher_query , database_ = brain_id )
731745 count_result = self .driver .execute_query (cypher_count , database_ = brain_id )
732746 total = 0
@@ -854,6 +868,7 @@ def search_entities(
854868 { "WHERE " + " AND " .join (filters ) if filters else "" }
855869 RETURN count(n) AS total
856870 """
871+ self .ensure_database (brain_id )
857872 result = self .driver .execute_query (cypher_query , database_ = brain_id )
858873 count_result = self .driver .execute_query (cypher_count , database_ = brain_id )
859874 total = 0
0 commit comments