Skip to content

Commit 3cbfc8f

Browse files
update docs and demos.
1 parent 3fbe208 commit 3cbfc8f

File tree

6 files changed

+42
-67
lines changed

6 files changed

+42
-67
lines changed

demos/react-supabase-todolist/src/components/providers/SystemProvider.tsx

Lines changed: 22 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,7 @@ import { AppSchema, ListRecord, LISTS_TABLE, TODOS_TABLE } from '@/library/power
33
import { SupabaseConnector } from '@/library/powersync/SupabaseConnector';
44
import { CircularProgress } from '@mui/material';
55
import { PowerSyncContext } from '@powersync/react';
6-
import {
7-
ArrayComparator,
8-
createBaseLogger,
9-
GetAllQuery,
10-
IncrementalWatchMode,
11-
LogLevel,
12-
PowerSyncDatabase,
13-
WatchedQuery
14-
} from '@powersync/web';
6+
import { createBaseLogger, DifferentialWatchedQuery, LogLevel, PowerSyncDatabase } from '@powersync/web';
157
import React, { Suspense } from 'react';
168
import { NavigationPanelContextProvider } from '../navigation/NavigationPanelContext';
179

@@ -28,7 +20,7 @@ export const db = new PowerSyncDatabase({
2820
export type EnhancedListRecord = ListRecord & { total_tasks: number; completed_tasks: number };
2921

3022
export type QueryStore = {
31-
lists: WatchedQuery<EnhancedListRecord[]>;
23+
lists: DifferentialWatchedQuery<EnhancedListRecord>;
3224
};
3325

3426
const QueryStore = React.createContext<QueryStore | null>(null);
@@ -39,32 +31,26 @@ export const SystemProvider = ({ children }: { children: React.ReactNode }) => {
3931
const [powerSync] = React.useState(db);
4032

4133
const [queryStore] = React.useState<QueryStore>(() => {
42-
const listsQuery = db.incrementalWatch({ mode: IncrementalWatchMode.COMPARISON }).build({
43-
comparator: new ArrayComparator({
44-
compareBy: (item) => JSON.stringify(item)
45-
}),
46-
watch: {
47-
placeholderData: [],
48-
query: new GetAllQuery<EnhancedListRecord>({
49-
sql: /* sql */ `
50-
SELECT
51-
${LISTS_TABLE}.*,
52-
COUNT(${TODOS_TABLE}.id) AS total_tasks,
53-
SUM(
54-
CASE
55-
WHEN ${TODOS_TABLE}.completed = true THEN 1
56-
ELSE 0
57-
END
58-
) as completed_tasks
59-
FROM
60-
${LISTS_TABLE}
61-
LEFT JOIN ${TODOS_TABLE} ON ${LISTS_TABLE}.id = ${TODOS_TABLE}.list_id
62-
GROUP BY
63-
${LISTS_TABLE}.id;
64-
`
65-
})
66-
}
67-
});
34+
const listsQuery = db
35+
.query<EnhancedListRecord>({
36+
sql: /* sql */ `
37+
SELECT
38+
${LISTS_TABLE}.*,
39+
COUNT(${TODOS_TABLE}.id) AS total_tasks,
40+
SUM(
41+
CASE
42+
WHEN ${TODOS_TABLE}.completed = true THEN 1
43+
ELSE 0
44+
END
45+
) as completed_tasks
46+
FROM
47+
${LISTS_TABLE}
48+
LEFT JOIN ${TODOS_TABLE} ON ${LISTS_TABLE}.id = ${TODOS_TABLE}.list_id
49+
GROUP BY
50+
${LISTS_TABLE}.id;
51+
`
52+
})
53+
.differentialWatch();
6854

6955
return {
7056
lists: listsQuery

packages/common/src/client/watched/processors/DifferentialQueryProcessor.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface WatchedQueryRowDifferential<RowType> {
1616
}
1717

1818
/**
19-
* Represents the result of a watched query that has been differentiated.
20-
* {@link WatchedQueryState.data} is of the {@link WatchedQueryDifferential} form when using the {@link IncrementalWatchMode.DIFFERENTIAL} mode.
19+
* Represents the result of a watched query that has been diffed.
20+
* {@link DifferentialWatchedQueryState#diff} is of the {@link WatchedQueryDifferential} form.
2121
*/
2222
export interface WatchedQueryDifferential<RowType> {
2323
readonly added: ReadonlyArray<Readonly<RowType>>;
@@ -74,7 +74,7 @@ export interface DifferentialWatchedQueryOptions<RowType> extends WatchedQueryOp
7474
}
7575

7676
/**
77-
* Settings for incremental watched queries using the {@link IncrementalWatchMode.DIFFERENTIAL} mode.
77+
* Settings for differential incremental watched queries using.
7878
*/
7979
export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialWatchedQueryOptions<RowType> {
8080
/**

packages/common/src/client/watched/processors/comparators.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type ArrayComparatorOptions<ItemType> = {
1313
};
1414

1515
/**
16-
* Compares array results of watched queries for incrementally watched queries created in the {@link IncrementalWatchMode.COMPARISON} mode.
16+
* Compares array results of watched queries for incrementally watched queries created in the standard mode.
1717
*/
1818
export class ArrayComparator<ItemType> implements WatchedQueryComparator<ItemType[]> {
1919
constructor(protected options: ArrayComparatorOptions<ItemType>) {}

packages/kysely-driver/tests/sqlite/watch.test.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AbstractPowerSyncDatabase, column, IncrementalWatchMode, Schema, Table } from '@powersync/common';
1+
import { AbstractPowerSyncDatabase, column, Schema, Table } from '@powersync/common';
22
import { PowerSyncDatabase } from '@powersync/web';
33
import { sql } from 'kysely';
44
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -265,18 +265,13 @@ describe('Watch Tests', () => {
265265
it('incremental watch should accept queries', async () => {
266266
const query = db.selectFrom('assets').select(db.fn.count('assets.id').as('count'));
267267

268-
const watch = powerSyncDb.incrementalWatch({ mode: IncrementalWatchMode.COMPARISON }).build({
269-
watch: {
270-
query,
271-
placeholderData: []
272-
}
273-
});
268+
const watch = powerSyncDb.customQuery(query).watch();
274269

275270
const latestDataPromise = new Promise<Awaited<ReturnType<typeof query.execute>>>((resolve) => {
276271
const dispose = watch.registerListener({
277272
onData: (data) => {
278273
if (data.length > 0) {
279-
resolve(data);
274+
resolve([...data]);
280275
dispose();
281276
}
282277
}

packages/react/tests/profile.test.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,13 @@ const testsInsertsCompare = async (options: {
282282
const notMemoizedDifferentialTest = async () => {
283283
await db.execute('DELETE FROM lists;');
284284

285-
const query = db.incrementalWatch({ mode: commonSdk.IncrementalWatchMode.DIFFERENTIAL }).build({
286-
watch: {
287-
query: new commonSdk.GetAllQuery<List>({
288-
sql: 'SELECT * FROM lists ORDER BY name ASC;'
289-
}),
285+
const query = db
286+
.query<List>({
287+
sql: 'SELECT * FROM lists ORDER BY name ASC;'
288+
})
289+
.differentialWatch({
290290
reportFetching: false
291-
}
292-
});
291+
});
293292

294293
const times: number[] = [];
295294
diffSpy(query, times);
@@ -300,8 +299,8 @@ const testsInsertsCompare = async (options: {
300299
initialDataCount,
301300
useMemoize: false,
302301
getQueryData: () => {
303-
const result = useWatchedQuerySubscription(query).data.all;
304-
return result;
302+
const { data } = useWatchedQuerySubscription(query);
303+
return [...data];
305304
}
306305
});
307306

@@ -322,14 +321,8 @@ const testsInsertsCompare = async (options: {
322321
// Testing Differential With Memoization
323322
await db.execute('DELETE FROM lists;');
324323

325-
// guardLog = true; // Prevents logging from TestItemWidget
326-
const query = db.incrementalWatch({ mode: commonSdk.IncrementalWatchMode.DIFFERENTIAL }).build({
327-
watch: {
328-
query: new commonSdk.GetAllQuery<List>({
329-
sql: 'SELECT * FROM lists ORDER BY name ASC;'
330-
}),
331-
reportFetching: false
332-
}
324+
const query = db.query<List>({ sql: 'SELECT * FROM lists ORDER BY name ASC;' }).differentialWatch({
325+
reportFetching: false
333326
});
334327

335328
const times: number[] = [];
@@ -341,8 +334,8 @@ const testsInsertsCompare = async (options: {
341334
initialDataCount,
342335
useMemoize: true,
343336
getQueryData: () => {
344-
const result = useWatchedQuerySubscription(query).data.all;
345-
return result;
337+
const { data } = useWatchedQuerySubscription(query);
338+
return [...data];
346339
}
347340
});
348341

packages/sqljs/sql.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 52e5649f3a3a2a46aa4ad58a79d118c22f56cf30

0 commit comments

Comments
 (0)