Introduction to Graphlite

What is Graphlite?

Graphlite is a social graph datastore. It doesn’t store properties between relations, but it can store thousands of relations between integers, which are most likely representing objects in your other databases, such as the ID column of your users/statuses table.

Being based on a relational DBM, Graphlite supports very atomic transactions, similar to the transactions that SQLite offers even with transactions, performance isn’t degraded because the SQLite library is very, very fast.

Graphlite aims to be performant, thread safe, and have a pleasant API for developer happiness. For example, to create a transaction:

with graph.transaction() as tr:
    for item in range(2, 6):

And one thing I’m very happy with is how querying works and the expressiveness of the querying “syntax”:



To connect to an existing SQLite database file (substitute URI and GRAPHS with the URI of your database file and the graphs that you want to query/insert to):

from graphlite import connect, V
graph = connect(URI, graphs=GRAPHS)

To insert (possibly multiple) relation(s), you must create a transaction and call methods of the transaction object:

with graph.transaction() as tr:

To query the graph, you can simply do:

graph.find(V(1).knows)   # people that 1 knows
graph.find(V().knows(1)) # people that knows 1

Querying has a few more “tricks”, notably the powerful set operations that you can do:


They should be quite familiar to you (remember the Venn diagrams from school?). If you are not familiar with set operations or would like a demo of how they work I would recommend looking at this diagram.

Graphlite also supports “graph-hopping” or graph traversal queries, in spite of the fact that it was inspired by FlockDB:


The above query states that “find all of the people that 1 knows, and then find all of the people that they know”. You can also pass in a destination node to the second query, to select the source nodes:


Which means “find all of the people that one knows, that knows 2”. This can also be expressed with the help of an intersection:


Note that you can traverse indefinitely, i.e. to find out who are the friends of friends of the people that know 2, you can do:


You can also count the nodes returned by a query via the count method:


To delete edges from the datastore, you have three options:

  • Specific deletes
  • Inverse & Forwards deletes
  • Relation-wide deletes

To illustrate,

with graph.transaction() as tr:
    # delete edges of type "1 knows ..."

    # delete edges of type "... knows 1"

    # delete edges of type "... knows ..."

Table Of Contents

Previous topic

Welcome to Graphlite

Next topic


This Page