{"library":"lowdb","title":"lowdb Local JSON Database","description":"lowdb is a lightweight, type-safe, local JSON database designed for Node.js, Electron, and browser environments. The current stable version is 7.0.1, with a frequent release cadence that has seen multiple major and minor versions released recently, indicating active development and continuous improvement. Key differentiators include its minimalist API for interacting with data using native JavaScript array methods, robust TypeScript support for data schema, and a highly extensible architecture via 'adapters'. These adapters allow for custom storage solutions (e.g., YAML, JSON5, encryption) and seamless integration across different environments (file system for Node, localStorage or sessionStorage for browsers). It is primarily an ESM-first package, simplifying usage in modern JavaScript projects but requiring specific import patterns. It also offers convenient presets for common use cases like JSON files and browser storage, alongside automatic in-memory mode for testing.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install lowdb"],"cli":null},"imports":["import { JSONFilePreset } from 'lowdb/node'","import { Low } from 'lowdb'","import { LocalStoragePreset } from 'lowdb/browser'","import { JSONFilePreset } from 'lowdb/node'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { JSONFilePreset } from 'lowdb/node';\nimport { fileURLToPath } from 'node:url';\nimport { dirname, join } from 'node:path';\n\n// Resolve __dirname equivalent in ESM\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = dirname(__filename);\nconst dbFilePath = join(__dirname, 'db.json');\n\ntype Post = { id: number; title: string; views: number };\ntype Data = { posts: Post[] };\n\nasync function runDbExample() {\n  // Define default data with type safety\n  const defaultData: Data = { posts: [] };\n\n  // Initialize the database with a JSON file adapter\n  // This will create 'db.json' if it doesn't exist, populated with defaultData\n  const db = await JSONFilePreset<Data>(dbFilePath, defaultData);\n\n  // Read the current data from the file\n  await db.read();\n  console.log('Initial data:', db.data);\n\n  // Add a new post using db.update() for atomic writes and automatic saving\n  const newPost = { id: 1, title: 'lowdb is awesome', views: 100 };\n  await db.update(({ posts }) => posts.push(newPost));\n  console.log('Data after adding post:', db.data);\n\n  // Query data using native JavaScript array methods\n  const { posts } = db.data;\n  const firstPost = posts.at(0);\n  console.log('First post:', firstPost);\n\n  // Modify an existing item and explicitly save\n  if (firstPost) {\n    firstPost.views += 10; // Modify in memory\n    console.log('Modified post in memory:', firstPost);\n    await db.write(); // Explicitly write changes back to db.json\n  }\n  console.log('Final data after explicit write:', db.data);\n}\n\nrunDbExample().catch(console.error);\n","lang":"typescript","description":"Demonstrates initializing a type-safe lowdb instance with a JSON file in Node.js (ESM), adding data using `db.update`, querying data, and explicitly saving modifications made to `db.data`.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}