Derived Attributes For DataScript

I’ve been exploring the idea of derived attributes in DataScript, i.e. attributes which are computed from a database value. By integrating these attributes with DataScript’s Pull API, this opens up a nice way to keep business logic out of components, while maintaining the benefits of declarative component queries à la om.next. The results look something like the following:

(component favourites-view
  (query [customer-id] 
    '[{(read :customer/favourite) [:product/name 
                                   (read :product/rating)]}])
  (render [props] ...))

In particular, exposing derived attributes alongside basic, reified ones, ensures that all of a component’s data dependencies are declared through the query, are read consistently, and only computed once during a single render-cycle.

A more detailed introduction can be found here: nikolasgoebel.com/2018/03/25/derived-attributes-datascript.html

So far I’ve been enjoying this approach quite a bit and have been using it in practice and would love to hear your feedback and questions.

1 Like