Skip to content

Scala/ZIO 2.x: Postgres/Slick/Tapir/zio-http Example

Notifications You must be signed in to change notification settings

xvThomas/zio-example

Repository files navigation

Scala/ZIO 2.x: Postgres/Slick/Tapir/zio-http Example

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

References

Running a Postgres docker container and running the example

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 data model

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
email 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

Http server

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.

img.png

About

Scala/ZIO 2.x: Postgres/Slick/Tapir/zio-http Example

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages