Fork me on GitHub
#datascript
<
2023-10-02
>
RJ Sheperd17:10:25

Can someone share some examples of using get-else and or-join to perform some logic in a DataScript/Datomic query? Trying to do something like:

(def db (atom (d/empty-db {:worksheet/inputs {:db/valueType   :db.type/ref
                                                :db/cardinality :db.cardinality/many}})))

  (d/transact db [{:worksheet/name   "Worksheet"
                   :worksheet/inputs [{:input/kind  :discrete
                                       :input/value 0}
                                      {:input/kind  :continuous
                                       :input/value 10
                                       :input/units "feet"}]}])

  ;; Only get the units when the input is :continuous
  (d/q '[:find ?w-name ?i ?value ?units
         :where
         [?w :worksheet/name ?w-name]
         [?w :worksheet/inputs ?i]
         [?i :input/value ?value]
         (or-join [?units]
                  (and
                   [?i :input/kind :continuous]
                   [?i :input/units ?units])
                  [(ground :none) ?units])]
       @db)
And instead of getting:
#{["Worksheet" 3 10 "feet"] ["Worksheet" 2 0 :none]}
I’m getting:
#{["Worksheet" 3 10 "feet"] ["Worksheet" 3 10 :none] ["Worksheet" 2 0 :none] ["Worksheet" 2 0 "feet"]}

dvingo21:10:02

i think you need ?value in the or-join vector [?value ?units]

dvingo21:10:32

can also try plain or

dvingo21:10:43

because you want to unify with all vars