Sample service using kotlin on backend and other fancy things.
- Javalin as service controller
- Ktorm as ORM and query builder
- HikariCP for database pooling and connection
- Liquibase for database migrations
Project requires Java 11 or newer to run
It uses gradle as build tool, so:
gradle run
will build the project and run it.
You also can open the project on Intellij Ultimate, everything will feel like home :-)
Liquibase and HikariCP aren't kotlin-native libraries. However, one of biggest kotlin advantages is to be able to use everything java ecosystem has to offer.
Unlike other fancy migration tools, Liquibase does not offer a tool to create migrate templates. I covered it in this article about what liquibase can do.
jackson had a difficult time serializing ktorm proxies when I used the
recommended strategy for object-relational mapping. I've left Media
mapping
as an example of what ktorm recommends.
However, javalin endpoint keeps throwing exceptions and no research produced a
solution. This configuration
must be made in order to make jackson understand how to deal with ktorm proxies.
Except when I used the alternative mapping method offered by ktorm:
package beer.store.models
import org.ktorm.dsl.QueryRowSet
import org.ktorm.schema.*
import java.time.LocalDateTime
// https://www.ktorm.org/en/entities-and-column-binding.html
// https://www.ktorm.org/en/define-entities-as-any-kind-of-classes.html
data class Beer(
var idBeer: Int?,
var creationDateBeer: LocalDateTime?,
var titleBeer: String?,
var descriptionBeer: String?,
var idMedia: Int?,
)
object Beers : BaseTable<Beer>("beer") {
var idBeer = int("idbeer").primaryKey()
var creationDateBeer = datetime("creationdatebeer")
var titleBeer = varchar("titlebeer")
var descriptionBeer = varchar("descriptionbeer")
var idMedia = int("idmedia")
override fun doCreateEntity(
row: QueryRowSet,
withReferences: Boolean
) = Beer(
idBeer = row[idBeer],
creationDateBeer = row[creationDateBeer],
titleBeer = row[titleBeer],
descriptionBeer = row[descriptionBeer],
idMedia = row[idMedia]
)
}
Mapping got way more verbose, but it doesn't use dynamic proxies anymore and then jackson started to work again.
So far the least pleasant ORM framework is ktorm.
Kotlin/Javalin/Liquibase? Sure!