Skip to content
typicodePublic

Latest commit

 

History

549 Commits

Folders and files

Repository files navigation

lowdb

Node.js CI

Small, fast and typed JSON database for Node 🦉

Note

New version: 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 init = { posts: [] }; // if db.jsonl is new
const db = open("db.jsonl", init);

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

db.jsonl after the update

{"posts":[]}
[{"op":"add","path":"/posts/0","value":{"title":"lowdb is awesome","views":100}}]

When the process exits, it is compacted back to the data

{"posts":[{"title":"lowdb is awesome","views":100}]}

Sponsors





Become a sponsor and have your company logo here 👉 GitHub Sponsors

Install

npm install lowdb

Why lowdb

An agent can write a JSON database, but it will ask you to choose: the file format, what a crash mid-write leaves, what a failed update does to the file. Then you maintain that code.

lowdb has made these choices, and it has no dependencies.

Why JSONL

A JSON file has to be rewritten whole on every change. A JSONL file can grow one line at a time: lowdb writes the data on the first line, then appends one JSON Patch per update.

  • an update takes microseconds, whatever the data size
  • a crash cannot damage what is written
  • a power cut can lose the last updates, nothing before
  • the file stays readable with cat, tail or jq

It is compacted back to the data alone when it gets too large, on db.close() and when the process exits normally. After Ctrl+C or a kill, the file is not compacted, but nothing is lost.

API

Open

The second argument is the data to create the file with, if it does not exist. The data is always an object.

:memory: writes no file, for tests:

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

Read

db.data is the data, read-only: read it like any object, change it with update only.

db.data.posts.filter((post) => post.views > 50);

Update

update gives you a draft of the data. Change it like any object.

db.update((draft) => {
  draft.posts[0].views++;
  draft.posts.pop();
});

If the function throws, nothing changes.

With TypeScript, the draft is typed

type Data = { posts: { title: string; views: number }[] };

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

db.update((draft) => {
  draft.posts[0].veiws++; // Property 'veiws' does not exist
});

Validate what comes from outside before it goes in, with any validation library

import * as z from "zod";

const Post = z.object({ title: z.string(), views: z.number() });

db.update((draft) => {
  draft.posts.push(Post.parse(req.body));
});

Close

Every update is already in the file: closing only compacts it and releases the file. A file can only be open once.

// Lives as long as the process: closed when the process exits normally
const db = open("db.jsonl", { posts: [] });

// Closed by hand, to release the file earlier
db.close();
// Closed at the end of the block: for a script or a test
{
  using db = open("db.jsonl", { posts: [] });
}

From v7 to v8

Important

v8 has breaking changes that make it better for agents.

// 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")));

What v8 does better:

  • 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.

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.

Releases

Sponsor this project

Used by

Contributors

Languages