«

Datomic Pull

Pull is a declarative way to make hierarchical (and possibly nested) selections of information about entities. Pull applies a pattern to a collection of entities, building a map for each entity. Pull is available

Patterns support forward and reverse attribute navigation, wildcarding, nesting, recursion, naming control, defaults, and limits on the results returned. Entities can be passed to pull by any kind of entity identifier: entity ids, idents, or lookup refs.

Pull patterns are written in the Extensible Data Notation (edn), which is programming language neutral. In programs, you can create patterns programmatically out of your basic language data types, e.g. Java Strings, Lists, and Maps. Alternatively, you can pass the pattern argument as a serialized edn string.

The results below are also written with edn, and they use an ellipsis ... where large results have been elided for brevity.

Example Data

The examples in this document use the mbrainz 1968-1973 sample set. Download and untar this file:

wget https://s3.amazonaws.com/mbrainz/datomic-mbrainz-1968-1973-backup-2014-10-15.tar -O mbrainz.tar
tar -xvf mbrainz.tar
bin/datomic restore-db file:///path/to/mbrainz-backup datomic:dev://localhost:4334/mbrainz-1968-1973

You can follow the examples in Java or Clojure code.

API

The pull API takes three arguments

and returns a map of data about an entity as specified by the pattern. In object-oriented languages, the database argument will be the method target. In Java:

db.pull("[*]", ledZeppelin);

In Clojure:

