Fork me on GitHub
#datascript
<
2016-11-22
>
escherize13:11:36

Hi guys. I'm worried I'm reinventing the wheel over here...

escherize13:11:38

(def conn (d/create-conn {}))

(d/transact! conn
             [{:msg "hi", :done? false}
              {:msg "hi", :done? true}
              {:msg "low", :done? false}])

(defmacro select [conn m]
  `(->> (d/q (quote [:find ~'?e :where
                     ~@(mapv (fn [[k# v#]] ['?e k# v#]) m)])
             ~conn)
        (mapv first)
        (d/pull-many ~conn '[~'*])))

(select @conn {:done? false})
;; => [{:db/id 3, :done? false, :msg "low"} {:db/id 1, :done? false, :msg "hi"}]

(select @conn {:done? true})
;; => [{:db/id 2, :done? true, :msg "hi"}]

(select @conn {:done? true :msg "hi"})
;; => [{:db/id 2, :done? true, :msg "hi"}]

escherize13:11:24

I want a select function/macro that works like above... I'm a datascript noob and curious if something like that already exists.

rauh13:11:59

@escherize Tonsky and other use es-by-av or e-by-av which works for just one attr-value pair, by going straight into the index.

escherize14:11:12

@rauh Thanks for the tip! Are those functions? what namespaces are they in?

escherize14:11:33

Appreciate it!

escherize14:11:18

now, what's devprod? 🙂

escherize14:11:08

Oh I bet you it does one in dev and the other in prod.

rauh14:11:42

@escherize Oh darn, I thought I removed those macro calls. But yeah, that's it 🙂