Fork me on GitHub
#datascript
<
2020-10-12
>
datran02:10:31

I'm playing around with a todo example app and I'm trying to add filtering. Here's my query: `

datran02:10:33

`[:find [?tid ...]
            :where
            [?tid :app/type :type/task]
            [?tid :task/done? ?status]
            [(= ?status (case filter
                            :filter/complete true
                            :filter/incomplete false
                            true))]]

Oliver George02:10:28

[:find [?tid ...]
 :in $ ?status
 :where
   [?tid :app/type :type/task]
   [?tid :task/done? ?status]]

Oliver George02:10:10

That approach is: pass the boolean as a param

Oliver George02:10:59

(let [status (case filter :filter/complete true :filter/incomplete false true))] (d/q query db status))

Oliver George02:10:36

As the the error... accessing cljs.core fns from queries in datomic requires some fiddling.

datran02:10:59

thanks, that's helpful and it works. If I want to take it further, and apply an optional filter, how would I go about that - where I want ?status to match true or false

datran02:10:04

(i.e. display all tasks)

datran02:10:52

Or should I just apply the filter to the results after fetching all tasks?

Oliver George02:10:27

I know there's some tips documented but can't find them right now

Oliver George02:10:43

I saw someone say they just pass the predicate in as a param

Oliver George02:10:56

Not sure that's the best way - others might comment

Oliver George02:10:28

It's all "in memory" so no shame in filtering the results except that you might find it less expressive since you need to specify what data is returned to work with

datran02:10:28

ah, in this case I wanted to do the filtering in the query so I could just return the ids, so I take your point

Oliver George02:10:20

It's been a little while for me so I'm not the best to give advice

datran02:10:47

This blows up with Error: Unknown predicate 'cljs.core/=

datran02:10:19

The datascript readme says that it supports "Predicates and user functions in query", so I'm surprised to see this blow up. Am I getting the syntax wrong?