hypercore-protocol / hypercore-next

Hypercore 10 is an append only log with multi-writer primitives built in.
MIT License
150 stars 15 forks source link

preupgrade hook #128

Open mafintosh opened 1 year ago

mafintosh commented 1 year ago

I think we need a "preupgrade" hook that, if set, runs when the user calls update, allowing them to ensure certain conditions around the upgrade. this would just be a view, like non-sparse mode (infact non-sparse could be implemented as a hook).

For example:

const s = core.session({
  preupgrade (latest) { // where latest is a session that is on the latest length
    await latest.get(latest.length - 1) // always ensure the latest block is there
    return latest.length // can optionally return the length we're upgraded to                                     
  }
})

// calls above
await s.update()

We might wanna allow simple naming of the hooks also so users can define a couple, but let's defer that to after we have one working. The pointer a preupgrade session is on should also be stored in the oplog, so we can checkout the current existing one on restart