Database using JSON file as storage for Node.JS
npx @tessl/cli install tessl/npm-node-json-db@2.3.0Node JSON DB is a lightweight JSON file-based database solution for Node.js applications that need simple persistent data storage without the complexity of traditional databases. It offers a filesystem-based storage approach using JSON files with a JavaScript Object interface, supporting async/await operations, DataPath traversal similar to XMLPath for accessing nested properties, and includes features like file locking for concurrent access protection.
npm install node-json-dbimport { JsonDB, Config } from "node-json-db";For advanced usage with custom adapters:
import { JsonDB, ConfigWithAdapter, IAdapter, JsonAdapter, FileAdapter } from "node-json-db";Error handling imports:
import { DatabaseError, DataError } from "node-json-db";CommonJS:
const { JsonDB, Config } = require("node-json-db");import { JsonDB, Config } from "node-json-db";
// Create database with file-based storage
// Parameters: filename, saveOnPush, humanReadable, separator, syncOnSave
const db = new JsonDB(new Config("myDatabase", true, false, "/"));
// Store data using DataPath (XMLPath-like syntax)
await db.push("/users/1", {
name: "Alice",
email: "alice@example.com",
age: 25
});
// Retrieve data
const user = await db.getData("/users/1");
console.log(user.name); // "Alice"
// Check if data exists
const exists = await db.exists("/users/1");
// Update with merge (non-destructive)
await db.push("/users/1", { active: true }, false);
// Delete data
await db.delete("/users/1");Node JSON DB is built around several key components:
Config class or ConfigWithAdapter for custom setupsrwlock libraryCore CRUD operations for managing data in the JSON database, including data retrieval, storage, updates, and deletion with DataPath navigation.
class JsonDB {
constructor(config: JsonDBConfig);
// Data retrieval
getData(dataPath: string): Promise<any>;
getObject<T>(dataPath: string): Promise<T>;
getObjectDefault<T>(dataPath: string, defaultValue?: T): Promise<T>;
exists(dataPath: string): Promise<boolean>;
// Data modification
push(dataPath: string, data: any, override?: boolean): Promise<void>;
delete(dataPath: string): Promise<void>;
}Flexible configuration system with built-in file storage adapter and support for custom storage backends through the adapter pattern.
class Config implements JsonDBConfig {
constructor(
filename: string,
saveOnPush?: boolean,
humanReadable?: boolean,
separator?: string,
syncOnSave?: boolean
);
}
interface IAdapter<T> {
readAsync(): Promise<T | null>;
writeAsync(data: T): Promise<void>;
}Advanced functionality including array operations, search capabilities, database management, locking, and path utilities for complex data scenarios.
class JsonDB {
// Array operations
count(dataPath: string): Promise<number>;
getIndex(dataPath: string, searchValue: string | number, propertyName?: string): Promise<number>;
getIndexValue(dataPath: string, searchValue: string | number): Promise<number>;
// Search operations
filter<T>(rootPath: string, callback: FindCallback): Promise<T[] | undefined>;
find<T>(rootPath: string, callback: FindCallback): Promise<T | undefined>;
// Database management
load(): Promise<void>;
save(force?: boolean): Promise<void>;
reload(): Promise<void>;
// Path utilities
fromPath(path: string, propertyName?: string): Promise<string>;
}
type FindCallback = (entry: any, index: number | string) => boolean;The library provides comprehensive error handling with specific error types:
class DatabaseError extends NestedError {
constructor(message: string, id: Number, inner?: Error);
}
class DataError extends NestedError {
constructor(message: string, id: Number, inner?: Error);
}
abstract class NestedError extends Error {
readonly inner?: Error;
readonly id: Number;
toString(): string;
}Common error scenarios:
interface JsonDBConfig {
readonly adapter: IAdapter<any>;
readonly saveOnPush: boolean;
readonly separator: string;
}
type FindCallback = (entry: any, index: number | string) => boolean;