vicpinm / Kotlin-Realm-Extensions

Kotlin extensions to simplify Realm API.
Apache License 2.0
535 stars 53 forks source link

Model migrations DSL (using inline functions) #17

Closed filipproch closed 7 years ago

filipproch commented 7 years ago

Hi, your library seems very interesting, I have been thinking about making the migration definitions more readable. Right now, I see as the best option to provide inline functions so you can write them like Anko does for layouts. What do you think? Once we agree on a way to add it, I will send a PR

Example:

val migration = realmMigration {
    migrate(fromVersion = 1) {
        newModel<MyModel>() {
            field<String>(name = "id") {
                primaryKey = true
            }
            field<Int>(name = "number")
        }
    }

    ...
}
vicpinm commented 7 years ago

Hi @filipproch, I think your approach is very interesting. I'm not very keen on realm migrations, and it would be great to have a simpler way to write them. One thing I really hate is that, as far as I know, if you add a new model to an existing realm module, you have to specify the new model field by field in your migration file. It would be great to automate this process with reflection, but I think this topic should be discussed in the oficial realm github account. At the moment, I think that a DSL model would be great, so if you send a PR, I will be pleased to merge it into master.

filipproch commented 7 years ago

@vicpinm ok, regarding the automatic generation, yes, that's pain. I was thinking the next step would be using KotlinPoet and Annotation Processing to write those migration files for you, using this DSL. It would be enabled by annotating your Realm model class with a specific annotation and because it would be using this DSL, if you wanted to see the generated code, it would be very readable too.

vicpinm commented 7 years ago

@filipproch I have found and active discussion about automatic migration on this post: https://github.com/realm/realm-java/issues/2530 . In summary, they don't have any short-term plans to implement this, at least for updating existing entities. But they seem to have some work in progress with automatic creation of new entities.