ProductPromotion
Logo

Scala

made by https://0x3d.site

GitHub - scalamolecule/molecule: Molecule translates custom Scala code to database queries for multiple databases.
Molecule translates custom Scala code to database queries for multiple databases. - scalamolecule/molecule
Visit Site

GitHub - scalamolecule/molecule: Molecule translates custom Scala code to database queries for multiple databases.

GitHub - scalamolecule/molecule: Molecule translates custom Scala code to database queries for multiple databases.

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

Compose an immutable "molecule" data structure:

Person.name.age.Address.street

instead of building queries

SELECT
  Person.name,
  Person.age,
  Address.street
FROM Person
  INNER JOIN Address 
    ON Person.address = Address.id;
[: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:

val persons: List[(String, Int, String)] =
  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 (+1500 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
object MyDomain extends DataModel(5) { 

  trait Person {
    val name     = oneString
    val age      = oneInt
    val birthday = oneLocalDate 
    val address  = one[Address]
  }

  trait Address {
    val street = oneString
    val zip    = oneString
    val city   = oneString
  }
}
  1. Run sbt compile -Dmolecule=true once to generate molecule-enabling boilerplate code and db schemas.
  2. 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

import molecule.datalog.datomic.sync._

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

Synchronous API, PostgreSQL

import molecule.sql.postgres.sync._

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

Asynchronous API

import molecule.sql.postgres.async._

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

ZIO API

import molecule.sql.postgres.zio._

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

IO API

import molecule.sql.postgres.io._

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

Transact data

Save one entity

Person
  .name("Bob")
  .age(42)
  .Address
  .street("Baker st")
  .save.transact

Insert multiple entities

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

Update

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

Delete

Person(bobId).delete.transact

Get started

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

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

Basic sbt setup

Add the following to your build files:

project/build.properties:

sbt.version = 1.10.2

project/plugins.sbt:

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

build.sbt:

lazy val yourProject = project.in(file("app"))
  .enablePlugins(MoleculePlugin)
  .settings(
    libraryDependencies ++= Seq(
      // One or more of:
      "org.scalamolecule" %%% "molecule-sql-postgres" % "0.12.1",
      "org.scalamolecule" %%% "molecule-sql-sqlite" % "0.12.1",
      "org.scalamolecule" %%% "molecule-sql-mysql" % "0.12.1",
      "org.scalamolecule" %%% "molecule-sql-mariadb" % "0.12.1",
      "org.scalamolecule" %%% "molecule-sql-h2" % "0.12.1",
      "org.scalamolecule" %%% "molecule-datalog-datomic" % "0.12.1",
    ),
    moleculeSchemas := Seq("app/dataModel") // paths to directories with Data Model definition files
  )

Explore code

The coreTests module in this repo has several data model definitions and +1500 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.

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

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

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

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory