kabukiza.tech 1 lt - scalikejdbc-async & skinny framework #kbkz_tech
DESCRIPTION
- ScalikeJDBC & ScalikeJDBC Async introduction - Skinny Framework introductionTRANSCRIPT
![Page 1: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/1.jpg)
JDBC-like non-blocking DB Access in Scala@seratchKazuhiro Sera Kabukiza.tech
is a workshop which is presented by Dwangoat Kabukiza tower
![Page 2: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/2.jpg)
ScalikeJDBC
- DB Access library in Scala- “Never stuck when using at work“- SQL, flexibility, expandability, speedy bug fixes and releases- Cool enough though it’s less well-known than Slick or Squeryl.
![Page 3: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/3.jpg)
DSL Example
import scalikejdbc._, SQLInterpolation._ val memberId = 123 val member = DB readOnly { implicit session => withSQL { select.from(Member as m) .where.eq(m.id, memberId).and.isNull(m.deletedAt) }.map(Member(m)).single.apply() }
![Page 4: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/4.jpg)
SQL Interpolation
import scalikejdbc._, SQLInterpolation._ val memberId = 123 val member = DB readOnly { implicit session => sql”””select id, name from member where id = ${memberId} and deleted_at is null””” .map(Member(m)).single.apply() }
![Page 5: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/5.jpg)
Get things done
- Easy to understand, few pitfalls- No need to google framework specific knowledges- Easy to find the first developer who use it correctly (required background is only Scala basics and SQL)
![Page 6: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/6.jpg)
Productive- Mostly type-safe DSL- Source code generator from existing tables via sbt plugin- Logging slow queries, it’s also possible to send data to external services (e.g. Fluentd)- AutoRollback testing support for specs2、ScalaTest
![Page 7: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/7.jpg)
ScalikeJDBC-Async
- ScalikeJDBC compatible API, but it doesn’t use JDBC internally- Inspired by Activate ‘s async API support (2013/7)- Though some introduction examples are already reported, this library is still in the alpha stage.
![Page 8: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/8.jpg)
How?- Just wrapped postgresql-async/mysql-async by @mauricio- Netty based database driver which is not compatible with JDBC- ConnectionPool with queue- Transaction = begin/commit in a non-shared connection
![Page 9: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/9.jpg)
Mostly same!
import scalikejdbc._, SQLInterpolation._, async._ val memberId = 123 val member = AsyncDB withPool { implicit session => withSQL { select.from(Member as m) .where.eq(m.id, memberId).and.isNull(m.deletedAt) }.map(Member(m)).single.future() }
![Page 10: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/10.jpg)
Transactions with for comprehensions
val name = “Typesafe” val programmers = Seq(“Alice”, “Bob”, “Chris”) val resultFuture = AsyncDB localTx { implicit tx => for { company <- Company.create(name) employees <- company.hireAll(programmers) } yield () }
![Page 11: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/11.jpg)
Tips
- Since you need to work with many Future[Option[_]] and Future[List[_]], for comprehensions are suitable- Play2’s Action can receive Future values, pretty good chemistry
![Page 12: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/12.jpg)
One more thing
This is a Lightning talk, but I have “One more thing”.
![Page 13: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/13.jpg)
Really Must?Indeed, some kinds of applications need non-
blocking DB access.However, many smaller
applications actually exist.
![Page 14: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/14.jpg)
Most cases?
- “Our application is built with Servlet & JDBC. No problem.”- “C10K? It’s not my problem.”- “Just need simple admin CRUD app.”- “Good at Java, but Java in 2013??”
![Page 15: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/15.jpg)
Rails? Reasonable choice for front end apps.
I myself am working with API servers in Java and Rails front end at the office.
Oops, most of you here would like to write Scala apps, right?
![Page 16: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/16.jpg)
Grails?
If you’re not a Groovy lover...Oops, most of you here prefer Scala than
Groovy, right?
![Page 17: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/17.jpg)
Play2? I understand Play2 makes Reactive/real-
time app development productive.Indeed, looks like Rails app but Play2 is
neither new Rails nor new Play1.
![Page 18: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/18.jpg)
Skinny!Small & full-stack web app framework in
Scala is still absent. So I just started
developing Scala on Rails which is named “Skinny Framework”!
Built with Scalatra + ScalikeJDBC + more, API is highly inspired by Rails.
http://git.io/skinny
![Page 19: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/19.jpg)
ORM Example case class Developer(id: Long, groupId: Opion[Long], group: Option[Group] = None, skills: Seq[Skill] = Nil) object Developer extends SkinnyCRUDMapper[Developer] { belongsTo[Group](Group, (d, g) => d.copy(group = g)).byDefault hasManyThrough[Skill](DeveloperSkill, Skill, (d, skills) => d.coply(skills = skills)) def extract ... }
Developer.findAll() Developer.findById(123) Developer.createWithAttributes(params.permit(“id”, “groupId”)) Developer.updateById(123).withAttributes(attrs) Developer.deleteById(123)
![Page 20: Kabukiza.tech 1 LT - ScalikeJDBC-Async & Skinny Framework #kbkz_tech](https://reader034.vdocuments.us/reader034/viewer/2022051323/54b6c30a4a7959d7268b4609/html5/thumbnails/20.jpg)
Thanks
- There is no async JDBC- ScalikeJDBC introduction- ScalikeJDBC-Async introduction- Skinny Framework introduction