Write powerful Datomic queries in Scala with the words of your domain:

Molecule is a Scala meta-DSL that translates “molecules” of your domain attributes to typed Datalog queries for Datomic - the database of immutable facts.

Molecule videos

Marc Grue presents his Molecule library in 4 parts:

  1. Datomic data model

    Introduction to the data model of Datomic that Molecule is based on.

  2. Building a DSL with Scala macros

    A look behind the scenes of how Molecule is build.

  3. Molecule tour

    A walk through of Molecule features.

  4. Molecule domain modelling

    Using Molecule in your domain modelling.

    More info

How does Molecule work?


Define schema (once)

Our domain could have a Person with attributes name and age having a relationship to an Address so we define a schema for our domain:

trait Person {
  val name    = oneString
  val age     = oneInt
  val address = one[Address]
trait Address {
  val street = oneString

Compile (once)

> cd yourProjectRoot
> sbt compile

Molecule uses our schema as a template to generate some boilerplate code so that we can compose intuitive and powerful query molecules. This step is only needed when we create or change our schema.


Use molecules

Now we can insert data with molecules:

Person.name.age.Address.street insert List(
  ("Lisa", 20, "Broadway"),
  ("John", 22, "Fifth Avenue")

And retrieve data:

Person.name.age.Address.street.get === List(
  ("Lisa", 20, "Broadway"),
  ("John", 22, "Fifth Avenue")

The implicit macros insert and get turns our molecules into type-safe Datalog inserts and queries at compile time. So there's no runtime overhead.

Try demo

  1. git clone https://github.com/scalamolecule/molecule-demo.git
  2. cd molecule-demo
  3. sbt compile
  4. Open in your IDE
  5. Run app - and build new molecules…

Read more