byteally/reflex-indexed-db

Running IDB actions on upgradeNeeded

Opened this issue · 1 comments

Is it possible to run IDB actions (specifically OpNewIndex) within the upgrade needed handler?

Naively when I try this, the types don't line up:

  db <- ReflexIDB.indexedDb myOpenRequest $ do
    objStore <- ReflexIDB.createObjectStore storeName Nothing
    ReflexIDB.createIndex objStore ("oid", "oid", Nothing)

createIndex (a function I added to my local clone of reflex-indexed-db) wraps the already available OpNewIndex StoreOp. But createObjectStore wraps a DatabaseOp, so they aren't operations from the same monad.

What would you suggest for a path forward, if my goal is just to create these indices?

  • Add a OpRunStore to DatabaseOp* for lifting store ops into Database
  • Do my index creation inside runTransaction (I hoped to avoid this because I'd rather create the indices imperatively rather than finding a suitable event to trigger their creation)
  • Something else?

Thanks for any guidance!

I started to make progress with the first option, here imalsogreg@c795cfd#diff-bb5d30c9ddaa520ab4113d220ed895f8R159 (adding a variant to DatabaseOp for running IDB actions).

It kind of works, in my test application I can say:

  maybeDB <- Reflex.IDB.indexedDB (Reflex.IDB.IndexedDBOpen dbName 1 never) $ do
    objectStore <- Reflex.IDB.createObjectStore storeName Nothing
    Reflex.IDB.runIDBAction $ do
      Reflex.IDB.createIndex objectStore ("name", "name", Nothing)
      Reflex.IDB.createIndex objectStore ("oid", "oid", Nothing)

and the indexed-db database shows me two indices name and oid. But

  • If I use two calls to runIDBAction instead of nesting both indices under a single runIDBAction in a do block, only the first index gets created (dunno why)
  • The code on my branch is awkward due to assumption in StoreOp interpreted. That interpreter assumes a valid input for ReaderT and assumes the existence of a transaction - neither of these are available in my usecase of doing index creation within the upgradeNeeded callback

Also I realized that this whole issue may be more of a nice-to-have than something critical, because there's an easy workaround. Inside my upgradeNeeded callback, I could get the ghcjs-dom ObjectStore out of the reflex-indexed-db ObjectStore t`, and run the native index creation commands imperatively.