https://kotlinlang.org logo
#exposed
Title
# exposed
r

Rasheed Sulayman

03/16/2020, 7:39 PM
Hello, Is there a way to get use store and retrieve
data
classes directly with exposed, something like:
Copy code
object Users : Table() {}

// Get list of users
val usersList: List<User> = Users.select { Users.city.eq("a city") }

//Insert users 

val allUsers: List<User> = //list of users 
val aUser: User = //User instance

Users.insertAll(allUsers) // Inserts everything in to the user table
Users.insert(aUser) // Inserts a single user in to the table.
i.e Something that automatically maps/converts rows to data classes when reading stuff, and vice versa when inserting.
c

codec

03/17/2020, 3:06 AM
depending on the fields your user data class contains.. it could look something like this:
Copy code
return transaction(db) {
            UsersTable.select { UsersTable.email.eq(email) }
                .map { User(it[UsersTable.id], email, it[UsersTable.first_name], it[UsersTable.last_name], it[UsersTable.password]) }
                .singleOrNull()
        }
👍 1
notice the call to .map { } after the .select { } completion block…
t

tjohnn

03/17/2020, 7:12 AM
I usually have extension functions that convert db record to data classes, do it manually for insertion
👍 3
t

tapac

03/17/2020, 7:18 AM
r

Rasheed Sulayman

03/17/2020, 11:27 AM
Thanks for the input @codec and @tjohnn, I was looking for something to automatically do this:
Copy code
.map { User(it[UsersTable.id], email, it[UsersTable.first_name], it[UsersTable.last_name], it[UsersTable.password]) }
without having to write it manually... Imagine if you have objects/data classes with 30+ fields (i.e 30+ table columns).
👍 1
Copy code
There is <https://github.com/TouK/krush> and also you could vote and suggest for
<https://github.com/JetBrains/Exposed/issues/24#issuecomment-566725685>
Thanks @tapac, this sounds like what i'm looking for.
3 Views