Search code examples
clojurescriptdatascript

How to write query to make result of a key with cardinality/many be a map while not a vector in datascript


Assume a datascript db has a schema:

(def schema {:maker/cars {:db/cardinality :db.cardinality/many
                          :db/valueType :db.type/ref}})
(def conn (d/create-conn schema)

And has inserted some entities:

{:db/id 1
 :car/name "x"}
{:db/id 2
 :car/name "y"}
{:db/id 3
 :car/name "z"}
{:db/id 4 
 :maker/name "Honda"
 :maker/cars [1 2 3]}

Then the query

(d/q '[:find [(pull ?e [:maker/name  {:maker/cars [:car/name]}])]
                 :where [?e :maker/name "Honda"]] @conn)

will get the result:

[#:maker{:name "Honda",
     :cars [#:car{:name "x"} #:car{:name "y"} #:car{:name "z"}]}]

The :maker/cars is a vector.

How to write the pull query to get the result like below?

[#:maker{:name "Honda",
     :cars {1 #:car{:name "x"}
            2 #:car{:name "y"}
            3 #:car{:name "z"}}]

Tried but got no lucky...


Solution

  • There’s no way. There aren’t many options to control the shape of data Pull API returns. Just do your own post-processing on a result that DataScript returns