kotlin-orm / ktorm

A lightweight ORM framework for Kotlin with strong-typed SQL DSL and sequence APIs.
https://www.ktorm.org
Apache License 2.0
2.13k stars 147 forks source link

Add support for DTO Projections #100

Open raderio opened 4 years ago

raderio commented 4 years ago
@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

vincentlauvlwj commented 4 years ago

Thanks. It'll be good to have it.

lyndsysimon commented 4 years ago

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.

raderio commented 4 years ago

@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.

martin-jamszolik commented 3 years ago

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 commented 3 years ago

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

egops commented 2 years ago

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