realm-kotlin icon indicating copy to clipboard operation
realm-kotlin copied to clipboard

Support default schema creation

Open nhachicha opened this issue 4 years ago • 4 comments

Currently, we have to pass in a class annotated with RealmModule which captures some or all the models that we would like to participate in the Realm scheam. This works fines for modules and library but adds an extra step to open a Realm of you want the default behaviour that consist of using all the annotated RealmObject classes

nhachicha avatar Nov 27 '20 09:11 nhachicha

Things we need to tackle:

  1. How to locate all classes implementing RealmObject? This should also work with incremental compilation steps.
  2. How to handle that model classes might be defined in multiple modules. Relying on autogenerated code can be problematic if multiple modules write to the same location.

Some background from Realm Java

  • The annotation processor there allows us to find all Realm model classes.
  • We write these model classes to a DefaultMediator class, which was looked up using reflection at runtime.
  • Libraries are required to create a class annotated with @RealmModule. This disables the generation of a DefaultMediator and also requires that people define a RealmConfiguration.modules(...) as a way to set the schema.

This approach means that app projects work out of the box, while library modules require extra work. So far, this seems to have been a sensible tradeoff, but it is unclear if the same would work for Multiplatform where Realm primarily is used within a library module.

cmelchior avatar Apr 23 '21 07:04 cmelchior

Also bear in mind the following compiler constraints expressed in https://github.com/realm/realm-kotlin/pull/156#issue-610868385

nhachicha avatar Apr 23 '21 11:04 nhachicha

Maybe some ideas for doing this can be found here: https://kotlinlang.slack.com/archives/C7L3JB43G/p1634316429144100?thread_ts=1634306190.142200&cid=C7L3JB43G

cmelchior avatar Mar 24 '22 09:03 cmelchior