Skip to content

Instantly share code, notes, and snippets.

@bwindels
Last active October 4, 2018 14:59
Show Gist options
  • Save bwindels/89e87d0a8e6b9b8141c6273cbd570511 to your computer and use it in GitHub Desktop.
Save bwindels/89e87d0a8e6b9b8141c6273cbd570511 to your computer and use it in GitHub Desktop.
<html>
<body>
<script>
function asPromise(req) {
return new Promise((resolve, reject) => {
req.onerror = (e) => reject(e.target.error);
req.onsuccess = (e) => resolve(e.target.result);
});
}
function openDBWithVersion(version) {
return asPromise(indexedDB.open("foo", version, (db) => {
console.log("running onversionupdate", db.oldVersion);
switch (db.oldVersion) {
case 0: // 0 -> 1
db.createObjectStore("person", {keyPath: "id"});
case 1: // 1 -> 2
var personStore = db.transaction.objectStore("person");
personStore.createIndex("name", "name");
}
}));
};
async function main() {
try {
await asPromise(indexedDB.deleteDatabase("foo"));
} catch(err) {}
const db = await openDBWithVersion(2);
db.close();
try {
await openDBWithVersion(1);
} catch(e) {
window.idberror = e;
alert(JSON.stringify({message: e.message, name: e.name}));
}
}
main().catch(e => {
console.error(e);
});
</script>
</body>
</html>
@bwindels
Copy link
Author

bwindels commented Oct 4, 2018

Fails with The operation failed because the stored database is a higher version than the version requested. on FF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment