Using Kodein-DB NoSQL database in a Kotlin Multiplatform project03 Oct 2020 Share on:
A big factor in the increasing adoption of Kotlin Multiplatform is the ever growing number of KMP libraries that are becoming available. A new addition to this list, albeit in beta right now, is Kodein-DB, described as an “embedded NoSQL indexed document database for Android, iOS, and client JVM”. In this post I’m going to describe what was involved in adding initial use of Kodein-DB to BikeShare project.
After 3 years of active development, we are extremely excited to release the first beta version of Kodein-DB: a #Kotlin/Multiplatform embedded NoSQL indexed document database for Android, iOS & the JVM.https://t.co/gMKaUDteqJ— Kodein Koders (@KodeinKoders) September 24, 2020
Our model in this example is the following data class which was updated to implement
Metadata. Note that Kodein-DB is
compatible with Kotlinx Serialization library which we’re using already in this project in conjunction with Ktor for remote API requests.
Following code is used to create the database (more on
And then, when app starts up, the following code is used to retrieve data (using Ktor) from remote endpoint and then stores the list of
Network objects returned directly to the database.
We then have following method, called from UI code, that returns list of networks.
Note use of
getApplicationFilesDirectoryPath() when creating the db. This is an
expect/actual function where, as shown below,
actual platform specific implementations are provided for iOS and Android.
The following are the Kodein-DB library dependencies that we added to gradle build file for common KMP shared module (current version is 0.3.0).
And this is what I have right now for the iOS client (using SwiftUI) consuming that
fetchNetworkList() method above (that reads data from db).
And likewise on Android
There are a number of additional capabilities provided by this library that we haven’t made use of yet including more complex querying and adding listener for db changes….some examples of those in this article. The changes made so far have been pushed to this branch of BikeShare repo.
Featured in Kotlin Weekly Issue #218
Purpose of providing this is primarily to help “close the loop” and provide way to post feedback/ask questions etc.