clojuredatomicdatalog

Possible to get enum value via Datomic pull syntax?


In the mbrainz sample data, the :artist/type is an enum. Is it possible to pull the value of the enum out of :db/ident and associate it as the value of the :artist/type key using pull syntax?

This is as close as I could get:

[:find (pull ?e [:artist/name {:artist/type [:db/ident]}])
 :where
 [?e :artist/name "Ray Charles"]
]

;;=> [[{:artist/name "Ray Charles", :artist/type {:db/ident :artist.type/person}}]]

Is it possible to use pull syntax to reshape the result into something like this?

;;=> [[{:artist/name "Ray Charles", :artist/type :artist.type/person}]]

Solution

  • I don't think you can do it using the Pull API the way you are seeking. You may find that it is easier to use the Tupelo Datomic library:

    (require '[tupelo.datomic :as td]
             '[tupelo.core :refer [spyx]] )
    (let [x1      (td/query-scalar  :let     [$ db-val]
                                    :find    [ ?e ]
                                    :where   [ [?e :artist/name "Ray Charles"] ] )
          x2      (td/entity-map db-val x1)
         ]
      (spyx x1)
      (spyx x2)
    )
    

    which gives the result:

    x1 => 17592186049074

    x2 => {:artist/sortName "Charles, Ray", :artist/name "Ray Charles", :artist/type :artist.type/person, :artist/country :country/US, :artist/gid #uuid "2ce02909-598b-44ef-a456-151ba0a3bd70", :artist/startDay 23, :artist/endDay 10, :artist/startYear 1930, :artist/endMonth 6, :artist/endYear 2004, :artist/startMonth 9, :artist/gender :artist.gender/male}

    So :artist/type is already converted into the :db/ident value and you can just pull it out of the map.