Blog

The @yaks packages: 143 packages built on one graph

The yak repository holds 143 TypeScript packages under packages/, each named @yaks/…. They share one data model, defined by @yaks/graph, and a package that keeps records declares their components in it. This post describes the model and counts the packages, components and effects built on it. Every figure comes from a command you can run in a checkout of the repository; each command is shown after a $, with its output below it.

$ git ls-files 'packages/*/package.json' | xargs jq -r .name | grep -c '^@yaks/'
143

Entities, components and bundles

An entity is a record identified by its eid. It has no type of its own. A component is a named object of properties attached to an entity, such as doc: { title, body }, and what an entity is follows from the components it carries: a task is an entity with a task component, and it carries doc as well for its title and text. A bundle is one entity's components as a JSON object. Reads return bundles, and a write is a list of bundles, a batch, that apply() commits in one transaction.

packages/README.md opens with this example. It stores one document in memory and reads it back with a query:

import { docDoc } from '@yaks/doc'
import { graph } from '@yaks/graph'
import { ram } from '@yaks/ram'
import { loadVocab } from '@yaks/vocab'

let vocab = loadVocab([docDoc])
let g = graph({ storage: ram(vocab), vocab })
await g.apply([{
  entity: { eid: 'first-note' },
  doc: { title: 'Getting started', body: 'A document stored as components.' },
}])
console.log(await g.read('.doc.title="Getting started"'))

The full set of terms is in @yaks/graph's data model.

A vocabulary per package

A vocabulary declares the components a graph accepts and the type of each property. A package ships its vocabulary as a vocab.json file beside its code: @yaks/doc declares doc, @yaks/task declares task, and an application passes the vocabularies of the packages it uses to loadVocab(). The format is described in @yaks/vocab.

There are 88 vocab.json files under packages/, and together they declare 464 distinct components:

$ git ls-files 'packages/*/vocab.json' | wc -l
88
$ git ls-files 'packages/*/vocab.json' | xargs jq -r '.["$defs"] | to_entries[] | select(.value.component == true) | .key' | sort -u | wc -l
464

Storage is given to the graph

@yaks/graph's package.json lists six dependencies: @yaks/fp, @yaks/id, @yaks/match, @yaks/query, @yaks/trace and @yaks/vocab. None of them stores data. A graph is given a storage adapter when it is made, and the same code runs on each:

  • @yaks/ram holds bundles in a JavaScript Map, for tests and browser state.
  • @yaks/sqlite builds a SQLite schema from the vocabulary.
  • @yaks/d1 stores the same schema in a Cloudflare D1 database.

Since the graph depends on no adapter, a test can give it ram() and run in memory, and a server can give it SQLite. 91 of the 143 packages list @yaks/graph as a direct dependency:

$ git ls-files 'packages/*/package.json' | xargs jq -r 'select(.dependencies["@yaks/graph"]) | .name' | wc -l
91

Effects run after the commit

Work that reaches outside the graph, such as sending mail or calling another service's API, is not part of a transaction. An effect is declared in a vocabulary with effect: true and names the changes that owe it a run, for example post_announce: { effect: true, created: ['post'] }. A handler registered with @yaks/effects does the work after the batch commits. A handler that fails is reported, and the batch it followed stays committed.

Since an effect is declared in the vocabulary, every process that loads the vocabulary knows what a commit owes. With the optional pool, runs are stored as effect components, so one process can claim and run work another process committed. The vocabularies declare 53 effects:

$ git ls-files 'packages/*/vocab.json' | xargs jq -r '.["$defs"] | to_entries[] | select(.value.effect == true) | .key' | sort -u | wc -l
53

Where the figures come from

Each count above was taken at commit 19ec52b03. Run the commands in a later checkout and they count what is there then. packages/README.md describes the packages and links to their READMEs.