Skip to content

Commit

Permalink
Add zio blog post (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
oyvindberg authored Nov 24, 2023
1 parent 6e2c64d commit a782bfd
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
80 changes: 80 additions & 0 deletions site/blog/2023-11-16-hello-zio.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
title: Hello, ZIO
description: Typo ported to ZIO
slug: hello-zio
authors:
- name: Øyvind Raddum Berg
# title:
url: https://github.com/oyvindberg
image_url: https://github.com/oyvindberg.png
tags: [typo, zio]
image: https://i.imgur.com/mErPwqL.png
hide_table_of_contents: false
---


Thanks to the efforts of Jules Ivanic in [PR #57](https://github.com/oyvindberg/typo/pull/57),
Typo now supports using `zio-jdbc` as a database library.

### Never heard of Typo?

You can check out the [introduction](https://oyvindberg.github.io/typo/docs/).
Essentially it's a code generator for database access code, which makes PostgreSQL integration type-safe and wonderful to use.

### Maturity
Note that `zio-jdbc` is a bit less mature than `doobie` and `anorm`, so it's a bit more likely to be some rough edges.
In particular, it handles nullable values [imperfectly](https://github.com/zio/zio-jdbc/issues/188).
We fixed a bunch of issues while working on this PR, so it should be pretty close.

### Implemented missing features in `zio-jdbc`

`zio-jdbc` does not support postgres arrays, and it does not support
the [COPY API for streaming inserts](/docs/other-features/streaming-inserts).

Typo outputs code which implements both of these features.

This can likely be upstreamed as a postgres integration module in `zio-jdbc` at some point.

<!-- truncate -->

### Repository signatures

To give a taste of the code you'll get, consider this repository definition:

```scala
trait AddressRepo {
def delete(addressid: AddressId): ZIO[ZConnection, Throwable, Boolean]
def delete: DeleteBuilder[AddressFields, AddressRow]
def insert(unsaved: AddressRow): ZIO[ZConnection, Throwable, AddressRow]
def insertStreaming(unsaved: ZStream[ZConnection, Throwable, AddressRow], batchSize: Int): ZIO[ZConnection, Throwable, Long]
def insert(unsaved: AddressRowUnsaved): ZIO[ZConnection, Throwable, AddressRow]
/* NOTE: this functionality requires PostgreSQL 16 or later! */
def insertUnsavedStreaming(unsaved: ZStream[ZConnection, Throwable, AddressRowUnsaved], batchSize: Int): ZIO[ZConnection, Throwable, Long]
def select: SelectBuilder[AddressFields, AddressRow]
def selectAll: ZStream[ZConnection, Throwable, AddressRow]
def selectById(addressid: AddressId): ZIO[ZConnection, Throwable, Option[AddressRow]]
def selectByIds(addressids: Array[AddressId]): ZStream[ZConnection, Throwable, AddressRow]
def update(row: AddressRow): ZIO[ZConnection, Throwable, Boolean]
def update: UpdateBuilder[AddressFields, AddressRow]
def upsert(unsaved: AddressRow): ZIO[ZConnection, Throwable, UpdateResult[AddressRow]]
}
```

Notice how the signatures use `ZIO`, `ZStream`, `ZConnection`.

### `zio-schema` is not used

We opted to *not* go through zio-schema for the generated code. It was not clear that it was possible to implement all
PostgreSQL features through `zio-schema`, and we wanted to generate code which is as [fast to compile](/docs/other-features/faster-compilation) as possible.

### Also support for `zio-json`

Typo supports generating [JSON codecs](/docs/other-features/json) for all the row types..
The PR also adds support for `zio-json`, so you can get codecs like this:

```scala
object AddressRow {
implicit lazy val jsonDecoder: JsonDecoder[AddressRow] = ???
implicit lazy val jsonEncoder: JsonEncoder[AddressRow] = ???
}
```
1 change: 1 addition & 0 deletions site/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const config = {
position: "left",
label: "Documentation",
},
{to: 'blog', label: 'Blog', position: 'left'}, // or position: 'right'
{
href: "https://github.com/oyvindberg/typo",
label: "GitHub",
Expand Down

0 comments on commit a782bfd

Please sign in to comment.