Skip to content

Commit

Permalink
Enhance find function in ObjectCollection class
Browse files Browse the repository at this point in the history
The find() function in the ObjectCollection class has been updated to take a value of any type, along with its serializer. A corresponding inline function is also created to accommodate inferred type parameter passing. This improvement provides more flexibility when querying data.
  • Loading branch information
lamba92 committed Jul 23, 2024
1 parent eccb908 commit 8e497fa
Showing 1 changed file with 11 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.serializer

public class ObjectCollection<T : Any>(
private val serializer: KSerializer<T>,
public val jsonCollection: JsonCollection,
) : KotlinxDatabaseCollection by jsonCollection {
public suspend fun find(

public suspend fun <K> find(
selector: String,
value: JsonPrimitive,
value: K,
valueSerializer: KSerializer<K>
): Flow<T> =
jsonCollection.find(selector, value)
jsonCollection.find(selector, json.encodeToJsonElement(valueSerializer, value))
.map { json.decodeFromJsonElement(serializer, it) }

public suspend fun insert(value: T): T {
Expand Down Expand Up @@ -83,3 +86,8 @@ public class ObjectCollection<T : Any>(
update = json.encodeToJsonElement(serializer, update).jsonObject,
)
}

public suspend inline fun <reified K, T : Any> ObjectCollection<T>.find(
selector: String,
value: K,
): Flow<T> = find(selector, value, json.serializersModule.serializer<K>())

0 comments on commit 8e497fa

Please sign in to comment.