reactive database access with slick 3

37
1 Reactive Database Access in Scala with Slick 3 Ihor Mielientiev AUGUST 3, 2015

Upload: igor-mielientiev

Post on 17-Aug-2015

63 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Reactive Database Access With Slick 3

1

Reactive Database Access in Scala with Slick 3

Ihor Mielientiev

AUGUST 3, 2015

Page 2: Reactive Database Access With Slick 3

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

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

4

Minimal Configuration

Page 5: Reactive Database Access With Slick 3

5

/resource/application.conf

Page 6: Reactive Database Access With Slick 3

6

Mapping

Page 7: Reactive Database Access With Slick 3

7

Slick Mapping

Page 8: Reactive Database Access With Slick 3

8

• Do mapping in Scala

• Type Safe

• No XML, no Annotations, no “magic”

Slick Mapping

Page 9: Reactive Database Access With Slick 3

9

Scala collection-like API

Page 10: Reactive Database Access With Slick 3

10

Slick Querying

Get users with age > 20

OR

Page 11: Reactive Database Access With Slick 3

11

Slick Querying

Slick Generate Next SQL (H2):

Page 12: Reactive Database Access With Slick 3

12

Slick Querying (Insert)

Inserting

Batching

Insert or update

Page 13: Reactive Database Access With Slick 3

13

Slick Querying (Insert 2)

Insert and return object

Batching with returning object

Page 14: Reactive Database Access With Slick 3

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

15

Joins

Cross Join

Inner Join

Page 16: Reactive Database Access With Slick 3

16

Joins

Left Join

Right Join

Full Join

Page 17: Reactive Database Access With Slick 3

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

18

Joins

Page 19: Reactive Database Access With Slick 3

19

Joins (for-comprehension)

Page 20: Reactive Database Access With Slick 3

20

Joins Generated SQL

Applicative join Monadic join

Page 21: Reactive Database Access With Slick 3

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

22

Transactions

- Transactions disabled by default

- To enable transaction just add .transactionally

Page 23: Reactive Database Access With Slick 3

23

Transactions

Page 24: Reactive Database Access With Slick 3

24

Pre Compiled Queries

Page 25: Reactive Database Access With Slick 3

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

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

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

28

Type-checked SQL

Bad syntax:

Page 29: Reactive Database Access With Slick 3

29

Type-checked SQL

Incorrect type:

Page 30: Reactive Database Access With Slick 3

30

Type-checked SQL

Table doesn't exist:

Field name error:

Page 31: Reactive Database Access With Slick 3

31

Type-checked SQL (Issues)

- Compilation depends on running DB

- Limited IDE support

- Increase compilation time

Page 32: Reactive Database Access With Slick 3

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

33

Reactive Slick 3

Page 34: Reactive Database Access With Slick 3

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

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

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

37

Thank you

Questions?