Fork me on GitHub
#datomic
<
2022-10-06
>
cjohansen09:10:03

It seems like I can look up attributes in an as-of database before they are created - is this expected behavior?

(d/create-database "datomic:")
(def conn (d/connect "datomic:"))

(d/transact conn [{:db/ident :my/attr
                   :db/valueType :db.type/string
                   :db/cardinality :db.cardinality/one}])

(def db (d/db conn))
(def then (java.util.Date.))

(d/transact conn [{:db/ident :my/attr-2
                   :db/valueType :db.type/string
                   :db/cardinality :db.cardinality/one}])

;; 1. Look up in the old db
(d/entity db :my/attr-2) ;;=> nil

;; 2. Look up in an equivalent as-of db
(d/entity (d/as-of (d/db conn) then) :my/attr-2) ;;=> {:db/id 74}
Using d/touch on the entity from the second example shows no data, but it surprised me that I was able to look up the eid from the attribute id at all in a database where it shouldn’t exist.

👀 1
favila03:10:27

Idents are “a-temporal” when resolved via d/entid (implicitly or explicitly). They are placed in a special db index/cache of elements which are indexed without considering retraction. This is what allows you to rename an ident and have the old one still work. It also allows code to reference the new name at times before it was created without erroring out

favila03:10:35

If you query using “normal” index lookup eg [?e :db/ident :some-value] you will get what you expect, but it will be slower and won’t give you the special behavior through renames

cjohansen05:10:44

I see, thanks 👍