Skip to content

Commit db0a397

Browse files
authored
Graph Collections (#52)
* Highlighting boolean values * Adding vertex and edge collection skeleton * Refactoring serializers * Using randomized graph name * Improving helper types * Facilitating edge and vertex collection creation * Vertex collection management * Edge collection management * Adding cluster testcase * Adding note about dictionary-like indexing * Inserting and retrieving vertex documents * Moving methods from StandardCollection to base Collection so they are available to other subclasses * Adding CRUD for vertex collections * Adding "has" for vertex collections * Marking tests as asyncio * Inserting and retrieving edges * Event loop scope * Event loop scope again * Updating edge * Edges CRUD * Extra edge methods * Fixing lint * Added github gist example * Adding graph docs * Adding graphs example in the readme
1 parent 741e3a3 commit db0a397

17 files changed

+3986
-845
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,67 @@ async def main():
7373
student_names = []
7474
async for doc in cursor:
7575
student_names.append(doc["name"])
76+
```
77+
78+
Another example with [graphs](https://docs.arangodb.com/stable/graphs/):
7679

80+
```python
81+
async def main():
82+
from arangoasync import ArangoClient
83+
from arangoasync.auth import Auth
84+
85+
# Initialize the client for ArangoDB.
86+
async with ArangoClient(hosts="http://localhost:8529") as client:
87+
auth = Auth(username="root", password="passwd")
88+
89+
# Connect to "test" database as root user.
90+
db = await client.db("test", auth=auth)
91+
92+
# Get the API wrapper for graph "school".
93+
if await db.has_graph("school"):
94+
graph = db.graph("school")
95+
else:
96+
graph = await db.create_graph("school")
97+
98+
# Create vertex collections for the graph.
99+
students = await graph.create_vertex_collection("students")
100+
lectures = await graph.create_vertex_collection("lectures")
101+
102+
# Create an edge definition (relation) for the graph.
103+
edges = await graph.create_edge_definition(
104+
edge_collection="register",
105+
from_vertex_collections=["students"],
106+
to_vertex_collections=["lectures"]
107+
)
108+
109+
# Insert vertex documents into "students" (from) vertex collection.
110+
await students.insert({"_key": "01", "full_name": "Anna Smith"})
111+
await students.insert({"_key": "02", "full_name": "Jake Clark"})
112+
await students.insert({"_key": "03", "full_name": "Lisa Jones"})
113+
114+
# Insert vertex documents into "lectures" (to) vertex collection.
115+
await lectures.insert({"_key": "MAT101", "title": "Calculus"})
116+
await lectures.insert({"_key": "STA101", "title": "Statistics"})
117+
await lectures.insert({"_key": "CSC101", "title": "Algorithms"})
118+
119+
# Insert edge documents into "register" edge collection.
120+
await edges.insert({"_from": "students/01", "_to": "lectures/MAT101"})
121+
await edges.insert({"_from": "students/01", "_to": "lectures/STA101"})
122+
await edges.insert({"_from": "students/01", "_to": "lectures/CSC101"})
123+
await edges.insert({"_from": "students/02", "_to": "lectures/MAT101"})
124+
await edges.insert({"_from": "students/02", "_to": "lectures/STA101"})
125+
await edges.insert({"_from": "students/03", "_to": "lectures/CSC101"})
126+
127+
# Traverse the graph in outbound direction, breath-first.
128+
query = """
129+
FOR v, e, p IN 1..3 OUTBOUND 'students/01' GRAPH 'school'
130+
OPTIONS { bfs: true, uniqueVertices: 'global' }
131+
RETURN {vertex: v, edge: e, path: p}
132+
"""
133+
134+
async with await db.aql.execute(query) as cursor:
135+
async for doc in cursor:
136+
print(doc)
77137
```
78138

79139
Please see the [documentation](https://python-arango-async.readthedocs.io/en/latest/) for more details.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy