pratiqdev / nestore-beta

Nested-wildcard event-based data store and state management API
0 stars 0 forks source link

Add support for db middleware #2

Open pratiqdev opened 1 year ago

pratiqdev commented 1 year ago

Feature

Nestore should support database syncing via middleware. Each storage provider can/will have their own middleware for interacting with the db.

Implementation

Nestore provides an event emitter interface for listening to events. The nst.on('*') global listener returns: key - the bottom level key that was changed Path - the full path to the value that was changed Prev - the previous value Value - the new value

Middleware will be invoked with these values when any value changes. The middleware can handle batching and diffing.

Syncing

The middleware will request all data from the db on the initial load and update the store with nst.set({...db.result...})

Batching

Middleware should store a unique Set of changes until a cool down period expires, to prevent excessive db reads / writes. When an update event occurs, the event is added to the set and a countdown starts, while the countdown runs, the set accumulates events. When the countdown expires the set is passed to a diffing function, the set is emptied and accumulates other events while the db is updated.

Diffing

The differencing function will convert the set to an array and sort by timestamp, then step thru each value, creating a local copy of the store, overriding the last event with the new event, then update the store a single time

const events = [
 {
    key: 'num',
    path: 'stats.num',
    prev: 'zero',
    new: 'one',
    timestamp: 12345678
  }
]
pratiqdev commented 1 year ago

The db middleware could also provide save and load functions for manually syncing data