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 an email item and create it in the pod.
print(f"Failed to start importer on {url}:\n{res.status_code}: {res.text}")
else:
print("Starting importer")
exceptrequests.exceptions.RequestExceptionase:
print("Error with calling importer {e}")
```
%% Cell type:markdown id: tags:
We communicate with the pod with the PodClient. The PodClient requires us to provide a [database key](https://gitlab.memri.io/memri/pod/-/blob/dev/docs/HTTP_API.md#user-content-api-authentication-credentials) and an [owner key](https://gitlab.memri.io/memri/pod/-/blob/dev/docs/HTTP_API.md#user-content-api-authentication-credentials). You don't have to worry about these keys: when you run an Integrator from a memri client, this goes via the pod, which provides these keys for you. For testing purposes, we can make our own keys.
Now that we have access to the pod, we can create items here and upload them to the pod. All items are defined in the memri [schema](https://gitlab.memri.io/memri/schema). When the schema is changed it automatically generates all the class definitions for the different languages used in memri, the python schema file lives in [schema.py](https://gitlab.memri.io/memri/pyintegrators/-/blob/master/integrators/schema.py) in the integrators package. When Initializing an Item, always make sure to use the from_data classmethod to initialize.
We can connect items using edges. Let's create another item, a person, and connect the email and the person.
%% Cell type:code id: tags:
``` python
person_item=Person.from_data(firstName="Alice")
item_succes=client.create(person_item)
edge=Edge(person_item,email_item,"author")
edge_succes=client.create_edge(edge)
assertitem_succesandedge_succes
edge
```
%% Output
Person (#279002) --author-> EmailMessage (#279001)
Person (#917041) --author-> EmailMessage (#917040)
%% Cell type:markdown id: tags:
# Fetching and updating Items
%% Cell type:markdown id: tags:
We can use the client to fetch data from the database. This is in particular usefull for indexers, which often use data in the database as input for their models. The simplest form of querying the database is by querying items in the pod by their uid (unique identifier).
%% Cell type:code id: tags:
``` python
person_item=Person.from_data(firstName="Alice")
client.create(person_item)
person_from_db=client.get(person_item.uid)
assertperson_from_dbisnotNone
assertperson_from_db==person_item
person_from_db
```
%% Output
Person (#279003)
Person (#917042)
%% Cell type:markdown id: tags:
Appart from creating, we might want to update existing items:
%% Cell type:code id: tags:
``` python
person_item.lastName="Awesome"
client.update_item(person_item)
person_from_db=client.get(person_item.uid)
assertperson_from_db.lastName=="Awesome"
person_from_db
```
%% Output
Person (#279003)
Person (#917042)
%% Cell type:markdown id: tags:
Sometimes, we might not know the uids of the items we want to fetch. We can also search by a certain property. We can use this for instance when we want to query all items from a particular type to perform some indexing on.