@@ -11,6 +11,7 @@ import 'package:sqlite_async/sqlite_async.dart';
1111import 'package:test/test.dart' ;
1212
1313import './utils/test_utils.dart' ;
14+ import 'generated/database.dart' ;
1415
1516class 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}
0 commit comments