Do entities in the Datomic metadata, for example, have creation and update times?

I want to know, for example. when the object was created or updated. Should I create an attribute like :created-at and :update-at or does Datomic have theses attributes by default? Or any way to find out when an object was created or updated?

+6
source share
1 answer

Time is fundamental to Datomic design. Part of the Datomic view of time is that objects are not created or updated, as in the traditional CRUD database, where a row is inserted into tables, and new facts overwrite old facts in the row. Instead, facts about objects are confirmed and removed over time. Given this unchanging story, Datomic knows how it should be in its current state. You can collect this information about the structure of your data in the history database over time:

http://docs.datomic.com/clojure/#datomic.api/history

So, to answer your question about added and updated, you can rely on Datomic's built-in understanding of time. There are two options for asking when things were created. If your schema contains a unique identifier of some type, you can limit your request to a transaction reference when this attribute was created (it should be once when the object was added for the first time):

 (d/q '[:find ?e ?tx-time :where [?e :user/id _ ?tx] [?tx :db/txInstant ?tx-time]] db) 

If you cannot rely on the attribute during initialization, you can use the history database to get the minimum time for all transactions corresponding to the entity, for example, in this parameterized query:

 (d/q ':find ?e (min ?tx-time) :in $ ?e :where [?e _ _ ?tx _] [?tx :db/txInstant ?tx-time] (history db) entity-id) 

Please note that this method will be slower, but potentially more reliable. If you need the latest confirmed fact ("update"), you can use sub max for min .

+12
source

Source: https://habr.com/ru/post/971953/


All Articles