--- title: Pod Client keywords: fastai sidebar: home_sidebar nb_path: "nbs/pod.client.ipynb" ---
We communicate with the pod with the PodClient. The PodClient requires us to provide a database key and an owner key. 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.
client = PodClient(database_key="0" * 64, owner_key="1" * 64)
success = client.test_connection()
assert success
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. 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 in the integrators package. When Initializing an Item, always make sure to use the from_data classmethod to initialize.
email_item = EmailMessage.from_data(content="example content field")
email_item
success = client.create(email_item)
assert success
email_item
We can connect items using edges. Let's create another item, a person, and connect the email and the person.
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)
assert item_succes and edge_succes
edge
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).
person_item = Person.from_data(firstName="Alice")
client.create(person_item)
person_from_db = client.get(person_item.uid)
assert person_from_db is not None
assert person_from_db == person_item
person_from_db
Appart from creating, we might want to update existing items:
person_item.lastName = "Awesome"
client.update_item(person_item)
person_from_db = client.get(person_item.uid)
assert person_from_db.lastName == "Awesome"
person_from_db
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.
person_item2 = Person.from_data(firstName="Bob")
client.create(person_item2);
all_people = client.search_by_fields({"_type": "Person"})
assert all([isinstance(p, Person) for p in all_people]) and len(all_people) > 0
all_people[:3]