Last active
December 13, 2020 07:05
-
-
Save GautamPanickar/5d4165cd66e608509c6c1c44601e73f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Here all objects are added into store in a single transaction. | |
*/ | |
connection.onsuccess = (e) => { | |
var db = e.target.result; | |
let startedTime = Date.now(); | |
console.log(`Started on - ${startedTime}`); | |
var transaction = db.transaction('contacts', 'readwrite'); | |
var store = transaction.objectStore('contacts'); | |
store.clear(); | |
let addedContacts = 0; | |
for (let i = 0; i < totalLength; i++ ) { | |
var req = store.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