Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for transaction rollbacks #178

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions core/src/jsMain/kotlin/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class Database internal constructor(
ensureDatabase().transaction(arrayOf(*store), "readonly", transactionOptions(durability)),
)
val result = transaction.action()
transaction.commit()
transaction.awaitCompletion()
result
}
Expand All @@ -107,9 +108,16 @@ public class Database internal constructor(
.openKeyCursor(autoContinue = false)
.collect { it.close() }
}
val result = transaction.action()
transaction.awaitCompletion()
result
try {
val result = transaction.action()
transaction.commit()
transaction.awaitCompletion()
result
} catch (e: Throwable) {
transaction.abort()
transaction.awaitFailure()
throw e
}
}

public fun close() {
Expand Down
25 changes: 24 additions & 1 deletion core/src/jsMain/kotlin/Transaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import org.w3c.dom.events.Event
import kotlin.js.Json

public open class Transaction internal constructor(
internal val transaction: IDBTransaction,
Expand All @@ -23,6 +24,26 @@ public open class Transaction internal constructor(
}
}

internal suspend fun awaitFailure() {
transaction.onNextEvent("complete", "abort", "error") { event ->
when (event.type) {
"abort" -> Unit
"error" -> Unit
else -> Unit
}
}
}
internal fun abort() {
transaction.abort()
}

internal fun commit() {
// Check if function exists before calling it.
Copy link
Contributor Author

@jacek-marchwicki jacek-marchwicki Nov 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: commit was added later to browser, so it might not be always available

https://developer.mozilla.org/en-US/docs/Web/API/IDBTransaction#browser_compatibility

note: Also from the commit documentation we can read:

For an active transaction, commits the transaction. Note that this doesn't normally have to be called — a transaction will automatically commit when all outstanding requests have been satisfied and no new requests have been made. commit() can be used to start the commit process without waiting for events from outstanding requests to be dispatched.

So calling it is optional.

if (jsTypeOf(transaction.asDynamic().commit) === "function") {
transaction.commit()
}
}

public fun objectStore(name: String): ObjectStore =
ObjectStore(transaction.objectStore(name))

Expand Down Expand Up @@ -132,6 +153,7 @@ public open class Transaction internal constructor(
): Flow<T> = callbackFlow {
var cursorStartAction = cursorStart
val request = open(query, direction).request
var finished = false
val onSuccess: (Event) -> Unit = { event ->
@Suppress("UNCHECKED_CAST")
val cursor = (event.target as IDBRequest<U?>).result
Expand All @@ -141,7 +163,7 @@ public open class Transaction internal constructor(
} else if (cursor != null) {
val result = trySend(wrap(cursor, channel))
when {
result.isSuccess -> if (autoContinue) cursor.`continue`()
result.isSuccess -> if (autoContinue && !finished) cursor.`continue`()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info: I've added this check, so continue() is not called after flow was closed.

Otherwise in the following code:

val returnedValue = database.transaction("customers") {
            objectStore("customers")
                .index("name")
                .openCursor(autoContinue = true)
                .first()
        }

Cursor might try to return new value after first() and after transaction is finished. This will cause TransactionInactiveError.

result.isFailure -> channel.close(IllegalStateException("Send failed. Did you suspend illegally?"))
result.isClosed -> channel.close()
}
Expand All @@ -153,6 +175,7 @@ public open class Transaction internal constructor(
request.addEventListener("success", onSuccess)
request.addEventListener("error", onError)
awaitClose {
finished = true
request.removeEventListener("success", onSuccess)
request.removeEventListener("error", onError)
}
Expand Down
68 changes: 68 additions & 0 deletions core/src/jsTest/kotlin/TransactionTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.juul.indexeddb

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class TransactionTest {
@Test
fun readWithinTransaction() = runTest {
val database = openDatabase("read-within-transaction", 1) { database, oldVersion, newVersion ->
if (oldVersion < 1) {
database.createObjectStore("users", KeyPath("id"))
}
}
onCleanup {
database.close()
deleteDatabase("read-within-transaction")
}

val user = database.writeTransaction("users") {
objectStore("users").add(
jso {
id = "7740f7c4-f889-498a-bc6d-f88dabdcfb9a"
username = "Username"
},
)
objectStore("users")
.get(Key("7740f7c4-f889-498a-bc6d-f88dabdcfb9a"))
}

assertEquals("Username", user.username)
}

@Test
fun whenExceptionIsThrowWithinTransaction_transactionIsAborted() = runTest {
val database = openDatabase("abort-transaction", 1) { database, oldVersion, newVersion ->
if (oldVersion < 1) {
database.createObjectStore("users", KeyPath("id"))
}
}
onCleanup {
database.close()
deleteDatabase("abort-transaction")
}

assertFailsWith<ExceptionToAbortTransaction> {
database.writeTransaction("users") {
objectStore("users").add(
jso {
id = "7740f7c4-f889-498a-bc6d-f88dabdcfb9a"
username = "Username"
},
)

// Abort transaction
throw ExceptionToAbortTransaction()
}
}

// because transaction is aborted, new values shouldn't be stored
val users = database.transaction("users") {
objectStore("users").getAll().toList()
}

assertEquals(listOf(), users)
}
}
private class ExceptionToAbortTransaction : Exception()
Loading