Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a new Objectstore to an existing indexeddb

Can anybody tell me how to add a new objectstore to an existing indexeddb instance which has a version number in it. When I try to create a new version existing object stores are getting deleted.

I have a version '1', which has around 10 object stores with data. When I try to open the same database with new version number, I lost the current data and object stores.

here is what I have tried.

var _upRequest = indexedDB.open("employees");

    _upRequest.onsuccess = function (e) {
        var thisDb = e.target.result;
        var version = parseInt(thisDb.version)+1;
        thisDb.close();
        var openRequest = indexedDB.open("employees", version);

        //handle setup, this will be run by Firefox
        openRequest.onupgradeneeded = function (e) {
            console.log("running onupgradeneeded");
            var thisDb = e.target.result;

            //Create Employee
            if (!thisDb.objectStoreNames.contains("employee")) {
                console.log("I need to make the employee objectstore");
                var objectStore = thisDb.createObjectStore("employee", { keyPath: "id", autoIncrement: true });
                objectStore.createIndex("searchkey", "searchkey", { unique: false });
            }
            thisDb.close();
        }

        openRequest.onsuccess = function (e) {

            db = e.target.result;

            db.onerror = function (e) {
                alert("Sorry, an unforseen error was thrown.");
                console.log("***ERROR***");
                console.dir(e.target);
            }

            db.close();
        }
    }
like image 399
Pradeep Avatar asked Aug 09 '16 13:08

Pradeep


People also ask

How can you insert a new record collection of data in IndexedDB?

add() adds a new record to the store. IDBCursor. update() updates the record at the current position of the cursor.

Is IndexedDB domain specific?

IndexedDB follows a same-origin policy. So while you can access stored data within a domain, you cannot access data across different domains.

Can I store object in IndexedDB?

It lets you store just about anything in the user's browser. In addition to the usual search, get, and put actions, IndexedDB also supports transactions. Here is the definition of IndexedDB on MDN: IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.


2 Answers

I don't have a direct answer but your code as it is currently written seems strange to me.

One thing I would try first is properly binding onupgradeneeded, onerror, and not closing the database prematurely.

Do not do this:

var request = indexedDB.open();
request.onsuccess = function() {
  request.onupgradeneeded = function() {};
};

Do this instead:

var request = indexedDB.open();
request.onsuccess = function() {};
request.onupgradeneeded = function() {};

Similarly, bind onerror immediately, not only later in onupgradeneeded or onsuccess, like this:

var request = indexedDB.open();
request.onsuccess = function() {};
request.onupgradeneeded = function() {};
request.onerror = function() {};

When indexedDB.open detects a higher version or first version, it will dispatch an upgradeneeded event, wait for the implied 'versionchange' transaction to complete, and then dispatch a success event.

I can't quite make sense of your code. I am not sure why you are opening a connection, closing it, then opening a second connection, then late binding the upgradeneeded handler.

This is all you need to do to add an object store and then access it:

var request = indexedDB.open('name', aNumberGreaterThanTheCurrentVersion);
request.onupgradeneeded = function(event) {
  console.log('Performing upgrade');
  var db = event.target.result;
  console.log('Creating object store');
  db.createObjectStore('mystore');
};

request.onsuccess = function(event) {
  console.log('Connected to database');
  var db = event.target.result;
  var tx = db.transaction('mystore');
  var store = tx.objectStore('mystore');
  console.log('Doing something with store "mystore"');
  // ...
  console.log('Finished doing something, now closing');
  db.close();
};

request.onerror = console.error;

Another note. indexedDB produces different types of errors, and it is important to distinguish them. request.onerror doesn't "throw" an exception as your log message suggests. Instead, what happened is that indexedDB dispatched an error event. Dispatched error events are not thrown. Now, with that aside, several of indexedDB's functions can throw exceptions. These need to be caught by a try/catch block. You won't even see a dispatched error event. You will get an actual script error that halts execution and will automatically appear in the console with a slightly-informative message.

like image 179
Josh Avatar answered Oct 31 '22 22:10

Josh


let OpenReq = indexedDB.open( 'dbName', version );
OpenReq.onupgradeneeded = function( e )
{
    let db = e.target.result;
    db.objectStoreNames.contains( "myStore" ) || db.createObjectStore( "myStore" );
}

if you have an exist db with version 1, run above code with version = 2 will remain the exist stores.

like image 42
saintthor Avatar answered Oct 31 '22 22:10

saintthor