Hey, is there a better way than this to implement ...
# exposed
h
Hey, is there a better way than this to implement
1: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 😕
Copy code
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:
Copy code
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) }
            )