EzDevInfo.com

LokiJS

javascript embeddable / in-memory database Lightweight javascript in-memory database: LokiJS lokijs - a lightweight javascript in-memory database

Find by number index

I try to find entries by indices:

var questions = db.addCollection('questions',{indices: ['key']});
questions.insert({
key: 1,
quest: 'any idea'
},
{
  key: 2,
  quest: 'no idea'
});

questions.find({'key':1});

but I get no result. Dumping the database shows the entries. Any idea ?


Source: (StackOverflow)

Are Lokijs ID's Unique?

Are lokijs id's ($loki) unique?

if I have 2 documents from one collection with $loki:1 and $loki:2 as id's, and I delete $loki:1, the next one I create should it be $loki:3 ???


Source: (StackOverflow)

Advertisements

LokiJS: Simple find query returns wrong result

In LokiJS I try a very simple query (which I assume is AND):

var dbRes = recsCol.find({'format':format, 'cardId':-1});

after inserting some data with

recsCol.insert({format:format, cardId:id, recCardId:key, amount:item[key]});

that doesn't contain a cardId of -1.

The query still yields results. Is this expected behaviour? If so, how can I make the fields match exactly so that I won't get a result in this case?


Source: (StackOverflow)

What (in_memory) graph DB if modeling data is focused

I am out of ideas and hope to get some useful input. I am using this question to compress my experiences and share them, hoping to inspire some distributors to go the next step with modeling graph databases as a first class question/way.

I am validating some graph database solutions usable by node.js about some weeks. My use case is to save interactions of different social user network accounts. The need is to use CPU and memory in the most efficient way.

My most important requirements are:

  • in_memory (at least for indexing)
  • open source (and free to use)
  • same JavaScript/Node.js performance as first class citizen
  • comfortable query and modeling language

Neo4J

I really like cypher so my best choice would be Neo4j. But the major issue about Neo4j is the JavaScript access is non-native. It uses the REST-API which is about ten times (10x) slower than direct Java access. So I took a look at node-neo4j-embedded, but it has been inactive for more than two years. It looks like its author isn't active at all (bad sign).

ArangoDB

The really nice core developers of ArangoDB answered to my question about internals. Finally it means JavaScript is first class citizen because native queries can be pushed out of JS. Looking at the open source benchmarks, I think it is fair. But I am afraid they didn't use node-neo4j-embedded for their benchmark. The benchmarks compare the REST-APIs (Edited because of @weinberger comment). I wished they compare the native APIs (maybe someone is snoopy enough and give it a try! - let us know!). Update: As I noticed now, OrientDB has answered the benchmark with a new node.js driver (using Command Cache by starting the server with -Dcommand.cache.enabled=true -Dcommand.cache.minExecutionTime=3, what isn't fair, because it wasn't a query caches benchmark!)

Because I like to use ArangoDB as a graph database I would have 3 choices (source: FAQ):

In general it isn't comfortable like cypher. And I am not sure how to compare and what is the right way modeling data (like Neo4J explains very well). I'd love to have something like this for ArangoDB Graphs. It feels like ArangoDB is focused on graph operations and Neo4J fits more the needs of using graphs if you have more relations than rows (the reason to use graphs instead of relations with joins).

MongoDB

The document based MongoDB isn't optimized for graph operations but latterly has gotten an experimental in_memory storage engine. Also there are some projects either in_memory or graph related but nothing is really compelling. And at this discussion it looks like MongoDB isn't what I like to use.

OrientDB

Because there is a comparison about OrientDB vs. MongoDB available (from OrientDB) I though about to use this one. "OrientDB has a hybrid Document-Graph engine" using SQL. I am a former PHP/MySQL expert. But where is the modeling part ? Their chapter working with graphs is not cypher like. It is like using SQL for Graphs. There is nothing wrong with that, but using cypher before I miss the modeling like feeling. If someone did a modeling process with OrientDB and Graphs maybe you could write a tutorial like Neo4J had done.

Update: About JavaScript access like first citizen there are news: "In the next release the speed of this driver will be comparable to the native Java one" The forked node.js driver had bin fixed last days.

LokiJS

