{"id":779,"date":"2023-06-05T21:13:49","date_gmt":"2023-06-05T19:13:49","guid":{"rendered":"https:\/\/www.voldrich.net\/?p=779"},"modified":"2023-06-05T21:13:49","modified_gmt":"2023-06-05T19:13:49","slug":"myhome-crud","status":"publish","type":"post","link":"https:\/\/www.voldrich.net\/index.php\/2023\/06\/05\/myhome-crud\/","title":{"rendered":"MyHome Crud"},"content":{"rendered":"\n<p>Lets start with create\/update\/delete using good old fashioned WebMvc Spring, Kotlin and JOOQ.<\/p>\n\n\n\n<p>Following checkpoint has:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Gradle kts build script<\/li>\n\n\n\n<li>JOOQ code generation from DB<\/li>\n\n\n\n<li>SQL script to initialize DB<\/li>\n\n\n\n<li>Controllers and repositories to create\/update\/delete many entities.<\/li>\n\n\n\n<li>Some basic error handling<\/li>\n\n\n\n<li>Some extension functions.<\/li>\n\n\n\n<li>Open API auto-generated<\/li>\n<\/ul>\n\n\n\n<p><a href=\"https:\/\/github.com\/MavoCz\/myhome\/tree\/checkpoint-01-crud\">https:\/\/github.com\/MavoCz\/myhome\/tree\/checkpoint-01-crud<\/a><\/p>\n\n\n\n<p>Notes:<\/p>\n\n\n\n<p>To set up logging of JOOQ queries:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/MavoCz\/94e26e64ef147c2503006c0951e24ca9.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/MavoCz\/94e26e64ef147c2503006c0951e24ca9\">Gist<\/a>.<\/noscript><\/div>\n<\/div><\/figure>\n\n\n\n<p>Which gives you awesome insight into every SQL query and result:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/MavoCz\/11010bedeabe33d1e6e4586790b6ea8e.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/MavoCz\/11010bedeabe33d1e6e4586790b6ea8e\">Gist<\/a>.<\/noscript><\/div>\n<\/div><\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>It can automatically map Kotlin\/Java enums for you, but each needs to be configured. See lines 27.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/MavoCz\/b8ec3fe052d3ad7f44d4fa300bf3da2d.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/MavoCz\/b8ec3fe052d3ad7f44d4fa300bf3da2d\">Gist<\/a>.<\/noscript><\/div>\n<\/div><\/figure>\n\n\n\n<p>Kotlin gotcha: For some reason injection into super class did not work without open keyword for every property.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/MavoCz\/9acea326055a4b4253a9d45fe8bff47f.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/MavoCz\/9acea326055a4b4253a9d45fe8bff47f\">Gist<\/a>.<\/noscript><\/div>\n<\/div><\/figure>\n\n\n\n<p>Some notes to DB update code:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<div class=\"oembed-gist\"><script src=\"https:\/\/gist.github.com\/MavoCz\/018ad4c575eb964b650cbf4efa4c1b71.js\"><\/script><noscript>View the code on <a href=\"https:\/\/gist.github.com\/MavoCz\/018ad4c575eb964b650cbf4efa4c1b71\">Gist<\/a>.<\/noscript><\/div>\n<\/div><\/figure>\n\n\n\n<p>Don&#8217;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.<\/p>\n\n\n\n<p>Jooq Records are a nice way to hold\/manipulate\/transfer DB data to insert\/update\/result. <\/p>\n\n\n\n<p>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&#8217;t want to deal with those pesky DTOs, it can serialize data to JSON too. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets start with create\/update\/delete using good old fashioned WebMvc Spring, Kotlin and JOOQ. Following checkpoint has: 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-779","post","type-post","status-publish","format-standard","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/779","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/comments?post=779"}],"version-history":[{"count":2,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions"}],"predecessor-version":[{"id":781,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/posts\/779\/revisions\/781"}],"wp:attachment":[{"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/media?parent=779"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/categories?post=779"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.voldrich.net\/index.php\/wp-json\/wp\/v2\/tags?post=779"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}