File System DB
File System DB (FSDB) is a lightweight, synchronous, file-based JSON database designed for Node.js environments, currently at version 2.1.0. It allows developers to store and retrieve data using a key-value paradigm, supporting dot notation for nested JSON fields. A key differentiator is its simplicity and lack of external dependencies, relying solely on Node.js's built-in File System module. Data is stored directly as human-readable JSON files, making it easy to inspect and manage backups. While highly accessible, its synchronous nature means operations can potentially block the event loop for very large datasets, and its reliance on persistent file storage makes it unsuitable for ephemeral hosting environments like Heroku or Vercel. The library ships with TypeScript types, enhancing developer experience for type-safe applications. New minor versions appear to be released on an as-needed basis.
Common errors
-
Error: ENOENT: no such file or directory, open './invalid/path/db.json'
cause The specified path for the database file or a backup file does not exist, and the library needs a valid directory to create the file.fixEnsure the directory where the database file or backup file is intended to be created already exists. Use `path.join()` for cross-platform paths and consider `fs.mkdirSync(path.dirname(dbPath), { recursive: true });` if directories need to be created dynamically. -
SyntaxError: Unexpected token ... in JSON at position ...
cause The underlying JSON database file has been corrupted or manually edited with invalid JSON syntax, preventing the library from parsing it.fixInspect the database JSON file at the specified path for syntax errors. If a backup is available, restore from the last valid backup. Avoid manual editing of the live database file. -
TypeError: db.set is not a function
cause The `db` variable was not correctly initialized as an instance of `FSDB`, or `require`/`import` did not correctly expose the `FSDB` class.fixEnsure you are correctly initializing the database with `const db = new FSDB();` or `const db = new FSDB('./my-db.json');`. Verify your `import` or `require` statement matches the package's export structure (e.g., `const { FSDB } = require('file-system-db');`).
Warnings
- breaking Migrating from v1 to v2 involves significant changes to the API and internal structure. The package authors recommend reviewing the specific pull request detailing the changes before upgrading.
- gotcha This package saves data persistently to the local file system. This makes it unsuitable for ephemeral hosting environments like Heroku, Vercel, AWS Lambda, or other serverless functions where file system changes are not preserved across instances or invocations.
- gotcha All database operations in File System DB are synchronous. While the package claims operations are fast, extensive use with very large datasets or frequent writes could potentially block the Node.js event loop, leading to performance bottlenecks and unresponsiveness in high-load scenarios.
- gotcha The database relies on a single JSON file. Manual corruption of this file (e.g., malformed JSON syntax) by external processes or unexpected power loss during a write operation can lead to data loss or unreadable database states.
Install
-
npm install file-system-db -
yarn add file-system-db -
pnpm add file-system-db
Imports
- FSDB
import FSDB from 'file-system-db';
import { FSDB } from 'file-system-db'; - FSDB
const FSDB = require('file-system-db');const { FSDB } = require('file-system-db'); - FSDBOptions
import type { FSDBOptions } from 'file-system-db';
Quickstart
import { FSDB } from 'file-system-db';
import * as path from 'path';
import * as fs from 'fs';
// Define a temporary database path for demonstration
const dbPath = path.join(process.cwd(), 'temp_fsdb_test.json');
// Ensure the database file doesn't exist from previous runs for a clean start
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
// 1. Initialize a database instance
// You can specify a custom path and compaction setting.
// Here, we use a temporary file and disable compaction for human readability during test.
const db = new FSDB(dbPath, false);
console.log(`Database initialized at: ${dbPath}`);
// 2. Set values using keys and dot notation
db.set('user.id', '12345');
db.set('user.name', 'Alice');
db.set('products', [{ id: 1, name: 'Laptop' }, { id: 2, name: 'Mouse' }]);
console.log('Data set for user and products.');
// 3. Retrieve values
const userId = db.get('user.id');
const products = db.get('products');
console.log(`Retrieved user ID: ${userId}`);
console.log('Retrieved products:', products);
// 4. Update a value
db.set('user.name', 'Alice Smith');
console.log(`Updated user name: ${db.get('user.name')}`);
// 5. Delete a value
db.delete('user.id');
console.log(`User ID after deletion: ${db.get('user.id')}`); // Expected: undefined
// 6. Backup the database
db.backup(path.join(process.cwd(), 'temp_fsdb_backup.json'));
console.log('Database backup created.');
// Clean up the temporary database files after the example
if (fs.existsSync(dbPath)) {
fs.unlinkSync(dbPath);
}
const backupPath = path.join(process.cwd(), 'temp_fsdb_backup.json');
if (fs.existsSync(backupPath)) {
fs.unlinkSync(backupPath);
}
console.log(`Cleaned up temporary database files.`);