Skip to content

Instantly share code, notes, and snippets.

@GautamPanickar
Last active December 13, 2020 07:05
Show Gist options
  • Save GautamPanickar/435fdfc8e0449bfb3c9d8a35415cf9f9 to your computer and use it in GitHub Desktop.
Save GautamPanickar/435fdfc8e0449bfb3c9d8a35415cf9f9 to your computer and use it in GitHub Desktop.
connection.onsuccess = (e) => {
var db = e.target.result;
let startedTime = Date.now();
console.log(`Started on - ${startedTime}`);
const trans = db.transaction('contacts', 'readwrite');
const store = trans.objectStore('contacts');
store.clear();
let addedContacts = 0;
for (let i = 0; i < totalLength; i++ ) {
const transaction = db.transaction('contacts', 'readwrite');
const storeInside = transaction.objectStore('contacts');
var req = storeInside.add(contacts[i], contact.id);
req.onsuccess = (evt) => {
addedContacts++;
console.log(`Contact - ${i + 1} added.`);
if (i+1 === totalLength) {
if (addedContacts === totalLength) {
console.log(`--------------------------------------------------------------------`);
console.log(`All contacts successfully added in DB.`);
const endedTime = Date.now();
console.log(`Ended on - ${endedTime}`);
console.log(`Total time taken - ${endedTime - startedTime} ms`);
} else {
console.log(`--------------------------------------------------------------------`);
console.log(`Could not add every conatcts to the db. Some may be missing.`)
}
}
};
req.onerror = (evt) => {
console.log(`XXXX Something went wrong while adding contact - ${i + 1}.`);
console.log(evt);
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment