So here's my first method. I created one store and I created an index "groupname" the value is the whole object which has multiple keys and values. This works but when I try to add another store, the store shows up and the index shows up but the values are empty...
Anyone have any tips for me on how to create a second storeobject?
var dbName = "DSD DB";
var request = window.indexedDB.open(dbName, 2);
var db;
request.onerror = function(event) {
console.log(event.target.errorCode);
};
request.onsuccess = function(event) {
db = event.target.result;
console.log(db);
};
// This event is only implemented in recent browsers
request.onupgradeneeded = function(event) {
// Save the IDBDatabase interface
var db = event.target.result;
// Create an objectStore for this database
var objectStore = db.createObjectStore("groups", { keyPath: "groupname" });
objectStore.transaction.oncomplete = function(event) {
// Store values in the newly created objectStore.
var groupObjectStore = db.transaction("groups", "readwrite").objectStore("groups");
jsonReceivingOneGroup.groups.forEach(function(group) {
groupObjectStore.add(group);
});
console.log(groupObjectStore);
};
};
Thanks in advance!
If you want to modify an existing database and add a store to it, then you need to open a connection to the database with a higher version number than before.
onupgradeneededonly gets called when either (a) the database is created for the first time or (b) the database is opened with a higher version number.First, modify your
onupgradeneededfunction to also create the additional object stores. Next, open the database with a higher version number. To open the database with a higher version number:I am using 3 here simply because your example shows 2, and 3 is one higher than 2.
Then, when you get this far, you will get an error. When you open with version 3, and the
onupgradeneededfunction runs, it will try to create thegroupsobject store again. But that store already exists. So now you need to introduce some logic to check for that. There are a couple ways to do it. One simple way is to check if the store already exists, and only create the store if it does not already exist. To check if an object store already exists in the database, you can usedb.objectStoreNames.contains