Recently Typesafe released Slick 3.0.0-RC1, the first release candidate for “Reactive Slick”. Reactive Slick has many new features like Support for Reactive Streams API, a new API for composing and executing database actions and many more.
Before moving forward, we recommended you to go through the Slick 3.0.0-RC1 Upgrade guide. So, that you can get a hint of major changes in Slick 3.0.
In this post, we will see how to build a Reactive Play application with Reactive Slick. In order to build a simple CRUD application in Play framework using Reactive Slick (Slick-3.0.0-RC1) we need to follow these steps:
1) Run activator new play-reactive-slick play-scala to create a new Play application.
2) Add following dependencies in build.sbt file
Here we are using PostgreSQL as database. So, if you have not installed it yet, you can download it from here.
3) Next, we need to add bidirectional mapping of our case class with the table, like this:
Important thing to note in above mapping is that we have omitted O.Nullable/O.NotNull. The reason behind it is that both O.Nullable/O.NotNull are deprecated in Slick-3.0.0-RC1. Instead, we have provided the mapping for optional fields in projection itself.
4) Then, we can write a Database Access Object (DAO) to perform CRUD operations:
As we can see in above code that now Slick returns result in Future. Also, now the database instance db have an associated connection pool and thread pool, so, it is important to call db.close when we are done using it.
5) At last, we need to provide Asynchronous Action response in controller. Example:
Here we are using Action.async to handle Future response from database.
In this way we can build a simple CRUD application in Play framework using Reactive Slick. For more information on Reactive Slick click here.
To download a demo Application click here.