Before I was looking at Neo4J, ArangoDB and MongoDB, I played around with that JavaScript based in_memory database called LokiJS, what seams to follow the strategy to ignore everything what slows down performance and efficiency. LokiJS is trying to complete the Mongo-Style (RoadMap). The major issue is the bad ability to scale. Of cause it isn't a graph database but it was an interesting solution while the beginning of my project. Also it wasn't a perfect feeling to find all the distributed documentation (maybe they should reboot with GitBook). Finally LokiJS is a very interesting project at all and I hope they will go forward!

LevelDB

Former when I wrote my degree paper I was looking at levelDB. Remembering this while writing this post, I searched for LevelDB in_memory and got a promising result called MemDown (see also). I haven't tested this find, but maybe someone has experiences working and modeling for this solution. Maybe it would be the most efficient way if all the others will not fit because I would simply write a lightweight cypher clone with the goal to stay much lightweight as I can do.

Conclusion

As you likely noticed I am not the super hero about graphs. But I am initial diving into this and try to get an overview. I assume there are a lot people out there has to ask same questions as me but hadn't got time to do. I hope this post will help a lot people and will change by comments and answers to become a well done overview how to modeling data for graphs.


@editors: You are welcome.

@commenters: This is my result of personal research - if you also have done a journey like me, please answer with a short summary like I have done for each DB I've evaluated (don't forget to target my 4 goals).


Source: (StackOverflow)

Does Lokijs generates its own IDs?

I've been trying to use lokijs on a project.

Does Lokijs generates its own IDs? Or do we need to handle that our selves?


Source: (StackOverflow)

Unable to loadDatabase with lokiJs

I have trying to create a database with a collection added to this database and the changes saved to a IndexedDB.

Below is my code

  1. Two controllers SaveController and LoadController.

myApp.controller('SaveController', ['$scope', 'Loki', function ($scope, Loki) {

// SAVE : will save App/Key/Val as 'finance'/'test'/{serializedDb}
// if appContect ('finance' in this example) is omitted, 'loki' will be used
var idbAdapter = new LokiIndexedAdapter('finance');

var db = new loki('ProjectDb', { adapter: idbAdapter });

var coll = db.addCollection('SampleCollection');
coll.insert({ SampleId: 'Sample text.....' });

db.saveDatabase();  // could pass callback if needed for async complete

}]);

and then in my LoadController i use the

myApp.controller('LoadController', ['$scope', 'Loki', function ($scope, Loki) {

var idbAdapter = new LokiIndexedAdapter('finance');

var db = new loki('ProjectDb', { adapter: idbAdapter, autoload: true });
db.loadDatabase({}, function (result) {
    console.log(result);
});

alert(db.getCollection("SampleCollection"));

}]);

I get a null when i alert "alert(db.getCollection("SampleCollection"));" . It never enters the call back of the "loadDatabase" method.

Is there something that i am missing ?

IndexedDB in Browser

IndexedDB

Here the page html

Page HTML

lokijs script controller


Source: (StackOverflow)

Update from the Html View triggers update to the Dynamic View where in saving it to the LocalStorage loki js

I have a a angular factory class User which gets instantialted via a dynamic view filling in data from the view to the user variable which is then bound to the view.

I need to update the localstorage when ever there is a change to the userView.Name which is bound to the view input type='text' i.e. any change to the value in the text field directly changes the text in the user instance of the class and then updates the dynamic view which would update the localStorage ?

I have a sample of the code hosted at CodePen

Sample Code illustrating the problem

app.controller('TestController', ['$scope', 'Loki', 'MobileConstants', 'LocalStorageApi', 'User', 
function ($scope, Loki, MobileConstants, LocalStorageApi, User, Book) {
    LocalStorageApi.initialize();
    var userData = LocalStorageApi.UsersView.data()[0];

    var user = new User(userData);

    $scope.userView = user;
}]);

app.factory('User', ['$rootScope', function ($rootScope) {
    function User(userData) {
        User.prototype.Name = userData.nameSample;
    };

    User.prototype = {
        Name: null,
    }

    return User;
}]);

below is how the code would look like illustrating the problem

Angular Code

Html View with data bound to it.


Source: (StackOverflow)

How can I get a nested object within lokijs Collection

I'm using lokijs to store some data that I will need to work with. My data is shown below. As a newbie to lokijs I've been struggling to find a way to get item_part_option_groupsarray out of part.

