No project description provided
Project description
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,
SequenceCriteriaModel,
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
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(SequenceCriteriaModel(name='Manufacturer'))
sequence_query.relationship_sequence.append(SequenceCriteriaModel()) # a relationship with no criteria specified
sequence_query.node_sequence.append(SequenceCriteriaModel() # a node with no criteria specified
sequence_query.relationship_sequence.append(SequenceCriteriaModel()) #a realtoinship with no criteria specified
sequence_query.node_sequence.append(SequenceCriteriaModel(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
nodes = await match_util.node_query(name='Manufacturer', criteria={name='Acme'})
for graph_id, node in nodes.items():
node.name = "Acme2"
await create_util.update_node(node=node)
- Update a relationship
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)
for graph_id, relationship in result.items():
relationship.design_revision = 4
await create_util.update_relationship(relationship=relationship)
- Delete a node
nodes = await match_util.node_query(name='Manufacturer', criteria={name='Acme'})
for graph_id, node in nodes.items():
await create_util.delete_node(node=node)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pydantic_neo4j-0.2.0.tar.gz
(7.5 kB
view details)
Built Distribution
File details
Details for the file pydantic_neo4j-0.2.0.tar.gz
.
File metadata
- Download URL: pydantic_neo4j-0.2.0.tar.gz
- Upload date:
- Size: 7.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88f49ceead6b41efb224638379d5f438b80a0586effe9b5f58ee4b9bc293d2ac |
|
MD5 | 1ffb87931bc7ca88725383c6693e7aa2 |
|
BLAKE2b-256 | b7ffd91f63f4e40837f27659e582cc5906509037387b4b6d85c47f2f20728d9a |
File details
Details for the file pydantic_neo4j-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_neo4j-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.5.1 CPython/3.11.3 Windows/10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 93056fa9a28bb1b616551789a105eaf8fba6e6052ac0a0bde009ff54d7572f4f |
|
MD5 | 6f4ee0def28dc6c0c982331f13e1ed6c |
|
BLAKE2b-256 | 150e5043bacb0c52b7e9dd19c8f8ef5e63ee893970a35e02e3d51a4dc088fc95 |