datascript

oly 2023-07-24T08:01:17.541899Z

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

Niki 2023-07-24T14:17:08.768949Z

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

oly 2023-07-24T14:31:23.377679Z

[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 ?

Niki 2023-07-24T15:08:10.994799Z

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"]]

Niki 2023-07-24T15:09:12.071529Z

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

oly 2023-07-24T15:17:41.463189Z

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.

oly 2023-07-24T15:21:31.159119Z

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

Niki 2023-07-24T15:29:59.701919Z

Oh wait you want to pull?

Niki 2023-07-24T15:31:56.083589Z

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)

Niki 2023-07-24T15:32:33.285279Z

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

oly 2023-07-24T15:34:26.253119Z

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.

oly 2023-07-24T15:35:07.870669Z

(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 😕

oly 2023-07-24T15:35:43.448349Z

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

Niki 2023-07-24T15:40:29.958909Z

Hope it helps! Yeah calling functions is a superpower