'IndexedDB Database version change not triggering onupgradeneeded
I have a function that basically grabs a bunch of delivery jobs from the remote database and saves them to the indexeddb for offline backup. After each download of the jobs it calls addToDatabase() function.
If the jobs for that date are already in the database i want to overwrite the existing jobs with the latest in case there were any updates to the details between downloading the data and delivery of the job.
At the start of the function I have the following code:
let unixTimestamp = parseInt(Math.round(+new Date()/1000));
console.log('Database version '+unixTimestamp+' loading.');
// open our database with the unix timestamp as a version to force onupgradeneeded
let DBOpenRequest = window.indexedDB.open(storeName,unixTimestamp);
If I call this for a second time the database version remains the same.
The full function is below...
function addToDatabase(storeName,date,jobs)
{
let unixTimestamp = parseInt(Math.round(+new Date()/1000));
console.log('Database version '+unixTimestamp+' loading.');
// open our database with the unix timestamp as a version to force onupgradeneeded
let DBOpenRequest = window.indexedDB.open(storeName,unixTimestamp);
DBOpenRequest.onerror = function(event) {
console.log('Error loading database.');
};
DBOpenRequest.onupgradeneeded = function(event)
{
console.log('Database version '+unixTimestamp+' initialised.');
db = event.target.result;
let transaction = event.target.transaction;
if(!db.objectStoreNames.contains(date))
{
//objectStore does not exist so create it...
console.log('block 1');
let store = db.createObjectStore(date,{keyPath: 'id', autoIncrement:true});
store.transaction.oncomplete =
function(evt) {
// Store values in the newly created objectStore.
let jobObjectStore = db.transaction(date, "readwrite").objectStore(date);
Object.entries(jobs).forEach((entry) => {
const [key, value] = entry;
jobObjectStore.put({
id:key,
contract:value.contract,
runContract:value.runContract,
items:value.items,
});
});
}
}
else
{
console.log('block 2');
let jobObjectStore = db.transaction(date, "readwrite").objectStore(date);
// Store values in the existing objectStore.
Object.entries(jobs).forEach((entry) => {
const [key, value] = entry;
jobObjectStore.put({
id:key,
contract:value.contract,
runContract:value.runContract,
items:value.items,
});
});
}
}
}
So when i call this function the first time i get the following console output..
Database version 1650561313 loading.
Database version 1650561313 initialised.
block 1
This is as expected and the database version is 1650561313. However when I call the function a second time I get the following console output. The version I am opening the db with is higher number but the onupgrade needed is not being fired and the database remains on verion 1650561313.
Database version 1650561338 loading.
Any help as to why it isnt triggering the onupgradeneeded would be much appreciated!
Solution 1:[1]
It was indeed due to the previous connection not being closed on the first run of the function. I also realised It would be better to remove the date entry and redownload from the server. The working function is below in case anyone is trying something similar...
function addToDatabase(storeName,date,jobs)
{
DBVersionRequest = window.indexedDB.open(storeName);
DBVersionRequest.onsuccess = function(ev1)
{
var dbr = DBVersionRequest.result;
var dbv = ev1.target.result;
version=dbv.version;
newVersion = version+1;
console.log('newVersion:'+newVersion);
dbv.close();
dbr.close();
//open the database with the latest version to trigger the onupgradeneeded
let DBOpenRequest = window.indexedDB.open(storeName,newVersion);
DBOpenRequest.onerror = async function(event)
{
console.log('Error loading database.');
};
DBOpenRequest.onupgradeneeded = function(event)
{
db = event.target.result;
let transaction = event.target.transaction;
if(db.objectStoreNames.contains(date))
{
//delete the existing date and re-download...
db.deleteObjectStore(date);
}
//recreate the object store for todays jobs
let store = db.createObjectStore(date,{keyPath: 'id', autoIncrement:true});
store.transaction.oncomplete =
function(evt) {
// Store jobs in the newly created objectStore.
let jobObjectStore = db.transaction(date, "readwrite").objectStore(date);
Object.entries(jobs).forEach((entry) => {
const [key, value] = entry;
jobObjectStore.put({
id:key,
contract:value.contract,
runContract:value.runContract,
items:value.items,
});
});
jobObjectStore.transaction.oncomplete =
function(evt2)
{
//when all transactions complete then close the db so function can run again.
db.close();
}
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | AKAust |
