Skip to content

Remove Kotlin Types from API #40

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 27 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
7524bc7
wip: cleanup API
stevensJourney Apr 23, 2025
dbaba58
Cleanup
stevensJourney Apr 28, 2025
d6fd7eb
cleanup apis
stevensJourney Apr 29, 2025
a9d605e
Merge remote-tracking branch 'origin/main' into api
stevensJourney Apr 29, 2025
1c59de4
update watched query implementation
stevensJourney Apr 29, 2025
074f35d
cleanup filestructure
stevensJourney Apr 29, 2025
f0fb9c4
temp
stevensJourney Apr 29, 2025
f23f364
move
stevensJourney Apr 29, 2025
b71eca4
update demo and changelog
stevensJourney Apr 29, 2025
977fa0e
more changelog
stevensJourney Apr 29, 2025
2277b9d
cleanup
stevensJourney Apr 29, 2025
24051f9
temp dependency
stevensJourney Apr 29, 2025
2153b22
cleanup
stevensJourney Apr 29, 2025
819f919
Update Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift
stevensJourney Apr 29, 2025
85a5fba
cleanup
stevensJourney Apr 29, 2025
1383c4b
cleanup
stevensJourney Apr 29, 2025
4e9c2a8
deprecate userId
stevensJourney Apr 29, 2025
52ed16d
update read and write lock docs
stevensJourney Apr 29, 2025
21dc8f4
Merge remote-tracking branch 'origin/main' into api
stevensJourney Apr 30, 2025
223c004
propagate connector upload errors
stevensJourney Apr 30, 2025
b6f62a7
move
stevensJourney Apr 30, 2025
70894b4
move again
stevensJourney Apr 30, 2025
5700b23
improve sqlcursor getter implementation
stevensJourney Apr 30, 2025
8da17b5
update time durations from ms to TimeInterval
stevensJourney Apr 30, 2025
2cbb640
Merge remote-tracking branch 'origin/main' into api
stevensJourney Apr 30, 2025
798c533
update package
stevensJourney May 2, 2025
42f9fa0
cleanup
stevensJourney May 2, 2025
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
84 changes: 84 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
# Changelog

# 1.0.0-Beta.14

- Improved the stability of watched queries. Watched queries were previously susceptible to runtime crashes if an exception was thrown in the update stream. Errors are now gracefully handled.

- Added `readLock` and `writeLock` APIs. These methods allow obtaining a SQLite connection context without starting a transaction.

- Removed references to the PowerSync Kotlin SDK from all public API protocols. Dedicated Swift protocols are now defined. These protocols align better with Swift primitives. See the `BRAKING CHANGES` section for more details. Updated protocols include:

- `ConnectionContext` - The context provided by `readLock` and `writeLock`
- `Transaction` - The context provided by `readTransaction` and `writeTransaction`
- `CrudBatch` - Response from `getCrudBatch`
- `CrudTransaction` Response from `getNextCrudTransaction`
- `CrudEntry` - Crud entries for `CrudBatch` and `CrudTransaction`
- `UpdateType` - Operation type for `CrudEntry`s
- `SqlCursor` - Cursor used to map SQLite results to typed result sets
- `JsonParam` - JSON parameters used to declare client parameters in the `connect` method
- `JsonValue` - Individual JSON field types for `JsonParam`

- Database and transaction/lock level query `execute` methods now have `@discardableResult` annotation.

- Query methods' `parameters` typing has been updated to `[Any?]` from `[Any]`. This makes passing `nil` or optional values to queries easier.

- `AttachmentContext`, `AttachmentQueue`, `AttachmentService` and `SyncingService` are are now explicitly declared as `open` classes, allowing them to be subclassed outside the defining module.

**BREAKING CHANGES**:

- Completing CRUD transactions or CRUD batches, in the `PowerSyncBackendConnector` `uploadData` handler, now has a simpler invocation.

```diff
- _ = try await transaction.complete.invoke(p1: nil)
+ try await transaction.complete()
```

- `index` based `SqlCursor` getters now throw if the query result column value is `nil`. This is now consistent with the behaviour of named column getter operations. New `getXxxxxOptional(index: index)` methods are available if the query result value could be `nil`.

```diff
let results = try transaction.getAll(
sql: "SELECT * FROM my_table",
parameters: [id]
) { cursor in
- cursor.getString(index: 0)!
+ cursor.getStringOptional(index: 0)
+ // OR
+ // try cursor.getString(index: 0) // if the value should be required
}
```

- `SqlCursor` getters now directly return Swift types. `getLong` has been replaced with `getInt64`.

```diff
let results = try transaction.getAll(
sql: "SELECT * FROM my_table",
parameters: [id]
) { cursor in
- cursor.getBoolean(index: 0)?.boolValue,
+ cursor.getBooleanOptional(index: 0),
- cursor.getLong(index: 0)?.int64Value,
+ cursor.getInt64Optional(index: 0)
+ // OR
+ // try cursor.getInt64(index: 0) // if the value should be required
}
```

- Client parameters now need to be specified with strictly typed `JsonValue` enums.

```diff
try await database.connect(
connector: PowerSyncBackendConnector(),
params: [
- "foo": "bar"
+ "foo": .string("bar")
]
)
```

- `SyncStatus` values now use Swift primitives for status attributes. `lastSyncedAt` now is of `Date` type.

```diff
- let lastTime: Date? = db.currentStatus.lastSyncedAt.map {
- Date(timeIntervalSince1970: TimeInterval($0.epochSeconds))
- }
+ let time: Date? = db.currentStatus.lastSyncedAt
```

# 1.0.0-Beta.13

- Update `powersync-kotlin` dependency to version `1.0.0-BETA32`, which includes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
"state" : {
"revision" : "633a2924f7893f7ebeb064cbcd9c202937673633",
"version" : "1.0.0-BETA30.0"
"branch" : "crudhasmore",
"revision" : "ae49c22188af0b0086b69b14787809b96f82899e"
}
},
{
Expand Down
11 changes: 5 additions & 6 deletions Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,18 @@ class SupabaseConnector: PowerSyncBackendConnector {

switch entry.op {
case .put:
var data: [String: AnyCodable] = entry.opData?.mapValues { AnyCodable($0) } ?? [:]
data["id"] = AnyCodable(entry.id)
var data = entry.opData ?? [:]
data["id"] = entry.id
try await table.upsert(data).execute()
case .patch:
guard let opData = entry.opData else { continue }
let encodableData = opData.mapValues { AnyCodable($0) }
try await table.update(encodableData).eq("id", value: entry.id).execute()
try await table.update(opData).eq("id", value: entry.id).execute()
case .delete:
try await table.delete().eq("id", value: entry.id).execute()
}
}

_ = try await transaction.complete.invoke(p1: nil)
try await transaction.complete()

} catch {
if let errorCode = PostgresFatalCodes.extractErrorCode(from: error),
Expand All @@ -127,7 +126,7 @@ class SupabaseConnector: PowerSyncBackendConnector {
/// elsewhere instead of discarding, and/or notify the user.
print("Data upload error: \(error)")
print("Discarding entry: \(lastEntry!)")
_ = try await transaction.complete.invoke(p1: nil)
try await transaction.complete()
return
}

Expand Down
7 changes: 3 additions & 4 deletions Demo/PowerSyncExample/PowerSync/SystemManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,8 @@ class SystemManager {
sql: "SELECT photo_id FROM \(TODOS_TABLE) WHERE list_id = ? AND photo_id IS NOT NULL",
parameters: [id]
) { cursor in
// FIXME Transactions should allow throwing in the mapper and should use generics correctly
cursor.getString(index: 0) ?? "invalid" // :(
} as? [String] // :(
try cursor.getString(index: 0)
}

_ = try transaction.execute(
sql: "DELETE FROM \(LISTS_TABLE) WHERE id = ?",
Expand All @@ -141,7 +140,7 @@ class SystemManager {
parameters: [id]
)

return attachmentIDs ?? [] // :(
return attachmentIDs
})

if let attachments {
Expand Down
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
"state" : {
"revision" : "144d2110eaca2537f49f5e86e5a6c78acf502f94",
"version" : "1.0.0-BETA32.0"
"branch" : "crudhasmore",
"revision" : "ae49c22188af0b0086b69b14787809b96f82899e"
}
},
{
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ let package = Package(
targets: ["PowerSync"]),
],
dependencies: [
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA32.0"),
// TODO
// .package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA32.0"),
.package(path: "/Users/stevenontong/Documents/platform_code/powersync/powersync-kotlin"),
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.12"..<"0.4.0")
],
targets: [
Expand Down
2 changes: 1 addition & 1 deletion Sources/PowerSync/Kotlin/DatabaseLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private class KermitLogWriterAdapter: Kermit_coreLogWriter {
///
/// This class bridges Swift log writers with the Kotlin logging system and supports
/// runtime configuration of severity levels and writer lists.
internal class DatabaseLogger: LoggerProtocol {
class DatabaseLogger: LoggerProtocol {
/// The underlying Kermit logger instance provided by the PowerSyncKotlin SDK.
public let kLogger = PowerSyncKotlin.generateLogger(logger: nil)
public let logger: any LoggerProtocol
Expand Down
5 changes: 2 additions & 3 deletions Sources/PowerSync/Kotlin/KotlinAdapter.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import PowerSyncKotlin


internal struct KotlinAdapter {
enum KotlinAdapter {
struct Index {
static func toKotlin(_ index: IndexProtocol) -> PowerSyncKotlin.Index {
PowerSyncKotlin.Index(
Expand All @@ -26,7 +25,7 @@ internal struct KotlinAdapter {
static func toKotlin(_ table: TableProtocol) -> PowerSyncKotlin.Table {
PowerSyncKotlin.Table(
name: table.name,
columns: table.columns.map {Column.toKotlin($0)},
columns: table.columns.map { Column.toKotlin($0) },
indexes: table.indexes.map { Index.toKotlin($0) },
localOnly: table.localOnly,
insertOnly: table.insertOnly,
Expand Down
Loading
Loading