Fork me on GitHub
#datomic
<
2020-02-05
>
Lone Ranger17:02:12

I couldn't find this in the documentation anywhere, I'm curious what the expected behavior of doing a pull on a composite tuple is. Would you expect back a map or a vector? i.e., based on the documentation, this makes sense to me, but I'm not sure if it matches with reality:

;; pseudo-data example

(d/q '[:find (pull ?e [*]) .
       :where
       [?e :db/id [:reg/semester+course+student [234561 345621 123456]]
  @conn)  ;;=> 
[{:db/id           234561
  :semester/year   2018
  :semester/season :fall}
 {:db/id    345621
  course/id "BIO-101"}
 {:db/id         123456
  :student/first "John"
  :student/last  "Doe"
  :student/email ""}]

Ivar Refsdal09:02:37

I would expect a map if you use the . notation in :find. Otherwise I would expect a 0 or 1 length set as the :reg/course+semester+student attribute is :db.unique/identity.

(defn db-id [lookup-ref]
  (d/q '[:find ?e .
         :in $ ?id-attr ?value
         :where
         [?e ?id-attr ?value]]
       (d/db conn)
       (first lookup-ref)
       (second lookup-ref)))

  (d/q '[:find (pull ?e [*]) .
         :in $ ?course-semester-student
         :where
         [?e :reg/course+semester+student ?course-semester-student]]
        (d/db conn)
       [(db-id [:course/id "BIO-101"])
        (db-id [:semester/year+season [2018 :fall]])
        (db-id [:student/email ""])]))
  ; =>
  ;{:db/id 17592186045422,
  ; :reg/course #:db{:id 17592186045419},
  ; :reg/semester #:db{:id 17592186045418},
  ; :reg/student #:db{:id 17592186045420},
  ; :reg/course+semester+student [17592186045419 17592186045418 17592186045420]}
The only thing I don't like with this is having to write and call the db-id function. Datomic should do this itself I think. Here is a working gist that demostrates this: https://gist.github.com/ivarref/dc2a5698cd0b791121bfadfa934bcd74#file-tuples2-clj-L79 Edit: The schema and basic data transactions is copied from https://docs.datomic.com/cloud/schema/schema-reference.html#tuples

Lone Ranger20:02:26

Brilliant, thank you

🙂 4
Lone Ranger20:02:47

That’s exactly what I was looking for

zane19:02:17

In the Datomic query grammar is find-rel short for something? find-relation?

favila19:02:24

Yes. A relation is a set of same-typed tuples

zane19:02:07

Awesome. What does "same-typed" mean in this context?

favila19:02:39

each tuple is the same type?

favila19:02:10

I guess specific to datomic, each tuple slot corresponds to the same binding expression in the find

favila20:02:54

All this grammar means is you’ll get results like #{[x y z]…} not [x y z] (tuple) or [x x x] (collection) or x (scalar)

favila20:02:06

and none of this is supported on datomic cloud

zane20:02:43

> each tuple slot corresponds to the same binding expression in the find This makes sense to me.

zane20:02:57

> none of this None of what?

favila20:02:58

it doesn’t support any destructuring in :find

favila20:02:22

IOW “find-rel” is the only option

zane20:02:50

Got it! Thanks.