Skip to content

Use cases

MeTTaScript is an embeddable database whose query language is also a programming language. Reach for it when you would reach for a local, in-process store that you query with logic and rules rather than SQL: a knowledge base in a web app, derived facts and inference in an agent, a graph you both query and transform in the same process. It is a TypeScript library, so it drops into a browser page, a Node service, or an edge function with nothing to install.

You store facts in a space, query them by pattern, and write rules that derive new facts from the ones you stored. Data nests (a fact can be about another fact), queries and rules use the same pattern matching, and any TypeScript value crosses the boundary as an ordinary value.

Instead of DataScript

DataScript is the immutable in-memory Datalog database most people use for local-first data in the browser, and it is what tools like Logseq are built on. MeTTaScript covers the same job: you load facts, query them declaratively, and keep immutable snapshots you can branch and roll back.

The join you write in DataScript's :where is a join here too:

ts
import { mettaDB, names, vars } from "@mettascript/edsl";

const db = mettaDB();
const { edge, A } = names();
const { x, y } = vars();
// ... load edges ...
db.query([edge(A, x), edge(x, y)], { x, y }); // two-hop join, rows of { x, y }

Two things are different. The data model is more expressive: DataScript stores flat entity-attribute-value datoms, while MeTTaScript stores a metagraph, so a fact can be about another fact, and rules and types live in the same space as the data. And on DataScript's own workloads, MeTTaScript is faster on every declarative query. At 120,000 records, uniform distribution, five isolated processes:

WorkloadDataScriptMeTTaScript
Source lookup (declarative)14.1 ms0.011 ms1349x
Anchored two-hop join26.1 ms0.14 ms184x
One-percent range42.3 ms2.08 ms20x
Group lookup14.5 ms1.72 ms8.4x
Triangle join3101 ms1079 ms2.9x
Bulk build386 ms323 ms1.20x
Retained heap after build48.8 MiB36.5 MiB1.34x
Immutable insert (1000)43.4 ms11.6 ms3.75x

Honestly: DataScript keeps its hand-tuned direct index reads. Entity-by-id, its index_range seek, and cold first calls stay in the microsecond range where MeTTaScript pays JIT warmup. So an app that writes queries is faster on MeTTaScript everywhere; an app hand-optimized on DataScript's raw index API keeps those point seeks. The full per-workload tables, both distributions, and the method are in the benchmark results.

Instead of another Datalog store

Datalevin and Datahike are Datalog databases with durable storage, and InstantDB is a hosted real-time database with Datalog-style queries. They are Clojure-first or sync-service-first. MeTTaScript is the pure-TypeScript one you embed directly, with a metagraph data model, rewrite rules, and gradual dependent types on top of the query engine.

Instead of a reactive store

TinyBase is a small reactive store for tabular and key-value data with queries, indexes, and metrics. Reach for it when your data is tables and you want reactivity. Reach for MeTTaScript when your data is a graph, when facts nest, or when you want rules and inference rather than aggregation over rows.

Instead of Prolog in the browser

If what you want is logic programming, rules and unification, you might reach for tau-prolog or SWI-Prolog compiled to WebAssembly. MeTTaScript gives you rules, unification, and backward search too, but over a metagraph you also store, query, and mutate, with first-class TypeScript interop so a rule can call your functions and return plain JS. If you specifically want ISO Prolog, MeTTaScript also has an optional Prolog adapter that calls real SWI-Prolog (native or WASM) through the same host-import contract.

When something else fits better

MeTTaScript is not a general-purpose SQL database and it is not the lightest possible store. If you only need key-value or a single flat table, a smaller store is simpler. If you need relational joins over large durable data with SQL, use SQLite or Postgres (pglite runs Postgres in WASM). If you need full-text search, use a search index. MeTTaScript is the right tool when your data is nested or graph-shaped, when you want to derive facts with rules, and when you want to query and compute over the same data in one process.

Released under the MIT License.