Announcing a painless Kotlin/Multiplatform NoSQL embedded database

Salomon BRYS
Kodein Koders
Published in
3 min readSep 24, 2020

WARNING: This library has been deprecated. The article remains for reference, but it is not available anymore.

A little less than a year ago, at KotlinConf 2019, I spoke about a new approach to data storage targeted at client applications on mobile (Android & iOS) and personal computer JVMs.

There, I shared my frustration about SQL in general and SQLite in particular for embedded data storage.
Don't get me wrong, SQL is an amazing tool for server databases handling huge data sets and complicated shemas. But in an application where my data is fast evolving and where I favour agility over rigidity, I don't want to handle schema creation, object mapping, query mapping, and all the ceremony that usually comes with an SQL database.

So, here we go: Introducing a new Kotlin/Multiplatform embedded NoSQL indexed document database for Android, iOS, and client JVM:

Adding a document to your database in your Kotlin Multiplatform Mobile project is as simple as:

The database is schema-less. All you have to do is open it, and you can immediately start using it:

Kodein-DB is compatible with KotlinX Serialization for Multiplatform, and Kryo if you are only targeting Android and/or the JVM.

A lot of the new paradigms we are seeing emerging right now are about one way data flow, and single source of truth. Because Kodein-DB embeds an object cache as well as an event bus, you don't have to maintain an in-memory repository of duplicated models. Kodein-DB can be your single source of model truth:

Thanks to the object cache, only old documents are deserialized when requested to the database, which means that most documents will be only deserialized once (when accessed the first time) or not at all (because it was put earlier).

Let's answer some questions that you may be wondering:

  • Can I use Kodein-DB without Kodein-DI?
    Absolutely! Kodein-DI is a completely separate project. While we do anticipate later providing utilities for our "full framework" users, each library we provide can be used on its own. We do and will always work so that our libraries can be used at their best individually.
  • How is migration handled when my app inevitably evolves?
    Right now, migration depends heavily on the Serialization system you decided to use. Both KotlinX and Kryo are configured by default to allow structural change. Kodein-DB's TypeTable allows you to handle model name / package change. We anticipate a guide to migration as well as several utilities to be released in the near future.
  • What about performance ?
    I'm so glad you asked! Kodein-DB relies on Google LevelDB, a data storage engine used by a lot of high performance projects (such as Bitcoin-Core). Here is a very simple benchmark that compares Kodein-DB (without object cache) versus SQLite:
  • What if I need relations ?
    Because get operations are so fast, even without cache, handling relations manually is very fast. An added benefit is that you are actually managing them in Kotlin, which gives you all the power of the language (a more detailed post is coming about that!).

Kodein-DB is in Beta. You can access its documentation here and its source here.

We already use it in production in some of our applications. However we are very interested in feedback in any form. You can contact us on the Kodein Channel of the Kotlin Slack, or using Github Issues.

This article is brought to you by Kodein Koders, a European service & training provider focused on Kotlin & Kotlin/Multiplatform.

--

--