Skip to content

Commit 916211d

Browse files
Remove Kotlin Types from API (#40)
1 parent adc7648 commit 916211d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2194
-414
lines changed

CHANGELOG.md

+115
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,120 @@
11
# Changelog
22

3+
# 1.0.0
4+
5+
- 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.
6+
7+
- Deprecated `PowerSyncCredentials` `userId` field. This value is not used by the PowerSync service.
8+
9+
- Added `readLock` and `writeLock` APIs. These methods allow obtaining a SQLite connection context without starting a transaction.
10+
11+
- 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:
12+
13+
- `ConnectionContext` - The context provided by `readLock` and `writeLock`
14+
- `Transaction` - The context provided by `readTransaction` and `writeTransaction`
15+
- `CrudBatch` - Response from `getCrudBatch`
16+
- `CrudTransaction` Response from `getNextCrudTransaction`
17+
- `CrudEntry` - Crud entries for `CrudBatch` and `CrudTransaction`
18+
- `UpdateType` - Operation type for `CrudEntry`s
19+
- `SqlCursor` - Cursor used to map SQLite results to typed result sets
20+
- `JsonParam` - JSON parameters used to declare client parameters in the `connect` method
21+
- `JsonValue` - Individual JSON field types for `JsonParam`
22+
23+
- Database and transaction/lock level query `execute` methods now have `@discardableResult` annotation.
24+
25+
- Query methods' `parameters` typing has been updated to `[Any?]` from `[Any]`. This makes passing `nil` or optional values to queries easier.
26+
27+
- `AttachmentContext`, `AttachmentQueue`, `AttachmentService` and `SyncingService` are are now explicitly declared as `open` classes, allowing them to be subclassed outside the defining module.
28+
29+
**BREAKING CHANGES**:
30+
31+
- Completing CRUD transactions or CRUD batches, in the `PowerSyncBackendConnector` `uploadData` handler, now has a simpler invocation.
32+
33+
```diff
34+
- _ = try await transaction.complete.invoke(p1: nil)
35+
+ try await transaction.complete()
36+
```
37+
38+
- `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`.
39+
40+
```diff
41+
let results = try transaction.getAll(
42+
sql: "SELECT * FROM my_table",
43+
parameters: [id]
44+
) { cursor in
45+
- cursor.getString(index: 0)!
46+
+ cursor.getStringOptional(index: 0)
47+
+ // OR
48+
+ // try cursor.getString(index: 0) // if the value should be required
49+
}
50+
```
51+
52+
- `SqlCursor` getters now directly return Swift types. `getLong` has been replaced with `getInt64`.
53+
54+
```diff
55+
let results = try transaction.getAll(
56+
sql: "SELECT * FROM my_table",
57+
parameters: [id]
58+
) { cursor in
59+
- cursor.getBoolean(index: 0)?.boolValue,
60+
+ cursor.getBooleanOptional(index: 0),
61+
- cursor.getLong(index: 0)?.int64Value,
62+
+ cursor.getInt64Optional(index: 0)
63+
+ // OR
64+
+ // try cursor.getInt64(index: 0) // if the value should be required
65+
}
66+
```
67+
68+
- Client parameters now need to be specified with strictly typed `JsonValue` enums.
69+
70+
```diff
71+
try await database.connect(
72+
connector: PowerSyncBackendConnector(),
73+
params: [
74+
- "foo": "bar"
75+
+ "foo": .string("bar")
76+
]
77+
)
78+
```
79+
80+
- `SyncStatus` values now use Swift primitives for status attributes. `lastSyncedAt` now is of `Date` type.
81+
82+
```diff
83+
- let lastTime: Date? = db.currentStatus.lastSyncedAt.map {
84+
- Date(timeIntervalSince1970: TimeInterval($0.epochSeconds))
85+
- }
86+
+ let time: Date? = db.currentStatus.lastSyncedAt
87+
```
88+
89+
- `crudThrottleMs` and `retryDelayMs` in the `connect` method have been updated to `crudThrottle` and `retryDelay` which are now of type `TimeInterval`. Previously the parameters were specified in milliseconds, the `TimeInterval` typing now requires values to be specified in seconds.
90+
91+
```diff
92+
try await database.connect(
93+
connector: PowerSyncBackendConnector(),
94+
- crudThrottleMs: 1000,
95+
- retryDelayMs: 5000,
96+
+ crudThrottle: 1,
97+
+ retryDelay: 5,
98+
params: [
99+
"foo": .string("bar"),
100+
]
101+
)
102+
```
103+
104+
- `throttleMs` in the watched query `WatchOptions` has been updated to `throttle` which is now of type `TimeInterval`. Previously the parameters were specified in milliseconds, the `TimeInterval` typing now requires values to be specified in seconds.
105+
106+
```diff
107+
let stream = try database.watch(
108+
options: WatchOptions(
109+
sql: "SELECT name FROM users ORDER BY id",
110+
- throttleMs: 1000,
111+
+ throttle: 1,
112+
mapper: { cursor in
113+
try cursor.getString(index: 0)
114+
}
115+
))
116+
```
117+
3118
# 1.0.0-Beta.13
4119

5120
- Update `powersync-kotlin` dependency to version `1.0.0-BETA32`, which includes:

Demo/PowerSyncExample.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

+2-11
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,13 @@
1010
"version" : "0.6.7"
1111
}
1212
},
13-
{
14-
"identity" : "powersync-kotlin",
15-
"kind" : "remoteSourceControl",
16-
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
17-
"state" : {
18-
"revision" : "633a2924f7893f7ebeb064cbcd9c202937673633",
19-
"version" : "1.0.0-BETA30.0"
20-
}
21-
},
2213
{
2314
"identity" : "powersync-sqlite-core-swift",
2415
"kind" : "remoteSourceControl",
2516
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
2617
"state" : {
27-
"revision" : "5041116d295e61d3c54f27117c02fd81071a1ab3",
28-
"version" : "0.3.12"
18+
"revision" : "3a7fcb3be83db5b450effa5916726b19828cbcb7",
19+
"version" : "0.3.14"
2920
}
3021
},
3122
{

Demo/PowerSyncExample/PowerSync/SupabaseConnector.swift

+5-6
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,18 @@ class SupabaseConnector: PowerSyncBackendConnector {
101101

102102
switch entry.op {
103103
case .put:
104-
var data: [String: AnyCodable] = entry.opData?.mapValues { AnyCodable($0) } ?? [:]
105-
data["id"] = AnyCodable(entry.id)
104+
var data = entry.opData ?? [:]
105+
data["id"] = entry.id
106106
try await table.upsert(data).execute()
107107
case .patch:
108108
guard let opData = entry.opData else { continue }
109-
let encodableData = opData.mapValues { AnyCodable($0) }
110-
try await table.update(encodableData).eq("id", value: entry.id).execute()
109+
try await table.update(opData).eq("id", value: entry.id).execute()
111110
case .delete:
112111
try await table.delete().eq("id", value: entry.id).execute()
113112
}
114113
}
115114

116-
_ = try await transaction.complete.invoke(p1: nil)
115+
try await transaction.complete()
117116

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

Demo/PowerSyncExample/PowerSync/SystemManager.swift

+3-4
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,8 @@ class SystemManager {
127127
sql: "SELECT photo_id FROM \(TODOS_TABLE) WHERE list_id = ? AND photo_id IS NOT NULL",
128128
parameters: [id]
129129
) { cursor in
130-
// FIXME Transactions should allow throwing in the mapper and should use generics correctly
131-
cursor.getString(index: 0) ?? "invalid" // :(
132-
} as? [String] // :(
130+
try cursor.getString(index: 0)
131+
}
133132

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

144-
return attachmentIDs ?? [] // :(
143+
return attachmentIDs
145144
})
146145

147146
if let attachments {

Package.resolved

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
"kind" : "remoteSourceControl",
66
"location" : "https://github.com/powersync-ja/powersync-kotlin.git",
77
"state" : {
8-
"revision" : "144d2110eaca2537f49f5e86e5a6c78acf502f94",
9-
"version" : "1.0.0-BETA32.0"
8+
"revision" : "ccd2e595195c59d570eb93a878ad6a5cfca72ada",
9+
"version" : "1.0.1+SWIFT.0"
1010
}
1111
},
1212
{
1313
"identity" : "powersync-sqlite-core-swift",
1414
"kind" : "remoteSourceControl",
1515
"location" : "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
1616
"state" : {
17-
"revision" : "5041116d295e61d3c54f27117c02fd81071a1ab3",
18-
"version" : "0.3.12"
17+
"revision" : "3a7fcb3be83db5b450effa5916726b19828cbcb7",
18+
"version" : "0.3.14"
1919
}
2020
}
2121
],

Package.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
targets: ["PowerSync"]),
1818
],
1919
dependencies: [
20-
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", exact: "1.0.0-BETA32.0"),
20+
.package(url: "https://github.com/powersync-ja/powersync-kotlin.git", "1.0.1+SWIFT.0"..<"1.1.0+SWIFT.0"),
2121
.package(url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git", "0.3.14"..<"0.4.0")
2222
],
2323
targets: [

Sources/PowerSync/Kotlin/DatabaseLogger.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private class KermitLogWriterAdapter: Kermit_coreLogWriter {
4444
///
4545
/// This class bridges Swift log writers with the Kotlin logging system and supports
4646
/// runtime configuration of severity levels and writer lists.
47-
internal class DatabaseLogger: LoggerProtocol {
47+
class DatabaseLogger: LoggerProtocol {
4848
/// The underlying Kermit logger instance provided by the PowerSyncKotlin SDK.
4949
public let kLogger = PowerSyncKotlin.generateLogger(logger: nil)
5050
public let logger: any LoggerProtocol

Sources/PowerSync/Kotlin/KotlinAdapter.swift

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import PowerSyncKotlin
22

3-
4-
internal struct KotlinAdapter {
3+
enum KotlinAdapter {
54
struct Index {
65
static func toKotlin(_ index: IndexProtocol) -> PowerSyncKotlin.Index {
76
PowerSyncKotlin.Index(
@@ -26,7 +25,7 @@ internal struct KotlinAdapter {
2625
static func toKotlin(_ table: TableProtocol) -> PowerSyncKotlin.Table {
2726
PowerSyncKotlin.Table(
2827
name: table.name,
29-
columns: table.columns.map {Column.toKotlin($0)},
28+
columns: table.columns.map { Column.toKotlin($0) },
3029
indexes: table.indexes.map { Index.toKotlin($0) },
3130
localOnly: table.localOnly,
3231
insertOnly: table.insertOnly,

0 commit comments

Comments
 (0)