forked from typelevel/skunk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimal2.scala
More file actions
76 lines (64 loc) · 1.99 KB
/
Copy pathMinimal2.scala
File metadata and controls
76 lines (64 loc) · 1.99 KB
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
// Copyright (c) 2018 by Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package example
import cats.effect._
import cats.implicits._
import skunk._
import skunk.implicits._
import skunk.codec.all._
import natchez.Trace
import cats.data.Kleisli
import natchez.Span
import cats.temp.par._
import natchez.honeycomb.Honeycomb
import natchez.EntryPoint
// import natchez.jaeger.JaegerTracer
object Minimal2 extends IOApp {
def session[F[_]: Concurrent: ContextShift: Trace]: Resource[F, Session[F]] =
Session.single(
host = "localhost",
port = 5432,
user = "postgres",
database = "world",
// debug = true
)
case class Country(code: String, name: String, pop: Int)
val country: Decoder[Country] =
(bpchar(3) ~ varchar ~ int4).map { case c ~ n ~ p => Country(c, n, p) }
val select: Query[String, Country] =
sql"""
select code, name, population
from country
WHERE name like $varchar
""".query(country)
def lookup[F[_]: Sync: Trace](pat: String, s: Session[F]): F[Unit] =
Trace[F].span("lookup") {
Trace[F].put("pattern" -> pat) *>
s.prepare(select).use { pq =>
pq.stream(pat, 1024)
.evalMap(c => Sync[F].delay(println(s"⭐️⭐ $c")))
.compile
.drain
}
}
def runF[F[_]: Concurrent: ContextShift: Trace: Par]: F[ExitCode] =
session.use { s =>
List("A%", "B%").parTraverse(p => lookup(p, s))
} as ExitCode.Success
def tracer[F[_]: Sync]: Resource[F, EntryPoint[F]] =
Honeycomb.entryPoint("skunk-example-new") { ob =>
Sync[F].delay(
ob.setWriteKey("<api key>")
.setDataset("<dataset>")
.build
)
}
def run(args: List[String]): IO[ExitCode] =
tracer[IO].use { t =>
t.root("root").use { s =>
runF[Kleisli[IO, Span[IO], ?]].run(s) *>
runF[Kleisli[IO, Span[IO], ?]].run(s)
}
}
}