Skip to content

Commit 9dea1b9

Browse files
authored
Deprecated rawTableNames (#315)
1 parent 9bff884 commit 9dea1b9

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

.changeset/brown-radios-report.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': minor
3+
---
4+
5+
Deprecated `rawTableNames` field in `SQLWatchOptions`. All tables specified in `tables` will now be watched, including PowerSync tables with prefixes.

packages/common/src/client/AbstractPowerSyncDatabase.ts

+7-15
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export interface SQLWatchOptions {
7979
/** The minimum interval between queries. */
8080
throttleMs?: number;
8181
/**
82+
* @deprecated All tables specified in {@link tables} will be watched, including PowerSync tables with prefixes.
83+
*
8284
* Allows for watching any SQL table
8385
* by not removing PowerSync table name prefixes
8486
*/
@@ -889,7 +891,9 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
889891
}
890892

891893
const resolvedOptions = options ?? {};
892-
const watchedTables = new Set(resolvedOptions.tables ?? []);
894+
const watchedTables = new Set<string>(
895+
(resolvedOptions?.tables ?? []).flatMap((table) => [table, `ps_data__${table}`, `ps_data_local__${table}`])
896+
);
893897

894898
const changedTables = new Set<string>();
895899
const throttleMs = resolvedOptions.throttleMs ?? DEFAULT_WATCH_THROTTLE_MS;
@@ -910,8 +914,7 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
910914
const dispose = this.database.registerListener({
911915
tablesUpdated: async (update) => {
912916
try {
913-
const { rawTableNames } = resolvedOptions;
914-
this.processTableUpdates(update, rawTableNames, changedTables);
917+
this.processTableUpdates(update, changedTables);
915918
flushTableUpdates();
916919
} catch (error) {
917920
onError?.(error);
@@ -976,24 +979,13 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
976979

977980
private processTableUpdates(
978981
updateNotification: BatchedUpdateNotification | UpdateNotification,
979-
rawTableNames: boolean | undefined,
980982
changedTables: Set<string>
981983
): void {
982984
const tables = isBatchedUpdateNotification(updateNotification)
983985
? updateNotification.tables
984986
: [updateNotification.table];
985987

986-
const filteredTables = rawTableNames ? tables : tables.filter((t) => !!t.match(POWERSYNC_TABLE_MATCH));
987-
if (!filteredTables.length) {
988-
return;
989-
}
990-
991-
// Remove any PowerSync table prefixes if necessary
992-
const mappedTableNames = rawTableNames
993-
? filteredTables
994-
: filteredTables.map((t) => t.replace(POWERSYNC_TABLE_MATCH, ''));
995-
996-
for (const table of mappedTableNames) {
988+
for (const table of tables) {
997989
changedTables.add(table);
998990
}
999991
}

packages/web/tests/on_change.test.ts

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { AbstractPowerSyncDatabase, WatchOnChangeEvent } from '@powersync/common';
2+
import { PowerSyncDatabase } from '@powersync/web';
3+
import { v4 as uuid } from 'uuid';
4+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
5+
import { testSchema } from './utils/testDb';
6+
7+
const UPLOAD_TIMEOUT_MS = 3000;
8+
9+
describe('OnChange Tests', () => {
10+
let powersync: AbstractPowerSyncDatabase;
11+
12+
beforeEach(async () => {
13+
powersync = new PowerSyncDatabase({
14+
database: { dbFilename: 'test-watch.db' },
15+
schema: testSchema,
16+
flags: {
17+
enableMultiTabs: false
18+
}
19+
});
20+
});
21+
22+
afterEach(async () => {
23+
await powersync.disconnectAndClear();
24+
await powersync.close();
25+
});
26+
27+
async function runOnChangeTest(tablesToWatch: string[], expectedChangedTables: string[]) {
28+
const changedTables: string[] = [];
29+
const abortController = new AbortController();
30+
const onChange = vi.fn((event: WatchOnChangeEvent) => {
31+
changedTables.push(...event.changedTables);
32+
});
33+
34+
powersync.onChange({ onChange }, { tables: tablesToWatch, signal: abortController.signal });
35+
powersync.execute('INSERT INTO assets(id, make, customer_id) VALUES (uuid(), ?, ?)', ['test', uuid()]);
36+
await vi.waitFor(
37+
() => {
38+
expect(onChange).toHaveBeenCalled();
39+
},
40+
{
41+
timeout: UPLOAD_TIMEOUT_MS
42+
}
43+
);
44+
45+
abortController.abort();
46+
expect(changedTables).toEqual(expectedChangedTables);
47+
}
48+
49+
it('basic onChange test', async () => {
50+
await runOnChangeTest(['assets'], ['ps_data__assets']);
51+
});
52+
53+
it('internal "ps_data" table onChange test', async () => {
54+
await runOnChangeTest(['ps_data__assets'], ['ps_data__assets']);
55+
});
56+
57+
it('internal "ps_oplog" table onChange test', async () => {
58+
await runOnChangeTest(['ps_oplog'], ['ps_oplog']);
59+
});
60+
});

0 commit comments

Comments
 (0)