Skip to content

Commit 98cb5d8

Browse files
authored
[Feature] Added user parameters support when connecting (#98)
* Added support for user parameters on connection * Added changelog notes and updated demos. * Updated .connect() signature to match powersync-js.
1 parent 1c1dc10 commit 98cb5d8

File tree

12 files changed

+58
-27
lines changed

12 files changed

+58
-27
lines changed

demos/django-todolist/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ packages:
262262
path: "../../packages/powersync"
263263
relative: true
264264
source: path
265-
version: "1.5.0"
265+
version: "1.5.3"
266266
powersync_flutter_libs:
267267
dependency: "direct overridden"
268268
description:

demos/supabase-anonymous-auth/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ packages:
318318
path: "../../packages/powersync"
319319
relative: true
320320
source: path
321-
version: "1.5.0"
321+
version: "1.5.3"
322322
powersync_flutter_libs:
323323
dependency: "direct overridden"
324324
description:

demos/supabase-edge-function-auth/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ packages:
318318
path: "../../packages/powersync"
319319
relative: true
320320
source: path
321-
version: "1.5.0"
321+
version: "1.5.3"
322322
powersync_flutter_libs:
323323
dependency: "direct overridden"
324324
description:

demos/supabase-simple-chat/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ packages:
358358
path: "../../packages/powersync"
359359
relative: true
360360
source: path
361-
version: "1.5.0"
361+
version: "1.5.3"
362362
powersync_flutter_libs:
363363
dependency: "direct overridden"
364364
description:

demos/supabase-simple-chat/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ dependencies:
3737

3838
supabase_flutter: ^1.10.25
3939
timeago: ^3.6.0
40-
powersync: ^1.4.2
40+
powersync: ^1.5.0
4141
path_provider: ^2.1.1
4242
path: ^1.8.3
4343
logging: ^1.2.0

demos/supabase-todolist/ios/Podfile.lock

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ PODS:
1717
- "sqlite3 (3.46.0+1)":
1818
- "sqlite3/common (= 3.46.0+1)"
1919
- "sqlite3/common (3.46.0+1)"
20+
- "sqlite3/dbstatvtab (3.46.0+1)":
21+
- sqlite3/common
2022
- "sqlite3/fts5 (3.46.0+1)":
2123
- sqlite3/common
2224
- "sqlite3/perf-threadsafe (3.46.0+1)":
@@ -25,7 +27,8 @@ PODS:
2527
- sqlite3/common
2628
- sqlite3_flutter_libs (0.0.1):
2729
- Flutter
28-
- sqlite3 (~> 3.46.0)
30+
- "sqlite3 (~> 3.46.0+1)"
31+
- sqlite3/dbstatvtab
2932
- sqlite3/fts5
3033
- sqlite3/perf-threadsafe
3134
- sqlite3/rtree
@@ -74,7 +77,7 @@ SPEC CHECKSUMS:
7477
powersync_flutter_libs: 5d6b132a398de442c0853a8b14bfbb62cd4ff5a1
7578
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
7679
sqlite3: 292c3e1bfe89f64e51ea7fc7dab9182a017c8630
77-
sqlite3_flutter_libs: 0d611efdf6d1c9297d5ab03dab21b75aeebdae31
80+
sqlite3_flutter_libs: c00457ebd31e59fa6bb830380ddba24d44fbcd3b
7881
url_launcher_ios: 6116280ddcfe98ab8820085d8d76ae7449447586
7982

8083
PODFILE CHECKSUM: f7b3cb7384a2d5da4b22b090e1f632de7f377987

demos/supabase-todolist/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ packages:
398398
path: "../../packages/powersync"
399399
relative: true
400400
source: path
401-
version: "1.5.0"
401+
version: "1.5.3"
402402
powersync_attachments_helper:
403403
dependency: "direct main"
404404
description:

packages/powersync/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.5.3
2+
3+
- Added support for client parameters when connecting.
4+
15
## 1.5.2
26

37
- Refactor `waitForFirstSync()` to iterate through the stream and remove the use of a `Future`.

packages/powersync/lib/src/powersync_database.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
7777
/// Use [attachedLogger] to propagate logs to [Logger.root] for custom logging.
7878
late final Logger logger;
7979

80+
Map<String, dynamic>? clientParams;
81+
8082
/// Open a [PowerSyncDatabase].
8183
///
8284
/// Only a single [PowerSyncDatabase] per [path] should be opened at a time.
@@ -219,7 +221,8 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
219221

220222
/// Throttle time between CRUD operations
221223
/// Defaults to 10 milliseconds.
222-
Duration crudThrottleTime = const Duration(milliseconds: 10)}) async {
224+
Duration crudThrottleTime = const Duration(milliseconds: 10),
225+
Map<String, dynamic>? params}) async {
223226
Zone current = Zone.current;
224227

225228
Future<void> reconnect() {
@@ -228,7 +231,8 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
228231
crudThrottleTime: crudThrottleTime,
229232
// The reconnect function needs to run in the original zone,
230233
// to avoid recursive lock errors.
231-
reconnect: current.bindCallback(reconnect)));
234+
reconnect: current.bindCallback(reconnect),
235+
params: params));
232236
}
233237

234238
await reconnect();
@@ -237,7 +241,9 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
237241
Future<void> _connect(
238242
{required PowerSyncBackendConnector connector,
239243
required Duration crudThrottleTime,
240-
required Future<void> Function() reconnect}) async {
244+
required Future<void> Function() reconnect,
245+
Map<String, dynamic>? params}) async {
246+
clientParams = params;
241247
await initialize();
242248

243249
// Disconnect if connected
@@ -330,8 +336,10 @@ class PowerSyncDatabase with SqliteQueries implements SqliteConnection {
330336
return;
331337
}
332338

333-
Isolate.spawn(_powerSyncDatabaseIsolate,
334-
_PowerSyncDatabaseIsolateArgs(rPort.sendPort, dbref, retryDelay),
339+
Isolate.spawn(
340+
_powerSyncDatabaseIsolate,
341+
_PowerSyncDatabaseIsolateArgs(
342+
rPort.sendPort, dbref, retryDelay, clientParams),
335343
debugName: 'PowerSyncDatabase',
336344
onError: errorPort.sendPort,
337345
onExit: exitPort.sendPort);
@@ -562,8 +570,10 @@ class _PowerSyncDatabaseIsolateArgs {
562570
final SendPort sPort;
563571
final IsolateConnectionFactory dbRef;
564572
final Duration retryDelay;
573+
final Map<String, dynamic>? parameters;
565574

566-
_PowerSyncDatabaseIsolateArgs(this.sPort, this.dbRef, this.retryDelay);
575+
_PowerSyncDatabaseIsolateArgs(
576+
this.sPort, this.dbRef, this.retryDelay, this.parameters);
567577
}
568578

569579
Future<void> _powerSyncDatabaseIsolate(
@@ -638,7 +648,9 @@ Future<void> _powerSyncDatabaseIsolate(
638648
invalidCredentialsCallback: invalidateCredentials,
639649
uploadCrud: uploadCrud,
640650
updateStream: updateController.stream,
641-
retryDelay: args.retryDelay);
651+
retryDelay: args.retryDelay,
652+
syncParameters: args.parameters);
653+
642654
openedStreamingSync = sync;
643655
sync.streamingSync();
644656
sync.statusStream.listen((event) {

packages/powersync/lib/src/streaming_sync.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ class StreamingSyncImplementation {
3838

3939
final Duration retryDelay;
4040

41+
final Map<String, dynamic>? syncParameters;
42+
4143
SyncStatus lastStatus = const SyncStatus();
4244

4345
AbortController? _abort;
@@ -50,7 +52,8 @@ class StreamingSyncImplementation {
5052
this.invalidCredentialsCallback,
5153
required this.uploadCrud,
5254
required this.updateStream,
53-
required this.retryDelay}) {
55+
required this.retryDelay,
56+
this.syncParameters}) {
5457
_client = http.Client();
5558
statusStream = _statusStreamController.stream;
5659
}
@@ -233,17 +236,18 @@ class StreamingSyncImplementation {
233236
initialBucketStates[entry.bucket] = entry.opId;
234237
}
235238

236-
final List<BucketRequest> req = [];
239+
final List<BucketRequest> buckets = [];
237240
for (var entry in initialBucketStates.entries) {
238-
req.add(BucketRequest(entry.key, entry.value));
241+
buckets.add(BucketRequest(entry.key, entry.value));
239242
}
240243

241244
Checkpoint? targetCheckpoint;
242245
Checkpoint? validatedCheckpoint;
243246
Checkpoint? appliedCheckpoint;
244247
var bucketSet = Set<String>.from(initialBucketStates.keys);
245248

246-
var requestStream = streamingSyncRequest(StreamingSyncRequest(req));
249+
var requestStream =
250+
streamingSyncRequest(StreamingSyncRequest(buckets, syncParameters));
247251

248252
var merged = addBroadcast(requestStream, _localPingController.stream);
249253

packages/powersync/lib/src/sync_types.dart

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,23 @@ Object? parseStreamingSyncLine(Map<String, dynamic> line) {
112112
class StreamingSyncRequest {
113113
List<BucketRequest> buckets;
114114
bool includeChecksum = true;
115+
Map<String, dynamic>? parameters;
115116

116-
StreamingSyncRequest(this.buckets);
117+
StreamingSyncRequest(this.buckets, this.parameters);
117118

118-
Map<String, dynamic> toJson() => {
119-
'buckets': buckets,
120-
'include_checksum': includeChecksum,
121-
// We want the JSON row data as a string
122-
'raw_data': true
123-
};
119+
Map<String, dynamic> toJson() {
120+
final Map<String, dynamic> json = {
121+
'buckets': buckets,
122+
'include_checksum': includeChecksum,
123+
'raw_data': true,
124+
};
125+
126+
if (parameters != null) {
127+
json['parameters'] = parameters;
128+
}
129+
130+
return json;
131+
}
124132
}
125133

126134
class BucketRequest {

packages/powersync/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: powersync
2-
version: 1.5.2
2+
version: 1.5.3
33
homepage: https://powersync.com
44
repository: https://github.com/powersync-ja/powersync.dart
55
description: PowerSync Flutter SDK - keep PostgreSQL databases in sync with on-device SQLite databases.

0 commit comments

Comments
 (0)