Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I update data in indexedDB?

I have tried to get some information from W3C regarding the update of an objectStore item in a indexedDB database, but with not so much susccess. I found here a way to do it, but it doesn't really work for me.

My implementation is something like this

DBM.activitati.edit = function(id, obj, callback){
    var transaction = DBM.db.transaction(["activitati"], IDBTransaction.READ_WRITE);
    var objectStore = transaction.objectStore("activitati");
    var keyRange = IDBKeyRange.only(id);

    objCursor = objectStore.openCursor(keyRange);
    objCursor.onsuccess = function(e){
        var cursor = e.target.result;
        console.log(obj);
        var request = cursor.update(obj);
        request.onsuccess = function(){
            callback();
        }
        request.onerror = function(e){
            conosole.log("DBM.activitati.edit -> error " + e);
        }

    }   
    objCursor.onerror = function(e){
        conosole.log("DBM.activitati.edit -> error " + e);
    }                   
}

I have all DBM.activitati.(add | remove | getAll | getById | getByIndex) methods working, but I can not resolve this.

If you know how I can manage it, please, do tell!

Thank you!

like image 636
Michael Avatar asked Jun 26 '12 23:06

Michael


People also ask

How do I refresh the data in IndexedDB?

To refresh the data, view an object store and then click Refresh ( ). To refresh all data, view a database and click Refresh database. IndexedDB keys and values aren't editable from the Application tool. However, since DevTools has access to page context, you can run JavaScript code within DevTools to edit IndexedDB data.

What is the update event in IndexedDB?

That event is raised when you use the IndexedDB open function with a database version number which is greater than the current database version number. For example, let's assume that there is an IndexedDB database with the name PeopleDB which has a version number of 1. The following code will raise the updateneeded event:

How do I delete a database in IndexedDB?

View an IndexedDB object store. Figure 10. Viewing an object store Click Clear object store . View the IndexedDB database that you want to delete. Click Delete database. Figure 11. The Delete database button Open the Clear storage pane. Make sure that the IndexedDB checkbox is enabled. Click Clear site data. Figure 12.

How do I view and change IndexedDB data in Chrome DevTools?

This guide shows you how to use Chrome DevTools to view and change IndexedDB data. It assumes you're familiar with DevTools. If not, see Get started. It also assumes you're familiar with IndexedDB. If not, see Using IndexedDB. Click the Application tab to open the Application panel. Expand the IndexedDB menu to see which databases are available.


2 Answers

Check out this jsfiddle for some examples on how to update IDB records. I worked on that with another StackOverflower -- it's a pretty decent standalone example of IndexedDB that uses indexes and does updates.

The method you seem to be looking for is put, which will either insert or update a record if there are unique indexes. In that example fiddle, it's used like this:

    phodaDB.indexedDB.addUser = function(userObject){
        //console.log('adding entry: '+entryTxt);
        var db = phodaDB.indexedDB.db;
        var trans = db.transaction(["userData"],IDBTransaction.READ_WRITE);
        var store = trans.objectStore("userData");
        var request = store.put(userObject);

        request.onsuccess = function(e){
            phodaDB.indexedDB.getAllEntries();
        };
        request.onerror = function(e){
            console.log('Error adding: '+e);
        };
    };

For what it's worth, you've got some possible syntax errors, misspelling "console" in console.log as "conosole".

like image 54
buley Avatar answered Oct 12 '22 15:10

buley


A bit late for an answer, but possible it helps others. I still stumbled -as i guess- over the same problem, but it's very simple:

If you want to INSERT or UPDATE records you use objectStore.put(object) (help)
If you only want to INSERT records you use objectStore.add(object) (help)

So if you use add(object), and a record key still exists in DB, it will not overwritten and fires error 0 "ConstraintError: Key already exists in the object store".

If you use put(object), it will be overwritten.

like image 20
Lutz Avatar answered Oct 12 '22 16:10

Lutz