{"library":"lum_lokijs","title":"LokiJS: In-Memory Document Database","description":"LokiJS is a fast, embeddable, document-oriented NoSQL database written entirely in JavaScript. It operates in-memory, making it suitable for performance-critical applications like client-side session stores, embedded databases in Electron or Node-WebKit apps, or mobile applications using frameworks like Nativescript and Cordova. It features collections with unique and binary indexes, dynamic views, a Changes API for synchronization, and supports joins. Persistence is handled through a pluggable adapter system, with built-in adapters for Node.js file system, browser IndexedDB, and localStorage. While the last published version on npm is 1.5.12 (last updated around 2019-2020), the project has seen minimal activity and is largely considered unmaintained, with its official successor being LokiDB. Users should be aware of its unmaintained status and consider the successor or other alternatives for new projects.","language":"javascript","status":"abandoned","last_verified":"Wed Apr 22","install":{"commands":["npm install lum_lokijs"],"cli":null},"imports":["import Loki from 'lokijs';\n// Or for CommonJS:\nconst Loki = require('lokijs');","import LokiIndexedAdapter from 'lokijs/src/loki-indexed-adapter';\n// Or for CommonJS:\nconst LokiIndexedAdapter = require('lokijs/src/loki-indexed-adapter');","const users = db.addCollection('users');\n// (Collection class is usually instantiated via db.addCollection, not directly imported)"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const Loki = require('lokijs');\nconst LokiIndexedAdapter = require('lokijs/src/loki-indexed-adapter');\n\nasync function initializeDatabase() {\n  const adapter = new LokiIndexedAdapter('my-loki-app');\n  const db = new Loki('my-database.db', {\n    adapter: adapter,\n    autosave: true,\n    autosaveInterval: 4000 // Save every 4 seconds\n  });\n\n  // Load the database from persistence, or create if it doesn't exist\n  await new Promise((resolve, reject) => {\n    db.loadDatabase({}, (err) => {\n      if (err) {\n        console.error('Error loading database:', err);\n        reject(err);\n      } else {\n        console.log('Database loaded or created.');\n        resolve();\n      }\n    });\n  });\n\n  let users = db.getCollection('users');\n  if (!users) {\n    users = db.addCollection('users', { unique: ['email'], autoupdate: true });\n    console.log('\"users\" collection created.');\n  }\n\n  // Insert some data if the collection is empty\n  if (users.count() === 0) {\n    users.insert({ name: 'Alice', email: 'alice@example.com', age: 30 });\n    users.insert({ name: 'Bob', email: 'bob@example.com', age: 24 });\n    users.insert({ name: 'Charlie', email: 'charlie@example.com', age: 35 });\n    console.log('Initial data inserted.');\n  }\n\n  // Find and update a document\n  let bob = users.findOne({ name: 'Bob' });\n  if (bob) {\n    bob.age = 25;\n    users.update(bob); // Explicit update might be needed for certain changes like array mutations\n    console.log('Bob updated:', users.findOne({ name: 'Bob' }));\n  }\n\n  // Query data\n  const youngUsers = users.find({ age: { '$lt': 30 } });\n  console.log('Users under 30:', youngUsers);\n\n  // Save changes explicitly (autosave also does this)\n  await new Promise((resolve, reject) => {\n    db.saveDatabase((err) => {\n      if (err) reject(err); else resolve();\n    });\n  });\n  console.log('Database saved.');\n\n  return db;\n}\n\ninitializeDatabase().catch(console.error);\n","lang":"javascript","description":"This quickstart initializes a LokiJS database with IndexedDB persistence, creates a collection, inserts and updates documents, and demonstrates a basic query. It highlights asynchronous database loading/saving and explicit updates.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}