datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
2020-10-12T02:10:31.030Z

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

2020-10-12T02:10:33.030200Z

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

2020-10-12T02:10:47.030500Z

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

2020-10-12T02:12:19.031900Z

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?

Oliver George 2020-10-12T02:12:28.032Z

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

Oliver George 2020-10-12T02:13:10.032300Z

That approach is: pass the boolean as a param

Oliver George 2020-10-12T02:13:59.032500Z

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

Oliver George 2020-10-12T02:14:36.032700Z

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

2020-10-12T02:16:59.032900Z

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

2020-10-12T02:17:04.033100Z

(i.e. display all tasks)

2020-10-12T02:17:52.033300Z

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

Oliver George 2020-10-12T02:18:27.033600Z

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

Oliver George 2020-10-12T02:18:43.033800Z

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

Oliver George 2020-10-12T02:18:56.034Z

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

Oliver George 2020-10-12T02:19:28.034200Z

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

2020-10-12T02:20:28.034400Z

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 George 2020-10-12T02:21:20.034600Z

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