orbit-db-docstore icon indicating copy to clipboard operation
orbit-db-docstore copied to clipboard

'data' event (from readme) does not exist

Open deanpress opened this issue 3 years ago • 0 comments

The readme describes db.events.on('data', (dbname, event) => ... ) as a way to listen for new data entries/updates, but this event does not exist.

Currently, it looks like you'd have to do this to get the latest entry:

    db.events.on('replicated', (address) => {
      const entry = db.get('')[0];
      console.log(entry);
    })

The problem with this is, if you restart the process, it'll retrieve the latest stored entry for each (existing) entry. Also you can't get the most recently updated entry this way when an existing document is updated. You'd have to keep an updated_at entry so you can query the highest value.

Alternatively you can use the replicate.progress event and read entry.payload.value, but when the process restarts, the data is fired newest to oldest, while oldest to newest would also be a common use case, e.g. for building a separate state or database.

      db.events.on('replicate.progress', (address, hash, entry, progress, total) => {
        console.log(entry.payload.value);
      })

Having the on('data', dbname, event) event as described in the readme would be much more efficient.

deanpress avatar Dec 17 '20 13:12 deanpress