Package: pydantic-neo4j
Purpose of the PackageTo translate from Pydantic models to Neo4j GraphsGetting StartedInstall the packagepipinstallpydantic-neo4jUsageImport the package and modelsfrompydantic_neo4jimport(PydanticNeo4j,RelationshipQueryModel,NodeModel,SequenceCriteriaNodeModel,SequenceCriteriaRelationshipModel,SequenceQueryModel,SequenceNodeModel)Initialize the class and get the utilitiespydantic_neo4j=PydanticNeo4j(username='neo4j',password='neo4j',uri='neo4j://localhost:7687)match_util=pydantic_neo4j.match_utilitiescreate_util=pydantic_neo4j.create_utilitiesdatabase_operations=pydantic_neo4j.database_operationsCreate some Pydantic modelsclassManufacturer(NodeModel):name:strclassDesign(NodeModel):color:strclassComponent(NodeModel):name:strclassIsOrderable(RelationshipModel):passclassProduces(RelationshipModel):design_revision:intCreate the nodes and relationships. All relationships must have a start_node and end_noderelationships=[]manufacturer=Manufacturer(name="Acme")design=Design(color="red")produces=Produces(design_revision=3,start_node=manufacturer,end_node=design)Add to listrelationships.append(produces)Create another relationship and add it to the listcomponent=Component(component_type="widget")is_orderable=IsOrderable(start_node=design,end_node=component)relationships.append(is_orderable)Add the nodes and relationships to the graphawaitcreate_util.create_relationships(relationships=relationships)Query the graph for a single node. Lets find a manufacturernodes=awaitmatch_util.node_query(node_name='Manufacturer')___Query the graph for multiple nodes. Lets find all nodes that are activenodes=awaitmatch_util.node_query(criteria={'active':True})Query the graph for a single relationship. Lets find a manufacturer that produces a red designThis will be depreciated soon, use sequence query insteadquery=RelationshipQueryModel(start_node_name="Manufacturer",start_criteria={},end_node_name="Design",end_criteria={"color":"red"},relationship_name="Produces",relationship_criteria={})result=awaitmatch_util.match_relationship(query=query)Query the graph for multiple relationships. Lets find all manufacturers that make a widget componentThis uses a sequence, which is a series of relationships. Similar to Neo4j Pathsequence_query=SequenceQueryModel()sequence_query.node_sequence.append(SequenceCriteriaNodeModel(name='Manufacturer'))sequence_query.relationship_sequence.append(SequenceCriteriaRelationshipModel())# a relationship with no criteriasequence_query.node_sequence.append(SequenceCriteriaNodeModel()# a node with no criteria specifiedsequence_query.relationship_sequence.append(SequenceCriteriaRelationshipModel())#a realtoinship with no criteriasequence_query.node_sequence.append(SequenceCriteriaNodeModel(component_type="widget",include_with_return=True))The sequence query must always have 1 more node than relationship.The order is important, and is a sequence. node - relationship - node - relationship - noderesult=awaitmatch_util.sequence_query(sequence_query=sequence_query)Run a specific query, lets delete everythingawaitdatabase_operations.run_query(query=f"match (n) detach delete n")Not ImplementedUpdate a nodeUpdate a sequenceDelete a node