Open Time Store™
Copyright © JUXT LTD 2018-2019

Introduction

Decorators are features that can be built on top of Crux that can be used optionally and can be created easily without needing understand how the core of Crux works. Use of decorators is currently an advanced topic and requires a basic understanding of Clojure to configure.

Aggregation

The aggregation.alpha namespace demonstrates a good example of building a highly general aggregation extension to the basic Crux query. The standard api/q function can now be ignored as the aggr/q function is used instead (it wraps api/q with transducer magic).

  (t/deftest test-count-aggregation
    (f/transact-entity-maps!
     *kv*
     [{:crux.db/id :a1 :user/name "patrik" :user/post 1 :post/cost 30}
      {:crux.db/id :a2 :user/name "patrik" :user/post 2 :post/cost 35}
      {:crux.db/id :a3 :user/name "patrik" :user/post 3 :post/cost 5}
      {:crux.db/id :a4 :user/name "niclas" :user/post 1 :post/cost 8}])

    (t/testing "with vector syntax"
      (t/is (= [{:user-name "niclas" :post-count 1 :cost-sum 8}
                {:user-name "patrik" :post-count 3 :cost-sum 70}]
               (aggr/q
                (api/db *api*)
                '{:aggr {:partition-by [?user-name]
                         :select
                         {?cost-sum [0 (+ acc ?post-cost)]
                          ?post-count [0 (inc acc) ?e]}}
                  :where [[?e :user/name ?user-name]
                          [?e :post/cost ?post-cost]]})))))

See the aggregation_test.clj for more examples of usage.