-
-
Notifications
You must be signed in to change notification settings - Fork 144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom getters with Global Database object removed #73
Comments
If you find it's a problem passing the database object around, you can define it as a top-level variable: val globalDatabase = Database.connect(..) You can even write an extension function inline fun <E : Any, T : BaseTable<E>> T.getList(predicate: (T) -> ColumnDeclaring<Boolean>): List<E> {
return globalDatabase.sequenceOf(this).filter(predicate).toList()
} Then just like before: interface Department : Entity<Department> {
val id: Int
val name: String
val employees get() = Employees.getList { it.departmentId eq id }
} |
Unfortunately, the top level global variable would not work for me. On a related note, I was checking out Exposed code base to see how they are doing it without passing a global DB object around as well as supporting nested transactions, and looks like they are doing very similar to what you were doing (using a transaction block) before 2.7. Have you considered doing something similar or it wouldn't just work for Ktorm? Any thoughts? |
Before 2.7, Ktorm saved a global database object to
I know Exposed. I read its code before. Actually, the design of |
I found your code OzoneProvider. Maybe in this class you connect to the database. Just need a little change: lateinit var globalDatabase: Database
class OzoneProvider : ServiceProvider {
override fun register(app: Application) {
val dbConfig = app.config { DatabaseConfig(it.make()) }
if (dbConfig.canConnect()) {
globalDatabase = dbConfig.connect()
}
}
} Would it work now? |
I actually thought about doing that but I'd have to make it a lateinit var making it mutable from anywhere. Maybe I leave it like that or do something so that it can only be initialized once. |
inline fun <E : Any, T : BaseTable<E>> T.getList(predicate: (T) -> ColumnDeclaring<Boolean>): List<E> {
return globalDatabase.sequenceOf(this).filter(predicate).toList()
}
Yes, but then |
You can change the visibility of the setter, so that it can only be modified inside the kt file: lateinit var globalDatabase: Database
private set |
Yes, that's exactly why I decided to remove |
I've been trying Ktorm 2.7-RC that you uploaded after my request (thank you!).
In practice, I've found the new way of using Ktorm quite uncomfortable as now I've to pass around a database object. While this is doable, one place where I've been struggling with the new APIs is the custom setters. A while back you gave us an example of using a custom getter inside an entity interface:
Do you have an idea how to achieve this with the new API?
The text was updated successfully, but these errors were encountered: