Fork me on GitHub
#xtdb
<
2021-11-22
>
richiardiandrea19:11:59

Hi team XTDB, is there an established pattern for conditional queries? Say I have a function that finds all the sets given some key filter

(defn- find-all-sets
  [repository {:keys [modality state id]}]
What would be the datalog that queries if the keys are non-nil? We have an implementation that conditionally builds the :where clause but it is very verbose

richiardiandrea20:11:13

Specifically that is:

(crux/q {:find '[(pull ?e [*])]
                              :in '[[?set-id ?modality ?state]]
                              :where (cond-> '[[?e :cohesic/type :reference-range-set]]
                                       id (conj '[?e :crux.db/id ?set-id])
                                       state (conj '[?e :reference-range-set/state ?state])
                                       modality (conj '[?e :reference-range-set/modality ?modality]))}
                             [id modality state])
It would be good not to depend on the specific keys but I am passing a map in and can't depend on the order

refset12:11:15

Hi 🙂 unfortunately, I can't think of a better way to express this without sacrificing some efficiency...although if you are repeating this pattern in other places then it feels quite ripe for turning into some sort of generic filter-map->conditional-pull-query function

❤️ 1