From b3ab7a43c33a6a10ad112fd539e627bfec69d9c5 Mon Sep 17 00:00:00 2001 From: ShaggyDemiurge Date: Thu, 20 Oct 2022 02:15:17 +0700 Subject: [PATCH] Add count() method support (#72) --- core/src/jsMain/kotlin/Index.kt | 3 +++ core/src/jsMain/kotlin/ObjectStore.kt | 3 +++ core/src/jsMain/kotlin/Queryable.kt | 1 + core/src/jsMain/kotlin/Transaction.kt | 10 ++++++++++ core/src/jsTest/kotlin/Samples.kt | 10 ++++++++++ 5 files changed, 27 insertions(+) diff --git a/core/src/jsMain/kotlin/Index.kt b/core/src/jsMain/kotlin/Index.kt index 26661d5..3579cd1 100644 --- a/core/src/jsMain/kotlin/Index.kt +++ b/core/src/jsMain/kotlin/Index.kt @@ -18,4 +18,7 @@ public class Index internal constructor( override fun requestOpenKeyCursor(query: Key?, direction: Cursor.Direction): Request = Request(index.openKeyCursor(query?.toJs(), direction.constant)) + + override fun requestCount(query: Key?): Request = + Request(index.count(query?.toJs())) } diff --git a/core/src/jsMain/kotlin/ObjectStore.kt b/core/src/jsMain/kotlin/ObjectStore.kt index c606537..0fb5aa3 100644 --- a/core/src/jsMain/kotlin/ObjectStore.kt +++ b/core/src/jsMain/kotlin/ObjectStore.kt @@ -18,4 +18,7 @@ public class ObjectStore internal constructor( override fun requestOpenKeyCursor(query: Key?, direction: Cursor.Direction): Request = Request(objectStore.openKeyCursor(query?.toJs(), direction.constant)) + + override fun requestCount(query: Key?): Request = + Request(objectStore.count(query?.toJs())) } diff --git a/core/src/jsMain/kotlin/Queryable.kt b/core/src/jsMain/kotlin/Queryable.kt index d6ea731..5130bb1 100644 --- a/core/src/jsMain/kotlin/Queryable.kt +++ b/core/src/jsMain/kotlin/Queryable.kt @@ -8,4 +8,5 @@ public sealed class Queryable { internal abstract fun requestGetAll(query: Key?): Request> internal abstract fun requestOpenCursor(query: Key?, direction: Cursor.Direction): Request internal abstract fun requestOpenKeyCursor(query: Key?, direction: Cursor.Direction): Request + internal abstract fun requestCount(query: Key?): Request } diff --git a/core/src/jsMain/kotlin/Transaction.kt b/core/src/jsMain/kotlin/Transaction.kt index 4508f0c..578f788 100644 --- a/core/src/jsMain/kotlin/Transaction.kt +++ b/core/src/jsMain/kotlin/Transaction.kt @@ -97,6 +97,16 @@ public open class Transaction internal constructor( } } + public suspend fun Queryable.count(query: Key? = null): Int { + val request = requestCount(query).request + return request.onNextEvent("success", "error") { event -> + when (event.type) { + "error" -> throw ErrorEventException(event) + else -> request.result + } + } + } + public fun ObjectStore.index(name: String): Index = Index(objectStore.index(name)) } diff --git a/core/src/jsTest/kotlin/Samples.kt b/core/src/jsTest/kotlin/Samples.kt index 7f40d22..b098e00 100644 --- a/core/src/jsTest/kotlin/Samples.kt +++ b/core/src/jsTest/kotlin/Samples.kt @@ -58,5 +58,15 @@ class Samples { .first { it.age < 32 } } assertEquals("Charlie", charlie.name) + + val count = database.transaction("customers") { + objectStore("customers").count() + } + assertEquals(4, count) + + val countBelowThirtyTwo = database.transaction("customers") { + objectStore("customers").index("age").count(upperBound(32)) + } + assertEquals(2, countBelowThirtyTwo) } }