Upgrade to Pro — share decks privately, control downloads, hide ads and more …

ZIO SQL

ZIO SQL

While there are a lot of existing solutions to access relational databases in Scala, each one is taking a slightly different approach.
Usually, users have to choose between SQL first approach and type safety.

ZIO SQL provides a nice embedded DSL that is 100% type-safe and just feels like writing SQL in Scala.

With the first release just around the corner, Jaroslav Regec showcases simple to complex queries, inserts, and some cool new features.

Jaroslav Regec

March 11, 2022
Tweet

More Decks by Jaroslav Regec

Other Decks in Programming

Transcript

  1. TABLE OF CONTENTS Who is ZIO SQL for? 01 ZIO

    SQL 101 02 Hello World 03 Future Plans 04
  2. 01. Who is ZIO SQL for? Let's find target market

    for ZIO SQL by comparing existing sql access solutions in scala by the following criteria: • DSL resembles raw SQL • Type safety
  3. class Persons(tag: Tag) extends Table[(UUID, String, Int)](tag, "persons") { def

    id = column[UUID]("id", O.PrimaryKey) def name = column[String]("name") def age = column[Int]("age") def * = (id, name, age) } val persons = TableQuery[Persons] persons.filter(_.age > 18).map(_.name) Slick
  4. final case class Person(id: UUID, name: String, age: Int) ctx.run(

    quote { query[Person].filter(p => p.age > 18).map(_.name) } ) Quill
  5. ?

  6. val persons = (uuid("id") ++ string("name") ++ int("age")).table("persons") val (id,

    name, age) = persons.columns select(name).from(persons).where(age > 18) ZIO SQL
  7. Typesafe queries? select count(id), customer_id from orders having count(id) >

    20 select(Count(id) ++ customerId) .from(orders) .having(Count(id) > 4)
  8. Typesafe queries? select count(id), customer_id from orders group by customer_id

    having count(id) > 20 select(Count(id) ++ customerId) .from(orders) .groupBy(customerId) .having(Count(id) > 4)
  9. Inserts insert into persons (id, age) values (1, 'Charles', 30)

    insertInto(persons)(id ++ age) .values((1, "Charles", 30))
  10. Inserts insert into persons (id, age) values (1, 'Charles', 30)

    insertInto(persons)(id ++ age) .values((1, "Charles", 30))
  11. Inserts insert into persons (id, age, name) values (1, 'Charles',

    30) insertInto(persons) (id ++ age ++ name) .values((1, "Charles", 30))
  12. Inserts insert into persons (id, age, name) values (1, 'Charles',

    30) insertInto(persons) (id ++ age ++ name) .values((1, "Charles", 30))
  13. Inserts insertInto(persons) (id ++ name ++ age) .values((1, "Charles", 30))

    insert into persons (id, name, age) values (1, 'Charles', 30)
  14. Inserts insertInto(persons) (id ++ name ++ age) .values( List( (1,

    "Charles", 30), (2, "Martin", 28), (3, "Harvey", 42))) insert into persons (id, age, name) values (1, 'Charles', 30), (2, 'Martin', 28), (3, 'Harvey', 42);
  15. Inserts case class Person(id: Int, name: String, age: Int) implicit

    schema = DeriveSchema.gen[Person] val friends : List[Person] = ??? insertInto(persons) (id ++ name ++ age) .values(friends) insert into persons (id, age, name) values (1, 'Charles', 30), (2, 'Martin', 28), (3, 'Harvey', 42);
  16. Joins select first_name, order_date from customers inner join orders on

    (customers.id = orders.customer_id) select(firstName ++ orderDate) .from(customers .join(orders) .on(id === customerId))
  17. Updates update customers set verified = false where first_name =

    'Ronald' update(customers) .set(verified, false) .where(name === "Ronald")
  18. Subqueries select first_name, last_name, (select count(orders.id) from orders where customers.id

    = orders.customer_id ) as "count" from customers val subquery = customers .subselect(Count(orderId)) .from(orders) .where(customerId === id) val query = select(fName ++ lName ++ (subquery as "Count")) .from(customers)
  19. 03. Hello World with ZIO SQL How to start writing

    web application that access relational data with power of scala and zio sql.
  20. Plans for the future Better error messages. Support user defined

    data types. Support more databases. Scala 3 support
  21. ZIO SQL Summary Type safety SQL-like DSL ZIO integration Connection,

    session, resource and transactional management & more
  22. RESOURCES Where to go from here? • https://github.com/zio/zio-sql Try it

    out today :) • https://zio.github.io/zio-sql/ Documentation • https://github.com/sviezypan/zio-sql-example Example app Reach out • Discord ◦ zio-sql ◦ jaro_regec