Skip to content

Default Param for CrudBatch Complete #47

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

Merged
merged 4 commits into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 1.1.1 (unreleased)

* Improved `CrudBatch` and `CrudTransaction` `complete` function extensions. Developers no longer need to specify `nil` as an argument for `writeCheckpoint` when calling `CrudBatch.complete`. The base `complete` functions still accept an optional `writeCheckpoint` argument if developers use custom write checkpoints.
``` diff
guard let finalBatch = try await powersync.getCrudBatch(limit: 100) else {
return nil
}
- try await batch.complete(writeCheckpoint: nil)
+ try await batch.complete()
```

## 1.1.0

* Add sync progress information through `SyncStatusData.downloadProgress`.
Expand Down
9 changes: 9 additions & 0 deletions Sources/PowerSync/Protocol/db/CrudBatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@ public protocol CrudBatch {
/// `writeCheckpoint` is optional.
func complete(writeCheckpoint: String?) async throws
}

public extension CrudBatch {
/// Call to remove the changes from the local queue, once successfully uploaded.
func complete() async throws {
try await self.complete(
writeCheckpoint: nil
)
}
}
6 changes: 2 additions & 4 deletions Sources/PowerSync/Protocol/db/CrudTransaction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ public protocol CrudTransaction {

public extension CrudTransaction {
/// Call to remove the changes from the local queue, once successfully uploaded.
///
/// `writeCheckpoint` is optional.
func complete(writeCheckpoint: String? = nil) async throws {
func complete() async throws {
try await self.complete(
writeCheckpoint: writeCheckpoint
writeCheckpoint: nil
)
}
}
20 changes: 20 additions & 0 deletions Tests/PowerSyncTests/CrudTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,25 @@ final class CrudTests: XCTestCase {
}

XCTAssertNil(afterCompleteBatch)

try await database.writeTransaction { tx in
for i in 0 ..< 100 {
try tx.execute(
sql: "INSERT INTO users (id, name, email, favorite_number) VALUES (uuid(), 'a', 'a@example.com', ?)",
parameters: [i]
)
}
}

guard let finalBatch = try await database.getCrudBatch(limit: 100) else {
return XCTFail("Failed to get crud batch")
}
XCTAssert(finalBatch.crud.count == 100)
XCTAssert(finalBatch.hasMore == false)
// Calling complete without a writeCheckpoint param should be possible
try await finalBatch.complete()

let finalValidationBatch = try await database.getCrudBatch(limit: 100)
XCTAssertNil(finalValidationBatch)
}
}