Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Commit e03fc02

Browse files
skunk video
1 parent 5f8ee77 commit e03fc02

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

_posts/2024-04-25-skunk-complete-guide.md

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ _by [Derick Bomen](https://www.linkedin.com/in/bomen-derick-b6b06517b/)_
1313

1414
In modern application development, efficient database interactions are crucial for building scalable and maintainable systems. Scala, being a versatile language, offers various tools and libraries to streamline these interactions. One such powerful tool among others ([Doobie](https://index.scala-lang.org/tpolecat/doobie/artifacts/doobie-hikari/0.9.0?binary-version=_2.13), [Slick](https://index.scala-lang.org/slick/slick), [Quill](https://index.scala-lang.org/zio/zio-quill) etc) is the [Skunk Scala library](https://typelevel.org/skunk/), which provides a functional and typesafe interface for PostgreSQL databases access in Scala applications. In this article, we'll delve deep into Skunk, exploring its features and demonstrating how to interact with a database effectively in a non-blocking manner.
1515

16+
For the video version, watch on [YouTube](https://youtu.be/xNCCHFljwBg):
17+
18+
{% include video id="xNCCHFljwBg" provider="youtube" %}
19+
1620
## 2. What is Skunk?
1721

1822
Skunk is a robust Scala library that has been specifically crafted to offer optimal database access to PostgreSQL. Its functional, typesafe, resource-safe session, and composable interface provides a high level of type-safety, ensuring that SQL queries are checked at compile-time. With Skunk, developers can expect a reliable and efficient way to interact with PostgreSQL databases. Its advanced features offer a seamless and typesafe experience that enables developers to streamline their database access and management. Skunk is the perfect choice for developers who strive to achieve efficient and secure database access while maintaining a high level of type-safety.
@@ -200,34 +204,34 @@ In Skunk, given instances such as `Temporal, Trace, Network, and Console` play e
200204

201205
object main extends IOApp {
202206

203-
def singleSession(config: Config): IO[Unit] = DbConnection.single[IO](config).use { session =>
204-
for {
205-
_ <- IO(println("Using a single session..."))
206-
dateAndTime <- session.unique(sql"select current_timestamp".query(timestamptz))
207-
_ <- IO(println(s"Current date and time is $dateAndTime."))
208-
} yield ()
209-
}
210-
211-
def pooledSession(config: Config): IO[Unit] = DbConnection.pooled[IO](config).use { resource =>
212-
resource.use { session =>
207+
def singleSession(config: Config): IO[Unit] =
208+
DbConnection.single[IO](config).use { session =>
213209
for {
214-
_ <- IO(println("Using a pooled session..."))
210+
_ <- IO(println("Using a single session..."))
215211
dateAndTime <- session.unique(sql"select current_timestamp".query(timestamptz))
216212
_ <- IO(println(s"Current date and time is $dateAndTime."))
217213
} yield ()
218214
}
219-
}
215+
216+
def pooledSession(config: Config): IO[Unit] =
217+
DbConnection.pooled[IO](config).use { resource =>
218+
resource.use { session =>
219+
for {
220+
_ <- IO(println("Using a pooled session..."))
221+
dateAndTime <- session.unique(sql"select current_timestamp".query(timestamptz))
222+
_ <- IO(println(s"Current date and time is $dateAndTime."))
223+
} yield ()
224+
}
225+
}
220226

221227
override def run(args: List[String]): IO[ExitCode] = {
222228
val config: Either[ConfigReaderFailures, Config] = ConfigSource.default.at("db").load[Config]
223-
config match
224-
case Left(configFailure) =>
225-
for {
226-
_ <- IO(println(configFailure.prettyPrint(2)))
227-
} yield ExitCode.Success
228-
229-
case Right(configValues) =>
230-
singleSession(configValues) *> pooledSession(configValues) *> IO.pure(ExitCode.Success)
229+
config match {
230+
case Left(configFailure) =>
231+
IO(println(configFailure.prettyPrint(2))).as(ExitCode.Success)
232+
case Right(configValues) =>
233+
singleSession(configValues) *> pooledSession(configValues) *> IO.pure(ExitCode.Success)
234+
}
231235
}
232236
}
233237
```

0 commit comments

Comments
 (0)