Skip to content

v0.17.0 Tapir backends, streaming

Choose a tag to compare

@marcgrue marcgrue released this 17 Apr 10:47
· 170 commits to main since this release

Using molecules from ScalaJS is now fast and using Tapir backends.

A backend server can simply use available moleculeServerEndpoints, like in this Netty setup:

object Netty extends MoleculeServerEndpoints(Rpc_h2) {

  def main(args: Array[String]): Unit = {
    val server = NettyFutureServer()
      .port(8080)
      .addEndpoints(moleculeServerEndpoints) <-- simply add these server endpoints!
      .start()

    server.foreach { _ =>
      println("✅ Netty server running on http://localhost:8080")
    }

    println("Press ENTER to stop the server.")
    StdIn.readLine() // Blocks until user presses ENTER

    println("🛑 Shutting down server...")
    server.flatMap(_.stop())
  }
}

Molecule now offers 8 different minimal backend setups that you can start out from and successively add your own api endpoints.

To see how you can test using molecules from Scala JS, you can for instance run coreTests in the Molecule project:

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

Then you can enter a number to choose which backend you want to use:

Multiple main classes detected. Select one to run:
 [1] molecule.server.Akka
 [2] molecule.server.Armeria
 [3] molecule.server.Http4s
 [4] molecule.server.Netty
 [5] molecule.server.Pekko
 [6] molecule.server.Play
 [7] molecule.server.VertX
 [8] molecule.server.ZioHttp

Enter number: 4
[info] running molecule.server.Netty 
Press ENTER to stop the server.
✅ Netty server running on http://localhost:8080

And in another process you can run one of the following commands to run the test with the database of your choice:

sbt datalogDatomicJS/test
sbt sqlH2JS/test
sbt sqlMariaDBJS/test
sbt sqlMySQLJS/test
sbt sqlPostgreSQLJS/test
sbt sqlSQliteJS/test

(MariaDB, MySQL, PostgreSQL and SQlite requires docker to be running)

Streaming

On the jvm side you can now stream data by calling stream on query instead of the normal get.

for {
  _ <- Entity.int.insert(1, 2, 3).transact

  // Returning an fs2.Stream[IO, Int]
  // Then you can use all the usual operation on the stream.
  // Here we simply convert it to a List in a Future to satisfy the munit test
  _ <- Entity.int.query.stream
    .compile
    .toList
    .unsafeToFuture()
    .map(_.sorted ==> List(1, 2, 3))
} yield ()

The various molecule apis return the following stream types:

  • sync: geny Generator, similar to Lihaoyi's ScalaSql library. This makes it easy to tap into the Lihaoyi ecosystem.
  • async: fs2.Stream
  • io: fs2.Stream
  • zio: ZStream

Some streaming solution is planned for the JS side but not available yet.