(pull db '[*] led-zeppelin)

The pullMany API is similar, except that it takes a collection of entity identifiers, and returns a collection of maps. For example, in Clojure:

(pull-many db '[*] [led-zeppelin jimi-hendrix janis-joplin])

Pull Grammar

Grammar Syntax

'' literal
"" string
[] = list or vector
{} = map {k1 v1 ...}
() grouping
| choice
+ one or more

Pull Pattern Grammar

pattern             = [attr-spec+]
attr-spec           = attr-name | wildcard | map-spec | attr-expr
attr-name           = an edn keyword that names an attr
map-spec            = { ((attr-name | attr-expr) (pattern | recursion-limit))+ }
attr-spec           = attr-name | wildcard | map-spec | attr-expr
attr-expr           = [attr-name attr-option+] | legacy-attr-expr
attr-option         = as-expr | limit-expr | default-expr
as-expr             = [attr-name :as any-value]
limit-expr          = [attr-name :limit (positive-number | nil)] 
default-expr        = [attr-name :default any-value]
wildcard            = "*" or '*'
recursion-limit     = positive-number | '...'
legacy-attr-expr    = legacy-limit-expr | legacy-default-expr
legacy-limit-expr   = [("limit" | 'limit') attr-name (positive-number | nil)]
legacy-default-expr = [("default" | 'default') attr-name any-value]

Terminals such as "limit" can be strings, but where languages have a symbol type you should prefer the idiomatic symbolic type, e.g. (limit :friends 100) in Clojure instead of ("limit" "friends" 100).

Patterns

A pattern is a list of Attribute Specifications.

pattern            = [attr-spec+]

Attribute Specifications

attr-spec           = attr-name | wildcard | map-spec | attr-expr
attr-expr           = [attr-name attr-option+] | legacy-attr-expr
attr-option         = as-expr | limit-expr | default-expr
wildcard            = "*" or '*'
recursion-limit     = positive-number | '...'
legacy-attr-expr    = legacy-limit-expr | legacy-default-expr
legacy-limit-expr   = [("limit" | 'limit') attr-name (positive-number | nil)]
legacy-default-expr = [("default" | 'default') attr-name any-value]

An attribute spec specifies an attribute to be returned, and (optionally) how that attribute should be returned. Attribute specs can be attribute names, wildcards, map specs, or attribute expressions.

Attribute Names

attr-name          = an edn keyword that names an attr

An attribute spec names an attribute, with an optional leading underscore on the local name to reverse the direction of navigation.

Attribute Name Example

The following pattern uses two attribute names to return an :artist/name and :artist/gid, pulling on ledZeppelin:

[:artist/name :artist/gid]
=> {:artist/gid #uuid "678d88b2-87b0-403b-b63d-5da7465aecc3", :artist/name "Led Zeppelin"}

Reverse Lookup

An underscore prefix (\_) on the local name component of an attribute name causes the attribute to be navigated in reverse.

Attribute names must not have a leading underscore. Attributes with a leading underscore can not be used with reverse lookup.

Attribute Name Reverse Lookup Example

You navigate backwards from :country/GB to British artists by pulling :artist/country:

[:artist/_country]
=> {:artist/_country [{:db/id 17592186045751} {:db/id 17592186045755} ...]}

Map Specifications

map-spec           = { ((attr-name | limit-expr) (pattern | recursion-limit))+ }
limit-expr         = [("limit" | 'limit') attr-name (positive-number | nil)]
recursion-limit    = positive-number | '...'

You can explicitly specify the handling of referenced entities by using a map instead of just an attribute name. The simplest map specification is a map specifying a specific pattern for a particular attr-name.

Map Specification Example

The :track/artists attribute appears in a map spec, causing the :db/id and :artist/name to be sub-pulled for each artist on the track ghostRiders:

[:track/name {:track/artists [:db/id :artist/name]}]
=>
{:track/artists [{:db/id 17592186048186, :artist/name "Bob Dylan"}
                 {:db/id 17592186049854, :artist/name "George Harrison"}],
 :track/name "Ghost Riders in the Sky"}

Map Specification Nesting Example

Map specs can nest arbitrarily. The pattern below pulls concertForBanglaDesh's media's tracks' titles and artists' names:

[{:release/media
    [{:medium/tracks
        [:track/name {:track/artists [:artist/name]}]}]}]
=>
[{:medium/tracks
 [{:track/artists
     [{:artist/name "Ravi Shankar"} {:artist/name "George Harrison"}],
     :track/name "George Harrison / Ravi Shankar Introduction"}
 {:track/artists [{:artist/name "Ravi Shankar"}],
     :track/name "Bangla Dhun"}]}
  :medium/tracks
    [{:track/artists [{:artist/name "George Harrison"}],
        :track/name "Wah-Wah"}
    {:track/artists [{:artist/name "George Harrison"}],
        :track/name "My Sweet Lord"}
    {:track/artists [{:artist/name "George Harrison"}],
        :track/name "Awaiting on You All"}
    {:track/artists [{:artist/name "Billy Preston"}],
        :track/name "That's the Way God Planned It"}]
            ...}    

Attribute Spec

attr-spec           = attr-name | wildcard | map-spec | attr-expr
attr-expr           = [attr-name attr-option+] | legacy-attr-expr
attr-option         = as-expr | limit-expr | default-expr

The Attribute Specification provides control of various aspects of the values returned by the pull of an attribute.

Note that the pattern appears in a seq. This necessitates that the whole clause be quoted or that the pattern is in a vector.

:as Option

[attr-name :as any-value]

The :as option allows replacement of the key for an attribute result map with an arbitrary value.

:as Option Example

The following pattern uses an :as option to pull an :artist/name, replacing the key in the result map with the string "Band Name", pulling on ledZeppelin:

[:artist/name :as "Band Name"]
=> {"Band Name" "Led Zeppelin"}

:limit Option

[attr-name :limit (positive-number | nil)]

The :limit option controls how many values will be returned for a cardinality-many attribute. A limit can be a positive number or nil. A nil limit causes all values to be returned, and should be used with caution.

In the absence of an explicit limit, pull will return the first 1000 values for a cardinality-many attribute.

:limit Option Example

To return only 10 of ledZeppelin's tracks:

[:artist/name (:track/_artists :limit 10)]
=>
{:artist/name "Led Zeppelin",
 :track/_artists
 [{:db/id 17592186057344}
  {:db/id 17592186057345}
  {:db/id 17592186057346}
  {:db/id 17592186057347}
  {:db/id 17592186057348}
  {:db/id 17592186057349}
  {:db/id 17592186057350}
  {:db/id 17592186057351}
  {:db/id 17592186057352}
  {:db/id 17592186057355}]}

:limit Inside a Map Specification Example

Pulling from ledZeppelin', you can get a limited set of nested track names with:

[{(:track/_artists :limit 10) [:track/name]}]
=>
{:track/_artists
 [{:track/name "Whole Lotta Love"}
  {:track/name "What Is and What Should Never Be"}
  {:track/name "The Lemon Song"}
  {:track/name "Thank You"}
  {:track/name "Heartbreaker"}
  {:track/name "Living Loving Maid (She's Just a Woman)"}
  {:track/name "Ramble On"}
  {:track/name "Moby Dick"}
  {:track/name "Bring It on Home"}
  {:track/name "Whole Lotta Love"}]}

Nil :limit Example

Ths pattern below returns all of Led Zeppelin's tracks, without limit:

[:artist/name (:track/_artists :limit nil)]
=>
{:artist/name "Led Zeppelin",
 :track/_artists
 [{:db/id 17592186057344}
  {:db/id 17592186057345}
  {:db/id 17592186057346}
  {:db/id 17592186057347}
  {:db/id 17592186057348}
  {:db/id 17592186057349}
  {:db/id 17592186057350}
  {:db/id 17592186057351}
  {:db/id 17592186057352}
  {:db/id 17592186057355}
  {:db/id 17592186057356}
  {:db/id 17592186057357}
  {:db/id 17592186057358}
  {:db/id 17592186057359}
  {:db/id 17592186057360}
  {:db/id 17592186057361}
  {:db/id 17592186057362}
  {:db/id 17592186057363}
  {:db/id 17592186057366}
  {:db/id 17592186057367}
  ...]} ;; lots more

:default Option

[attr-name :default any-value]

The :default option specifies a value to use if an attribute is not present for an entity.

:default Option Example

The following select reports a zero :artist/endYear for Paul McCartney, who is still active:

[:artist/name (:artist/endYear :default 0)] 
=> {:artist/endYear 0, :artist/name "Paul McCartney"}

The default need not be of the same type as the attribute's values:

[:artist/name (:artist/endYear :default "N/A")] 
=> {:artist/endYear "N/A", :artist/name "Paul McCartney"}

:xform Option

[attr-name :xform symbol]

The :xform option provides the ability to transform the value returned by pull for an attribute.

The resources/datomic/extensions.edn resource contains an :xforms key that is a vector of fully qualified symbols naming functions allowlisted for use by xforms. You can use a whitelisted function or one of the following built-ins:

The fn takes the value returned from the pull expression and returns a value that will be included in the result instead. The return value needs to be supported by transit without any extension handlers.

:default values are not transformed by :xform, and the :xform result takes precedence.

cancel can be used to cancel an xform.

:xform Option Example

The following example uses the unqualified symbol str (from the default functions) to transform the result of pulling the :artist/endYear for led-zeppelin from an integer to a string:

[[:artist/endYear :xform str]]
=> {:artist/endYear "1980"}

Wildcards

wildcard           = "*" or '*'

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

Wildcard Example

The wildcard pulls all the direct attributes of the release. It also recursively pulls :release/media because it is a component attribute. It does not recursively pull :release/artists or :release/country, because those are not component attributes.

[*]
=>
{: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}]}]}
  ...
  ]}

Combining Wildcards and Map Specifications

A map specification can be used in conjunction with the wildcard to provide subpatterns for specific attributes.

Combining Wildcards and Map Specifications Example

The wildcard pulls all attributes of the ghostRiders track, and an explicit map overrides the handling of :track/artists to pull :artist/name.

["*" {:track/artists [:artist/name]}]
=>
{:db/id 17592186063810,
 :track/duration 218506,
 :track/name "Ghost Riders in the Sky",
 :track/position 11,
 :track/artists
 [{:artist/name "Bob Dylan"} {:artist/name "George Harrison"}]}

Recursion Limits

recursion-limit    = positive-number | '...'
map-spec           = { ((attr-name | limit-expr) (pattern | recursion-limit))+ }

A map specification can govern recursion. If a map specification has a numeric value, then the selector containing that specification will be applied recursively up to N times. The ellipsis symbol (...) will follow recursive references to arbitrary depth, and should be used with caution!

If a recursive subselect encounters an entity that it has already seen, it will not apply the pattern, instead returning only the :db/id of the entity. Thus recursive select is safe in the presence of cycles.

Limited Recursion Example

The following (non-mbrainz) specification will pull the first and last names of friends-of-friends up to six degrees of separation from the original entity.

[:person/firstName :person/lastName {:person/friends 6}]

