- Postgres Driver /
- Flyway to add reactive non-blocking: flyway/flyway-play (play 2.4 +)/
- Scalikejdbc-async here
- Edit conf/application.conf add
play.modules.enabled += "org.flywaydb.play.PlayModule"
Add in conf/application.conf
# https://www.playframework.com/documentation/latest/Configuration play.modules.enabled += "org.flywaydb.play.PlayModule" # https://www.playframework.com/documentation/latest/Configuration # Database configuration # ~~~~~ # You can declare as many datasources as you want. # By convention, the default datasource is named `default` db.default.driver=org.postgresql.Driver db.default.url="jdbc:postgresql://locahost:5432/playapp" db.default.username=postgres db.default.password=sa logger.scalikejdbc=DEBUG # ScalikeJDBC original configuration #db.default.poolInitialSize=10 #db.default.poolMaxSize=10 #db.default.poolValidationQuery= scalikejdbc.global.loggingSQLAndTime.enabled=true scalikejdbc.global.loggingSQLAndTime.singleLineMode=false scalikejdbc.global.loggingSQLAndTime.logLevel=debug scalikejdbc.global.loggingSQLAndTime.warningEnabled=true scalikejdbc.global.loggingSQLAndTime.warningThresholdMillis=5 scalikejdbc.global.loggingSQLAndTime.warningLogLevel=warn play.modules.enabled += "scalikejdbc.PlayModule"
- Implement SuperMail model
- Create REST endpoints
- Add to routes
GET /bars:username controllers.Application.getSuperMails(username: String) POST /bars controllers.Application.createSuperMail
- Implement Controller
Original reactive post here.
More details to handle request here (Official lightbend)
Create a new project from scala seed
sbt new playframework/play-scala-seed.g8
Import in IntelliJ IDEA.
Open build.sbt and add:
Add as following
In plugin.sbt add the following
And enable the reverse plugin at build.sbt
Create a configuration file in project/scalikejdbc.properties
This configuration will allow us to create a model from existing db tables. Next step is to go a terminal (In IntelliJ IDEA there is a Terminal access on the botton, next to sbt terminal) in the root directory of your project and execute:
sbt "scalikejdbcGen messages Messages"
If all configurations are correct you will get a result like this:
After that a models package containing a Messages case class with the implementation of methods for CRUD are created in our project.
However the automation provided is awesome for this type of examples, since our table has a serial id we need to change the signature of our case class to exclude the id and to implement the date automatically (not provided by)
Since a few days ago I’ve played (wink, wink) with Play, Lagom and Reactive Systems, I’ve found in the need of trying Some Scala and implement a REST API to access a Postgres Database, so instead of downloading, installing and configuring a database in my local machine I’ve felt tempted to use Vagrant instead.
Vagrant you say?
Vagrant is a tool for building and managing virtual machine environments in a single workflow. This tools “packages” our special box which is a small VM. A remarkable aspect is Vagrant relies on “providers” for virtualization. In this case, we’ll proceed first to install both Vagrant AND Virtualbox.
The beauty of Vagrant is that several boxes with preconfigured OSs, tools and services are ready to be implemented. Just search here for a Vagrant box of your needs.
sudo apt install virtualbox-qt
sudo apt install vagrant
Let’s get to it!
Get Postgres Vagrant box.
Open a Terminal. Create a directory in which you will download and place your Vagrant Machine.
The previous image comprises the following actions we have performed:
- Create a Postgres_Vagrant directory
- Download our Vagrant box.
- Open Vagranfile for this box.
Uncomment the following line of Vagrantfile
config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "forwarded_port", guest: 5432, host: 5432, host_ip: "127.0.0.1"
Save and close the Vagrantfile. Go back to your terminal and execute
This command will start the vm for this exercise.
Now we open VirtualBox and effectively our vm is ready.
That’s all! Now we can focus our effort on building our REST API to access a database, but that’s for another day).
Since I’m now in Russia, the Postgresql locale was set to “ru_RU.UTF-8”, but it’s necessary to change it to support locale to match my language “en_US.UTF-8”.
Change it like this