1.1 Orientation
Slick is a Scala library for accessing relational databases using an interface similar to the Scala collections library. You can treat queries like collections, transforming and combining them with methods like map
, flatMap
, and filter
before sending them to the database to fetch results. This is how well be working with Slick for the majority of this text.
Standard Slick queries are written in plain Scala. These are type safe expressions that benefit from compile time error checking. They also compose, allowing us to build complex queries from simple fragments before running them against the database. If writing queries in Scala isnt your style, youll be pleased to know that Slick also supports plain SQL queries that look more like the prepared statements you may be used to from JDBC.
In addition to querying, Slick helps you with all the usual trappings of relational database, including connecting to a database, creating a schema, setting up transactions, and so on. You can even drop down below Slick to deal with JDBC directly, if thats something youre familiar with and find you need.
This book provides a compact, no-nonsense guide to everything you need to know to use Slick in a commercial setting:
- Chapter 1 provides an abbreviated overview of the library as a whole, demonstrating the fundamentals of data modelling, connecting to the database, and running queries.
- Chapter 2 covers basic select queries, introducing Slicks query language and delving into some of the details of type inference and type checking.
- Chapter 3 covers queries for inserting, updating, and deleting data.
- Chapter 4 discusses data modelling, including defining custom column and table types.
- Chapter 5 explores advanced select queries, including joins and aggregates.
- Chapter 6 provides a brief overview of Plain SQL queriesa useful tool when you need fine control over the SQL sent to your database.
Slick isnt an ORM
If youre familiar with other database libraries such as Hibernate or Active Record, you might expect Slick to be an Object-Relational Mapping (ORM) tool. It is not, and its best not to think of Slick in this way.
ORMs attempt to map object oriented data models onto relational database backends. By contrast, Slick provides a more database-like set of tools such as queries, rows and columns. Were not going to argue the pros and cons of ORMs here, but if this is an area that interests you, take a look at the Coming from ORM to Slick article in the Slick manual.
If you arent familiar with ORMs, congratulations. You already have one less thing to worry about!
1.2 Running the Examples and Exercises
The aim of this first chapter is to provide a high-level overview of the core concepts involved in Slick, and get you up and running with a simple end-to-end example. You can grab this example now by cloning the Git repo of exercises for this book:
bash $ git clone git@github.com:underscoreio/essential-slick-code.git Cloning into 'essential-slick-code' ... bash $ git checkout 2.1 bash $ cd essential-slick-code bash $ ls -1 README.md chapter-01 chapter-02 chapter-03 chapter-04 chapter-05 chapter-06