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();
}
}