This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
745 lines (652 loc) · 19.7 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Slick</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/black.css" id="theme">
<!-- Code syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="slides.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section data-background="img/slick_rick.jpg">
<h1 style="font-size:10em;font-weight:bolder;color:goldenrod;">SLICK</h1>
<aside class="notes">
<p>
presenter deg selv!
</p>
<p>
ScalaQuery, Slick 1, Slick 2
</p>
Agenda:
<ul>
<li>Survival guide</li>
<li>How to do things, so you can do things ;-)</li>
<li>experience report</li>
</aside>
</section>
<section>
<p>
Slick is a modern <span class="fragment highlight-current-red" data-fragment-index="1">database query and access library</span>
for Scala. It allows you to work with stored data almost as if you were
using Scala collections while at the same time giving you
full control over when a database access happens and which
data is transferred. You can write your database queries in
<span class="fragment highlight-current-red" data-fragment-index="1">Scala instead of SQL</span>, thus profiting from the
<span class="fragment highlight-current-red" data-fragment-index="1">static checking</span>,
<span class="fragment highlight-current-red" data-fragment-index="1">compile-time safety</span> and
<span class="fragment highlight-current-red" data-fragment-index="1">compositionality</span> of Scala. Slick features
an extensible query compiler which can generate code for different backends.
</p>
<aside class="notes">
Hard to know exactly what this means
</aside>
</section>
<section data-background="img/platoon.jpg">
<h1 style="color:red"><span class="pxb-1">NOT AN ORM</span></h1>
<aside class="notes">
The important thing! - its not an ORM
<ul>
<li>
relational databases are quite ok
</li>
<li>
mutable object graphs are not
</li>
</ul>
</aside>
</section>
<section data-background="img/fish_slap.gif">
<h1 style="color:red">Can still be annoying</h1>
<aside class="notes">
<p>
Its still a database library - they are all annoying!
</p>
<p>
Bridging two worlds always has friction
</p>
</aside>
</section>
<section data-background="img/fish_love.jpg">
<h1 style="color:pink">Can be quite lovely</h1>
<aside class="notes">
But unlike most, it can also be enjoyable
</aside>
</section>
<section data-background="img/sausage_factory.png">
<aside class="notes">
quick peek at the components
</aside>
</section>
<section data-background="img/sausage_factory.png">
<img src="img/sausage2.svg" class="stretch" style="background:#3F3F3F">
<aside class="notes">
<div>Lifted embedding - looks like collections</div>
<div>
Drivers for each database
</div>
<div>
Query compiler, because databases differ
</div>
<div>
Transaction api
</div>
<div>
Connection api
</div>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>DDL</h2>
<pre>
<code data-trim class="sql">
create table producer(
id bigserial primary key,
name text not null
);
create table coffee(
id bigserial primary key,
name text not null,
price double precision,
producer_id bigserial references producer
);
</code>
</pre>
<aside class="notes">
Schema - classic domain of coffee / producer
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>slick</h2>
<pre>
<code data-trim class="scala">
class Coffee(tag: Tag) extends
Table[(Long, String, Double, Long)](tag, "coffee") {
def id = column[Long]("id")
def name = column[String]("name")
def price = column[Option[Double]]("price")
def producerId = column[Long]("producer_id")
def * = (id, name, price, producer_id)
}
val Coffee = TableQuery(tag => new Coffees(tag))
class Producer(tag: Tag) extends
Table[(Long, String)](tag, "producer") {
def id = column[Long]("id")
def name = column[String]("name")
def * = (id, name)
}
val Producer = TableQuery(tag => new Producer(tag))
</code>
</pre>
<aside class="notes">
Represent the database schema in slick
<p>
* defines the entire row
</p>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>query</h2>
<pre>
<code data-trim class="sql">
select producer.name, coffee.name, coffee.price
from coffee, producer
where coffee.producer_id = producer.id
</code>
<code data-trim class="scala">
for {
c <- Coffee
p <- Producer
if c.producerId === p.id
} yield (p.name, c.name, c.price)
</code>
</pre>
<aside class="notes">
<p>
Looks like sql - upside down
</p>
<p>
map, flatMap, filter + more
</p>
<p>
following the FK-constraint
</p>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>Foreign Keys</h2>
<pre>
<code data-trim class="scala">
class Coffee(tag: Tag) extends
Table[(Long, String, Double, Long)](tag, "coffee") {
...
def producerId = column[Long]("producer_id")
lazy val producerFk =
foreignKey("coffee_producer_id_fkey", producerId, Producer)(p => p.id)
}
</code>
<code data-trim class="scala">
for {
c <- Coffee
p <- c.producerFk
} yield (p.name, c.name, c.price)
</code>
</pre>
<aside class="notes">
<div>
Model foreign keys explicitly
</div>
<div>
better typesafety
</div>
<div>
"nested collections" - matches mental model
</div>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>case classes</h2>
<pre>
<code data-trim class="scala">
case class ProducerRow(id:Long, name:String)
class Producer(tag: Tag) extends
Table[ProducerRow](tag, "producer") {
def id = column[Long]("id")
def name = column[String]("name")
def * = (id, name) <> (ProducerRow.tupled, ProducerRow.unapply)
}
</code>
</pre>
<aside class="notes">
case classes has named attributes unlike tuples
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>typesafe ?</h2>
<pre>
<code data-trim class="scala">
def get(id:Long):Option[ProducerRow] = ...
</code>
<code data-trim class="scala fragment">
def get(id:ProducerId):Option[ProducerRow] = ...
def get(id:CoffeeId):Option[ProducerRow] = ...
</code>
</pre>
<aside class="notes">
<div>
What does that long represent ?
ProducerId and CoffeeID
</div>
<div>
be MORE precise!
</div>
<div>
only as typesafe as its types allow them to be
</div>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<pre>
<code data-trim class="scala">
implicit val MappedProducerId =
MappedColumnType.base[ProducerId, Long](_.id, ProducerId)
implicit val MappedCoffeeId =
MappedColumnType.base[CoffeeId, Long](_.id, CoffeeId)
class Coffee(tag: Tag) extends
Table[(CoffeeId, String, Double, ProducerId)](tag, "coffee") {
def id = column[CoffeeId]("id")
...
def producerId = column[ProducerId]("producer_id")
def * = (id, name, price, producer_id)
lazy val producerFk =
foreignKey("coffee_producer_id_fkey", producerId, Producer)(r => r.id)
}
</code>
</pre>
<aside class="notes">
<div>
exponential / invariant functor - xmap
</div>
<div>
column takes a column-type implicitly
</div>
</aside>
</section>
<section data-background="img/coffee2.jpg">
<h2>typesafe !</h2>
<pre>
<code data-trim class="scala">
def get(id:ProducerId):Option[ProducerRow] = db.withSession{ implicit s =>
Producer.filter(_.id === id).firstOption
}
def get(id:CoffeeId):Option[ProducerRow] = db.withSession{ implicit s =>
(for {
c <- Coffee
if c.id === id
p <- c.producerFk
} yield p).firstOption
}
</code>
</pre>
<aside class="notes">
<div>
obvious impl judging from the types
</div>
<div>
reduced the need to depend on names
</div>
</aside>
</section>
<section data-background="img/er-big2.png">
<h2 class="fragment" style="color:red">Write mapping code</h2>
<h2 class="fragment" style="color:red">Refactor Database</h2>
<h2 class="fragment" style="color:red">Repeat</h2>
<h1 class="fragment" style="color:red">Hate Slick!</h1>
<aside class="notes">
<div>
Real world is more like this
</div>
<div>
keeping mapping code and schema in sync hard
</div>
<div>
no static guarantees from slick
</div>
<div>
slick can't drive ddl
</div>
</aside>
</section>
<section data-background="img/generators.jpg">
<h1 style="font-size:4em">Code Generator</h1>
</section>
<section data-background="img/generators.jpg">
<img src="img/koala.png">
<aside class="notes">
Sounds bad from 4GL and other madness
</aside>
</section>
<section data-background="img/generators.jpg">
<pre>
<code data-trim class="scala" style="font-size:1.1em">
"com.typesafe.slick" %% "slick-codegen" % "2.1.0"
scala.slick.codegen.SourceCodeGenerator.main(
Array(slickDriver, jdbcDriver, url, outputFolder, pkg))
</code>
</pre>
<aside class="notes">
<div>
reads the db-meta-model, generates code
</div>
<div>
derive slick from schema
</div>
<div>
typesafe -> code in sync with schema
</div>
</aside>
</section>
<section data-background="img/generators.jpg">
<h2>Customize</h2>
<pre>
<code data-trim class="scala" style="font-size: 90%">
// https://www.parleys.com/tutorial/customizable-code-generation-other-slick-2-features
val typemap = Map(
"coffee.id" -> "coffee.CofeeId",
"coffee.provider_id" -> "coffee.ProviderId",
"provider.id" -> "coffee.ProviderId")
val generator = new SourceCodeGenerator(model){
override def parseType(tpe: String) =
Map("java.sql.Timestamp" -> "org.joda.time.DateTime").getOrElse(tpe, tpe)
override def Table = new Table(_){
override def Column = new Column(_){
override def rawType = typemap.getOrElse(
model.table.table+"."+model.name, super.rawType)
}
}
}
</code>
</pre>
<aside class="notes">
Presentation by Christopher Vogt
<ul>
<li>
Insert returning generated PK
</li>
<li>
Insert row updated with generated PK
</li>
<li>
Idempotent inserts
</li>
</ul>
<div>
Don't over do it... Generators have a bad reputation for a reason!
</div>
</aside>
</section>
<section data-background="img/er-big2.png">
<h2 style="color:red"><strike>Write mapping code</strike></h2>
<h2 style="color:red">Refactor Database</h2>
<h2 style="color:red">Repeat</h2>
</section>
<section data-background="img/branches.jpg">
<img src="img/git.png" class="stretch" style="background:none; border:none; box-shadow:none">
<aside class="notes">
needs to support development on multiple branches
</aside>
</section>
<section data-background="img/branches.jpg">
<img src="img/flyway.png" class="stretch" style="background:white">
<aside class="notes">
<div>
One forces you to write a ton of xml
</div>
<div>
The other is flyway, where you write sql and/or jdbc migrations
</div>
</aside>
</section>
<section data-background="img/branches.jpg">
<img src="img/sbt.png" class="stretch" style="background:none; border:none; box-shadow:none">
<aside class="notes">
Put it all together... other tools should also work well
</aside>
</section>
<section data-background="img/branches.jpg">
<pre>
<code data-trim class="scala">
Db.dbName := "coffee_${Git.branch}"
Flyway.flywayUrl <<= Db.dbUrl
Slick.packageName := "coffee.db"
Slick.typemap ++= Seq(
"coffee.id" -> "coffee.CoffeeId",
"coffee.provider_id" -> "coffee.ProviderId",
"provider.id" -> "coffee.ProviderId")
Slick.generator <<= Slick.generator.dependsOn(Flyway.flywayMigrate)
sourceGenerators in Compile <+= Slick.generator
</code>
</pre>
<aside class="notes">
<ul>
<li>
which database to connect to
</li>
<li>
configuration of Slick code generator
</li>
<li>
circular dependencies with flyway-plugin
</li>
<li>
tricky to detect branch-name when rebasing
</li>
</ul>
</aside>
</section>
<section data-background="img/er-big2.png">
<h2 style="color:red"><strike>Write mapping code</strike></h2>
<h2 style="color:red"><strike>Refactor Database</strike></h2>
<h2 style="color:red"><strike>Repeat</strike></h2>
<h1 class="fragment" style="color:red">♥ Slick!</h1>
<aside class="notes">
or less reason to hate..
</aside>
</section>
<section data-background="img/miles.jpg">
<h2>compose queries</h2>
<pre>
<code data-trim class="scala">
def coffees(producer:Query[Producer, ProducerRow, Seq]) =
for {
p <- Producer
c <- Coffee
if c.producerId === p.id
} yield c
def all = coffees(Producer)
def forProducer(p:ProducerId) =
coffees(Producer.filter(_.id === p))
</code>
</pre>
<aside class="notes">
<p>
Query composition is the unique feature in Slick!
</p>
<p>
Its all a Query producing a type
</p>
</aside>
</section>
<section data-background="img/miles.jpg">
<h2>participate in tx</h2>
<pre>
<code data-trim class="scala">
def getCoffees(implicit s:Session) = Coffees.list
def getProducers(implicit s:Session) = Producers.list
def getCoffeesAndProducers(implicit s:Session) =
(getCoffees, getProducers)
</code>
</pre>
<aside class="notes">
Dislike threading a Session through, event with implicits
Slick 3 makes this a proper type
</aside>
</section>
<section data-background="img/miles.jpg">
<h2>define tx</h2>
<pre>
<code data-trim class="scala">
val database = Database.forDataSource(...)
def coffeesAndProducers = db.withTransaction{ implicit s =>
getCoffeesAndProducers
}
</code>
</pre>
<aside class="notes">
Slick 3 returns Futures, instead of blocking current thread
</aside>
</section>
<section data-background="img/testing.jpg">
<h2>Testing from the inside</h2>
<pre>
<code data-trim class="scala">
trait SessionSuite extends fixture.Suite {
type FixtureParam = Session
private val database = Database.forDataSource(...)
protected def withFixture(test: OneArgTest) =
database.withTransaction{ s =>
try { test(s) } finally s.rollback()
}
}
class ProducerTest extends fixture.FunSuite with SessionSuite {
test("producer"){ implicit s =>
val names = Producer.map(_.name)
names.insert("Hello")
names.insert("World")
assert(names.list === List("Hello", "World"))
}
}
</code>
</pre>
<aside class="notes">
<p>
Using scala-test. auto-rollback transaction
</p>
<p>
Never change environment! allows running tests in parallell
</p>
</aside>
</section>
<section data-background="img/testing.jpg">
<h2>Testing from the outside</h2>
<pre>
<code data-trim class="scala">
trait DbSuite extends fixture.Suite {
type FixtureParam = Database
private val database = Database.forDataSource(...)
protected def withFixture(test: OneArgTest) =
database.withTransaction{ s =>
object rollbackDb extends DatabaseDef {
def createConnection() = ???
override def withSession[T](f: Session => T) = f(s)
override def withTransaction[T](f: Session => T) = f(s)
}
try { test(rollbackDb) } finally s.rollback()
}
}
</code>
</pre>
<aside class="notes">
all transactions are the same. rollback at end
</aside>
</section>
<section data-background="img/testing.jpg">
<h2>Testing from the outside</h2>
<pre>
<code data-trim class="scala">
class Producers(db: Database) {
private val q = Producer.map(_.name)
def +=(name: String) = db.withSession { implicit s => q.insert(name) }
def names = db.withSession { implicit s => q.list }
}
class ProducersTest extends fixture.FunSuite with DbSuite {
test("producers.names"){ db =>
val p = new Producers(db)
p += "Hello"
p += "World"
assert(p.names === List("Hello", "World"))
}
}
</code>
</pre>
</section>
<section data-background="img/escape_hatch.jpg">
<h2>Escape hatch</h2>
<pre>
<code data-trim class="scala">
sql"select distinct(name) from coffee".as[String].list
</code>
</pre>
<aside class="notes">
<p>
slick doesn't support everything
</p>
<p>
no shame in writing sql - its awesome!
</p>
</aside>
</section>
<section data-background="img/fin.jpg">
</section>
<section data-background="img/query.jpg">
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// Full list of configuration options available at:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: false,
progress: true,
history: true,
center: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
// Optional reveal.js plugins
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, condition: function() { return !!document.querySelector( 'pre code' ); }, callback: function() {
hljs.initHighlightingOnLoad();
hljs.configure({tabReplace: ' '})
}
},
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'plugin/notes/notes.js', async: true }
]
});
</script>
</body>
</html>