Essential Slick 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 Slick’s 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 looks at action combinators.
- Chapter 5 discusses data modelling, including defining custom column and table types.
- Chapter 6 explores advanced select queries, including joins and aggregates.
- Chapter 7 provides a brief overview of Plain SQL queries. This is a useful tool when you need fine control over the SQL sent to your database.
To find out more about the book and download the preview chapters, see Underscore.io.
If you're looking for the example code for Slick 2.1, use the 2.1 branch.
The code is organised as a folder for each chapter. Each folder contains an SBT project.
Each file is either the examples from the book, or the scaffolding for the exercises.
These projects contain a single source file, main.scala. Use the SBT run or ~run command.
For example:
$ cd chapter-01
$ sbt
...
> run
...
[info] Running Example
Creating database table
Inserting test data
Selecting all messages:
Message(Dave,Hello, HAL. Do you read me, HAL?,1)
Message(HAL,Affirmative, Dave. I read you.,2)
Message(Dave,Open the pod bay doors, HAL.,3)
Message(HAL,I'm sorry, Dave. I'm afraid I can't do that.,4)
Selecting only messages from HAL:
Message(HAL,Affirmative, Dave. I read you.,2)
Message(HAL,I'm sorry, Dave. I'm afraid I can't do that.,4)
[success] Total time: 5 s, completed 06/05/2015 2:22:22 PM
Chapter 5 contains several applications. Using the SBT run command will prompt you for the file to run.
Alternatively, use runMain or ~runMail and supply the name of the class to run a particular example:
$ cd chapter-05
$ sbt
> ~runMain StructureExample
The examples are:
StructureExamplein structure.scala - an illustration of separating schema and profile.HListExampleAppin hlists.scala - the HList example from the book.NestedCaseClassExampleAppin nested_case_class.scala - is the exercise on custom case class mapping.NullExamplein nulls.scala - where theUsertable has an optionalemailfield.PKExamplein primary_keys.scala - theUser.idbecomes anOption[Long], and theOccupantTableis added.ForeignKeyExamplein foreign_keys.scala - whereMessageTablehas a foreign key to theUserTable.ValueClassesExamplein value_classes.scala - introduces types for primary keys, asMessagePKandUserPk.SumTypesExamplein sum_types.scala - the messageFlaging example from the book.CustomBooleanExamplein custom_boolean.scala - is the "Custom Boolean" exercise code.
This project contains a chat_schema.scala file that defines the schema for the chapter.
It also defines the method populate which inserts our standard cast, rooms, and messages into the database.
The schema is re-used in the following examples:
JoinsExamplein joins.scala - runs through a variety of joins using the sample data in chat_schema.scala.AggregatesExamplein aggregates.scala - various group by and aggregation examples..
joins.scala contains much that is commented out. Remove the comments from around the code you are interest in to run it.
This project contains the following examples:
SelectExamplein select.scala - gives examples with thesqlinterpolator.UpdateExamplein updates.scala - gives examples with thesqluinterpolator.TsqlExamplein tsql.scala - examples using typed plain queries.