Skip to content

A Kotlin Multiplatform embedded nosql document database

Notifications You must be signed in to change notification settings

lamba92/kotlinx.document.store

Repository files navigation

Kotlin Multiplatform Embedded NoSql Document Database

Kotlinx.document.store is an abstraction on top of platform-specific key-value stores like RocksDB, MVStore, etc. It provides a simple API to store and retrieve JSON documents.

Features

  • Multiplatform:

    • ✔️ JVM (MVStore, RocksDB)
      • com.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT
      • com.github.lamba92:kotlinx-document-store-rocksdb:1.0.0-SNAPSHOT
    • ✔️ JS/Browser (IndexedDB idb-keyval)
      • com.github.lamba92:kotlinx-document-store-browser:1.0.0-SNAPSHOT
    • ⌛ macOS (RockDB)
      • com.github.lamba92:kotlinx-document-store-rocksdb:1.0.0-SNAPSHOT
    • ⌛ iOS (RockDB)
      • com.github.lamba92:kotlinx-document-store-rocksdb:1.0.0-SNAPSHOT
    • ❌ watchOs
    • ❌ tvOs
    • ❌ Linux
    • ❌ Windows
  • Simple

// JVM
val dataStore: DataStore = MVStore.open("test.db").asDataStore()

// JS
val datataStore = IndexedDBStore

// common
val db = KotlinxDocumentDatabase {
    store = dataStore
}

@Serializable // kotlinx.serialization
data class User(val name: String, val age: Int)

val usersCollection: ObjectCollection<User> = db.getObjectCollection<User>("users")

usersCollection.createIndex("name")

val jhon = User("John", 30)

usersCollection.insert(jhon)

val aJhon: User = usersCollection.find("name", "John") // Flow<User>
  .filter { it.age > 20 }
  .first()

Schemaless

While one can work with typed objects, kotlinx.document.store also supports schemaless documents relying on kotlinx.serialization for serialization JsonObject

val db = KotlinxDocumentDatabase {
    store = MVStore.open("test.db").asDataStore()
}

val jsonCollection: JsonCollection = db.getJsonCollection("users")

val jhon: JsonObject = jsonCollection.find("name", "John") // Flow<JsonObject>
  .filter { it["age"].jsonPrimitive.int > 20 }
  .first()

jsonCollection.insert(
  buildJsonObject {
    put("surname", "117")
    put("age", 30)
  }
)

Of course deserialization into object matters! Don't forget to update your data classes accordingly if you also access the same collection with typed objects.

Installation

// settings.gradle.kts
dependecyResolutionManagement {
    repositories {
        maven("https://packages.jetbrains.team/maven/p/kpm/public")
    }
}

// build.gradle.kts Kotlin/Multiplatform
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation("com.github.lamba92:kotlinx-document-store-core:1.0.0-SNAPSHOT")
            }
        }
        val jvmMain by getting {
            dependencies {
                implementation("com.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT")
            }
        }
        val jsMain by getting {
            dependencies {
                implementation("com.github.lamba92:kotlinx-document-store-browser:1.0.0-SNAPSHOT")
            }
        }
    }
}

// build.gradle.kts Kotlin/JVM
dependencies {
    implementation("com.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT")
}

We also publish the version catalog for kotlinx.document.store, so you can use it in your project:

// settings.gradle.kts
dependecyResolutionManagement {
    repositories {
        maven("https://packages.jetbrains.team/maven/p/kpm/public")
    }
    versionCatalogs {
        create("kotlinxDocumentStore") { // you can name it as you like, it will change the name of the variable
            from("com.github.lamba92:kotlinx-document-store-version-catalog:1.0.0-SNAPSHOT")
        }
    }
}

// build.gradle.kts Kotlin/Multiplatform
kotlin {
    sourceSets {
        val commonMain by getting {
            dependencies {
                implementation(kotlinxDocumentStore.core)
            }
        }
        val jvmMain by getting {
            dependencies {
              implementation(kotlinxDocumentStore.mvstore)
            }
        }
        val jsMain by getting {
            dependencies {
                implementation(kotlinxDocumentStore.browser)
            }
        }
    }
}

// build.gradle.kts Kotlin/JVM
dependencies {
    implementation(kotlinxDocumentStore.mvstore)
}