doobie - feedbacks from the trenches - scala.io 2016

42
Feedbacks from the trenches François ARMAND @fanf42 - 2016-10

Upload: normation

Post on 15-Apr-2017

1.593 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Doobie - feedbacks from the trenches - Scala.io 2016

Feedbacks from the trenches

François ARMAND @fanf42 - 2016-10

Page 2: Doobie - feedbacks from the trenches - Scala.io 2016

?

Who knows...

Page 3: Doobie - feedbacks from the trenches - Scala.io 2016

??JdbcTemplate

Who knows...

Page 4: Doobie - feedbacks from the trenches - Scala.io 2016

??JdbcTemplate

?Who knows...

Page 5: Doobie - feedbacks from the trenches - Scala.io 2016

?

Anorm ??Who knows...

?JdbcTemplate

Page 6: Doobie - feedbacks from the trenches - Scala.io 2016

?

Anorm ?Squeryl ? ?JdbcTemplate

?Who knows...

Page 7: Doobie - feedbacks from the trenches - Scala.io 2016

?

Anorm ?Squeryl ? ?JdbcTemplate

?

?

Who knows...

Page 8: Doobie - feedbacks from the trenches - Scala.io 2016

?

Anorm ?Squeryl ? ?JdbcTemplate

?

?SQL?

Who knows...

OBJECT MAPPING

PURESQL

Page 9: Doobie - feedbacks from the trenches - Scala.io 2016

?

Anorm ?Squeryl ? ?JdbcTemplate

?

?SQL?

Love SQL ?

Who knows...

Page 10: Doobie - feedbacks from the trenches - Scala.io 2016

Who’s talkin’ for why?

● 10 years of Scala

● CTO @Normation● Lead-dev on Rudder● chose Scala 7y ago for Rudder

Continuous Configuration for Effective ComplianceFrançois ARMAND / @fanf42

Page 11: Doobie - feedbacks from the trenches - Scala.io 2016

● 10 years of Scala

● CTO @Normation● Lead-dev on Rudder● chose Scala 7y ago for Rudder

Continuous Configuration for Effective Compliance

Squeryl

François ARMAND / @fanf42

Who’s talkin’ for why?

Page 12: Doobie - feedbacks from the trenches - Scala.io 2016

● 10 years of Scala

● CTO @Normation● Lead-dev on Rudder● chose Scala 7y ago for Rudder

Continuous Configuration for Effective Compliance

SquerylJdbcTemplate

François ARMAND / @fanf42

Who’s talkin’ for why?

Page 13: Doobie - feedbacks from the trenches - Scala.io 2016

But what ?

● 10 years of Scala

● CTO @Normation● Lead-dev on Rudder● chose Scala 7y ago for Rudder

Continuous Configuration for Effective Compliance

SquerylJdbcTemplate

François ARMAND / @fanf42

Who’s talkin’ for why?

Page 14: Doobie - feedbacks from the trenches - Scala.io 2016

Slick 3 ?

● Fairly popular, ~ default choice in 2016 for Scala world

● Let’s test it ! ⇒ ported ~ 50% of Rudder code to it.

Page 15: Doobie - feedbacks from the trenches - Scala.io 2016

● Fairly popular, ~ default choice in 2016 for Scala world

● Let’s test it ! ⇒ ported ~ 50% of Rudder code to it.

● Does not work for us :● Feels magic and ORM-y, with *Surprises*

○ in generated SQL■ you can’t precisely control it (that’s the point)

○ in performance (using path of least resistance)■ ex: run-time compilation for EACH request in the path of least resistance

○ with lift embeding type ■ i.e working with Rep[MyClass] in place of MyClass

● An awful lot of boilerplate

Slick 3 ?

Page 16: Doobie - feedbacks from the trenches - Scala.io 2016

Doobie ?

Page 17: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie. It’s GREAT.

Doobie

Page 18: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie. It’s GREAT.

Questions ?

Doobie

Page 19: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.Doobie

● A pure functional, principled database access layer for Scala

Page 20: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.Doobie

● A pure functional, principled database access layer for Scala

● Mapping is a joy (actually make me smile)

Page 21: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.Doobie

● A pure functional, principled database access layer for Scala

● Mapping is a joy (actually make me smile)

● No surprises - particularly in performances and optimization

Page 22: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.Doobie

● A pure functional, principled database access layer for Scala

● Mapping is a joy (actually make me smile)

● No surprises - particularly in performances and optimization

● Rob Norris (@tpolecat) is über nice and helpful

Page 23: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.Doobie

● A pure functional, principled database access layer for Scala

● Mapping is a joy (actually make me smile)

● No surprises - particularly in performances and optimization

● Rob Norris (@tpolecat) is über nice and helpful

● It actually makes me like SQL ○ (it tooks 15 years, and PostgreSQL helps a lot)

Page 24: Doobie - feedbacks from the trenches - Scala.io 2016

Use Doobie.It’s GREAT.

● A pure functional, principled database access layer for Scala

● Mapping is a joy (actually make me smile)

● No surprises - particularly in performances and optimization

● Rob Norris (@tpolecat) is über nice and helpful

