Skip to main content

Purpose of the Package

  • To translate from Pydantic models to Neo4j Graphs

Getting Started

  • Install the package
pip install pydantic-neo4j

Usage

  • Import the package and models
from pydantic_neo4j import (PydanticNeo4j, 
                            RelationshipQueryModel,
                            NodeModel,
                            SequenceCriteriaNodeModel,
                            SequenceCriteriaRelationshipModel,  
                            SequenceQueryModel, 
                            SequenceNodeModel)

  • Initialize the class and get the utilities
pydantic_neo4j = PydanticNeo4j(username='neo4j', password='neo4j', uri='neo4j://localhost:7687)
match_util = pydantic_neo4j.match_utilities
create_util = pydantic_neo4j.create_utilities
database_operations = pydantic_neo4j.database_operations

  • Create some Pydantic models
class Manufacturer(NodeModel):
    name: str

class Design(NodeModel):
    color: str
    
class Component(NodeModel):
    name: str
    
class IsOrderable(RelationshipModel):
    pass

class Produces(RelationshipModel):
    design_revision: int

  • Create the nodes and relationships. All relationships must have a start_node and end_node
relationships = []

manufacturer = Manufacturer(name="Acme")
design = Design(color="red")
produces = Produces(design_revision=3, start_node=manufacturer, end_node=design)
  • Add to list
relationships.append(produces)
  • Create another relationship and add it to the list
component = Component(component_type="widget")
is_orderable = IsOrderable(start_node=design, end_node=component)

relationships.append(is_orderable)
  • Add the nodes and relationships to the graph
await create_util.create_relationships(relationships=relationships)

  • Query the graph for a single node. Lets find a manufacturer
nodes = await match_util.node_query(node_name='Manufacturer')
___
  • Query the graph for multiple nodes. Lets find all nodes that are active
nodes = await match_util.node_query(criteria={'active': True})

  • Query the graph for a single relationship. Lets find a manufacturer that produces a red design
  • This will be depreciated soon, use sequence query instead
query = RelationshipQueryModel(
    start_node_name="Manufacturer",
    start_criteria={},
    end_node_name="Design",
    end_criteria={"color": "red"},
    relationship_name="Produces",
    relationship_criteria={})
result = await match_util.match_relationship(query=query)

  • Query the graph for multiple relationships. Lets find all manufacturers that make a widget component
  • This uses a sequence, which is a series of relationships. Similar to Neo4j Path
sequence_query = SequenceQueryModel()

sequence_query.node_sequence.append(SequenceCriteriaNodeModel(name='Manufacturer'))
sequence_query.relationship_sequence.append(SequenceCriteriaRelationshipModel()) # a relationship with no criteria
sequence_query.node_sequence.append(SequenceCriteriaNodeModel() # a node with no criteria specified
sequence_query.relationship_sequence.append(SequenceCriteriaRelationshipModel()) #a realtoinship with no criteria
sequence_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 - node
result = await match_util.sequence_query(sequence_query=sequence_query)

  • Run a specific query, lets delete everything
await database_operations.run_query(query=f"match (n) detach delete n")

Not Implemented

  • Update a node

  • Update a sequence

  • Delete a node

Metadata

Release files for pydantic-neo4j 0.3.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-neo4j 0.3.7
File Size Uploaded
pydantic_neo4j-0.3.7.tar.gz 7.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-neo4j 0.3.7
File Interpreter ABI Platform
pydantic_neo4j-0.3.7-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / pydantic_neo4j-0.3.7.tar.gz

Download URL pydantic_neo4j-0.3.7.tar.gz
Size 7.8 kB
Tags Source
SHA-256 checksum
How to use checksums
1c58b1a97ebbe7116481f736b4dc992776120f7d867d80d13d7e9ee263c566c2
BLAKE2b-256 checksum
How to use checksums
7255e3e87beecc6c61545fb7972f3b0665239bd4e1c86c443354981cbfd46a8d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Windows/10

Release files / pydantic_neo4j-0.3.7-py3-none-any.whl

Download URL pydantic_neo4j-0.3.7-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25c2b1c5094e60f36a764d397cb7d281b2a04ab22cba93c9809ada34c1744bf2
BLAKE2b-256 checksum
How to use checksums
9095c89ab49b62879475ed779ea7849a2f88199253fc9621b12b4b007b301459
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.5.1 CPython/3.11.3 Windows/10

Release history Release notifications | RSS feed

This release

0.3.7 This release

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.4

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page