-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Added README.md
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# kotlinx.document.store | ||
|
||
A 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 documents. | ||
|
||
## Features | ||
|
||
- **Multiplatform**: | ||
- :done: JVM (MVStore, RocksDB) | ||
`org.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT` | ||
- :done: JS/Browser (IndexedDB idb-keyval) | ||
`org.github.lamba92:kotlinx-document-store-browser:1.0.0-SNAPSHOT` | ||
- :loading: macOS (RockDB) | ||
- :loading: iOS (RockDB) | ||
- :no: watchOs | ||
- :no: tvOs | ||
- :no: Linux | ||
- :no: Windows | ||
|
||
- **Simple** | ||
|
||
```kotlin | ||
// 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](https://github.com/Kotlin/kotlinx.serialization/blob/c75b46dee6216f600f2c94a0817f0f90fc8ed029/formats/json/commonMain/src/kotlinx/serialization/json/JsonElement.kt#L191) | ||
|
||
```kotlin | ||
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 | ||
|
||
```kotlin | ||
// 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("org.github.lamba92:kotlinx-document-store-core:1.0.0-SNAPSHOT") | ||
} | ||
} | ||
val jvmMain by getting { | ||
dependencies { | ||
implementation("org.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT") | ||
} | ||
} | ||
val jsMain by getting { | ||
dependencies { | ||
implementation("org.github.lamba92:kotlinx-document-store-browser:1.0.0-SNAPSHOT") | ||
} | ||
} | ||
} | ||
} | ||
|
||
// build.gradle.kts Kotlin/JVM | ||
dependencies { | ||
implementation("org.github.lamba92:kotlinx-document-store-mvstore:1.0.0-SNAPSHOT") | ||
} | ||
``` |