Essential Slick by Jonathan Ferguson and Richard Dallaway was recently updated to include Slick 3, which was a big update from Slick 2. Slick is a “functional relational mapping” library for Scala. Instead of focusing on Table ↔ Object relations, Slick uses functional programming concepts to build queries, bind data, and stream results. In Slick 3, the preferred API treats queries and IO operations (called
DBIO) as values, which can be modified and stored without the presence of a database. This is familiar to anyone who has used scalaz's
Task or Haskell's
Anyone interested in Slick should first look through the free documentation available here. The Slick site obviously includes API docs, as well as a sample project that is examined and explained in the written documentation.
In my experience, the docs serve more strongly as a reference, and less so as a ground-up, conversational, step-by-step introduction to Slick. Additionally, it frequently skims over interesting and—frankly—necessary topics.
Essential Slick, on the other hand, is great for learners who want to be introduced to ideas one-at-a-time, and are okay with hand-wavy explainations for topics that haven't been convered yet. The book takes the reader straight into using Slick with a supplied sample project. The text provides many examples and exercises. This is particularly helpful to someone who wants to get to the meat of the library, instead of fighting DB configuration.
The book walks the reader through nearly all major features of Slick 3 in an incrimental manner. If you're starting out with Slick, or perhaps even moving from Slick 2 to 3, it's a great resources to get you feeling comfortable with the library.
It shines—well beyond the official docs—in a few ways:
- Type annotations! In several tricky spots, they include helpful annotations to readers can follow along with the transformations as they occur.
- Showing where things go wrong. For example, when explaining type equivalence, they show several errors for when it doesn't work. This is helpful for beginners.
- Exercises with solutions. It's very rare to truly learn from a book; we learn by doing. After each lesson, the book provides exercises for the reader to explore what has been covered.
- Going beyond just documentation, and explaining concepts that are glossed over elsewhere: using different databases, alternate row mappings, custom column mappings, etc.
- Spending a good amount of time on joins. This is the most difficult part of Slick (and perhaps, SQL) to learn, so the pages here are well spent.
Because Slick 3 encourages non-blocking IO (more precisely, blocking for DB calls in an isolated threadpool used only for queries) and treating IO as values vs side-effects, Essential Slick uses a helper method called
exec to run queries and block on the results. It's a Catch 22, because it misses how IO operations will be combined and ran in a production environment, but does let readers jump in and run queries more simply.
Moving beyond the “this is how you query” basics, the most useful chapter, in my opinion, is Chapter 5. This covers things beyond the basic table and querying DSL. Since patterns and codebase structure becomes important as projects become larger, you see some suggestions on how to:
- Use different databases, which lets you use a lightweight database like H2 in tests while using Postgres/MySQL in production.
- Make your own row mappings. In any larger production deployment, your pretty model classes and pretty columns will diverge, so seeing concrete examples of handling this is invaluable.
- Create your own column mappings (from value classes and ADTs). You want to do this—the rest of your application will thank you—so that you're storing
EmailAddresses instead of
With all the positives, here's a few things I would have loved more of:
- More detail on
Effects, which are a really cool way to enforce read-only transactions (which is important for when you eventually build a caching layer).
- Production suggestions / examples. For beginners, moving beyond “how to do something” and to learning patterns of system development is often a stumbling block.
- Compiled queries. In some real world benchmarks, query building can take as much as .5-1ms. Small wins from compiled queries add up, and are felt across your system.
- Testing, which is completely left out.
For what is included, however, it's a great resource, and I wholeheartedly recommend it.
I've used Slick since before the 1.0 release, so am familiar with a lot of its inner workings (and their changes over time). Slick 3 is a great step forward in the library.
However, the library can be difficult to use at times because of liberal use of implicits and unwieldy types. The magic of these produces an elegant and clean DSL, especially for those who have more experience using Scala than SQL. Because of this, Essential Slick is an invaluable guide to anyone working with Slick in production. It will help get your engineers up and running more quickly, and serves as a great reference for difficult concepts.
You can buy the book here. While you're there, go read the Underscore.io’s blog with great Scala-related content.
Find me on Twitter, @connerdelights, and check out my library of great Scala content on Kifi.