0

I`d like to use Exposed in Ktor. The simplest question is how can I get an Entity from Query options?

Such as these code:

/**
 * get when exist
 */
fun checkExist(phone: String): DbUser? {
    return transaction(db) {
        addLogger(StdOutSqlLogger)
        DbUser
            .select { DbUser.phoneNumber eq phone }
            .firstOrNull()
            .???  // how to converter ResultRow to Entity ?
    }
}

I want to check whether the user existing when login and get info when existing. But I can only get the reuslt with type ResultRow, How can I converter it to DbUser object?

I got a way in this Kotlin exposed DSL Query mapping but it is so clumsy and time wasting. How can I accomplish it easily?

1
  • Basically you need to write a mapper from the table row to your entity. You should create a class that extends the Table class and a separate data class that is to be used as DAO. At this moment there are no options to have this mapping be performed by exposed automatically. Commented Sep 26, 2022 at 8:34

1 Answer 1

5

Please check wiki article. In your case it can be resolved in two ways:

DbUser.select { DbUser.phoneNumber eq phone }.firstOrNull()?.let { Entity.wrapRow(it) }

If Entity is mapped to DbUser table:

Entity.find { DbUser.phoneNumber eq phone }.limit(1).firstOrNull()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.