This example illustrates use of:
- Postgres database and Slick for data access
- Tapir/zio-http for building the underlying REST API with Swagger UI.
As Slick does not provide support for ZIO, zio-slick-interop is used to ensure a bottom-up use of zio as far as possible.
This example also:
- Leverages the ZIO dependencies injection pattern ZLayer (IoC)
- Provides some database integration tests by combining zio-test and test-container
- Use zio-prelude ZValidation to ensure that data model objects are always clean and validated
- ZIO
- Introduction to Dependency injection in ZIO
- ZIO validation with ZValidation from zio Prelude
- Introduction to ZIO Test
- Provides idiomatic, easy-to-use ZLayers for Testcontainers-scala
- Slick library for Scala
- zio-slick-interop, small library, that provides interop between Slick and ZIO
- Introduction to ZIO http
- Tapir, running as a zio-http server
Before running the example and/or tests, you need a Postgres image and a running container:
$ docker run -d --name zio-example-postgres -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
Pay attention to the configuration file settings to correctly address the postgres container:
example = {
driver = "org.postgresql.Driver",
url = "jdbc:postgresql://localhost:5432/postgres",
user = "postgres",
password = "password",
connectionPool = disabled
}
To run the example, type $ sbt run
The object data model resides in the model package. This quite dummy model is composed by two tables: User and Post.
User
Field | Data type | Description | Constraint |
---|---|---|---|
login | String | id of a user | Primary key, Not empty, Length must be upper or equal than 6 |
String | Email of the user | Not Null, Must be a valid email |
Post
Field | Data type | Description | Constraint |
---|---|---|---|
id | Long | id of a post | Primary key |
dateOfIssue | Instant | Date of issue | Not Null |
userLogin | String | The user who issued the post | Foreign key (User) |
message | String | The message | Not Null, Not empty |
This example provides a simple REST API for managing users and posts, based on zio-http. As currently this library is not yet available within the official dev.zio ecosystem, the maven version is used ([email protected]). Hope [email protected] will be soon available and would provide test server features. This example leverages tapir capabilities for swagger docs and UI. Try http://localhost:8080/docs/ to get the swagger UI.