My code

        partDb = new loki('part.json');
        partData =  partDb.addCollection('part');
        $.get(urlHelper.createUrl('item/parts', 'part', config.partId + '.json'), function(data){
           partData.insert(data);
            var data = partData.find({'part':'item_part_option_groups'});
            console.log(data);
        });

The Data

   {
        "part": {
            "item_part_id": 10,
            "name": "Sleeve",
            "has_validation": 0,
            "description": "",
            "is_disabled": "1",
            "created": "2015-07-22T23:55:09+0000",
            "updated": null,
            "item_part_option_groups": [{
                "item_part_option_group_id": 10,
                "order_index": 0,
                "description": "",
                "item_part_id": 10,
                "created": "2015-07-22T23:55:39+0000",
                "updated": null,
                "item_part_options": [{
                    "item_part_option_id": 8,
                    "name": "Sleeve Type",
                    "is_conform_value": 0,
                    "has_validation": 1,
                    "item_part_option_type_id": 3,
                    "created": "2015-07-22T23:57:24+0000",
                    "updated": null,
                    "item_part_option_values": [{
                        "value": "Short Sleeve",
                        "order_index": 0,
                        "item_part_option_id": 8,
                        "item_part_option_value_id": 7,
                        "item_part_option_includes": [

                        ]
                    }, {
                        "value": "Long Sleeve",
                        "order_index": 0,
                        "item_part_option_id": 8,
                        "item_part_option_value_id": 8,
                        "item_part_option_includes": [

                        ]
                    }, ]
                }]
            }]
        }

    }

Source: (StackOverflow)

LokiJS with cordova

I am currently I'm working an app by using Onsen UI+Cordova.

I am trying to use LokiJS as my server-less database. As per LokiJS's documents:

If nothing is specified, Loki tries default methods (fs in Node, localStorage in browser and cordova).

Does that mean I can't write the database file *.json to filesystem? ie. SDCard or internal storage somewhere.


Source: (StackOverflow)

browser app unable to access loki.js

I'm trying to use loki.js in a browser app. I included the following script in the HTML

<script src="./bower_components/lokijs/src/lokijs.js"></script>

However the following js code gave me "unexpected token" error

db = new loki('test');

Source: (StackOverflow)

LokiJS: insert existing value for index doesn't error - how to make unique indices?

if I try to override an existing indexed field, I do not get an error. It should error, because it is not update()!

var loki = require('lokijs');
var db = new loki('test.json');
var users = db.addCollection('users', { indices: ['userID']});
users.insert(
   {
     'name': 'Anna',
     'userID': 1
   },
   {
     'name': 'Bernd',
     'userID': 2
   },
   {
     'name': 'Christa',
     'userID': 3
   });
db.save();
users.insert({'name': 'Dieter','userID': 2}); // this should error!!

How can I make an unique index to get an error when trying to inset an existing userID ?


Source: (StackOverflow)

Reorganize simple app with nodejs and lokijs

It is my first node.js app. I use http://www.nodebeginner.org/ as example and http://lokijs.org/ as in-memory db. So, i have problem with code orgatization. Now it is looks like following:

index.js

var server = require('./server');
var router=require('./router');
var reqHandler=require('./handler');

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

var handle={
    '/getById':reqHandler.getById
};
db.loadDatabase({},function(){
    server.start(router.route, handle,db)
});

server.js

var http = require('http');
var url = require('url');

function start(route,handle,db){
    http.createServer(function (req, res) {
            route(req, res, handle,db);
    }).listen(8080);
}

exports.start=start;

router.js

var url = require('url');

function route(req,res,handle,db) {
    var pathname = url.parse(req.url).pathname;
    if (typeof handle[pathname] === 'function') {
        handle[pathname](res, req,db);
    } else {
        //process error
    }
}

exports.route = route;

handler.js

var url = require('url');
var loki=require('lokijs');
var querystring=require('querystring');

function getById(res, req,db){
    //process req. send response
    //HERE db MUST BE ACCESSIBLE
}

exports.getById=getById;

With current code structure I have to pass db variable from the most begining index.js to the last handler.js. It seems like not brilliant solution for me.

Can anybody help me with it? Thx in advance.


Source: (StackOverflow)