Chapter 10. Working with a database

 

This chapter covers

  • Working with persistent data in a Scalatra application
  • Introducing the Slick library and integrating it into a Scalatra application
  • Creating a persistence layer using Slick’s table models, queries, and DBIO actions

In this chapter, you’ll work with persistent data in a Scalatra application and use the Slick library. Slick integrates relational databases in the Scala language, providing a type-safe query language.

10.1. Working with a relational database and example scenario

A database is typically used by an application to store persistent data. That way, the data can be efficiently queried, loss of data can be minimized, and scaling the database is possible. The application doesn’t talk to the database directly, but uses a database library that provides methods for querying and updating the database.

Scalatra has no built-in support or preference for a specific database system or database library. In principle, all databases that provide a connector library for the JVM can be used.

This chapter shows how you can integrate the Slick library in a Scalatra web application and includes a tutorial on using Slick. Interaction with the database is often abstracted away in the application architecture and encapsulated in a persistence layer. This is shown in figure 10.1.

Figure 10.1. Components involved in the database interaction

10.2. Integrating Slick and working with a DBIO action

10.3. Defining table models and working with a TableQuery

10.4. Using the query language

10.5. Summary