file-json-db

file-json-db

A resilient, zero-dependency, MongoDB-style JSON document database for Node.js.

What’s new in 1.5.0
  • Search API – collection.search({ keyword, fields, exclude, caseInsensitive })
  • Improved project – proper inclusion / exclusion / mixed (exclusion wins), _id kept by default
  • keepIdIndex option (default false) for lower memory with auto IDs
  • Better TypeScript definitions with full JSDoc
See the Search API page and the updated Query Builder docs.

file-json-db lets you store structured documents on the local filesystem while giving you a familiar document-database API. There is no separate database server to install, no external dependencies to manage, and the data remains human-readable JSON files.

Perfect for Prototypes and MVPs · CLI tools · Electron / desktop apps · Small backends · Local configuration stores · Learning how document databases work

What problem does it solve?

Most applications eventually need persistent structured data. Reaching for MongoDB, PostgreSQL, or even SQLite is often overkill for small projects, scripts, or desktop applications. At the same time, hand-rolling fs.readFile / JSON.parse becomes painful the moment you need queries, indexes, concurrent writes, or recovery from partial writes.

file-json-db sits in the sweet spot: it gives you a real document API (insert, find, update, delete, aggregation, transactions) while storing everything as plain JSON files on disk. Collections are directories. Large collections are automatically split into multiple part-XXXX.json files so a single file never grows unboundedly.

Core ideas

Key features at a glance

FeatureDescription
Zero dependenciesOnly Node.js built-in modules (fs, path, crypto, events)
MongoDB-style APIinsert, find, updateOne, $set, $push, aggregation, etc.
Chunked storageLarge collections automatically split across multiple part files
Secondary indexesFast equality lookups on configured fields
Atomic writesTemp-file + rename with retry logic
Snapshot transactionsCommit / rollback without touching disk until commit
Sync + AsyncEvery major operation has both variants
Self-healingCorrupted parts are isolated; the rest of the collection keeps working
TypeScript readyFull .d.ts declarations included

What it is not

Important file-json-db is an embedded database. It runs inside your Node.js process. It is not a network database server. Multiple processes writing to the same collection concurrently are not supported without external coordination.

It is also not designed for datasets of millions of documents or for high-concurrency production web servers. For those cases you should use a proper database engine.

Quick example

const { JSONDB } = require('file-json-db');

const db = new JSONDB('./data');

const users = db.collection({
  name: 'users',
  autoId: true,
  indexes: ['email']
});

// Insert
const user = users.insert({
  name: 'Ada Lovelace',
  email: 'ada@analytical.engine',
  role: 'admin'
});

// Find by indexed field (very fast)
const found = users.findOne({ email: 'ada@analytical.engine' });

// Update
users.updateOne(
  { email: 'ada@analytical.engine' },
  { $set: { lastLogin: new Date().toISOString() }, $push: { tags: 'verified' } }
);

// Query with operators
const admins = users.find({ role: 'admin', age: { $gte: 18 } }).toArray();

Next steps