Fork me on GitHub
#datascript
<
2017-01-30
>
eslachance06:01:38

Alright. I need to make one query, then insert the id of that query as a reference, in a second query right after. What, exactly, do I need to grab from the return of d/transact! that is used as a reference?

eslachance06:01:49

(my schema does indicate that in the second query, that field is {:db/type :db.type/ref})

eslachance06:01:55

Huh. is it really just {:db/id id}? seems so.

eslachance07:01:07

is there any way to.... "resolve" this automatically when querying? like if I have a reference to another item, it could append the whole thing in the result? aka if I do (seq (d/entity @conn [:message/id "275380583893434368"])) I now get this

([:message/type 0]
 [:message/timestamp "2017-01-29T21:44:07.524000+00:00"]
 [:message/channel_id "274707618222833674"]
 [:message/author {:db/id 3}]
 [:message/id "275380583893434368"]
 [:message/mention_everyone false]
 [:message/tts false]
 [:message/nonce "275380580646912000"]
 [:message/pinned false]
 [:message/content "with?"])
I'd like to get this instead
([:message/type 0]
  [:message/timestamp "2017-01-29T21:44:07.524000+00:00"]
  [:message/channel_id "274707618222833674"]
  [:message/author ([:user/avatar "a_354e581b44cfc815036869f81c7810c6"]
                     [:user/discriminator "4611"]
                     [:user/id "139412744439988224"]
                     [:user/username "eslachance"])]
  [:message/id "275380583893434368"]
  [:message/mention_everyone false]
  [:message/tts false]
  [:message/nonce "275380580646912000"]
  [:message/pinned false]
  [:message/content "with?"])

eslachance07:01:51

(as an arbitrary "Expand all references, event nested ones" if possible)

Niki07:01:17

@eslachance entities are lazy, no need to expand them

Niki07:01:58

when printed, you don’t see that, but if you try to access (:user/id (:message/author (d/entity @conn [:message/id "275380583893434368"]))) you’ll get the id

Niki07:01:33

so entities automatically follow references and any reference attribute is seen as another full entity

Niki07:01:48

but they are realized as you query them, e.g. lazily

eslachance07:01:27

What if I need to simply output the whole thing?

Niki07:01:54

(d/touch (d/entity ...))

Niki07:01:01

but that won’t follow nested refs

Niki07:01:23

if you need something like that, you can write your own function that walks all attributes and checks their types

Niki07:01:36

and touches them recursively

eslachance07:01:48

Will do. What happens if there's a circular reference though... (yes this will most likely happen)

Niki07:01:53

alternatively, if you know which attributes you want to expand, use Pull API

Niki07:01:09

that’s why DataScript doesn’t do it by default

eslachance07:01:39

Alright. I'll need to figure out a way to do this then. I'll go from d/touch and see what I can do. Thank you!

eslachance07:01:21

If all goes well within a few months I'll be making some nice tutorial videos for a few things I've learned... most likely Datascript will be part of that because, well, it's awesome 😄

Niki07:01:07

great! Please let me know

adamfrey22:01:20

I have an app using Datascript that works in dev mode but when I switch advanced compilation I’m getting errors from Datascript, like Lookup ref attribute should be marked as :db/unique: [:state/part :ui] even though I have that attribute set as unique in my schema.

adamfrey22:01:13

Is it common for users to run into issues like this with advanced compilation, or is that suprising?

adamfrey23:01:07

it doesn’t look like simply switching to advanced compilation is the problem. I’ll keep looking

adamfrey23:01:22

I found the culprit! Setting the CLJS compiler option :pseudo-names to true with advanced compilation throws crazy datascript errors. That sounds pretty crazy, but I’ve found it to be true.

adamfrey23:01:36

Without :pseudo-names

(db/-attrs-by db :db/unique) => #{:db/uuid :state/part}
With :pseudo-names
(db/-attrs-by db :db/unique) => #{{:db.unique :db.unique/identity}}