项目作者: scalamolecule

项目描述 :
Scala meta-DSL for the Datomic database
高级语言: Scala
项目地址: git://github.com/scalamolecule/molecule.git
创建时间: 2014-07-01T23:57:41Z
项目社区:https://github.com/scalamolecule/molecule

开源协议:

下载


Molecule is a Scala library to query SQL and NoSQL databases with the words of your domain.

Compose an immutable “molecule” data structure:

  1. Person.name.age.Address.street

instead of building queries









SQL Datalog


sql SELECT Person.name, Person.age, Address.street FROM Person INNER JOIN Address ON Person.address = Address.id;


clojure [:find ?name ?age ?street :where [?a :Person/name ?name] [?a :Person/age ?age] [?a :Ns/address ?b] [?b :Address/street ?street]]

and get typed data matching the molecule from the database:

  1. val persons: List[(String, Int, String)] =
  2. Person.name.age.Address.street.query.get

Data can also be fetched asynchronously in a Future or ZIO.

Main features of Molecule

  • Support for PostgreSQL, SQlite, MySQL, MariaDB, H2 and Datomic databases. More can easily be added
  • Molecules for any database behave identically. Each db pass the same SPI compliance test suite (+1700 tests).
  • Targets Scala 3.3, 2.13 and 2.12 on JVM and JS platforms
  • Synchronous, Asynchronous (Future), ZIO and cats.effect.IO APIs
  • All Scala primitive types and collection types available as molecule attributes (!)
  • Typed methods to compose even complex molecules:
    • Filter/aggregation functions
    • Validation
    • Nested and optional relationships
    • Sorting
    • Pagination (offset/cursor)
    • Subscriptions
  • Molecules on ScalaJS side transparently operates server database with no JSON marshalling/wiring setup
  • Fast transparent binary serialization between Client and Server with Boopickle (no
    manual setup)
  • No macros
  • No complex type class implicits
  • Maximum type inference in IDE to easily choose available attributes/expressions/relationships

How does it work?

1) Define a model of your domain data with Molecule’s meta DSL

  1. object MyDomain extends DataModel(5) {
  2. trait Person {
  3. val name = oneString
  4. val age = oneInt
  5. val birthday = oneLocalDate
  6. val address = one[Address]
  7. }
  8. trait Address {
  9. val street = oneString
  10. val zip = oneString
  11. val city = oneString
  12. }
  13. }

2) Run sbt compile -Dmolecule=true once to generate molecule-enabling boilerplate code and db schemas.
3) Compose fluent molecules with your domain terms to save and read data from your database.

Examples

Molecules using any Database/API combination return the same data, just in different wrappings:

Synchronous API, Datomic

  1. import molecule.datalog.datomic.sync._
  2. val persons: List[(String, Int, String)] =
  3. Person.name.age.Address.street.query.get

Synchronous API, PostgreSQL

  1. import molecule.sql.postgres.sync._
  2. val persons: List[(String, Int, String)] =
  3. Person.name.age.Address.street.query.get

Asynchronous API

  1. import molecule.sql.postgres.async._
  2. val persons: Future[List[(String, Int, String)]] =
  3. Person.name.age.Address.street.query.get

ZIO API

  1. import molecule.sql.postgres.zio._
  2. val persons: ZIO[Conn, MoleculeError, List[(String, Int, String)]] =
  3. Person.name.age.Address.street.query.get

IO API

  1. import molecule.sql.postgres.io._
  2. val persons: cats.effect.IO[List[(String, Int, String)]] =
  3. Person.name.age.Address.street.query.get

Transact data

Save one entity

  1. Person
  2. .name("Bob")
  3. .age(42)
  4. .Address
  5. .street("Baker st")
  6. .save.transact

Insert multiple entities

  1. Person.name.age.Address.street.insert(
  2. ("Bob", 42, "Baker st"),
  3. ("Liz", 38, "Bond road")
  4. ).transact

Update

  1. Person(bobId).age(43).update.transact

Delete

  1. Person(bobId).delete.transact

Get started

Clone molecule-samples and use one of the template projects
to get started.

  1. git clone https://github.com/scalamolecule/molecule-samples.git

Basic sbt setup

Add the following to your build files:

project/build.properties:

  1. sbt.version = 1.10.7

project/plugins.sbt:

  1. addSbtPlugin("org.scalamolecule" % "sbt-molecule" % "1.12.0")

build.sbt:

  1. lazy val yourProject = project.in(file("app"))
  2. .enablePlugins(MoleculePlugin)
  3. .settings(
  4. libraryDependencies ++= Seq(
  5. // One or more of:
  6. "org.scalamolecule" %%% "molecule-sql-postgres" % "0.16.0",
  7. "org.scalamolecule" %%% "molecule-sql-sqlite" % "0.16.0",
  8. "org.scalamolecule" %%% "molecule-sql-mysql" % "0.16.0",
  9. "org.scalamolecule" %%% "molecule-sql-mariadb" % "0.16.0",
  10. "org.scalamolecule" %%% "molecule-sql-h2" % "0.16.0",
  11. "org.scalamolecule" %%% "molecule-datalog-datomic" % "0.16.0",
  12. ),
  13. // Paths to directories with your domain structure definition files
  14. moleculeDomainPaths := Seq("app")
  15. )

Explore code

The coreTests module in this repo has several data model definitions and +1700 tests that show all details of
how molecule can be used. This forms the Service Provider Interface that each database implementation needs to comply to
in order to offer all functionality of Molecule.



Run JVM tests

Make sure Docker is running to run tests for Postgres, SQlite, Mysql and MariaDB. Datomic and H2 can be run in memory for tests.
On a mac you can for instance start Docker Desktop.

Run the same test suite on jvm targeting various databases:

sbt sqlPostgresJVM/test
sbt sqlSQliteJVM/test
sbt sqlMysqlJVM/test
sbt sqlMariadbJVM/test
sbt sqlH2JVM/test
sbt datalogDatomicJVM/test



Run JS tests

To run tests from the client side with Scala.js, first run a jvm server (Akka Http) in one process:

sbt sqlPostgresJVM/run

Then in another process/terminal window:

sbt sqlPostgresJS/test



Test latest snapshot locally

To be completely up-to-date, you can pull the latest snapshot from Github.
Initially you clone the sbt-molecule and molecule repositories

git clone https://github.com/scalamolecule/sbt-molecule.git
cd ..
git clone https://github.com/scalamolecule/molecule.git

And hereafter you can just pull the latest changes in each repository directory

cd sbt-molecule
git pull
cd ../molecule
git pull

To generate the boilerplate code with the latest plugin, run the following commands:

cd molecule
sbt ++2.12.20 “project baseJVM” publishLocal # Used by sbt-molecule
cd ../sbt-molecule
sbt publishLocal # Make the plugin available
cd ../molecule
sbt compile -Dmolecule=true # Generate boilerplate code

Now the boilerplate code for the core tests is generated and the various test suites can be run from your IDE
(be prepared that it takes a while to compile all the tests for all SPI implementations).

Author

Marc Grue

License

Molecule is licensed under the Apache License 2.0