jakearchibald / idb

IndexedDB, but with promises
https://www.npmjs.com/package/idb
ISC License
6.22k stars 348 forks source link

Access event.currentTarget in upgrade function #231

Closed ChocolateLoverRaj closed 2 years ago

ChocolateLoverRaj commented 2 years ago

Is your feature request related to a problem? Please describe. I'm trying to modify an existing store in the upgrade function, but if I do db.transaction() I get an error saying that I can't do a transaction while the db is being upgraded. I saw this article which used evt.currentTarget.transaction, which seems to be an existing transaction for upgrading the db. However, the upgrade function doesn't get the event or the current target in its parameters, so there's currently no way to get data from the db during an upgrade.

Describe the solution you'd like

Preferred solution (has breaking change)

// Upgrade function
async (db, upgradeNeededEvent) {

}

Another solution (no breaking changes)

// Upgrade function
async (db, _oldVersion, _newVersion, upgradeNeededEvent) {

}
jakearchibald commented 2 years ago

The 4th argument of the upgrade callback is the transaction you're looking for - https://github.com/jakearchibald/idb#opendb

const db = await openDB(name, version, {
  upgrade(db, oldVersion, newVersion, transaction) {
    // transaction is what you want
  },
});
ChocolateLoverRaj commented 2 years ago

@jakearchibald sorry didn't realize there was a parameter for it