As developers using a functional programming language, we embrace immutable collections and pure functions in our code. However, we often have separate rules for how we work with our persistent collections in databases. In this talk we'll look at why & how to take a more functional approach to persistence with databases, from managing the connections, using immutable table rows, and considering event-source persistence
15. ● Avoid shared mutable SESSIONS
● Avoid shared mutable CURSORS
● Mutating state? Cool! But MODEL IT FIRST
● Execute state changes at THE EDGE
Safe Database Interactions
19. case class Issue(id: Int, issueId: Int, title: String,
assignee: Int, status: String)
val issue: Issue = sql"""
select id, issue_id, title, assignee, status
from issues where issue_id = ${req.issue_id}
order by id desc limit 1
"""
.query[Issue]
.unique
.transact(xa)
.unsafePerformSync
20. ● ResultSet to Case Class Conversion
● Async IO modeling with Task (scalaz version)
● Think in SQL, not an ORM
● Plain SQL with sql"" interpolation
● SQL functions supported
Why Doobie?
21. 1. Functional DB’s In Scala
a. Doobie
2. Functional DB’s In The Database
a. Immutable Rows
b. Window Functions
c. Events, Not State
Agenda
22. 1. Functional DB’s In Scala
a. Doobie
2. Functional DB’s In The Database
a. Immutable Rows
b. Window Functions
c. Events, Not State
Agenda
70. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
71. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
72. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
73. with latest_issues as (
select *, row_number() over (
partition by issue_id
order by id
desc
)
from issues where created > now() - interval '7 day'
)
select * from latest_issues where row_number = 1
101. 1. Frankly, It’s Weird
2. Requires Events. No Events, No Event-Sourcing.
3. As Of March 2017, It’s Still Non-Standard
Reasons Against Event-Sourcing