Thursday 11 July 2013

Basic Datomic database example using Scala

I was reading and watching a lot about Rich Hickey's Datomic database lately and started a simple Scala application showing basic Datomic operations.

The repository can be found here: https://github.com/tinoadams/datomic_scala_test

I'm planning on adding more examples down the track.

The basic example demonstrates how to create an in-memory database, define a simple schema, add a custom partition, insert some entities and querie the database to retrieve the entities again.

Lists and maps

The Datomic library comes with convenience methods to create Java lists and maps:

 import datomic.Util._  
 val javaMap = map("key1", "value1", "key2", "value2")  
 val javaList = list("value1", "value2")  

Scala's lists and maps can also be used but need to be converted into Java types:

 import scala.collection.JavaConversions._  
 val javaMap: java.util.Map[_, _] = Map("key1" -> "value1", "key2" -> "value2")  
 val javaList: java.util.List[_] = List("value1", "value2")