Items
Any data class in pymemri inherits from Item
. It is a base class for items with some handy functionalities to create new items and edges, retrieve all edges to other items, and sync with the pod.
With the Item
and Edge
classes we can create an item and its surrounding graph. The schema is defined in schema.py, in general we want to use the from_data staticmethod to generate new items, because it ensures that edges are linked from both the source and the target object. Let's make a new item and add it to the pod.
class MyItem(Item):
properties = Item.properties + ["name", "age"]
edges = Item.edges + ["friend"]
def __init__(self, name: str=None, age: int=None,friend: list=None, **kwargs):
super().__init__(**kwargs)
self.name = name
self.age = age
self.friend = fried if friend is not None else []
from pymemri.pod.client import PodClient
client = PodClient()
assert client.add_to_schema(MyItem(name="abc", age=1))
x = MyItem(name="me", age=30)
target = MyItem(name="my friend", age=31)
client.create(target)
x.add_edge("friend", MyItem(name="my friend", age=31))
We can now create our MyItem
, as a side-effect of creating it, it will receive an id
print(x.id)
assert client.create(x)
print(x.id)
y = client.get(x.id)
assert len(y.friend) > 0
assert y.friend[0].name == "my friend"
assert y.name == "me"
assert y.age == 30
# One year later
y.age = 31
y.add_edge("friend", MyItem(name="my friend2", age=29))
y.update(client)
assert y.age == 31
assert len(y.friend) == 2
y.friend
y.to_json(dates=False)