Using the Groovy Shell

In order to give both Java and Groovy developers an interactive environment, Datomic provides a Groovy shell configured with the appropriate libraries. This document provides basic information about using the shell.

Why Groovy?

Groovy offers four key advantages for a Java programmer using Datomic:

  • Groovy provides interactive development through groovysh, the Groovy shell. When combined with Datomic's dynamic, data-driven style, this makes it easy to interactively develop code in real time. The source code for this post has a number of other examples designed for interactive study within the Groovy shell.
  • Groovy's collection literals make it easy to see your data. Lists and maps are as easy as:
aList = ['John', 'Doe'];
aMap = [firstName: 'John',
        lastName: 'Doe'];
  • Groovy's closures make it easy to write functions, without the noise of single-method interfaces and anonymous inner classes. For instance, you could grab all the lastNames from a collection of maps:
lastNames = people.collect { it['lastName'] }
  • Of the popular expressive languages that target the JVM, Groovy's syntax is most similar to Java's.

Starting the shell

You start the Groovy shell by running bin/groovysh from the Datomic distribution's root directory.

bin/groovysh

Using the shell

To use the shell, just enter a Groovy expression or statement.

import datomic.Peer

uri = "datomic:mem://hello"
Peer.createDatabase(uri)
conn = Peer.connect(uri)

Most Java code is also valid Groovy. See http://groovy.codehaus.org/ for more information on the Groovy language.

Groovy First Transaction

You can start the Groovy shell from the Datomic root directory with:

bin/groovysh

You should see the Datomic Groovy Shell prompt:

Groovy shell (1.8.9, JVM: 1.7.0_60)
Type 'help' or '\h' for help.
-----------------------------------
groovy:000>

Import the Datomic Peer library

import datomic.Peer

Create a memory database by calling Peer.createDatabase. You will need to define a database URI to connect to that will include the storage protocol.

uri = "datomic:mem://hello"
Peer.createDatabase(uri)

Now, connect to the database by calling Peer.connect:

conn = Peer.connect(uri)

Next, add a fact to the system. The following transaction will create a new entity with the doc string "hello world":

datom = [":db/add", Peer.tempid(":db.part/user"),
         ":db/doc", "hello world"]
resp = conn.transact([datom])

You can exit the Datomic Groovy Shell with exit or Ctrl+D.

Groovy First Query

You can query for the fact you just created. Queries act against an immutable value of the database. Call the db method to get the current value of the database:

db = conn.db()

The following query will find all entities in the system with the doc string "hello world":

peer.query("[:find ?entity :where [?entity :db/doc \"hello world\"]]",
           db)

Datalog query is flexible and powerful, and you can issue many types of queries. The following query, for example, returns the count of all entities in the database that have values for the :db/doc attribute.

peer.query("[:find (count ?entity) . :where [?entity :db/doc]]", db)