● It actually makes me like SQL ○ (it tooks 15 years, and PostgreSQL helps a lot)

Doobie

Page 25: Doobie - feedbacks from the trenches - Scala.io 2016

A principled database access layer for Scala

● everything is a value● standard functional programming idioms apply● Clear separation of concepts

○ (I won’t talk about Free Monad - oups)

Page 26: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. No boilerplate.

Page 27: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. No boilerplate.

Page 28: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. No boilerplate.

Page 29: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. No boilerplate.

Page 30: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. No boilerplate.

Page 31: Doobie - feedbacks from the trenches - Scala.io 2016

Mapping just works. Returns what you want.

Page 32: Doobie - feedbacks from the trenches - Scala.io 2016

No surprises - Sane (performant) defaults

● The exact, 50 lines if you want,SQL queries you want to write.

● PreparedStatement by default.● Batch updates are given.

● Comparison with JDBC

Page 33: Doobie - feedbacks from the trenches - Scala.io 2016

Rob Norris @tpolecat

(like, he produced working code for SQLXML mapping. At 3 a.m. Just for me)(OK, 3 a.m for my timezone. But still :)

is extremely nice and helpful

actually likes SQL

Page 34: Doobie - feedbacks from the trenches - Scala.io 2016

Update 0.4.0: rainbows and wonders

● Fragments○ https://tpolecat.github.io/doobie-scalaz-0.4.0/08-Fragments.html ○ Doobie 0.4.0 add fragments, which allows to safely build and factor out part of requests

(typically: “select column1, …, column42 in the_table”)

● Logging○ https://tpolecat.github.io/doobie-scalaz-0.4.0/10-Logging.html ○ Allows to log queries… based on conditions ! Plus, access to full, fine grained timing.

■ “only the one for which you get an error from Database”■ Or “all the queries taking more than 200ms” !

○ über easy to connect with your logging framework

● Cats or Scalaz○ now, you can choose!

Page 35: Doobie - feedbacks from the trenches - Scala.io 2016

Yes. Really.

Savant word for

“Makes you love SQL - No surprise, it just works”

Questions ?

The End

Page 36: Doobie - feedbacks from the trenches - Scala.io 2016

References

● Book of Doobie : https://tpolecat.github.io/doobie-0.3.0/00-index.html

● Really nice presentation of concepts and use of Free Monad:○ SBTB 2015: Rob Norris, Programs as Values: JDBC Programming with Doobie:

https://www.youtube.com/watch?v=M5MF6M7FHPo

● https://twitter.com/tpolecat

● https://gitter.im/tpolecat/doobie● The faq is pretty cool: https://tpolecat.github.io/doobie-0.3.0/15-FAQ.html (“in”

clause, outer join, pure SQL strings and more)

● Rudder is full of examples, feel free to read files (importing Doobie) in: https://github.com/Normation/rudder/tree/branches/rudder/4.0/rudder-core/src/main/scala/com/normation/rudder/repository/jdbc

Page 37: Doobie - feedbacks from the trenches - Scala.io 2016

Question: testing mapping in the repl ?

This is integrated with testing frameworks, in particular scalacheck

Page 38: Doobie - feedbacks from the trenches - Scala.io 2016

Question: more typed, safer queries ?

● Some things are coming in Doobie 0.4.0 !○ but nothing is fixed for now

○ [update] Actually, was reported post 0.4.0

● I sum-up all the feedback/ideas I get at Scala.io 2016 here: https://github.com/tpolecat/doobie/issues/375

Page 39: Doobie - feedbacks from the trenches - Scala.io 2016

Question: variables number or parameters?

How I deal with a query whose number of parameter is know at runtime ?Doobie has several level of API:

● sql””” …. “”” : the simplest, but also the less flexible● Query/Query0: give a return type, a type for parameters, and a string for query: allows to build

queries by string concatenation (ideal for porting from JDBC)

● HC: “high interface”: allows to add parameter by index, “à la” JDBC, but typed. ○ full, complexe example on next page that adapt a query at runtime based on user

parameters, and use HC● and a low level interface, where all JDBC ugliness is exposed

Page 40: Doobie - feedbacks from the trenches - Scala.io 2016

Question: variables number or parameters?

} Request depends of parameters of the function

Query is a pure string built with concatenation

Dynamically set parameters

with HPS & HC

Page 41: Doobie - feedbacks from the trenches - Scala.io 2016

Question: retrieving variable depths of

dependent objects?

One big question in data persistance layer is to manage the depth in the dependency graph of object you are going to retrieve. Typically, with an employee having a property “manager”, which is an ID in DB but also (of course) an employee, how I manage to get only the first employee with the manager ID ? The first employee with the full employee manager ? And his manager ?

Rob Norris gave a super talk exactly on that subject at ScalaWorld 2016:

● “Pure Functional Database Programming with Fixpoint Types “○ slides: http://tpolecat.github.io/presentations/sw2016/slides.html#1○ video: https://www.youtube.com/watch?v=7xSfLPD6tiQ

Page 42: Doobie - feedbacks from the trenches - Scala.io 2016

License

This work is licensed under a Creative Commons

Attribution-NonCommercial-ShareAlike 4.0 International License.

http://www.slideshare.net/normation