lowdb README

The project's README from github.com/typicode/lowdb. As one markdown file: README.md.

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.

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:

Limits

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

lowdb (typicode/lowdb)

lodash
Searching Database…

22.6K ·GitHub
What is this?lowdb is a small, type-safe local JSON database for JavaScript: you read or create a db.json file and change data with plain JavaScript, including native Array functions.
So it's a library I install, not a database server I have to run?It's a library you install from npm, not a server. There's no process to run — db.data is just a JavaScript object that lowdb keeps in sync with a JSON file.
How do I start using it in a project?Run npm install lowdb, then create the database with JSONFilePreset('db.json', defaultData). After that db.update(fn) changes data and writes for you, or use db.read() and db.write() manually.
Does it need anything besides npm?Just Node.js 18 or newer — it's a pure ESM package with a single runtime dependency (steno), no database binary or background service.
Any limits I should know about before betting on it?It doesn't support Node's cluster module, and large objects (~10-100MB) get slow because every db.write serializes the whole database with JSON.stringify. If you plan to scale, the README recommends PostgreSQL or MongoDB instead.
Can I use it in the browser too?Yes. Besides Node.js it ships LocalStorage and SessionStorage presets for window.localStorage and window.sessionStorage, and the README points to CLI, Server and Browser examples.
Where does my data actually live?In a plain JSON file on disk (db.json by default) that you control. Nothing runs in the cloud — no server, accounts or telemetry.
Can I use it from TypeScript?Yes — lowdb is type-safe, so you can describe db.data with TypeScript types and catch shape errors at compile time.
Can it save data in other formats, like YAML?Yes, via adapters: lowdb ships TextFile and DataFile utility adapters, and a custom adapter is just a class with two methods, so it can target YAML, XML, encryption, compression or remote storage.
Can I use Lodash or Ramda with it?Yes. Instead of the JSONPreset shortcut you use the lower-level classes, then extend your queries with Lodash or Ramda as the README suggests.
What happens when tests run against it?It automatically switches to a fast in-memory mode during tests, and the Memory and MemorySync adapters exist for the same purpose.
Creator
typicode
Size
1.1 MB
Stars
22.6K
Age
12y
Commits · 90 Days