Skip to content

Instantly share code, notes, and snippets.

@techfort
Created November 11, 2014 21:12
Show Gist options
  • Save techfort/de24acc4ed90fc1095f8 to your computer and use it in GitHub Desktop.
Save techfort/de24acc4ed90fc1095f8 to your computer and use it in GitHub Desktop.
LokiJS DynamicView simple example
var loki = require('lokijs'),
db = new loki('test.json'),
doctors = db.addCollection('doctors');
var Log = console.log;
doctors.insert({
name: 'David Tennant',
doctorNumber: 10
});
doctors.insert({
name: 'Matt Smith',
doctorNumber: 11
});
Log('Collection data:');
Log(doctors.data);
// create dynamic view
Log('DynamicView');
var view = doctors.addDynamicView('newerDoctors');
view.applyWhere(function (obj) {
return obj.doctorNumber > 8;
});
view.applySimpleSort('doctorNumber', true);
Log(view.data());
Log('-------------------');
// insert doctor number 8, shouldn't turn up in the view
// and a doctor number 12, should turn up
Log('Re-inspect view after inserts');
doctors.insert({
name: 'Paul McGann',
doctorNumber: 8
});
doctors.insert({
name: 'Peter Capaldi',
doctorNumber: 12
});
Log(view.data());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment