https://kotlinlang.org logo
#ktor
Title
# ktor
h

Haruki

01/28/2023, 12:25 AM
Hello, I am currently working on a full-stack web app using Kotlin and following this tutorial: https://kotlinlang.org/docs/multiplatform-full-stack-app.html#include-kmongo-in-the-process. We have been using MongoDB and KMongo, but I am trying to replace MongoDB with Postgres DB and Kmongo with Exposed. As an example, using Kmongo, we have this code:
Copy code
val client = KMongo.createClient().coroutine
val database = client.getDatabase("shoppingList")
val collection = database.getCollection<ShoppingListItem>()
get {
    call.respond(collection.find().toList())
}
I am wondering if there is a way to do the same thing with Exposed. My current understanding is to create an object DatabaseFactory for setup in Server.kt, create a Table and then use select:
Copy code
object DatabaseFactory {
    fun init() {
        Database.connect(
            "jdbc:<postgresql://host>:port/5432",
            driver = "org.postgresql.Driver",
            user = "user",
            password = "password"
        )
    }
}
object ShoppingListItems: Table() {
    val id = integer("id").autoIncrement().primaryKey()
    val name = varchar("name", length = 255)
    val owners = varchar("owners", length = 255)
}
get {
    call.respond(ShoppingListItems.select { ShoppingListItems.owners like user }.toList())
}
I am also wondering if we need to create a table. Instead, I want to use a data class like this:
Copy code
val collection = database.getCollection<ShoppingListItem>.
Is this the correct approach for replacing MongoDB with Postgres and KMongo with Exposed? I have added the necessary dependencies for this change, and I would really appreciate some guidance and help with this. Thank you in advance for your help!
a

Aleksei Tirman [JB]

01/28/2023, 1:43 PM
I suggest reading comments from this issue.
2 Views