File System DB

2.1.0 · active · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

Initializes a file-system database, sets and retrieves key-value data with dot notation, updates, deletes, and creates a backup, then cleans up.

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.`);

view raw JSON →