Online Shopping - Persistence

In the next four chapters, we will write a project using the most common libraries and framework from the Scala ecosystem.

We are going to implement the cart management of an online shopping website, from the front-end to the database.

Starting from the database, we are going to implement a persistence layer. The responsibility of this layer is to persist in a relational database the content of the cart, for that purpose we are going to use a relational persistence framework named Slick.

Then, we are going to spend times to define an API to access the database, this API will use RESTfull web services architecture and JSON as message protocol. The API will be fully documented and testable from a generated website.

Finally, we are going to implement the user interface layer. With this interface, the user can add products into its cart, remove products and update the quantity of a particular product in the cart. Scala.js is used to implement this interface.

In this chapter, we will explain how to persist data in a relational database. The data will be the contents of a cart for a shopping website.

If we want to build a robust website accepting lots of simultaneous connection, special care is needed to have all of the layers of the solution to scale with the demand.

At the level of the persistence layer, a key point to scale would be to not overuse the system resources, more precisely the threads, each time data is written into the database. Indeed, if each request to the database is blocking a thread, the limit of concurrent connections will be reached quickly.

For that purpose, we will use an asynchronous framework called Slick to perform database actions.

In order to use the Slick framework, an introduction to Scala Future will be necessary. Future is one of the basic tools to handle asynchronous code in Scala.

As we rarely host a website at home, we are going to deploy this layer, and later the whole website, to the internet by using a cloud service provider named Heroku. This means that the shopping cart will be accessible from all over the world. 

In this chapter, we will cover the following topics:

  • Creating the project
  • Persistence
  • Deploying the application
  • Heroku configuration
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset