Reactive Database Access in Scala with Slick 3 Ihor Mielientiev AUGUST 3, 2015
AGENDA What is Slick? 1 Introduction To Slick 2 Reactive Slick 3 3 Reactive Streams in Slick 3 4 Advices 5 Pros & Cons (Summary) 6 Q&A 7
WHAT IS SLICK? Slick is database query and access library for Scala Slick IS NOT ORM it's Functional-Relational Mapper Brings relational model to Scala (not hidden) Natural fit (no impedance mismatch) Stateless
Minimal Configuration
/resource/application.conf
Mapping
Slick Mapping
No XML, no Annotations, no “magic” Slick Mapping Do mapping in Scala Type Safe No XML, no Annotations, no “magic”
Scala collection-like API
Slick Querying Get users with age > 20 OR
Slick Querying Slick Generate Next SQL (H2):
Slick Querying (Insert) Inserting Batching Insert or update
Slick Querying (Insert 2) Insert and return object Batching with returning object
Slick Querying (Update / Delete) A query for an UPDATE statement must resolve to a comprehension with a single table Delete A query for deleting must only select from a single table.
Joins Cross Join Inner Join
Joins Left Join Right Join Full Join
Joins It's too simple... Lets consider more complex example. Get all users' addresses, who subscribed on “New York Times“ and has age > 18 and lives in USA
Joins
Joins (for-comprehension)
Joins Generated SQL Applicative join Monadic join
Joins Generated SQL If changes 'filter' order in applicative joins... Be careful, and look at generated SQL
Transactions - Transactions disabled by default - To enable transaction just add .transactionally
Transactions
Pre Compiled Queries
Plain SQL - Slick allows you to write your own SQL - Plain SQL queries in Slick are built via string interpolation using the sql, sqlu and tsql - sqlu is used for DML statements which produce a row count instead of a result set.
Plain SQL - sql is used for statements which produce a result set. - For mapping sql result to Model, you need to implement GetResult
Type-checked SQL - Ability to type check hand-written SQL statements Add annotation: Use it (tsql):
Type-checked SQL Bad syntax:
Type-checked SQL Incorrect type:
Type-checked SQL Table doesn't exist: Field name error:
- Compilation depends on running DB Type-checked SQL (Issues) - Compilation depends on running DB - Increase compilation time - Limited IDE support
Supported Databases - DB2 (via slick-extensions) - Derby/JavaDB - H2 - HSQLDB/HyperSQL - Microsoft SQL Server (via slick-extensions) - MySQL - Oracle (via slick-extensions) - PostgreSQL - SQLite
Reactive Slick 3
Reactive Streams In Slick 3 - Reactive Streams is an initiative to provide a standard for asynchronous stream processing with non-blocking back pressure. http://www.reactive-streams.org/ - Reactive Streams is a “protocol” for efficiently passing data between two independent units of execution (producer & consumer) in an asynchronous, non-blocking and bounded fashion.
Advices 1. Always view on generated SQL 2. Try to get data you actually need in that moment (Query granularity) 3. Stay tuned with updates 4. Learn SQL
Pros Cons Summary - Generated SQL is far from perfect + Full Control + Type Safe + Composable Queries + Reactive + Minimal Boilerplate - Generated SQL is far from perfect - Not always intuitive - DSL has limitations
Thank you Questions?