Skip to content

Add scalasql-zio module: ZIO interop layer#121

Open
ivan-klass wants to merge 1 commit into
com-lihaoyi:mainfrom
ivan-klass:add-zio-module
Open

Add scalasql-zio module: ZIO interop layer#121
ivan-klass wants to merge 1 commit into
com-lihaoyi:mainfrom
ivan-klass:add-zio-module

Conversation

@ivan-klass

Copy link
Copy Markdown
Collaborator

What

Adds a new scalasql-zio module that wraps ScalaSql's JDBC DbApi/DbClient in ZIO effects — type-safe database operations and transactions with savepoints. Scala 3 only.

This ports https://github.com/typebricks/scalasql-zio (authored by me) into the main repo, converting the sbt build to Mill and moving the code under the scalasql namespace.

API

  • scalasql packagePrimaryKey (zero-cost typed PKs), DbEntityName, DbLookupException (NotFound / MultipleResults), IsCovariantHK / widenK.
  • scalasql.dbzio packageZDbApi / ZDbTxnApi / ZDbClient (ZIO wrappers around DbApi / DbClient), DbOp/DbZIO helpers, Savepoint, and atMostOne / orNotFound syntax.

The subpackage is named dbzio (not zio) on purpose: scalasql.zio would shadow _root_.zio under wildcard imports.

import scalasql.dbzio.*

ZDbClient.transaction:
  for
    _    <- DbOp.run(User.insert.columns(_.email := email, _.name := name))
    user <- DbOp.runSingle(User.select.filter(_.email `=` email))
  yield user

JDBC errors are wrapped in DbException; the enclosing transaction commits on success and rolls back on failure.

Build / test

  • New Mill module scalasql-zio (depends on scalasql + dev.zio::zio), mirroring the layout of scalasql-simple.
  • Tests run on an in-memory SQLite DB via zio-test — no external services needed. They are picked up by the existing __.test CI step.
  • The original test dependency on iron was dropped; refined Email/UserName types in tests are now plain opaque types.
  • Formatted with the repo's scalafmt config and clean under __.fix (scalafix RemoveUnused).

Notes / open questions

  • Publishing inherits the repo's com.lihaoyi org → artifact scalasql-zio_3. Happy to add a developer entry / adjust attribution as maintainers prefer.
  • readme.md gains a short "ZIO integration" section.

🤖 Generated with Claude Code

Wraps ScalaSql's JDBC `DbApi`/`DbClient` in ZIO effects, providing
type-safe database operations and transactions with savepoints. JDBC
errors are wrapped in `DbException` and committed/rolled back automatically
by the enclosing `transaction` scope.

- New Mill module `scalasql-zio` (Scala 3, depends on `scalasql` + `dev.zio::zio`)
- Packages `scalasql` (PrimaryKey, DbEntityName, DbLookupException, IsCovariantHK)
  and `scalasql.dbzio` (ZDbApi, ZDbTxnApi, ZDbClient, DbOp, Savepoint, syntax)
- Tests on in-memory SQLite via zio-test (no external services)

Ported from https://github.com/typebricks/scalasql-zio

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@ivan-klass ivan-klass requested review from NPCRUS and lihaoyi June 27, 2026 21:16
Comment thread readme.md
effects, providing type-safe database operations and transactions with savepoints. Scala 3 only.

```scala
ivy"com.lihaoyi::scalasql-zio:0.3.0"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0.3.1 or 0.3.2 i guess, depends on how we will publish this

@@ -0,0 +1,23 @@
package scalasql

object IsCovariantHK:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • doesn't look like this should live inside dbzio
  • don't know if we need it at all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants