Trying to see when database content has changed.

Hi, I;m new to observable and I’m trying to work out how I would make this Loki database observable to see when items are added/deleted/changed.

Not sure where the viewof goes or if I would be plopping in yeild or mutable to expose the data.

Any help welcome.

One of the ways you could approach this is to use Observable’s mutable operator, which lets you assign the value of a cell from another cell, and trigger any downstream cells with (non-mutable) references to that mutable cell to run again.

Here’s a suggestion that does that:

The basic steps are:

  1. Any cell that wants to mutate your database references mutable te_items exclusively, and never te_items (otherwise, you’d likely get an infinite loop).
  2. After the cell mutates the database, it reassigns mutable te_items to itself to trigger downstream cells to run. (It’s generally better to use a copy-on-write technique here similar to React state, but that’s not possible given that this database is inherently mutable.)
  3. Any cell that you want to run automatically whenever te_items changes just references te_items as normal.
  4. But, if a cell depends on a specific mutation (such as the find cell depending on the add_data cell that inserts the tyrfing record), that cell must include a reference to the cell that applies the mutation. That’s because Observable runs cells in topological order.

Hope this helps.

2 Likes

Thanks so much, that really helped a lot in my understanding of observable.

1 Like