This episode is for subscribers only. Sign Up or Log in to watch it.

37. Datomic Quickstart, part 2

Published 17 October 17

Datomic is a database based on the same principles that underly the design of Clojure itself. Learn what makes it different, and how to start using from Clojure immediately.

This second part dives deeper into transacting entities and querying them.

Show notes

In the previous episode you created this transaction which adds two entities to Datomic, each with a single :user/name attribute. I showed you that once you dereference the result you get a map back with four keys, the first two of which represent the database before and after the transaction.

The transaction result also contains :tx-data, a list of datoms that were created as part of this tranaction. It can provide some great insights into what Datomic is doing.

Notice that these are exactly the 5-tuples we were talking about earlier.

You inserted two new entities, each with one attribute, so you’d expect to see two new datoms, but Datomic actually created three datoms, for three separate entities.

The first entity is the transaction. Each time you call d/transact, Datomic will create a new transaction entity, and all datoms created in that same transaction will point to it with their transaction field. The transaction stores the timestamp of when this data was inserted. It’s also possible to add other meta-information to this transaction entity, for instance to store a reference to the user who performed this operation.

(:tx-data @tx-result)
;;=>
[#datom[13194139534316 50 #inst "2017-10-12T10:16:31" 13194139534316 true]
 #datom[17592186045421 63 "jillosaurus"               13194139534316 true]
 #datom[17592186045422 63 "jonnyboy"                  13194139534316 true]]
;;            🡑        🡑               🡑                    🡑         🡑
;;          entity   attribute        value             transaction  added?

Another thing you’ll notice is that the attributes are just numbers, but before we always referred to them as keywords. For Datomic entities are just numbers, and since attributes are entities it refers to those by number as well. But you can give an entity a name by assiging it a :db/ident attribute.