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.
npm install lum_lokijsVerified import paths — ran on the pinned version, not inferred.
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.
For new projects, evaluate migrating to `@lokidb/loki` or other actively maintained in-memory databases like NeDB (though also unmaintained) or Dexie.js. For existing projects, be aware of the lack of updates and potential issues.
Always initialize `Loki` with an `adapter` and `autosave: true`, or ensure you manually call `db.loadDatabase()` at startup and `db.saveDatabase()` before shutdown or critical data changes. For browsers, `LokiIndexedAdapter` is generally preferred over `LokiLocalStorageAdapter` for larger datasets due to storage limits.
For changes involving array mutations (e.g., `document.array.push(item)`) or deep nesting, explicitly call `collection.update(document)` after modification to ensure the database registers the change and updates its internal indexes and persistence state.
When using ESM, import `Loki` as a default export (`import Loki from 'lokijs';`). For adapters, ensure correct sub-path imports (`import LokiIndexedAdapter from 'lokijs/src/loki-indexed-adapter';`). If working in a pure ESM environment, dynamic `import()` might be needed for some CJS-only modules if direct transpilation fails. If using TypeScript, ensure `esModuleInterop` is enabled in `tsconfig.json`.
Use `import Loki from 'lokijs';` for ESM or `const Loki = require('lokijs');` for CommonJS. Do not use `import { Loki } from 'lokijs';`.Ensure the path to the adapter is correct and matches the file structure of the installed `lokijs` package, typically `lokijs/src/loki-indexed-adapter` (or `loki-fs-adapter`, etc.).
Initialize `Loki` with an `adapter` (e.g., `new LokiIndexedAdapter()`) and ensure `db.loadDatabase()` is called with a callback to handle loading the data before interacting with collections. For automatic saving, set `autosave: true` and `autosaveInterval` during database instantiation.
Consider using `serializationMethod: 'destructured'` and `destructureDelimiter` options when initializing `Loki` for databases with large documents to mitigate serialization overhead. Optimize queries to be more specific, utilize indexes, or consider breaking down very large operations. Upgrade Node.js if applicable.
No dependency data recorded yet.