Premium Economy Software


Singapore
Perth W Australia

Loki JS Quick Tutorial

January 22, 2017 Coding 0 comments

Loki JS is a lightweight Javscript based no sql database that you can use in your hybrid apps.

This week I messed around a bit, but I was upset that the documentation pretty much sucked, so I wrote some for you.

 

1. Installing

Use the Node Package Manager (NPM)

npm install lokijs

2. Setup with Browsify

So that you can run it in browser. Put this in your main.js file.

var loki = require('lokijs')
var db = new loki('loki.json')

 

 

3. Create your database (collection)

var namesDB = db.addCollection('names')

 

4. Next Add a Row. For say a names object 

namesDB.insert({name:"John"})

5. Get that Row.

Lokijs automatically gives your row a unique ID called $loki

var namesObj = exerciseDB.get(exerciseID)

6. Update that Row

namesObj.name = 'John Woo'

namesDB.update(namesObj)

6a. Remove

namesDB.remove($loki)

 

 

7. Running a Where query

results = namesDB.where(function(obj) {
    return obj.name == 'John Woo';
})

//Add functionality 

 .simplesort('Name')
 .offset(100)
 .limit(25)
 .data();

 

8. Display All Rows (for debugging)

var namesFull = namesDB.data;

JSON.stringify(namesFull);