What is lokijs?
LokiJS is an in-memory database that prioritizes performance and simplicity. It is written in JavaScript and can be used in web browsers, Node.js, and Cordova/PhoneGap applications. It offers a powerful query system and supports indexing for fast lookups.
What are lokijs's main functionalities?
In-memory database
LokiJS allows you to create an in-memory database, add collections, and insert documents.
const loki = require('lokijs');
const db = new loki('example.db');
const users = db.addCollection('users');
users.insert({ name: 'Odin', age: 1000 });
users.insert({ name: 'Thor', age: 30 });
Powerful query system
You can query the database using a MongoDB-like query syntax to retrieve data based on complex criteria.
const elderlyGods = users.find({ age: { '$gt': 500 } });
Indexing for fast lookups
LokiJS supports indexing of collections to enable faster query response times, especially beneficial for large datasets.
users.ensureIndex('age');
const youngGods = users.find({ age: { '$lte': 100 } });
Persistence adapters
LokiJS can be configured to use various persistence adapters, allowing you to save the database state to a file, local storage, or other storage systems.
const lokiFsAdapter = require('lokijs/src/loki-fs-structured-adapter');
const adapter = new lokiFsAdapter();
const db = new loki('example.db', { adapter: adapter });
Changes API
The Changes API allows you to listen for changes in the database, which can be used for syncing with a remote database or triggering other actions.
db.on('changes', function(changes) {
console.log('Database changes:', changes);
});
users.insert({ name: 'Loki', age: 1042 });
Other packages similar to lokijs
nedb
NeDB is another lightweight JavaScript database, similar to LokiJS, that works both in Node.js and the browser. It also offers in-memory storage and has a MongoDB-like API. However, NeDB is no longer actively maintained.
pouchdb
PouchDB is a database that runs in the browser or in Node.js and is designed to sync with CouchDB. It offers similar query capabilities to LokiJS but focuses more on synchronization and offline-first applications.
rxdb
RxDB is a reactive database for JavaScript applications, which can be used in browsers, Node.js, and hybrid apps. It is built on top of PouchDB and offers real-time synchronization and event-driven data flows, which is different from LokiJS's approach.
lowdb
LowDB is a small local JSON database powered by Lodash, suitable for small projects (Node, Electron, and the browser). It is simpler than LokiJS and does not offer in-memory storage, but it is easy to use for small datasets and simple applications.
LokiJS
Overview
LokiJS is a document oriented database written in javascript.
Its purpose is to store javascript objects as documents in a nosql fashion and retrieve them with a similar mechanism.
Runs in node (including cordova/phonegap and node-webkit) and the browser.
LokiJS is ideal for the following scenarios:
- where a lightweight in-memory db is ideal (e.g., a session store)
- cordova/phonegap mobile apps where you can leverage the power of javascript and avoid interacting with native databases
- data sets loaded into a browser page and synchronised at the end of the work session
- node-webkit desktop apps
LokiJS supports indexing and views and achieves high-performance through maintaining a binary-index for data.
Demo
The following demos are available:
Wiki
Example usage can be found on the wiki
Main Features
- Fast performance NoSQL in-memory database, collections with binary-index
- Runs in multiple environments (browser, node)
- Dynamic Views for fast access of data subsets
- Built-in persistence adapters, and the ability to support user-defined ones
- Changes API
Current state
LokiJS is at version 1.2 [Schnee]. While the roadmap is exciting, LokiJS is at the moment stable.
As LokiJS is written in Javascript it can be run on any environment supporting javascript such as browsers, node.js/node-webkit, and hybrid mobile apps (such as phonegap/cordova).
Made by @techfort, with the precious help of Dave Easterday. Leave a tip or give us a star if you find LokiJS useful!
Installation
For browser environments you simply need the lokijs.js file contained in src/
You can use bower to install lokijs with bower install lokijs
For node environments you can install through npm install lokijs
.
Roadmap
- key-value datastore
- MRU cache
- MongoDB API compatibility
- server standalone (tcp and http servers and clients)
- replication and horizontal scaling
Contact
For help / enquiries contact joe.minichino@gmail.com
License
Copyright (c) 2015, Joe Minichino joe.minichino@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- All advertising materials mentioning features or use of this software
must display the following acknowledgement:
This product includes software developed by TechFort.
- Neither the name of TechFort nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.