What Is LokiJS?

LokiJS

 
So, basically, LokiJS is a fast in-memory document-oriented data store with persistence for node.js, browser, and HTML5 based applications, like Cordova or Phonegap etc.
 
LokiJS can be easily used on the web, it uses a Redis-style store, and it is capable of in-browser NoSQL database with syncing and persisting.
 
Before we learn more about LokiJS, let us first know the below few things:
 

In-memory data store

 
As the name suggests, the data is stored in the main memory and just because of that, we get an extremely fast query response time. There are other in-memory databases too already available and LokiJS is one of them.
 

Redis

 
Redis is an opensource in-memory data structure that allows users to organize data using the key-value storage method. Learn more here.
 
Now, let us talk about some best features of LokiJS
  • Fast performance
  • Removes SQLite in Cordova, works as a session store and full-blown NoSQL database in node.js, works as an in-browser database with syncing capabilities.
  • Indexing/Seconday Indexing/Unique Indexing
  • The “Dynamic View”, a kind of “live filter” (self-materializing views)
  • Resultset with Fluent API
  • A built-in indexedDB and node.js FS adapter
  • Changes API
  • Compound sort for sorting on multiple columns
  • Insert class instance and deserialize/inflate objects into the class instance
  • Partial compatibility with MongoDB API
  • RethinkDB-style joins
So, we have learned what are the best features of LokiJS, let us look at the working of LokiJS.
 

Working of LokiJS

 
Now, we will look at how we can install LokiJS and use it on our projects.
 

Installation

 
LokiJS is available on npm and bower.
 
Run the below command,
 
npm install lokijs
bower install lokijs
 
It can be also be used in HTML, all you have to do is use the minified loki js file inside your script tag.
 

Usage

 
Creating a database,
  1. const db = new loki(dbname.json')   
Once the database is created we are required to create a collection in which we will store our required data.
 
For example, let's say we want to store the user's details so we will create a collection named `users`.
 
Creating collection named `users`,
  1. const users = db.addCollection('users')   
Now we will see how we can store data in our collection.
 
Inserting document
  1. children.insert({    
  2.    fullname: ‘sam’,    
  3.    email: ‘[email protected]’    
  4. })   
How to retrieve your data from the collection.
 
Retrieve documents
  1. users.get(1); // returns sam    
  2. users.find( {'fullname':'sam'} )   
Try LokiJS and build some amazing projects in HTML.
 
References
 
http://lokijs.org