Creating Datomic Data Sources

Queries in Datomic are primarily performed on Datomic databases, but other sources can also be used. Anything that implements the clojure.lang.Indexed protocol is queryable by the datomic.api/q function.

A commonly used example is vectors of vectors. Stuart Halloway has assembled some interesting examples in this gist. An example taken from there is presented here

This strategy requires you to structure your data in such a way that the attribute parameter is removed from the :where clauses. Using maps as entities instead would allow us to have more structured information as input. In this recent thread Rich Hickey suggests providing an indexed view.

This approach would force us to specify the used keys in the function call, and the attributes are no longer available in the :where clauses. Furthermore, we cannot query for the original entities (the maps) anymore. Instead, if we create an Entity-Attribute-Value (EAV) relation for each key/value pair in the input maps, we can retain the syntax normally used on Datomic databases.

This approach allows us to perform more complicated joins in a Datomic database idiomatic way. For example, if we have a collection of maps representing persons and their likes, and another collection representing their names and emails, we can extract the emails of all that like chocolate.

Joining data across Datomic databases and in-memory collections of maps becomes much easier to write and read. Furthermore, data with the same syntax can be read from either type of source, without the query itself needing to be written in a way that reflects this.

This post has illustrated ways in which existing data can be converted to EAV relations which can be combined with other Datomic data sources to perform advanced queries. Even without a Datomic transactor, it is possible to do quite advanced experiments. It also makes it possible to try out Datomic queries and concepts without having to define a schema for the data.