Databases is a huge pain. You need to validate the data, transfer to a database object, insert to the database, probably with a SQL query, select from the database with another SQL-query, handle SQL-exceptions etc. But why do we need this? Why can’t I just use a simple list, like repository.users.add(user)
when I save a user and repository.users.filter {it.id == userId}
when I select a user?
Answer: Because the users are just saved in memory, and not permanently to a disk. That means if I start a new instance of the application, the list would go back to its default value. But it doesn’t need to be like that. Couldn’t there be an annotation that says "save the value of this variable (or constant) to the disk?
Example:
class Repository {
@SaveToDisk("users")
val users: MutableList<User> = mutableListOf()
}
It’s exactly like a normal list, just that it’s save to a disk rather than the memory.
The list would potentially have millions of rows, but that’s not a problem with today’s computation speed. This “query”, for example, took 1.5 seconds on 100 million rows
repository.users
.filter { it.firstname == "bob" && it.age > 90 }
.sortedBy { it.lastname }
data class User(val firstname: String, val lastname: String, val age: Int)
What do you think? Shall we just get rid of databases and SQL injections?