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:
StructureExample
in structure.scala - an illustration of separating schema and profile.HListExampleApp
in hlists.scala - the HList example from the book.NestedCaseClassExampleApp
in nested_case_class.scala - is the exercise on custom case class mapping.NullExample
in nulls.scala - where theUser
table has an optionalemail
field.PKExample
in primary_keys.scala - theUser.id
becomes anOption[Long]
, and theOccupantTable
is added.ForeignKeyExample
in foreign_keys.scala - whereMessageTable
has a foreign key to theUserTable
.ValueClassesExample
in value_classes.scala - introduces types for primary keys, asMessagePK
andUserPk
.SumTypesExample
in sum_types.scala - the messageFlag
ing example from the book.CustomBooleanExample
in 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:
JoinsExample
in joins.scala - runs through a variety of joins using the sample data in chat_schema.scala.AggregatesExample
in 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:
SelectExample
in select.scala - gives examples with thesql
interpolator.UpdateExample
in updates.scala - gives examples with thesqlu
interpolator.TsqlExample
in tsql.scala - examples using typed plain queries.