Fork me on GitHub
#datascript
<
2023-07-24
>
oly08:07:17

I have a query which fetch data one of the values is a vector of maps, with a key value pointing to a different entity id is there a way to pull the current entity and traverse the vector of maps also pulling the related entities ? I know I could do this in 2 passes fetching then using pull on the vector after but curious if there is a datalog built in way

Niki14:07:08

It’s better if you show the data and what you want to do with it

oly14:07:23

[1 :title "some data"]
[1 :members [{:entity-id 2} {:entity-id 2}]]
[2 :first_name "user 1"]
[2 :last_name "user 1"]
[3 :first_name "user 1"]
[3 :last_name "user 1"]

(d/q '[:find  [(pull ?e [*])]
       :where [?e :title "some data"]] db)
Just made up this dummy example so it may not be 100% correct, so in the above it will pull :title and :members but I basically want to link :entity-id back to entity 2 & 3 including :first_name and :last_name if that makes sense ?

Niki15:07:10

Well, if you make :entity-id unique and also add it to 2/3, that should work

[[1 :title "some data"]
 [1 :members [{:entity-id 2} {:entity-id 3}]]
 [2 :entity-id 2]
 [2 :first_name "user 1"]
 [2 :last_name "user 1"]
 [3 :entity-id 3]
 [3 :first_name "user 1"]
 [3 :last_name "user 1"]]

Niki15:07:12

I am even more sure it should work if you transact 2/3 as maps, not as separate datoms

oly15:07:41

so pull should auto resolve ? [2 :entity-id 2] not sure why you put the 2 on either side feel like I am missing something there, the basic idea is that I want to store users and them link to them in members while adding in extra data.

oly15:07:31

I don't have the actually queries to hand at my current location, but in essence I have a load of objects store in datalog, then a diagram which I want to link to the objects while also adding in location data so it would be like [{:entity-id 2 :points [100 100]}] in essence a join I guess where I need the data on how to draw things which is associated to a different entity in the EAV store

Niki15:07:59

Oh wait you want to pull?

Niki15:07:56

I think you can do something like that in query, but not in pull:

(d/q '[:find  [(pull ?e2 [*])]
       :where
       [?e :title "some data"]
       [?e :members ?m]
       [(get ?m :entity-id) ?e2]]
  db)

Niki15:07:33

But my best advice is to make those entity-id real references during db population time. It’ll make your life much easier

oly15:07:26

yeah pull is what I am after, it was more the how to get data which is not directly part of the entity ?e1 being the source having a vector of maps with a value I need to get the rest of the data.

oly15:07:07

(get ?m :entity-id)
that looks like what I missing, I always forget I can just use clojure functions I guess I have sql to thank for that 😕

oly15:07:43

thanks for the advice and what looks like a solution, I shall think that over and try it out when I get a chance 🙂

Niki15:07:29

Hope it helps! Yeah calling functions is a superpower