Fork me on GitHub
#datomic
<
2022-11-14
>
ivana14:11:41

Hello! This query works, but only if I have some status attached to the entity. Is there a way to get some defolt value for transaction and it's attribute if status wasn't set to an entity?

(d/q '[:find ?e ?status ?last-tx ?caused-by
       :in $ [?e ...]
       :where
       [?e :encounter-transmission/plan]
       [(get-else $ ?e :encounter-transmission/status false) ?status]
       [?e :encounter-transmission/status ?status ?last-tx true] ;; empty ?last-tx here if ?status = false by get-else
       [(get-else $ ?last-tx :transaction/caused-by false) ?caused-by]]
.....)
Unfortunatelly looks like I can't call get-else on pre-last where clause

favila14:11:13

Consider pulling from ?e instead of extracting fields using where clauses

ivana14:11:34

Actually I'm pulling ?e, just tried to show you simple example. But anyway, even if I pull ?e, how can I access last transaction which added status attribute (if it was added at all)?

favila14:11:50

Why can you not use get-else? Is it cardinality many?

ivana14:11:09

Cause I cant call get-else with 5 or 6 parameters May you show how I can use it for pre-last clause in my query?

favila14:11:11

Sorry my fault for not reading this carefully

favila14:11:38

so you want to capture last-tx, but default to something if there is none.

ivana14:11:57

Yep, exactly

favila14:11:55

you can either write your own get-else, or use a rule with two branches: one that matches if status is absent, and one if its present, and both bind to ?last-tx

ivana14:11:06

Sorry, I'm not an expert in Datalog - may you show me the clause code for it?

ivana14:11:55

2 branches rule I mean

favila14:11:44

'[:find ?e ?status ?last-tx
  :in $ [?e ...]
  :where
  [?e :encounter-transmission/plan]
  (or
   [?e :encounter-transmission/status ?status ?last-tx]
   (and
    (not [?e :encounter-transmission/status])
    [(ground false) ?status]
    [(ground -1) ?last-tx]))]

👍 1
1
ivana15:11:39

Thanks alot, I checked it, and looks like it works! Didn't check it's performance on real data, but on test one it returns all that needed!