Fork me on GitHub
#datomic
<
2015-12-24
>
currentoor00:12:23

If I want all entities of a certain type, what's the correct way to do this? I'm doing the following but it feels wrong.

(d/q '[:find [(pull ?e [*]) ...]
       :in $ ?id-attr
       :where
       [?e ?id-attr _]]
     (d/db conn)
     id-attr)

zentrope19:12:26

I see a doc about rules, but not how to get them into a query.

zentrope19:12:11

Ah, a tiny bit buried: First, you have to pass the rule set as an input source and reference it in the :in section of your query using the '%' symbol.

zentrope19:12:35

Suggestion: An actual example of a query with rule right there in the "rules" section?

zentrope20:12:35

Super cool. Rules to do a "do some of these not-always present attributes contain this string" works really well.

zentrope20:12:04

By "works well" I mean it's a tiny amount of code.

currentoor22:12:26

Is there a way to parameterize a pull expression inside a query? I want to do something like:

(d/q '[:find [(pull ?e
                    ?pull-exp) ...]
       :in $ ?id-attr ?pull-exp
       :where
       [?e ?id-attr]]
     (d/db conn)
     :user/id
     [:user/first-name {:user/task [:task/id]}])

currentoor22:12:58

But I get the following error:

:db.error/invalid-pull Invalid pull expression (pull ?e ?pull-exp)

currentoor22:12:22

I have to write several queries like and it would be nice to be DRY and avoid writing macros...

currentoor22:12:20

So I figured out I can work around with the back quote.

(let [pull-exp [:user/first-name :user/last-name :user/id
                  :user/email {:user/task [:task/id]}]]
    (->> (d/q `[:find [(~'pull ~'?e
                       ~pull-exp) ...]
               :in ~'$
               :where
               [~'?e :user/id]]
             (d/db conn))
        (map normalize)))
Not sure if this is an anti-pattern though.

zentrope23:12:05

You can pass in the pull-spec as a parameter. Is that what you're talking about?

zentrope23:12:42

(d/q '{:find [[(pull ?e pat) ...]] :in [$ pat] ....} (d/db conn) the-pat)

zentrope23:12:50

Something like that.

zentrope23:12:23

currentoor ^^

zentrope23:12:15

currentoor: In your first example, remove the ? prefix from the pattern reference?

zentrope23:12:35

Oh, hm. Maybe @currentoor is the alert? Oy. Slack.

currentoor23:12:08

Oh does ? have special semantic meaning?

zentrope23:12:28

Not sure. But I don't use it for my patterns and they work.

currentoor23:12:53

Yup that worked. Thanks!