{"library":"node-json-db","title":"JSON File Database for Node.js","description":"node-json-db is a lightweight, file-based database for Node.js, storing data directly in a JSON file. It is currently at stable version 2.6.0 and maintains an active release cadence, with several minor and patch releases occurring monthly or bi-monthly in the past year. Its primary differentiator is the use of a \"DataPath\" system, akin to XMLPath, for navigating and accessing nested data structures within the JSON file. All operations are asynchronous, leveraging `async/await`. It supports configurable database names, auto-save on push, human-readable file formats, custom separators, and since v2.6.0, serialization of complex JavaScript types like `Set`, `Map`, `Date`, `RegExp`, and `BigInt` via an `ISerializer` contract. This makes it suitable for simple, local data persistence where a full-fledged database system is overkill.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install node-json-db"],"cli":null},"imports":["import { JsonDB } from 'node-json-db'","import { Config } from 'node-json-db'","import { DataError, DatabaseError } from 'node-json-db'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { JsonDB, Config, DataError } from \"node-json-db\";\nimport * as fs from 'fs'; // For cleanup\n\nasync function runExample() {\n  const dbFileName = \"myQuickstartDatabase\";\n  const dbFilePath = `${dbFileName}.json`;\n\n  // Clean up previous run if exists\n  if (fs.existsSync(dbFilePath)) {\n    fs.unlinkSync(dbFilePath);\n  }\n\n  // Initialize JsonDB with a Config object\n  // Arguments: filename, autoSave (true), humanReadable (false), separator ('/'), syncWrites (false)\n  const db = new JsonDB(new Config(dbFileName, true, false, \"/\", false));\n\n  console.log(\"Database initialized.\");\n\n  // Push simple string data to a path\n  await db.push(\"/user/name\", \"Alice Wonderland\");\n  console.log(\"Pushed /user/name: Alice Wonderland\");\n\n  // Push an object, creating hierarchy if needed\n  await db.push(\"/settings\", { theme: \"dark\", notifications: true });\n  console.log(\"Pushed /settings: { theme: 'dark', notifications: true }\");\n\n  // Merge new data into an existing object path (third argument 'false' for merge)\n  await db.push(\n    \"/settings\",\n    { notifications: false, language: \"en-US\" },\n    false\n  );\n  console.log(\"Merged into /settings: { notifications: false, language: 'en-US' }\");\n  console.log(\"Current /settings:\", await db.getData(\"/settings\"));\n\n  // Get data from a specific path\n  const userName = await db.getData(\"/user/name\");\n  console.log(\"Retrieved /user/name:\", userName);\n\n  // Attempt to get data from a non-existent path and catch the expected error\n  try {\n    await db.getData(\"/nonexistent/path\");\n  } catch (error) {\n    if (error instanceof DataError) {\n      console.error(`Caught expected error for non-existent path: ${error.message}`);\n    } else {\n      console.error(\"Caught unexpected error:\", error);\n    }\n  }\n\n  // Get the entire database content\n  const fullData = await db.getData(\"/\");\n  console.log(\"Full database content:\\n\", JSON.stringify(fullData, null, 2));\n\n  console.log(\"\\nQuickstart example finished.\");\n}\n\nrunExample().catch(console.error);","lang":"typescript","description":"Demonstrates initializing the database, pushing data (including merging objects), retrieving data, and handling errors for non-existent paths. Requires Node.js ESM support.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}