A few weeks ago I was assigned a task to retrieve missing data from the past. I must admit I was intrigued and a little daunted: “how am I going to do this?”. Here I will share a solution to a real use case using a unique and interesting feature of Datomic.

The problem ahead seems strange at first, because there are no databases (as far as we know) with a deep concept of time. However, with Datomic the solution becomes quite simple, if I may say, and I’ll explain why. So let’s talk a little about Datomic and some of its capabilities before getting to the fancy stuff.

Datomic is not your usual database and has some brilliant features, one of which is being that Datomic tracks facts over time. That is, in Datomic each event is a transaction and each transaction places datoms/facts into the database; these datoms come with a time-stamp. When you delete something in Datomic you aren’t actually erasing it, the database just says: “ Ok! This isn’t true anymore!” That is, it’s just a fact that is no longer true. Cool huh?.So, as we already know that those facts aren’t actually deleted, we know that they are still there somewhere and we just need to walk through Datomic’s history and retrieve them. Datomic has a lot of powerful tools and we could spend all day talking about them but for now, keep this one in mind.

Next, I will illustrate this rather theoretical introduction with some screenshots of a use case we are working on in our precision agriculture project.

The use case is from an SPA (Single-page application) developed with Clojure, ClojureScript (re-frame and Reagent), PostgreSQL (PostGIS) and Datomic, and entails tracking the activity of a farmer on a crop field. The tracking is done only in a designated area (the field), defined by the owner through the application.

In the home page you can find the fields the farmer is managing. The field is defined geographically by a polygon, and name, location, crop type, and more data. In the second image you can see the activity performed by the farmer in the field.

The problem is that if the farmer deletes a field that is no longer in use, the activities performed in the past, when the field was there, they will be displayed with the actual state of the fields. So we’ll see the track of the activity from the past, but not the field from the past. As I mentioned before we just need to retrieve that “lost” data. So what I’ve said about Datomic comes in handy, doesn’t it?!

First of all, we have a cljs function which is responsible for displaying the view of a selected activity. You can see there that we do multiple bindings but the one that we are looking for is the “field”.