Categories
Development

MyHome Crud

Lets start with create/update/delete using good old fashioned WebMvc Spring, Kotlin and JOOQ.

Following checkpoint has:

  • Gradle kts build script
  • JOOQ code generation from DB
  • SQL script to initialize DB
  • Controllers and repositories to create/update/delete many entities.
  • Some basic error handling
  • Some extension functions.
  • Open API auto-generated

https://github.com/MavoCz/myhome/tree/checkpoint-01-crud

Notes:

To set up logging of JOOQ queries:

Which gives you awesome insight into every SQL query and result:

Code generation of JOOQ works well, but its a bit hard to set up. The configuration below scans a database and creates JOOQ table definitions and records.

It can automatically map Kotlin/Java enums for you, but each needs to be configured. See lines 27.

Kotlin gotcha: For some reason injection into super class did not work without open keyword for every property.

Some notes to DB update code:

Don’t forget to call execute at the end. updatedRecord is R : Record, where record is org.jooq.Record. Idea keeps importing java.lang.Record which is quite annoying. updatedRecord creates a minimal set or changes, so if it contains only one changed field, it will have only one field in final SQL. Nice.

Jooq Records are a nice way to hold/manipulate/transfer DB data to insert/update/result.

It can be mapped nicely to and from a DTO/data class, generator is able to create on per entity record class with convenience methods named after columns. And if you don’t want to deal with those pesky DTOs, it can serialize data to JSON too.