Fork me on GitHub
#asami
<
2021-10-26
>
jjttjj20:10:48

Hi just wanted to check if this is a typo or if I'm doing something wrong: https://github.com/threatgrid/asami/wiki/4.-Transactions#temporary-ids When I use a new db:

(da/transact db
  [{:db/id                 -1
    :inventory/label       "Widget"
    :inventory/part-nr     "THX-1138"
    :inventory/stock-count 2187}
   {:db/id                 -2
    :inventory/label       "Doohicky"
    :inventory/part-nr     "AA-23"
    :inventory/stock-count 5
    :inventory/replaces    -1}
   [:db/add -1 :inventory/replaced-by -2]])

(->>
  (da/q
    '[:find ?e
      :where [?e]]
    db)
  (map first)
  (map (partial da/entity (da/db db)))
  )
I get the following:
({:inventory/label "Widget",
  :inventory/part-nr "THX-1138",
  :inventory/stock-count 2187,
  :inventory/replaced-by {:db/ident :tg/node-113217}}
 {:inventory/label "Doohicky",
  :inventory/part-nr "AA-23",
  :inventory/stock-count 5,
  :inventory/replaces -1})
Unlike in the wiki, :inventory/replaces in the second result doesn't refer to another entity, it just sets the value to -1. I'm also not sure I understand it if that's NOT the correct behavior. For example, with the
[:db/add -1 :inventory/replaced-by -2]
How does asami know that -2 is a reference and not just a negative number value?

quoll20:10:18

This is a semantic issue which I think is vague, yes. It’s fine when it’s in the “entity” position, but not in the “value” position. This is why using {:db/id -2} is better.

quoll20:10:08

BTW, this was written before it was possible to say:

(da/q
    '[:find [?e ...]
      :where [?e]]
    db)
Which lets you skip the (map first) step

jjttjj20:10:10

Ah cool. Yeah I missed the part where datoms in asami aren't [e a v] but [:tg/node-10502 :relates-to :tg/node-10499 1 true] So to assign values in a node you need to transact a map right? And to refer to other nodes in the same transaction with a tempid, you would need a separate :db/add vector to make the association, right?

quoll20:10:05

Datoms are that way for Datomic as well

quoll20:10:08

You can add values to a node by either transacting a map or individual :db/add statements. Either approach works.

quoll20:10:14

I usually use maps though

quoll20:10:37

[{:db/ident              "THX-1138"
  :inventory/label       "Widget"
  :inventory/part-nr     "THX-1138"
  :inventory/stock-count 2187
  :inventory/replaced-by {:db/ident "AA-23"}}
 {:db/ident              "AA-23"
  :inventory/label       "Doohicky"
  :inventory/part-nr     "AA-23"
  :inventory/stock-count 5
  :inventory/replaces    {:db/ident "THX-1138"}]

jjttjj20:10:42

Can that example be done using tempids though? Or is it generally better to always use explicit :db/ident values like that

quoll20:10:23

It can be, though I need to check that I have the syntax right

quoll20:10:12

[{:db/id                 -1
  :inventory/label       "Widget"
  :inventory/part-nr     "THX-1138"
  :inventory/stock-count 2187
  :inventory/replaced-by {:db/id -2}}
 {:db/id                 -2
  :inventory/label       "Doohicky"
  :inventory/part-nr     "AA-23"
  :inventory/stock-count 5
  :inventory/replaces    {:db/id -1}]

quoll20:10:09

And because it’s :db/id now and not :db/ident, then I believe we also support the Datomic syntax of:

[{:db/id                 -1
  :inventory/label       "Widget"
  :inventory/part-nr     "THX-1138"
  :inventory/stock-count 2187
  :inventory/replaced-by [:db/id -2]}
 {:db/id                 -2
  :inventory/label       "Doohicky"
  :inventory/part-nr     "AA-23"
  :inventory/stock-count 5
  :inventory/replaces    [:db/id -1]]
(someone else submitted this as a patch)

jjttjj20:10:26

Gotcha, that makes sense, thanks!