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

Kuba Petržílka

01/28/2021, 7:58 PM
Hi, why would one prefer Exposed over JOOQ? I hear some complains about lot of boilerplate code needed in case of Exposed. Can you sum up what are the key benefits of using Exposed?
e

Endre Deak

01/28/2021, 8:08 PM
I can only speak for myself, but my use case is that users can write “queries” against our system and I’m transforming those queries into SQL. I have a solution in place which I’m currently refactoring. I found Exposed very flexible and easily extendable when it came to some missing functions, and it works very well on how to represent different expressions in memory. I’m still in the middle of the work, and I never used jOOQ before (worked with Hibernate). In my current case, DB is already set up, persistence with Hibernate is set up, and I only needed a thing where I can efficiently work with SQL in memory and that’s where I found Exposed really handy. No annotations, no connection to the existing entities, I was simply able to rewrite my SQL schema with Exposed and then refer to those tables. But, honestly, if I’m about to start a brand new project, I’d just simply do Exposed because of the same things. Also, since it’s low level, you can build up your “kind-of-orm” (if you want). There are a few good articles out there about why not to use an ORM (which is a totally different topic) - I guess the main caveat is that you might need to write a few things for yourself. But again, it’s only my opinion, and I don’t have experience with JOOQ. But hope it still helps.
j

Joel

01/29/2021, 12:59 PM
@Kuba Petržílka your question sent me on a deep dive. Does jOOQ's active record (hello Rails) support associations? I think it might be a much thinner DAO than Exposed. The SQL DSLs look very similar, jOOQ's is more fleshed out because it's an older library. I'm sure Exposed will get there.
k

Kuba Petržílka

01/29/2021, 1:13 PM
@Joel I don't know much about JOOQ either, we have been using Hibernate so far. But the Exposed DSL seems better to me since JOOQ has only the Java's fluent notation, it is not a true Kotlin DSL. Also JOOQ forces you to generate the schema objects which I don't like..
j

Joel

01/29/2021, 1:16 PM
I’m sure you can define them manually, but that is one of the core features
I switched from Hibernate to Exposed and haven’t looked back since. I’ve learned a lot about Kotlin from reading the source code too.
j

Jabez Magomere

01/29/2021, 3:53 PM
@Joel how do you handle migrations ?
j

Joel

01/29/2021, 3:54 PM
Copy code
fun Collection<Table>.migrateAll() {
    logger.warn("Migrating tables '{}'", map(Table::tableName))
    logger.debug("ddl {}", map(Table::ddl).joinToString())
    SchemaUtils.createMissingTablesAndColumns(*toTypedArray())
}
It's not a migration per-say, it just updates all the tables to match the current definition
👍 1
j

Jabez Magomere

01/29/2021, 6:30 PM
@Joel i’m trying to wrap my head around the difference between
SchemaUtils.createMissingTablesAndColumns()
and db migrations using a tool like flyway, what effects do both have on a database, and what would work in a prod env?
j

Joel

01/29/2021, 8:19 PM
Flyway is a dedicated migration tool. You can use that w/ Exposed, just make sure the mappings are the same on both sides. I don't ever find myself doing complex migrations, just adding columns and tables. So
SchemaUtils.createMissingTablesAndColumns()
has worked just fine.
At the end of the day Exposed is a wrapper around SQL, so you can use all your favorite tools.
p

pserzh

02/07/2021, 8:24 PM
Can be useful for those who used hibernate https://github.com/TouK/krush
12 Views