If you’re using a frontend framework like React or Vue, it’s a bad idea to query the database everytime you want to render its items in your components. Your component might rerender multiple times a second, and while BlinkDB makes queries very fast, there is a better alternative.
watch()
will observe a table and call the provided callback every time an item is inserted, updated, or deleted
(The callback will also be called once with all items in the table right after you register your watcher).
const dispose = await watch(userTable, users => {
console.log('All users', users);
});
Only retrieving certain items from the table is also possible - just provide a filter to watch()
,
and your callback will only be called if items matching the filter are inserted/updated/deleted.
const dispose = await watch(userTable, { where: { age: { lt: 3 } } }, babies => {
console.log('All babies: ', babies);
});
If you want to stop receiving updates, call the function returned from watch()
.
dispose();