Skip to content

Commit 33dafbb

Browse files
authored
Merge pull request #102 from SkillDevs/custom_stream_handling
Allow transforming table updates from sqlite_async.
2 parents 6705d13 + 09b841b commit 33dafbb

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

packages/drift_sqlite_async/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.2.4
2+
3+
- Allow transforming table updates from sqlite_async.
4+
15
## 0.2.3+1
26

37
- Update a dependency to the latest release.

packages/drift_sqlite_async/lib/src/connection.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@ import 'package:sqlite_async/sqlite_async.dart';
1515
class SqliteAsyncDriftConnection extends DatabaseConnection {
1616
late StreamSubscription _updateSubscription;
1717

18-
SqliteAsyncDriftConnection(SqliteConnection db, {bool logStatements = false})
19-
: super(SqliteAsyncQueryExecutor(db, logStatements: logStatements)) {
18+
/// [transformTableUpdates] is useful to map local table names from PowerSync that are backed by a view name
19+
/// which is the entity that the user interacts with.
20+
SqliteAsyncDriftConnection(
21+
SqliteConnection db, {
22+
bool logStatements = false,
23+
Set<TableUpdate> Function(UpdateNotification)? transformTableUpdates,
24+
}) : super(SqliteAsyncQueryExecutor(db, logStatements: logStatements)) {
2025
_updateSubscription = (db as SqliteQueries).updates!.listen((event) {
21-
var setUpdates = <TableUpdate>{};
22-
for (var tableName in event.tables) {
23-
setUpdates.add(TableUpdate(tableName));
26+
final Set<TableUpdate> setUpdates;
27+
if (transformTableUpdates != null) {
28+
setUpdates = transformTableUpdates(event);
29+
} else {
30+
setUpdates = <TableUpdate>{};
31+
for (var tableName in event.tables) {
32+
setUpdates.add(TableUpdate(tableName));
33+
}
2434
}
2535
super.streamQueries.handleTableUpdates(setUpdates);
2636
});

packages/drift_sqlite_async/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: drift_sqlite_async
2-
version: 0.2.3+1
2+
version: 0.2.4
33
homepage: https://github.com/powersync-ja/sqlite_async.dart
44
repository: https://github.com/powersync-ja/sqlite_async.dart
55
description: Use Drift with a sqlite_async database, allowing both to be used in the same application.

packages/drift_sqlite_async/test/basic_test.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import 'package:sqlite_async/sqlite_async.dart';
1111
import 'package:test/test.dart';
1212

1313
import './utils/test_utils.dart';
14+
import 'generated/database.dart';
1415

1516
class EmptyDatabase extends GeneratedDatabase {
1617
EmptyDatabase(super.executor);
@@ -245,4 +246,43 @@ INSERT INTO test_data(description) VALUES('test data');
245246
expect(row, isEmpty);
246247
});
247248
});
249+
250+
test('transform table updates', () async {
251+
final path = dbPath();
252+
await cleanDb(path: path);
253+
254+
final db = await setupDatabase(path: path);
255+
final connection = SqliteAsyncDriftConnection(
256+
db,
257+
// tables with the local_ prefix are mapped to the name without the prefix
258+
transformTableUpdates: (event) {
259+
final updates = <TableUpdate>{};
260+
261+
for (final originalTableName in event.tables) {
262+
final effectiveName = originalTableName.startsWith("local_")
263+
? originalTableName.substring(6)
264+
: originalTableName;
265+
updates.add(TableUpdate(effectiveName));
266+
}
267+
268+
return updates;
269+
},
270+
);
271+
272+
// Create table with a different name than drift. (Mimicking a table name backed by a view in PowerSync with the optional sync strategy)
273+
await db.execute(
274+
'CREATE TABLE local_todos(id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT)',
275+
);
276+
277+
final dbu = TodoDatabase.fromSqliteAsyncConnection(connection);
278+
279+
final tableUpdatesFut =
280+
dbu.tableUpdates(TableUpdateQuery.onTableName("todos")).first;
281+
282+
// This insert will trigger the sqlite_async "updates" stream
283+
await db.execute("INSERT INTO local_todos(description) VALUES('Test 1')");
284+
285+
expect(await tableUpdatesFut.timeout(const Duration(seconds: 2)),
286+
{TableUpdate("todos")});
287+
});
248288
}

packages/drift_sqlite_async/test/generated/database.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class TodoItems extends Table {
1515
@DriftDatabase(tables: [TodoItems])
1616
class TodoDatabase extends _$TodoDatabase {
1717
TodoDatabase(SqliteConnection db) : super(SqliteAsyncDriftConnection(db));
18+
19+
TodoDatabase.fromSqliteAsyncConnection(SqliteAsyncDriftConnection super.conn);
1820

1921
@override
2022
int get schemaVersion => 1;

0 commit comments

Comments
 (0)