Unlimited Recursion Example

The following specification will find all reachable friends, which might be most of the friends in the entire database.

[:person/firstName :person/lastName {:person/friends ...}]

Empty Results

If there is no match between a pattern and an entity, then pull will return nil (not an empty map):

;; pattern
[:penguins]
;; entity
led-zeppelin
=> nil

Non-matching results will be removed entirely from collections. Even though ghost-riders has artists, none of those artists have :penguins:

;; pattern
[{:track/artists [:penguins]}]

;; entity
ghost-riders
;; result
=> {:track/artists []}

Pull Results

Component Defaults

If a pull attr-name names a reference attribute, pull will return a map for the referenced value. If the attribute is a component attribute, the map will contain all attributes of the related entity as well.

Component Defaults Example

:medium/tracks is a component attribute, so pulling :release/media will also pull related tracks. The example below pulls from darkSideOfTheMoon.

[:release/media]
=>
{:release/media
 [{:db/id 17592186121277,
   :medium/format {:db/id 17592186045741},
   :medium/position 1,
   :medium/trackCount 10,
   :medium/tracks
   [{:db/id 17592186121278,
     :track/duration 68346,
     :track/name "Speak to Me",
     :track/position 1,
     :track/artists [{:db/id 17592186046909}]}
    {:db/id 17592186121279,
     :track/duration 168720,
     :track/name "Breathe",
     :track/position 2,
     :track/artists [{:db/id 17592186046909}]}
    {:db/id 17592186121280,
     :track/duration 230600,
     :track/name "On the Run",
     :track/position 3,
     :track/artists [{:db/id 17592186046909}]}
    ...]}]}

Non-Component Defaults

If a reference is to a non-component attribute, the default is to pull only the :db/id.

Non-Component Defaults Example

Pulling :artist/_{country} of :country/GB returns only the entity ids for the artists from Great Britain:

[:artist/_country]
=> {:artist/_country [{:db/id 17592186045751} {:db/id 17592186045755} ...]}

Multiple Results

If navigating an attribute might lead to more than one value, the pull result will be a list of the values found. These cases include:

  • All forward cardinality-many references
  • Reverse references for non-component attributes.

Multiple Results Example

Pulling :artist/_{country} of :country/GB returns a list of the entity ids for the artists from Great Britain:

[:artist/_country]
=> {:artist/_country [{:db/id 17592186045751} {:db/id 17592186045755} ...]}

Missing Attributes

In the absence of a default, attribute specifications that do not match an entity are omitted from that entity's result map, rather than e.g. appearing with a nil value.

Missing Attributes Example

Paul McCartney has an :artist/name but not a died-in-1966, so only the former appears in a pull result:

[:artist/name :died-in-1966?]
=> {:artist/name "Paul McCartney"}

Legacy Attribute Expressions

NOTE: Attributes Specifications provides a superset of the functionality of Attribute Expressions and is preferred, however limit and default Attribute Expressions will continue to be supported.

attr-expr           = [attr-name attr-option+] | legacy-attr-expr
legacy-attr-expr    = legacy-limit-expr | legacy-default-expr
legacy-limit-expr   = [("limit" | 'limit') attr-name (positive-number | nil)]
legacy-default-expr = [("default" | 'default') attr-name any-value]

Attribute specifications can be wrapped in expressions to control the attribute's default or limit. Each is shown below.

Legacy Limit Expression

legacy-limit-expr   = [("limit" | 'limit') attr-name (positive-number | nil)]

The legacy limit expression is an alternate syntax for Limit Option. Limit Option is preferred.

Legacy Default Expressions

legacy-default-expr = [("default" | 'default') attr-name any-value]

The legacy default expression is an alternate syntax for Default Option. Default Option is preferred.

Pull API vs. Entity API

The Pull API has two important advantages over the Entity API:

Pull uses a declarative, data-driven spec, whereas Entity encourages building results via code. Data-driven specs are easier to build, compose, transmit and store. Pull patterns are smaller than entity code that does the same job, and can be easier to understand and maintain.

Pull API results match standard collection interfaces (e.g. Java maps) in programming languages, where Entity results do not. This eliminates the need for an additional allocation/transformation step per entity.