3

I was reading about the Datomic pull syntax here:

http://docs.datomic.com/pull.html

And it explains under wildcard that a pattern like [*] will pull an entity and recursively pulls its entity attributes. Direct quote:

The wildcard specification * pulls all attributes of an entity, and recursively pulls any component attributes:

;; pattern
[*]

;; result
{:release/name "The Concert for Bangla Desh",
 :release/artists [{:db/id 17592186049854}],
 :release/country {:db/id 17592186045504},
 :release/gid #uuid "f3bdff34-9a85-4adc-a014-922eef9cdaa5",
 :release/day 20,
 :release/status "Official",
 :release/month 12,
 :release/artistCredit "George Harrison",
 :db/id 17592186072003,
 :release/year 1971,
 :release/media
 [{:db/id 17592186072004,
   :medium/format {:db/id 17592186045741},
   :medium/position 1,
   :medium/trackCount 2,
   :medium/tracks
   [{:db/id 17592186072005,
     :track/duration 376000,
     :track/name "George Harrison / Ravi Shankar Introduction",
     :track/position 1,
     :track/artists [{:db/id 17592186048829} {:db/id 17592186049854}]}
    {:db/id 17592186072006,
     :track/duration 979000,
     :track/name "Bangla Dhun",
     :track/position 2,
     :track/artists [{:db/id 17592186048829}]}]}
  ...
  ]}

However, when I try this myself I only get db/id values for ref types. I have to basically call out any ref type in a more verbose way in the pull expression, with something like this:

[* {:content/type [:db/ident] :content/locales [:db/ident] :content/groups [*]}]

And that's not even actually the end of it, because :content/groups itself has ref types inside of it. Did the behavior of wildcarding change at some point and the docs are not up to date? Or am I doing something wrong? Is there a better (more concise) way to go about saying, "just recursively pull it all in"?

1 Answer 1

3

The wildcard in a pull expression only populates component attributes. That is, attributes with :db/isComponent true in the schema.

Looking at the mbrainz schema, only :release/media and :medium/tracks are component attributes, so they would be the only ones populated with a [*] pull pattern.

If your data model includes hierarchal reference chains (i.e. :person/friend as a reference to any person, who may also have friends referenced), you can define recursive pull patterns and specify their recursive depth (or use ... for unbounded).

This topic is also discussed on the Datomic Google Group, and includes a link to an example recursive pull pattern.

6
  • ok, is there any reason why I might choose to not use :db/isComponent true, if my intention is to get the whole thing pulled in each time?
    – Kevin
    Jun 8, 2016 at 19:17
  • So reading from here: blog.datomic.com/2013/06/component-entities.html it seems like components are to take care of sub-objects that don't exist on their own without the parent. Does it make sense to make an enum value have :isComponent true?
    – Kevin
    Jun 8, 2016 at 19:27
  • 1
    from what I can tell, all enums have the same :db/id, so if a record got retracted that :db/id would also get retracted. So it seems like its desirable to have enum values be :db/isComponent true because I don't want to manually specify every single enum on a pull expression, but on the other hand they're by definition shared across entities, and so not suitable for :db/isComponent. Is that correct?
    – Kevin
    Jun 8, 2016 at 19:46
  • Correct - component entities should be things that only exist as part of their parent.
    – Marshall
    Jun 8, 2016 at 22:08
  • 2
    so is there no easy way to "automatically" pull in refs for enum values? As an example, in a given component, I may have 4-5 enums in various places, and generally a (pull ?c [*]) suffices, except for the enums which then create this really annoying and long pull expression. It almost (almost) makes me want to just stick strings in there and to heck with the enums.
    – Kevin
    Jun 8, 2016 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.