Fork me on GitHub
#asami
<
2023-01-22
>
Jakub Holý (HolyJak)08:01:43

I am confused: why is the whole sub-entity address returned, even though I explicitly requested nested? false? The code:

@(d/transact conn {:tx-data [{:id               [:person/id "ann"]
                              :person/id        "ann"
                              :person/addresses [{:address/id     "a-one"
                                                  :address/street "First St."}]}]})
(d/entity conn [:person/id "ann"] false)
; =>
{:id [:person/id "ann"], :person/id "ann",
 :person/addresses [#:address{:id "a-one", :street "First St."}]}
I.e. I expected to get back just ... :person/addresses [#:address{:id "a-one"}] , i.e. w/o the :street . Thank you!

2
quoll10:01:10

Hmmm, I would have expected that too. It’s still 5am here and I shouldn’t be up for a few hours yet (just looked at my phone because I couldn’t sleep). Let me check this out and get back to you.

Jakub Holý (HolyJak)12:01:00

No hurry! Please do take care of yourself!

quoll03:01:49

Well… family ended up taking priority today, which was not the schedule I expected. But this has been on my mind all day. I want to get to it as soon as I can

Jakub Holý (HolyJak)08:01:58

family is always a priority 🙂 Thank you

quoll14:01:28

Ah... I see what it is. The problem is twofold: • improper documentation • me not having worked in that area of the code for too long, and being forgetful

quoll14:01:44

There are entities that are considered "top level entities" and others that are nested. Any entity that you transact will be marked as "top level". Anything nested inside of it won't be.

👍 2
quoll14:01:25

What's the difference? Top level entities will always have an ID assigned to them, and they will always have a property of :a/entity true

👍 2
quoll14:01:23

You can reference a top level entity inside another entity, which is really useful for declaring them separately, or if multiple entities refer to them, or if you want a loop of some type

quoll14:01:16

However, when you reference an entity this way, you might just want to see the reference to the entity, and not see the entire thing nested inside

2
Jakub Holý (HolyJak)14:01:18

I see I did not include :id on the nested entities here, which is not what I intended to do 😅

Jakub Holý (HolyJak)14:01:47

Ah, so nested? works only for referenced top entities?

quoll14:01:55

Yes. It's an explicit test

Jakub Holý (HolyJak)14:01:08

I see, that makes sense.

quoll14:01:14

The idea is that if you nest a structure, then it's an implicit part of it's parent. You will always see it

quoll14:01:07

But if an entity references an existing entity, then it's like a foreign address. You can nest it if you want (set the nested? property to true) or you can just see the reference (`nested?` set to false)

👍 2
Jakub Holý (HolyJak)14:01:35

And I suppose the default is false, right?

2
quoll14:01:52

Ummm.... I need to look 😄

Jakub Holý (HolyJak)14:01:07

well, I can look too 🙂

quoll14:01:16

First of all though, I haven't typed this in (yet), but you should be able to say something like this...

(d/transact conn {:tx-data [{:id "a-one"
                             :address/id     "a-one"
                             :address/street "First St."}
                            {:id               [:person/id "ann"]
                             :person/id        "ann"
                             :person/addresses [{:id "a-one"}]}]})

Jakub Holý (HolyJak)14:01:21

I mean, my statement is true and the value is false 😅

👍 2
Jakub Holý (HolyJak)14:01:00

Thank you, that is exactly what I need. I will try that and get back to you if it does not work.

Jakub Holý (HolyJak)14:01:12

Thank you very much for your help and insights, most appreciated!

quoll14:01:49

PS. I initially messed up the closing parens on that example, but I've fixed it now

quoll14:01:06

Also, I just checked if my suggestion worked, and it does

quoll19:01:32

I updated the docs around entity. This was needed, because it turned out that it had gone stale. The previous version: • Did not mention the nested? argument • Incorrectly stated that references to top-level entities would be returned as nil https://github.com/quoll/asami/wiki/7.-Asami-API#entity

🙌 2
quoll19:01:23

I mean... that's how it used to work, but the docs were not updated as the function changed