11

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}]]
Upgradingdave
  • 12,916
  • 10
  • 62
  • 72

3 Answers3

2

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.

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
1

You can use specter on the result that the pull expression returns:

(->> pull-result                                                                                     
     (sp/transform (sp/walker :db/ident) :db/ident))

The value of key :db/ident is extracted for every map that has that key.

Chris Murphy
  • 6,411
  • 1
  • 24
  • 42
1

Was quite easy to do with postwalk

for any pulled :db/ident you can transform with this function

(defn flatten-ident [coll]
  (clojure.walk/postwalk
   (fn [item] (get item :db/ident item)) coll))
Max Prokopov
  • 1,308
  • 11
  • 16