Scala
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
core/src
coretests/src
examples
project
.gitignore
README.md
build.sbt

README.md

Molecule is a type safe and intuitive Scala query/modelling DSL for Datomic - the immutable database of facts.

Visit ScalaMolecule.org to learn more or visit the Molecule forum

What Molecule does

As an example: to find

Names of twitter/facebook_page communities in neighborhoods of southern districts

we can compose a "molecule query" that is very close to our human sentence:

Community.name.`type`("twitter" or "facebook_page")
  .Neighborhood.District.region("sw" or "s" or "se")

Molecule transforms this at compile time (with macros) to a little more elaborate Datalog query string and input rules that finds those communities in the Datomic database:

[:find ?a
 :in $ %
 :where
   [?ent :community/name ?a]
   (rule1 ?ent)
   [?ent :community/neighborhood ?c]
   [?c :neighborhood/district ?d]
   (rule2 ?d)]

INPUTS:
List(
  datomic.db.Db@xxx,
  [[[rule1 ?ent] [?ent :community/type ":community.type/twitter"]]
   [[rule1 ?ent] [?ent :community/type ":community.type/facebook_page"]]
   [[rule2 ?d] [?d :district/region ":district.region/sw"]]
   [[rule2 ?d] [?d :district/region ":district.region/s"]]
   [[rule2 ?d] [?d :district/region ":district.region/se"]]]
)

By not having to write such complex Datalog queries and rules "by hand", Molecule allows you to

  • Type less
  • Make type safe queries with inferred return types
  • Use your domain terms directly as query building blocks
  • Model complex queries intuitively (easier to understand and maintain)

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 tests and poke around...

Use in your own project

Add the following to your build files:

project/build.properties:

sbt.version=0.13.16

project/buildinfo.sbt:

addSbtPlugin("org.scalamolecule" % "sbt-molecule" % "0.4.5")

build.sbt:

lazy val yourProject = project.in(file("demo"))
  .enablePlugins(MoleculePlugin)
  .settings(
    resolvers ++= Seq(
      "datomic" at "http://files.datomic.com/maven",
      "clojars" at "http://clojars.org/repo",
      Resolver.sonatypeRepo("releases")
    ),
    libraryDependencies ++= Seq(
      "org.scalamolecule" %% "molecule" % "0.13.2",
      "com.datomic" % "datomic-free" % "0.9.5697"
    ),
    moleculeSchemas := Seq("demo") // paths to your schema definition files...
  )

Molecule 0.13.2 for Scala 2.12.6 is available at Sonatype.

Getting started...

Read more

Author

Marc Grue

Credits/inspiration/acknowledgements

  • Rogue, type-safe Scala DSL for MongoDB
  • Datomisca, a Scala API for Datomic
  • SqlTyped, Embedding SQL as an external DSL into Scala
  • JScala, Scala macro that produces JavaScript from Scala code
  • Parboiled2, A macro-based PEG parser generator for Scala

License

Molecule is licensed under the Apache License 2.0