Hildebrandt Tobias
04/24/2024, 9:35 AM1:n
relations with the DSL?
Major downside is that I cannot chain multiples of these if I happen to join two Tables and need two Lists filled 😕
fun <K, L, R> Query.mapWithList(
keySelector: (K),
listTransform: (ResultRow) -> L,
valueTransform: ResultRow.(List<L>) -> R
): List<R> =
groupBy { keySelector }
.map { (_, entries) ->
val list = entries.map { map -> ( listTransform(map)) }
entries.first().valueTransform(list)
}
usage:
CompanyTable
.innerJoin(CompanyRelationsTable)
.leftJoin(CompanyAliasTable)
.selectAll()
.where { CompanyRelationsTable.subId eq companyId.value }
.mapWithList(
keySelector = { CompanyTable.id },
listTransform = { CompanyAlias(it[CompanyAliasTable.alias]) },
valueTransform = { alias -> mapToCompany(alias) }
)