Lawnchair : check store and its document exists

184 Views Asked by At

I've created a Lawnchair store and saved it. See the following code:

var DB = new Lawnchair({ name: "DB", adapter: ["indexed-db", "webkit-sqlite", "ie-userdata", "blackberry-persistent-store", "dom", "window-name", "gears-sqlite", "memory"] });
DB.save({ key: "resKey", res: res});

Here res is a javascript object, it is the data that gets stored.

But when I close and reopen the web page the next time, I want to check whether this store exists. If the store exists, I want to check whether this document exists. How to do these checks?

Thanks

PS - Is there any good resource where I can learn Lawnchair?

1

There are 1 best solutions below

1
Temp O'rary On

Finally after a lot of trials, I came up with the following :

// create a store //* CORRECT TO USE
DB = new Lawnchair({ name: "DB", adapter: ["indexed-db", "webkit-sqlite", "ie-userdata", "blackberry-persistent-store", "dom", "window-name", "gears-sqlite", "memory"] });
//Save a document/table
DB.save({ key: "resKey", data: res }, function () {
    //Access the created store
    DB = Lawnchair({ name: "DB", adapter: ["indexed-db", "webkit-sqlite", "ie-userdata", "gears-sqlite", "blackberry-persistent-store", "dom", "window-name", "memory"] });
    console.log(DB)
    //check if the document exists based on the key we used to create it
    DB.exists("resKey", function (e) {
        console.log("exists : " + e)
        if (e == true) {
        //get all records from the store
            DB.all(function (r) {
                console.log(r);
        //remove all document/table from the store
                DB.nuke(function () {
                    console.log("Data nuke-ed");
            //check if the document exists based on the key we used to create it
                    DB.exists("resKey", function (e) {
                        console.log("exists : " + e)
                        if (e == true) {
                //get all records from the store
                            DB.all(function (r) {
                                console.log(r);
                            });
                        } else {
                            console.log("Data deleted");
                        }
                    });
                });
            });
        } else {

        }
    });
});

Please let me know if this is the right way to do it or is there a better way. If you liked my effort give me points :)