A JSON store on disk with flatdb

Jennifer

Load a JSON file, query and edit it with JSON Pointer, save it crash-atomically, and know when a single file is no longer enough.

November 2026, by @mplx.
Based on Jennifer 0.26.0

Powered by Jennifer's Cantrip - Markdown into slide decks, https://github.com/jennifer-language/cantrip
1 / 19

What flatdb is

It is not a database engine: no queries, no indexes, no transactions across files. It is crash-atomic snapshotting of small data.

2 / 19

The handle is a value

export def struct DB { path as string, data as json.Value, version as string };
3 / 19

First steps

use io;
use json;
import "flatdb.j" as flatdb;

def db as flatdb.DB init flatdb.open("notes.json");   # {} if absent
$db = flatdb.set($db, "/title", json.decode("\"Groceries\""));
$db = flatdb.set($db, "/items", json.list());
$db = flatdb.append($db, "/items", json.decode("\"milk\""));
$db = flatdb.save($db);
io.printf("%d items, first: %s\n", flatdb.length($db, "/items"),
    json.asString(flatdb.get($db, "/items/0")));
2 items, first: milk
4 / 19

Values are json.Value, writes are strict

flatdb.set($db, "/n", 5)
# argument 3 to "set" must be json.Value, got int
flatdb.set($db, "/n", json.decode("5"))           # a scalar
flatdb.set($db, "/tags", json.list(["a", "b"]))   # from a Jennifer list
flatdb.set($db, "/a/b", json.decode("1"))
# json.set: no key "a"         (no auto-vivification: create /a first)
flatdb.get($db, "/nope")       # json.get: no key "nope"
flatdb.getOr($db, "/nope", json.decode("0"))      # the fallback
5 / 19

Value semantics in practice

def before as flatdb.DB init flatdb.open("t.json");
def after as flatdb.DB init flatdb.set($before, "/n", json.decode("99"));
io.printf("before %v, after %v\n",
    flatdb.get($before, "/n"), flatdb.get($after, "/n"));
before 2, after 99
6 / 19

Worked example: a bookmark store

{"items": {
   "2": {"url": "https://academy.jennifer-lang.dev/",
         "title": "Jennifer Academy", "tags": ["jennifer", "talks"]},
   "3": {"url": "https://docs.jennifer-lang.dev/",
         "title": "Jennifer docs", "tags": ["docs"]}},
 "next": 4}
7 / 19

bm: adding a bookmark

func addItem(db as flatdb.DB) {     # argv: add URL TITLE TAG...
    def next as json.Value init flatdb.getOr($db, "/next", json.decode("1"));
    def id as int init json.asInt($next);
    if (not flatdb.has($db, "/items")) {
        $db = flatdb.set($db, "/items", json.map());
    }
    def rec as json.Value init json.map({"url": $argv[1], "title": $argv[2]});
    $rec = json.set($rec, "/tags", json.list($argv[3..]));
    $db = flatdb.set($db, "/items/{$id}", $rec);
    return flatdb.set($db, "/next", json.decode("{$id + 1}"));
}
8 / 19

bm: removing, listing, dispatch

func removeItem(db as flatdb.DB) {
    def ptr as string init "/items/" + $argv[1];
    if (not flatdb.has($db, $ptr)) {
        throw Error{kind: "value", message: "no bookmark " + $argv[1]};
    }
    return flatdb.remove($db, $ptr);
}
match ($argv[0]) {
    when "add" { flatdb.update(STORE, addItem); }
    when "rm" { flatdb.update(STORE, removeItem); }
    when "list" { listItems($argv[1..]); }   # open, keys, print
}
9 / 19

bm in use

$ alias bm='jennifer run bm.j'
$ bm add https://www.jennifer-lang.dev/ "Jennifer home" jennifer
$ bm add https://academy.jennifer-lang.dev/ "Jennifer Academy" jennifer talks
$ bm add https://docs.jennifer-lang.dev/ "Jennifer docs" docs
$ bm list talks
2  Jennifer Academy
$ bm rm 1
$ bm rm 9
bm.j: uncaught error at .../bm.j:24:9: Error{kind: "value", message: "no bookmark 9", ...}
$ stat -c '%A %s %n' bookmarks.json
-rw------- 210 bookmarks.json
10 / 19

Crash-atomic save

  1. Create bookmarks.json.tmp.<uuid>, empty, with the store's mode (0600 for a new store)
  2. Write the JSON, fs.sync it to disk
  3. fs.rename it over bookmarks.json: atomic on POSIX
11 / 19

Pretty files and a backup

def opts as flatdb.SaveOptions init flatdb.saveDefaults();
$opts.pretty = true;    # indented, for a file people edit
$opts.backup = true;    # keep the previous content as t.json.bak
$db = flatdb.saveWith($db, $opts);
t.json:      {                     t.json.bak:  {"n":2,"x":true}
               "n": 7,
               "x": true
             }
12 / 19

save notices a change

def w1 as flatdb.DB init flatdb.open("t.json");
def w2 as flatdb.DB init flatdb.open("t.json");
flatdb.save(flatdb.set($w1, "/n", json.decode("2")));
flatdb.save(flatdb.set($w2, "/n", json.decode("3")));   # throws
flatdb: flatdb.save: the document at t.json changed since it was opened. ...
13 / 19

Loud is not the same as safe

Four tasks, 25 increments each, plain open + save, conflicts counted:

visits: {"visits":26}, conflicts: 23
visits: {"visits":43}, conflicts: 11
visits: {"visits":26}, conflicts: 25
14 / 19

update: the locked read-modify-write

func addVisit(db as flatdb.DB) {
    def n as int init json.asInt(flatdb.getOr($db, "/visits", json.decode("0")));
    return flatdb.set($db, "/visits", json.decode(convert.toString($n + 1)));
}
flatdb.update("visits.json", addVisit);   # lock, open, transform, save, unlock
visits: {"visits":100}        (the same four tasks, three runs out of three)
15 / 19

Other doors in and out

CallFor
openString(text)a store from a string or HTTP body: read-only, save throws
saveAs(db, path)write a copy and get a DB bound to the new path
openWith(path, maxBytes)refuse a file over maxBytes before decoding it
lock / unlocka multi-step transaction by hand; prefer update
flatdb: flatdb.openWith: big.json is larger than 10 bytes
runtime: json.decode: expected string key in object at line 1, column 9
16 / 19

Many readers: open once

Cost of the whole-file model, 100,000 bookmarks, a 7.7 MB file:

open: 121 ms       one change + save: 94 ms
17 / 19

When to use something else

You needflatdbInstead
Settings, a cache, a small app's stateyes
A file people read and edityes
Many writers all the timeupdate serializes themsql, redis
Search across records, indexes, joinsyou loopsql
Data much larger than memorynosql
Several changes, all or noneone file onlysql transactions

sql talks to MariaDB™ / MySQL™ or PostgreSQL™ servers, default binary only.

18 / 19

Read more

Based on Jennifer 0.26.0, November 2026.

19 / 19