Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the object store gracefully when it doesn’t exist #138

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

iamakulov
Copy link

Fixes #134.

This PR ensures idb-keyval creates the object store when the DB exists but the object store doesn’t.

I stumbled upon the same issue @getflourish experienced (#134), and also in Safari. This PR seems to fix the issue nicely.

Tested in Chrome, Safari, and Firefox.

@jakearchibald
Copy link
Owner

Last time I tried something like this, I ran into deadlock issues in IE11, so I'll need to do a lot of testing there.

@iamakulov
Copy link
Author

iamakulov commented Oct 31, 2021

FWIW here’s the code I used to reproduce the issue and test the fix:

<!-- dist/index.html -->
<!doctype html>
<script src="./umd.js"></script>
// In the console:
// ...Create the DB
await idbKeyval.set('a', 'b')
await idbKeyval.get('a') // → Must return b
window.location.reload() // Reload to close all connections to the DB

// ...Destroy the DB and create the DB without the store
indexedDB.deleteDatabase('keyval-store')
indexedDB.open('keyval-store')
window.location.reload() // Reload to close all connections to the DB

// ...Ensure the missing store is added
await idbKeyval.set('a', 'b')
await idbKeyval.get('a') // → Must return b
const dbOpenRequest = indexedDB.open('keyval-store')
dbOpenRequest.onsuccess = function(event) {
  console.log(db.version); // → Must return 2
};

@jakearchibald
Copy link
Owner

How does this happen in the real world? Is it when someone manually creates the database of that name but doesn't create the store, or are you seeing cases where the store disappears?

@iamakulov
Copy link
Author

or are you seeing cases where the store disappears

That was the case for me. I can’t really reproduce it; it’s just that I wrote the code that used idb-keyval; viewed the PR preview in Safari; closed Safari; opened Safari a few days later; got the error from #134.

@getflourish
Copy link

"opened Safari a few days later" sounds as if this is somewhat related to the "ephemeral" nature of IDB in Safari…

@jakearchibald
Copy link
Owner

Right, but it shouldn't be removing individual stores.

@SukkaW
Copy link

SukkaW commented Nov 7, 2021

Actually, the PR might accidentally introduces a new feature: create multiple key-value instance (in different object store) under one database.
Every time a new key-value instance is initialized, the database version will be bumped and new object store created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants