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 client side database written in javascript.
Its purpose is to store JSON documents in a mongodb fashion (the API is similar but not strictly compliant) and retrieve them with a similar mechanism.
Runs in node and the browser.
LokiJS is ideal for the following scenarios:
- where a lightweight in-memory db is ideal
- data sets are not so large that it wouldn't be a problem loading the entire db from a server and synchronising at the end of the work session
- cross-platform mobile apps where you can leverage the power of javascript and avoid interacting with native databases
LokiJS has limited supports for indexing, transactions, couchdb-style views.
example usage here
Roadmap
In the near future mongodb-style dbRef will be implemented, wider and more specific support for indexes such as unique, compound.
License
Copyright (c) 2013, 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.