Search⌘ K
AI Features

Functions for Data Access

Learn how to implement pure functional data access functions in Scala using Doobie and fs2. Understand how to load single and all products, safely save data with transactions, and update records by managing translations, all while leveraging functional programming patterns.

Loading a product

Scala
override def loadProduct(id: ProductId) =
sql"""SELECT products.id, names.lang_code, names.name
FROM products
JOIN names ON products.id = names.product_id
WHERE products.id = $id"""
.query[(ProductId, LanguageCode, ProductName)]
.to[Seq]
.transact(tx)

The loadProduct function simply returns all rows for a single product from the database, exactly like its Slick counterpart. The parameter will be correctly interpolated by Doobie ...