Skip to content

Commit

Permalink
feature: add executeWrite method to PowerSyncDB that wraps the execut…
Browse files Browse the repository at this point in the history
…ing in a writeTransaction
  • Loading branch information
Manrich121 committed Mar 12, 2024
1 parent 29d75bf commit e4d6036
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ let package = Package(
)
,
]
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ internal class PowerSyncDatabaseImpl(
return@readTransaction CrudTransaction(
crud = entries, transactionId = txId,
complete = { writeCheckpoint ->
println("[CrudTransaction::complete] Completing transaction with checkpoint $writeCheckpoint")
handleWriteCheckpoint(entries.last().clientId, writeCheckpoint)
}
)
Expand Down Expand Up @@ -208,6 +209,10 @@ internal class PowerSyncDatabaseImpl(
return internalDb.execute(sql, parameters)
}

override suspend fun executeWrite(sql: String, parameters: List<Any>?): Long {
return internalDb.executeWrite(sql, parameters)
}

private suspend fun handleWriteCheckpoint(lastTransactionId: Int, writeCheckpoint: String?) {
writeTransaction {
internalDb.queries.deleteEntriesWithIdLessThan(lastTransactionId.toLong())
Expand Down
2 changes: 2 additions & 0 deletions core/src/commonMain/kotlin/com/powersync/db/WriteQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ interface WriteQueries {
*/
suspend fun execute(sql: String, parameters: List<Any>? = listOf()): Long

suspend fun executeWrite(sql: String, parameters: List<Any>?): Long

suspend fun <R> writeTransaction(body: suspend SuspendingTransactionWithReturn<R>.() -> R): R

}
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,12 @@ class PsInternalDatabase(val driver: PsSqlDriver, private val scope: CoroutineSc
return transactor.transactionWithResult(noEnclosing = true, body)
}

override suspend fun executeWrite(sql: String, parameters: List<Any>?): Long {
return writeTransaction {
execute(sql, parameters)
}
}

// Register callback for table updates
private fun tableUpdates(): Flow<List<String>> {
return driver.tableUpdates()
Expand Down

0 comments on commit e4d6036

Please sign in to comment.