The smallest programΒΆ

examples/minimal/run.py is the least a program needs to use ZypherDB: open a database, store a small graph, and ask it a question with a parameter.

import tempfile

import zypherdb


def main() -> None:
    with tempfile.TemporaryDirectory() as path, zypherdb.Database(path) as db:
        db.query("""
            CREATE (ada:Person {name: 'Ada Okafor'}), (dalia:Person {name: 'Dalia Haddad'}),
                   (lab:Room {name: 'BSL-3 Lab'}), (office:Room {name: 'Genomics Office'}),
                   (ada)-[:MAY_ENTER]->(lab), (ada)-[:MAY_ENTER]->(office),
                   (dalia)-[:MAY_ENTER]->(lab)
        """)
        for room, people in db.query("""
            MATCH (p:Person)-[:MAY_ENTER]->(r:Room {name: $room})
            RETURN r.name, collect(p.name)
        """, room="BSL-3 Lab"):
            print(f"{room}: {', '.join(sorted(people))}")


if __name__ == "__main__":
    main()

Run it from the repository root, with the package installed (pip install zypherdb) or built from source (make python, then PYTHONPATH=python):

python -m examples.minimal.run
BSL-3 Lab: Ada Okafor, Dalia Haddad
  • zypherdb.Database(path) opens the database in that directory, creating it if needed, and the with block closes it. Here the directory is temporary, so the data is gone at the end; give it a lasting path to keep it.

  • One CREATE stores five nodes and three relationships, in one transaction.

  • $room is a parameter, passed as a keyword argument. Values never go into the query text.

  • query returns a list of rows, each a list of values: here one row, with a string and a list.

The tutorial starts from here.