motleycrew.storage.graph_store

Classes

MotleyGraphStore()

Abstract class for a graph database store.

class motleycrew.storage.graph_store.MotleyGraphStore

Bases: ABC

Abstract class for a graph database store.

abstract check_node_exists_by_class_and_id(node_class: Type[MotleyGraphNode], node_id: int) bool

Check if a node of given class with given id is present in the database.

Parameters:
  • node_class – Python class of the node

  • node_id – id of the node

abstract check_node_exists(node: MotleyGraphNode) bool

Check if the given node is present in the database.

Parameters:

node – node to check

Returns:

whether the node is present in the database

abstract check_relation_exists(from_node: MotleyGraphNode, to_node: MotleyGraphNode, label: str | None) bool

Check if a relation exists between two nodes with given label.

Parameters:
  • from_node – starting node

  • to_node – ending node

  • label – relation label. If None, check if any relation exists between the nodes.

abstract get_node_by_class_and_id(node_class: Type[MotleyGraphNodeType], node_id: int) MotleyGraphNodeType | None

Retrieve the node of given class with given id if it is present in the database. Otherwise, return None.

Parameters:
  • node_class (Type[MotleyGraphNodeType])

  • node_id (int)

Return type:

MotleyGraphNodeType, None

abstract insert_node(node: MotleyGraphNode)

Insert a new node, populate its id and freeze it. If node table or some columns do not exist, this method also creates them.

Parameters:

node (MotleyGraphNode)

Returns:

abstract create_relation(from_node: MotleyGraphNode, to_node: MotleyGraphNode, label: str) None

Create a relation with given label between existing nodes. If relation table does not exist, this method also creates them.

Parameters:

Returns:

abstract upsert_triplet(from_node: MotleyGraphNode, to_node: MotleyGraphNode, label: str)

Create a relation with given label between nodes, if such does not already exist. If the nodes do not already exist, create them too. This method also creates and/or updates all necessary tables.

Parameters:

Returns:

abstract delete_node(node: MotleyGraphNode) None

Delete a given node and its relations.

Parameters:

node (MotleyGraphNode)

Returns:

abstract update_property(node: MotleyGraphNode, property_name: str)

Update a graph node’s property with the corresponding value from the node object.

Parameters:

Returns:

ensure_node_table(node_class: Type[MotleyGraphNode]) str

Create a table for storing nodes of that class if such does not already exist. If it does exist, create all missing columns.

Parameters:

node_class (Type[MotleyGraphNode])

Returns:

Table name

Return type:

str

ensure_relation_table(from_class: Type[MotleyGraphNode], to_class: Type[MotleyGraphNode], label: str)

Create a table for storing relations from from_node-like nodes to to_node-like nodes, if such does not already exist.

Parameters:

Returns:

run_cypher_query(query: str, parameters: dict | None = None, container: Type[MotleyGraphNodeType] | None = None) list[list | MotleyGraphNodeType]

Run a Cypher query and return the results. If container class is provided, deserialize the results into objects of that class.

Parameters:
  • query (dict, None)

  • parameters (dict, optional)

  • container (Type[MotleyGraphNodeType], optional)

Returns: