ktorm icon indicating copy to clipboard operation
ktorm copied to clipboard

Add support for DTO Projections

Open raderio opened this issue 4 years ago • 6 comments

@Introspected
data class EmployeeDTO(
    val id: Long, 
    val name: String
)

val result = database.from(Employees).select(EmployeeDTO::class)

This will reduce the boilerplate code that can be deducted from table schema. @Introspected is needed to generate the projection on compile time.

https://www.jooq.org/doc/3.12/manual/sql-execution/fetching/pojos/ https://micronaut-projects.github.io/micronaut-data/latest/guide/#dto

raderio avatar Feb 15 '20 22:02 raderio

Thanks. It'll be good to have it.

vincentlauvlwj avatar Feb 18 '20 16:02 vincentlauvlwj

This is relevant to what I'm working on right now.

@raderio - do you see this as independent of the serialization library being used? I'm working on a project using ktor and Jackson, but this would seem to apply to kotlinx.serialization, GSON, and presumably other libraries.

lyndsysimon avatar Jul 27 '20 15:07 lyndsysimon

@lyndsysimon It will be nice to have a tool which works with different libraries, yes. At the moment I am looking to be possible to have this feature for database data retrieval.

raderio avatar Jul 28 '20 07:07 raderio

Hi Team, I am just wondering; what's wrong with simply Utilizing DTO objects like so?

fun projectsWhere(condition: ColumnDeclaring<Boolean>): List<ProjectDTO> {
        return db
            .from(Projects)
            .innerJoin(Locations, on = Projects.locationId eq Locations.id)
            .innerJoin(Companies, on = Projects.companyId eq Companies.id)
            .innerJoin(Estimators, on = Projects.estimatorId eq Estimators.id)
            .select(
                Projects.id,
                Projects.name,
                Locations.address,
                Projects.status,
                Projects.date,
                Companies.name,
                Estimators.name
            )
            .where(condition)
            .map { row ->
                ProjectDTO(
                    id = row[Projects.id],
                    title = row[Projects.name].orEmpty(),
                    address = row[Locations.address].orEmpty(),
                    company = row[Companies.name].orEmpty(),
                    estimator = row[Estimators.name].orEmpty(),
                    client = "",
                    status = row[Projects.status].orEmpty(),
                    date = row[Projects.date]
                )
            }
    }

It's verbose, but flexible at the same time. What kind of a DTO mapping would bring such flexibility to the table?

As a reference, spring jdbc template provides a BeanPropertyRowMapper class as a convenience. Is this the kind of thing we are looking for in KTORM?

Thanks! Great framework, btw!

martin-jamszolik avatar May 31 '21 17:05 martin-jamszolik

@lyndsysimon @raderio any new thoughts or directions on this topic?f Thanks

martin-jamszolik avatar Jul 20 '21 18:07 martin-jamszolik

遇到一样的问题, 写起来好痛苦, 有没有解决方案啊现在

egops avatar Jan 06 '22 10:01 egops