https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
v

Vishnu Shrikar

11/04/2023, 5:46 AM
Looking for a Hierarchical Data Store that can save locally to desktop and android. Something like how sqlite works on all three systems but hierarchical. OFC a KMP lib for this datastore would also be nice...
👀 1
j

jw

11/04/2023, 12:14 PM
How about the filesystem?
v

Vishnu Shrikar

11/04/2023, 7:24 PM
I need to store database oriented data.
e

efemoney

11/05/2023, 5:01 PM
Yeah and you can do that in any file format you want … on the file system
v

Vishnu Shrikar

11/05/2023, 5:04 PM
Right but that does not give me a lot of the features databases have like transactions. This is like saying you can use the file system to store SQL data. It's technically true but not viable for many reasons. I am still storing text but I need it to be stored as a hierarchy. I think object box will do what I want
👍🏾 1
👍 1
j

Jeff Lockhart

11/06/2023, 3:59 AM
ObjectBox doesn't support KMP. If a NoSQL database solves your hierarchical data store requirements, you might check out my KMP NoSQL database library, Kotbase.
v

Vishnu Shrikar

11/06/2023, 4:02 AM
Thats a good point, ill check out kotbase, ObjectBox does support all my platforms but I would need to expect/actual it so a KMP native solution would be better
j

Jeff Lockhart

11/06/2023, 4:07 AM
Which platforms are you targeting? (You mention Android and desktop. Is that JVM or native desktop?)
v

Vishnu Shrikar

11/06/2023, 4:10 AM
JVM and Android and I expect MSI with bundled JVM to work as well (it should be a given but some bad libs like KorAU disagree)
Another question, I am looking through this Kotbase library and it seems to do what I want (I guess i can throw away my ER diagram), do I assume that I use JSON for POJO
j

Jeff Lockhart

11/06/2023, 5:22 AM
Kotbase's JVM platform support will mirror Couchbase Lite's (Windows/macOS/Linux). I would expect a MSI with bundled JVM should work, but haven't tested this use case. The JSON document schema is enforced by your application code. It can be as flexible or strict as your code makes it. It's common to map JSON documents to POJOs using something like kotlinx-serialization, for example, or using the KTX extensions. You can work with the JSON document object directly as well, for example, or using the fragment subscript APIs.
v

Vishnu Shrikar

11/06/2023, 5:29 AM
Ok that is good to know thank you. I just wanted to make sure I was not missing out on something. There are so many useful libraries everywhere, blink and you will miss them. I now have a new diagram 🙂
j

Jeff Lockhart

11/06/2023, 5:38 AM
Great, let me know if you have any feedback. I use the library for Android and iOS, myself. Always interested in seeing other use cases.
1
v

Vishnu Shrikar

11/09/2023, 3:29 AM
Hi I just started using this library and when I tried to create a database it did not function, calling the CouchbaseLite.init() function seems to fix it but I am wondering what initialization is best practice here
j

Jeff Lockhart

11/09/2023, 3:37 AM
Calling
CouchbaseLite.init()
should be optional in Kotbase, as it is automatically initialized under the hood, here on JVM and here on Android. Other platforms don't require init. You can call the
init
function with arguments if needed, but the API is only available in JVM and Android platform code. If you can, share the code you're finding isn't working for you.
Also, be sure you're using the
kotbase
package. If you're using the
com.couchbase.lite
package directly (which is only accessible in JVM or Android platform code), then it will throw an exception for not initializing the library.
v

Vishnu Shrikar

11/09/2023, 3:54 AM
Copy code
package com.mypackage.name.database

import com.couchbase.lite.CouchbaseLite
import com.couchbase.lite.Database
import inject



class Database {

    init {
        CouchbaseLite.init()
    }
    private val database: Database by inject()


    val p = database.name

}
Copy code
inline fun <reified T> inject() = org.koin.java.KoinJavaComponent.inject<T>(T::class.java)
Copy code
import com.couchbase.lite.Database

object InjectableModules {

    private const val DATBASE_NAME = "appDb"

    fun module() = org.koin.dsl.module {
        single<Database> {
            Database(DATBASE_NAME)
        }

        single<com.mypackage.name.database.Database> {
            com.mypackage.name.database.Database()
        }
    }
}
Above is the code I use for injecting the Database
j

Jeff Lockhart

11/09/2023, 3:58 AM
Ok, yes, it's the package. Change
import com.couchbase.lite.Database
to
import kotbase.Database
. Then you can remove the init call. Be sure to use the
kotbase
package instead of
com.couchbase.lite
, which is the underlying Couchbase Lite dependency. Typically if you have a native target, you won't even see this package from common code.
v

Vishnu Shrikar

11/09/2023, 6:25 AM
yes that seems to be it
👍 1
Is there a way to create a unique field? I need to have a document where a field must be uniuqe and it is not the id
j

Jeff Lockhart

11/10/2023, 5:24 AM
There's no unique constraint. But you could use a lookup document as described here. I'm not sure why he's escaping some characters in the ID though, since all UTF-8 characters are allowed in document IDs, so that shouldn't be necessary.
3 Views