Fork me on GitHub
#datascript
<
2017-03-27
>
bbss14:03:32

how can I query for one or the other:

(d/q '[:find ?thing
            :where
            (or [?thing :attribute]
                [?thing :maybe-existing-attribute])
            ]
          (-> (d/create-conn {})
              (d/transact! [{:db/id -1 :attribute true}])
              :db-after
              ))

bbss14:03:59

the error here is cannot compare :attribute to [?thing :attribute] but looking at this example I'd kind of expect it to work: https://github.com/tonsky/datascript/blob/3fbe1d842f14e176a0fb26a28a41a721eda6a417/test/datascript/test/query_or.cljc#L28

misha14:03:23

@bbss

(let [conn (ds/create-conn)
      rules '[[(has-foo-or-bar ?e)
               [?e :foo]]
              [(has-foo-or-bar ?e)
               [?e :bar]]]]
  (ds/transact! conn [{:foo 1} {:bar 2} {:baz 3} {:foo 4 :bar 4}])
  (ds/q '[:find [?e ...]
          :in $ %              ;<--- '%' is for passing rules into query
          :where
          (has-foo-or-bar ?e)]
    @conn rules))
=> [4 2 1]
(->> ids  (ds/pull-many @conn '[*])))
=> [{:db/id 4, :bar 4, :foo 4} {:db/id 2, :bar 2} {:db/id 1, :foo 1}]

misha14:03:44

basically, to have an or, you write a bunch of rules (one per or branch) with identical signature (name, args), and pass them into query together

bbss14:03:40

still a little confused by the %, I know you can pass function names and vars that way

misha14:03:25

it is either "by convention" or "by implementation", I don't actually know which one is it (in datascript)

bbss14:03:07

Okay. Thanks for the insight 🙂 will be playing around with it a bit.

misha14:03:21

docs are pretty much datomic ones: http://docs.datomic.com/query.html

misha14:03:21

with a bit of ugliness you can build rules automatically to avoid boilerplate code

bbss14:03:35

sounds lispy 😄

bbss14:03:16

seriously loving datascript + rum. It's so much simpler than using "vanilla" es6 + jsx.

bbss14:03:57

I guess it's using sablono? That stuff is so much more intuitive than jsx.

misha14:03:18

tests are passing, I wonder why I or does not work, or what I don't see/understand

bbss14:03:12

I didn't try the test I assumed they work, but my example was so similar I couldn't see how they're different.

misha14:03:55

tonsky 10:17:58 <@U051HUZLD> it's also the only way

bbss15:03:04

cheers for digging back for me 🙂

misha15:03:50

not sure what those or are about, might be just boolean or, however it looks like query or to me too

bbss15:03:04

could just be work in progress, the tests also have some stuff with not which is mentioned not to be supported

misha15:03:16

looks like it. it tests query_v3 ns, core uses query ns.

misha15:03:58

tests are, however, 1.5 years old opieop