diff --git a/site/blog/2023-11-16-hello-zio.md b/site/blog/2023-11-16-hello-zio.md new file mode 100644 index 0000000000..b96efdc674 --- /dev/null +++ b/site/blog/2023-11-16-hello-zio.md @@ -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. + + + +### 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] = ??? +} +``` diff --git a/site/docusaurus.config.js b/site/docusaurus.config.js index 786be5828f..ea4dcafc37 100644 --- a/site/docusaurus.config.js +++ b/site/docusaurus.config.js @@ -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",