-
Notifications
You must be signed in to change notification settings - Fork 3
Example Code
Simon Y. Blackwell edited this page Apr 23, 2015
·
6 revisions
<script src="joqular.js" type="text/javascript"></script>
// define a base class, we could use Object, but this rightly scares some people!
function Entity () { }
// enhance the base class, make sure to re-assign the class to the returned value
Entity = JOQULAR.enhance(Entity, {
enhancePrimitives:true,
enhanceArray:true,
enhanceSet:true,
enhanceDate:true,
index:true,
datastore:{name:"JOQULARExample",type:"IndexedDB", format:"pseudo-blob"}});
// define business model classes
function Person(name,age) {
this.name = name;
this.age = age;
}
Person.prototype = new Entity();
// create the index, make sure to re-assign the class
Person = JOQULAR.createIndex(Person);
// create a couple of people
var p1 = new Person('Joe',24);
var p2 = new Person('Mary',18);
// match them
console.log(p1.joqularMatch({age: 24})); // prints true
console.log(p1.joqularMatch({age: {neq: 18}})); // prints true
console.log(p1.joqularMatch({age: {eq: 18}})); // prints false
// find them
console.log(JSON.stringify(Person.joqularFind({age: 24})); // prints [{name: 'Joe', age: 24}]
console.log(JSON.stringify(Person.joqularFind({age: {gt: 16}})); // prints [{name: 'Joe', age: 24},{name: 'Mary', age: 18}]
// save them, waiting 1 second for the operation to complete
Person.joqularSave(1000);
// clear the index from memory, but leave the data on disk
Person.joqularClear(true);
// you can't find them
console.log(JSON.stringify(Person.joqularFind({name: 'Joe'})); // prints []
// load them, waiting 1 second fro operation to complete
Person.joqularLoad(1000);
// say hello to Joe!
console.log(JSON.stringify(Person.joqularFind({name: 'Joe'})); // prints [{name: 'Joe', age: 24}]