Friday, November 27, 2026

TypeDB

TypeDB

What is TypeDB, and how does it differ from a graph database?

Abstract. TypeDB is a strongly-typed database built on an entity–relation–attribute model, queried with TypeQL. This post covers three ideas: the schema as a type system, relations as first-class objects, and functions (since TypeDB 3.0) as the reasoning mechanism.

1. The schema is a type system

Every piece of data is an entity (person), a relation whose participants play roles (employment with employee, employer), or an attribute (name, salary).

define
  entity person, owns name, plays employment:employee;
  entity company, owns name, plays employment:employer;
  relation employment, relates employee, relates employer, owns salary;
  attribute name, value string;
  attribute salary, value integer;

Remark. Unlike a property-graph edge, a relation can have more than two participants, own attributes, and play roles in other relations. N-ary and nested relationships need no workarounds.

2. Pattern queries in TypeQL

A query describes a pattern with variables $x; TypeDB returns every match.

match
  $c isa company, has name "TypeDB";
  employment (employee: $p, employer: $c);
  $p has name $n;
fetch { "name": $n };

Intuition. Each line is a constraint; answers satisfy all of them at once. Queries are type-checked, so a nonsensical query is an error, not an empty result.

3. Reasoning: from rules to functions

TypeDB 3.0 (December 2024, rewritten in Rust) replaced rules with functions: named, reusable subqueries that can be nested, recursive, and negated.

define
fun colleagues($p: person) -> { person }:
  match
    employment (employee: $p, employer: $c);
    employment (employee: $q, employer: $c);
    not { $q is $p; };
  return { $q };

Caution. Functions compute on demand; they do not insert inferred data. Data and computation stay separate.

4. When to use it

Good fit: complex, n-ary or nested relations; strict schemas; recursive reasoning (hierarchies, lineage, dependencies).

Poor fit: unstructured text or vector data; a handful of simple triples, where declaring a full schema costs more than it gives.

Popular Posts