-
Notifications
You must be signed in to change notification settings - Fork 3
Example Code
Simon Y. Blackwell edited this page Apr 24, 2015
·
6 revisions
This basic example is also available at /examples/index.html in the deployment npm package.
<script src="../joqular.js" type="text/javascript"></script>
Open the developer tools in your browser to see this sample in action.
<script>
// define a base class, we could use Object, but this rightly scares some people! If you do that, then
// data constructed with {} will no longer be an instance of an Object, so be VERY careful.
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,true);
// create a couple of people
var p1 = new Person('Joe',24);
var p2 = new Person('Mary',18);
// match them
console.log("Matched: " + JSON.stringify(p1.joqularMatch({age: 24}))); // prints {name: "Joe", age: 24}
console.log("Matched: " + JSON.stringify(p1.joqularMatch({age: {neq: 18}}))); // {name: "Joe", age: 24}
console.log("Matched: " + p1.joqularMatch({age: {eq: 18}})); // prints null
// find them
console.log("Found: " + JSON.stringify(Person.joqularFind({age: 24}))); // prints [{name: 'Joe', age: 24}]
console.log("Found: " + JSON.stringify(Person.joqularFind({age: {gt: 16}}))); // prints [{name: 'Joe', age: 24},{name: 'Mary', age: 18}]
// save them,
Person.joqularSave().then(function(saved) {
console.log("Saved " + saved + " records");
// clear the index from memory, but leave the data on disk
Person.joqularClear(true);
// you can't find them
console.log("Found: " + JSON.stringify(Person.joqularFind({name: 'Joe'}))); // prints []
// load them
Person.joqularLoad().then(function(loaded) {
console.log("Loaded " + loaded + " records");
// say hello to Joe!
console.log("Found: " + JSON.stringify(Person.joqularFind({name: 'Joe'}))); // prints [{name: 'Joe', age: 24}]
});
});
</script>