LokiJS is a fast, document-oriented JavaScript in-memory database designed for various environments including browsers, Node.js, and NativeScript. It functions by storing JavaScript objects as documents in a NoSQL fashion, enabling high-performance retrieval through indexing and dynamic views. Currently at version 1.5.12, its release cadence appears infrequent, with the last major update several years ago. Key differentiators include its small footprint, suitability for client-side session stores, and built-in persistence adapters (like localStorage, IndexedDB, or filesystem) that can be extended with custom solutions. It prioritizes speed by maintaining unique and binary indexes and offering dynamic views for frequently accessed data subsets, making it ideal for performance-critical applications where data can reside primarily in memory.
npm install lokijsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize a LokiJS in-memory database, configure it with the LocalStorage adapter for persistence, add a collection with a unique constraint, insert documents, and perform basic queries and updates. It includes an `autoload` and `autosave` setup to manage data lifecycle across sessions.
For new projects, consider modern alternatives like RxDB or other actively maintained in-memory databases. For existing projects, be aware of the potential for limited support or future breaking changes without clear migration paths.
Always initialize LokiJS with a suitable persistence adapter (e.g., `LokiLocalStorageAdapter`, `LokiIndexedAdapter`, `LokiFsAdapter` for Node.js) and enable `autoload: true` and `autosave: true` options to prevent data loss. Remember to manually call `db.saveDatabase()` before application exit if `autosave` is not active or for immediate writes.
If multi-tab support is required, consider wrapping LokiJS with a solution that handles leader election or provides a more robust multi-instance synchronization mechanism, such as the RxDB LokiJS plugin which addresses this issue. Alternatively, ensure only one tab is actively writing to the database.
Retrieve the document from the collection first using `find()` or `findOne()`, modify the retrieved object, and then pass that *same object* back to `collection.update()`. Alternatively, use `collection.findAndUpdate()` with a query and an update function.
Always retrieve the document from the collection, modify the returned object reference, and then pass that specific object to `collection.update()`. Example: `const doc = collection.findOne({ id: 1 }); doc.field = 'newValue'; collection.update(doc);`For Node.js, ensure `const Loki = require('lokijs');` is used. For modern browsers or bundlers supporting ESM, use `import Loki from 'lokijs';`. If using a specific adapter, ensure the path `lokijs/src/loki-adapter-name` is correct. Verify `lokijs` is correctly installed in `node_modules` and your build process includes it.No dependency data recorded yet.