realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
906 stars 52 forks source link

Type safe queries #982

Open sync-by-unito[bot] opened 1 year ago

sync-by-unito[bot] commented 1 year ago

The Kotlin SDK currently supports string-based queries which are standard RQL (Realm Query Language) queries.

This has the advantage over the query system in Realm Java, that it is much more expressive and automatically picks up new features as they are implemented in Core.

The downside is that the query syntax is largely unsafe, and there is no IDE support or guidance outside linking to the docs.

There are a number of ways to fix this:

Custom Language Plugin IntelliJ supports a feature called Custom Language Plugin https://plugins.jetbrains.com/docs/intellij/custom-language-support-tutorial.html which is a way to tell the IDE that String arguments should actually be treated as a separate language.

Using this approach, it would be possible to keep the string-based syntax, but when writing the code, you would be able to get autocomplete and syntax help.

We have already experimented with this approach and validated it is possible, albeit time-consuming to do right, and it would only work within the IntelliJ ecosystem.

DSL The Kotlin Language has rather nice support for custom DSLs, and it is possible to reference class properties in a type-safe way, but it is unclear if we can express the more advanced query features with a DSL. This approach has not really been investigated yet.

vonox7 commented 1 year ago

Have a look here what a typesafe query language with a custom Kotlin DSL for MongoDB may look like. I guess for realm it can be archived with similar patterns: https://github.com/studoverse/katerbase

cmelchior commented 1 year ago

@vonox7 Interesting. Thanks for the pointer.