tonsky / datascript

Immutable database and Datalog query engine for Clojure, ClojureScript and JS
Eclipse Public License 1.0
5.45k stars 304 forks source link

`:xform` is not called on ref attributes #455

Closed mortenschioler closed 11 months ago

mortenschioler commented 11 months ago

I want to dereference tag enumeration entities as part of a pull expression. For example:

(pull db [{[:user/preferred-lang :xform #(:db/ident %)] [:db/ident]}] eid)

;; expected
;; => {:user/preferred-lang :lang/en}

;; actual
;; => {:user/preferred-lang {:db/ident :lang/en}}

Unfortunately, this doesn't work -- I've boiled it down to being because xforms don't get called for reference attributes:

(let [res (d/with (d/empty-db {:a-ref-attr {:db/valueType :db.type/ref}})
                 [[:db/add -1 :db/ident :a-tag]
                  [:db/add -2 :a-ref-attr -1]])
      ent-id (get-in res [:tempids -2])
      db (:db-after res)]
  (d/pull db [[:a-ref-attr :xform (fn [%] (println "xform was called") 42)]] ent-id))
;;; [SILENCE -- NO PRINT, and value is not 42 but #db{:id 1}]
=> {:a-ref-attr #:db{:id 1}}
tonsky commented 11 months ago

Thanks! Should be fixed in 1.5.4

mortenschioler commented 10 months ago

Wow, cool @tonsky! 💯 Thank you so much for your work on DataScript. Am using it in a POC with re-frame, after reading Into The Tar Pit, and am loving it.