Skip to content

v8.0.0

Latest

Choose a tag to compare

@typicode typicode released this 03 Dec 12:13
548bb10

🦉 lowdb 8 is built on JSONL, JSON Patch and Proxies. Rethought for code written by agents 🤖: one way to write, and mistakes throw with the fix.

import { open } from "lowdb";

const db = open("db.jsonl", { posts: [] });

db.update((draft) => {
  draft.posts.push({ title: "lowdb is awesome", views: 100 });
});

db.data.posts[0].title; // 'lowdb is awesome'
db.data.posts[0].views = 0; // TypeError: lowdb: db.data is read-only

What's New

  • Faster writes: v7 rewrote the whole file on each write, v8 appends one line. With 10,000 entries: about 1.7 ms → 7 µs.
  • No lost changes: in v7, changing db.data without db.write() was silently lost. v8 throws.
  • No half-applied changes: in v7, a failed update left db.data half changed. In v8, nothing changes.
  • One API: open, instead of sync and async classes, presets and adapters.
  • Crash safe: a crash can only damage the last line, and open drops a cut last line. Other damage is reported with its line number, and the file is left untouched.
  • In memory: open(":memory:", init) writes no file, for tests.
  • No dependencies.

File Format

  • Line 1 holds the data, always an object.
  • Each update appends one line: a JSON Patch (RFC 6902) array, applied all or nothing.
  • The file is compacted back to the data alone when the log outgrows it, on db.close() and on a normal exit.
{"posts":[]}
[{"op":"add","path":"/posts/0","value":{"title":"lowdb is awesome","views":100}}]

Breaking Changes

  • API removed: the sync and async classes (Low, LowSync), the adapters (JSONFile, LocalStorage, …), the presets and the lowdb/node and lowdb/browser entry points are gone.
  • File is .jsonl: see below to migrate an existing db.json.
  • Node 26 required.

From v7 to v8

// v7
const db = await JSONFilePreset("db.json", { posts: [] });
await db.update(({ posts }) => posts.push(post));

// v8
const db = open("db.jsonl", { posts: [] });
db.update(({ posts }) => posts.push(post));

From an existing db.json:

import { readFileSync } from "node:fs";

const db = open("db.jsonl", JSON.parse(readFileSync("db.json", "utf8")));

Limits

  • One process per file. A second one is detected at its next write, not prevented.
  • All the data is in memory.

If you need queries, large data or several processes, use SQLite.

Sponsors

Your support is essential, you can sponsor this project on GitHub Sponsors ❤️