reactive database access with slick 3

Post on 17-Aug-2015

63 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Reactive Database Access in Scala with Slick 3

Ihor Mielientiev

AUGUST 3, 2015

2

AGENDA

What is Slick?1

Introduction To Slick2

Reactive Slick 33

Reactive Streams in Slick 34

Advices5

Pros & Cons (Summary)6

Q&A7

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?

4

Minimal Configuration

5

/resource/application.conf

6

Mapping

7

Slick Mapping

8

• Do mapping in Scala

• Type Safe

• No XML, no Annotations, no “magic”

Slick Mapping

9

Scala collection-like API

10

Slick Querying

Get users with age > 20

OR

11

Slick Querying

Slick Generate Next SQL (H2):

12

Slick Querying (Insert)

Inserting

Batching

Insert or update

13

Slick Querying (Insert 2)

Insert and return object

Batching with returning object

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.

15

Joins

Cross Join

Inner Join

16

Joins

Left Join

Right Join

Full Join

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

18

Joins

19

Joins (for-comprehension)

20

Joins Generated SQL

Applicative join Monadic join

21

Joins Generated SQL

If changes 'filter' order in applicative joins...

Be careful, and look at generated SQL

22

Transactions

- Transactions disabled by default

- To enable transaction just add .transactionally

23

Transactions

24

Pre Compiled Queries

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.

26

Plain SQL

- sql is used for statements which produce a result set.

- For mapping sql result to Model, you need to implement GetResult

27

Type-checked SQL

- Ability to type check hand-written SQL statements

Add annotation:

Use it (tsql):

28

Type-checked SQL

Bad syntax:

29

Type-checked SQL

Incorrect type:

30

Type-checked SQL

Table doesn't exist:

Field name error:

31

Type-checked SQL (Issues)

- Compilation depends on running DB

- Limited IDE support

- Increase compilation time

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

33

Reactive 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.

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

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

37

Thank you

Questions?

top related