11/26/2014

indexedDB Wrapper

/*****************************************************************************
Ref:
http://www.dexie.org/

Starting point:
http://rawgit.com/dfahlander/Dexie.js/master/samples/codeproject-article/DexieAlgorithmsSamples.html

http://blog.nparashuram.com/2012/10/indexeddb-example-on-cordova-phonegap_12.html
http://nparashuram.com/IndexedDBShim/

    
    
*****************************************************************************/
//
// App global database instance and schema
//
        var db = new Dexie("MyDB");
        db.version(1).stores({
            friends: "++id,name,shoeSize"
        });
        db.open();
//
// Populate some data
//
        function populateSomeData() {
            log("Populating some data", "heading");
            return db.transaction("rw", db.friends, function () {  // objectStore??
                db.friends.clear();
                db.friends.add({ name: "David", shoeSize: 43 });
                db.friends.add({ name: "Ylva", shoeSize: 37 });
                db.friends.add({ name: "Jon", shoeSize: 44 });
                db.friends.add({ name: "Måns", shoeSize: 42 });
                db.friends.add({ name: "Daniel", shoeSize: 39 });
                db.friends.add({ name: "Nils", shoeSize: 45 });
                db.friends.add({ name: "Zlatan", shoeSize: 47 });
                // Log data from DB:
                db.friends.orderBy('name').each(function (friend) {
                    log(JSON.stringify(friend));
                });
            }).catch(function (e) {
                log(e, "error");
            });
        }

// Query Example

db.friends.where('name').equalsIgnoreCase('david')
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });

//  Modify
db.friends.where('name').equalsIgnoreCase('david')   // query (read) only
                             .each(function (friend) {
                                 log(JSON.stringify(friend));
                             });


db.transaction("rw", db.friends, function () { // read (query) & modify    
    db.friends.where("name").equalsIgnoreCase('david').modify({shoeSize: 100});
})
 

No comments: