Download - Reactive Database Access With Slick 3
![Page 1: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/1.jpg)
1
Reactive Database Access in Scala with Slick 3
Ihor Mielientiev
AUGUST 3, 2015
![Page 2: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/2.jpg)
2
AGENDA
What is Slick?1
Introduction To Slick2
Reactive Slick 33
Reactive Streams in Slick 34
Advices5
Pros & Cons (Summary)6
Q&A7
![Page 3: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/3.jpg)
3
• 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
WHAT IS SLICK?
![Page 4: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/4.jpg)
4
Minimal Configuration
![Page 5: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/5.jpg)
5
/resource/application.conf
![Page 6: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/6.jpg)
6
Mapping
![Page 7: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/7.jpg)
7
Slick Mapping
![Page 8: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/8.jpg)
8
• Do mapping in Scala
• Type Safe
• No XML, no Annotations, no “magic”
Slick Mapping
![Page 9: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/9.jpg)
9
Scala collection-like API
![Page 10: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/10.jpg)
10
Slick Querying
Get users with age > 20
OR
![Page 11: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/11.jpg)
11
Slick Querying
Slick Generate Next SQL (H2):
![Page 12: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/12.jpg)
12
Slick Querying (Insert)
Inserting
Batching
Insert or update
![Page 13: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/13.jpg)
13
Slick Querying (Insert 2)
Insert and return object
Batching with returning object
![Page 14: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/14.jpg)
14
Slick Querying (Update / Delete)
Update
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.
![Page 15: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/15.jpg)
15
Joins
Cross Join
Inner Join
![Page 16: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/16.jpg)
16
Joins
Left Join
Right Join
Full Join
![Page 17: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/17.jpg)
17
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
![Page 18: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/18.jpg)
18
Joins
![Page 19: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/19.jpg)
19
Joins (for-comprehension)
![Page 20: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/20.jpg)
20
Joins Generated SQL
Applicative join Monadic join
![Page 21: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/21.jpg)
21
Joins Generated SQL
If changes 'filter' order in applicative joins...
Be careful, and look at generated SQL
![Page 22: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/22.jpg)
22
Transactions
- Transactions disabled by default
- To enable transaction just add .transactionally
![Page 23: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/23.jpg)
23
Transactions
![Page 24: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/24.jpg)
24
Pre Compiled Queries
![Page 25: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/25.jpg)
25
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.
![Page 26: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/26.jpg)
26
Plain SQL
- sql is used for statements which produce a result set.
- For mapping sql result to Model, you need to implement GetResult
![Page 27: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/27.jpg)
27
Type-checked SQL
- Ability to type check hand-written SQL statements
Add annotation:
Use it (tsql):
![Page 28: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/28.jpg)
28
Type-checked SQL
Bad syntax:
![Page 29: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/29.jpg)
29
Type-checked SQL
Incorrect type:
![Page 30: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/30.jpg)
30
Type-checked SQL
Table doesn't exist:
Field name error:
![Page 31: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/31.jpg)
31
Type-checked SQL (Issues)
- Compilation depends on running DB
- Limited IDE support
- Increase compilation time
![Page 32: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/32.jpg)
32
Supported Databases
- DB2 (via slick-extensions)- Derby/JavaDB- H2- HSQLDB/HyperSQL- Microsoft SQL Server (via slick-extensions)- MySQL- Oracle (via slick-extensions)- PostgreSQL- SQLite
![Page 33: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/33.jpg)
33
Reactive Slick 3
![Page 34: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/34.jpg)
34
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.
![Page 35: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/35.jpg)
35
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
![Page 36: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/36.jpg)
36
Summary
Pros+ Full Control
+ Type Safe
+ Composable Queries
+ Reactive
+ Minimal Boilerplate
Cons- Generated SQL is far from perfect
- Not always intuitive
- DSL has limitations
![Page 37: Reactive Database Access With Slick 3](https://reader030.vdocuments.site/reader030/viewer/2022013011/55d1e369bb61eb786c8b46a3/html5/thumbnails/37.jpg)
37
Thank you
Questions?