-
Notifications
You must be signed in to change notification settings - Fork 95
Ensure OnyxDB store is created & recover from corrupted database #684
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import {promisifyRequest} from 'idb-keyval'; | ||
| import type {UseStore} from 'idb-keyval'; | ||
| import {logInfo} from '../../../Logger'; | ||
|
|
||
| // This is a copy of the createStore function from idb-keyval, we need a custom implementation | ||
| // because we need to create the database manually in order to ensure that the store exists before we use it. | ||
| // If the store does not exist, idb-keyval will throw an error | ||
| // source: https://github.com/jakearchibald/idb-keyval/blob/9d19315b4a83897df1e0193dccdc29f78466a0f3/src/index.ts#L12 | ||
| function createStore(dbName: string, storeName: string): UseStore { | ||
| let dbp: Promise<IDBDatabase> | undefined; | ||
| const getDB = () => { | ||
| if (dbp) return dbp; | ||
| const request = indexedDB.open(dbName); | ||
| request.onupgradeneeded = () => request.result.createObjectStore(storeName); | ||
| dbp = promisifyRequest(request); | ||
|
|
||
| dbp.then( | ||
| (db) => { | ||
| // It seems like Safari sometimes likes to just close the connection. | ||
| // It's supposed to fire this event when that happens. Let's hope it does! | ||
| // eslint-disable-next-line no-param-reassign | ||
| db.onclose = () => (dbp = undefined); | ||
| }, | ||
| // eslint-disable-next-line @typescript-eslint/no-empty-function | ||
| () => {}, | ||
| ); | ||
| return dbp; | ||
| }; | ||
|
|
||
| // Ensures the store exists in the DB. If missing, bumps the version to trigger | ||
| // onupgradeneeded, recreates the store, and returns a promise to the new DB. | ||
| const verifyStoreExists = (db: IDBDatabase) => { | ||
| if (db.objectStoreNames.contains(storeName)) { | ||
| return db; | ||
| } | ||
|
|
||
| logInfo(`Store ${storeName} does not exist in database ${dbName}.`); | ||
| const nextVersion = db.version + 1; | ||
| db.close(); | ||
|
|
||
| const request = indexedDB.open(dbName, nextVersion); | ||
| request.onupgradeneeded = () => { | ||
| const updatedDatabase = request.result; | ||
| if (updatedDatabase.objectStoreNames.contains(storeName)) { | ||
| return; | ||
| } | ||
|
|
||
| logInfo(`Creating store ${storeName} in database ${dbName}.`); | ||
| updatedDatabase.createObjectStore(storeName); | ||
| }; | ||
|
|
||
| dbp = promisifyRequest(request); | ||
| return dbp; | ||
| }; | ||
|
|
||
| return (txMode, callback) => | ||
| getDB() | ||
| .then(verifyStoreExists) | ||
| .then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName))); | ||
| } | ||
|
|
||
| export default createStore; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to do this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IndexedDB only allows creating object stores during a version upgrade. If the store is missing (e.g., first run, cleared storage, or a previous version didn’t create it)