-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e2c64d
commit a782bfd
Showing
2 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] = ??? | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters