Fork me on GitHub
#datomic
<
2023-02-16
>
favila13:02:04

Is there any way within a query to propagate the current rule environment (`%`) into it? I am frequently in a situation where I need a subquery for an existence check (i.e. “any of these values may match”) but need a rule in the subquery. E.g. a rule like

[(rule-any-matches ?e ?test-values)
 [(q '[:find ?e
       :in $ [?test-value ...]
       :where (some-other-rule ?e ?test-value)] $ ?test-values) [[?_ignored]]]]

favila13:02:33

If I mention % I get an exception; if I don’t, the rule seems to silently just not work

Keith13:02:58

Yes, pass the rule to the outer query as something like rule instead of %, then pass that to the subquery as %

favila13:02:53

hm, it’s kinda annoying that datasources are ambient in rules and I can’t ever have more than one of them (and often would like it); yet rule sets are not, I would never want more than one, and yet I have to manage its propagation myself?

kenny20:02:20

My google-fu seems to be failing me. Does anyone happen to know where in the docs the behavior of a pull reverse lookup + isComponent=true + cardinality=one is specified as returning the result not in a vector? e.g.,

;; schema 
{:db/ident       :not-a-component
 :db/cardinality :db.cardinality/one
 :db/valueType   :db.type/ref}
{:db/ident       :a-component
 :db/cardinality :db.cardinality/one
 :db/valueType   :db.type/ref
 :db/isComponent true}

(d/pull db
        '[{:_not-a-component [:title]}]
        [:id "a"])
=> {:_not-a-component [{:title "foo"}]}

(d/pull db
        '[{:_a-component [:title]}]
        [:id "a"])
=> {:_a-component {:title "foo"}}

favila14:02:26

Maybe it was only in old docs describing the entity map api, before client+pull became the “preferred” apis

favila14:02:54

I’m also not sure I ever read it anywhere; it’s just one of those datomic trivia things at this point

kenny16:02:05

Haha. I vaguely remember a blog post discussing components that may have mentioned it. I recall the blog content would slide up from the bottom of the screen haha.

dvingo18:02:22

ah, but the first example contradicts this doesn't it..

dvingo18:02:11

oh nvm, it's there "Reverse references for non-component attributes."

1