Skip to content

Commit 3fbe208

Browse files
cleanup docs. More readonly work.
1 parent 5c7c76e commit 3fbe208

File tree

9 files changed

+42
-31
lines changed

9 files changed

+42
-31
lines changed

docs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ npm-debug.log*
1919

2020
docs/attachments-sdk/
2121
docs/common-sdk/
22+
docs/node-sdk/
2223
docs/react-native-sdk/
2324
docs/react-sdk/
2425
docs/vue-sdk/

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -891,18 +891,18 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
891891

892892
/**
893893
* Allows defining a query which can be used to build a {@link WatchedQuery}.
894-
* The defined query will be executed with {@link AbstractPowerSyncDatabase.getAll}.
894+
* The defined query will be executed with {@link AbstractPowerSyncDatabase#getAll}.
895895
* An optional mapper function can be provided to transform the results.
896896
*
897897
* @example
898898
* ```javascript
899899
* const watchedTodos = powersync.query({
900-
* sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
901-
* parameters: [],
902-
* mapper: (row) => ({
903-
* ...row,
904-
* created_at: new Date(row.created_at as string)
905-
* })
900+
* sql: `SELECT photo_id as id FROM todos WHERE photo_id IS NOT NULL`,
901+
* parameters: [],
902+
* mapper: (row) => ({
903+
* ...row,
904+
* created_at: new Date(row.created_at as string)
905+
* })
906906
* })
907907
* .watch()
908908
* // OR use .differentialWatch() for fine-grained watches.

packages/common/src/client/Query.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { ComparisonWatchedQuery } from './watched/processors/OnChangeQueryProces
77
import { WatchedQueryOptions } from './watched/WatchedQuery.js';
88

99
/**
10-
* Query parameters for {@link ArrayQueryDefinition.parameters}
10+
* Query parameters for {@link ArrayQueryDefinition#parameters}
1111
*/
1212
export type QueryParam = string | number | boolean | null | undefined | bigint | Uint8Array;
1313

@@ -17,7 +17,7 @@ export type QueryParam = string | number | boolean | null | undefined | bigint |
1717
*/
1818
export interface ArrayQueryDefinition<RowType = unknown> {
1919
sql: string;
20-
parameters?: ReadonlyArray<QueryParam>;
20+
parameters?: ReadonlyArray<Readonly<QueryParam>>;
2121
/**
2222
* Maps the raw SQLite row to a custom typed object.
2323
* @example
@@ -63,14 +63,14 @@ export interface Query<RowType> {
6363
* By default the returned watched query will emit changes whenever a change to the underlying SQLite tables is made.
6464
* These changes might not be relevant to the query, but the query will emit a new result set.
6565
*
66-
* A {@link StandardWatchedQueryOptions.comparator} can be provided to limit the data emissions. The watched query will still
66+
* A {@link StandardWatchedQueryOptions#comparator} can be provided to limit the data emissions. The watched query will still
6767
* query the underlying DB on a underlying table changes, but the result will only be emitted if the comparator detects a change in the results.
6868
*
6969
* The comparator in this method is optimized and returns early as soon as it detects a change. Each data emission will correlate to a change in the result set,
7070
* but note that the result set will not maintain internal object references to the previous result set. If internal object references are needed,
71-
* consider using {@link Query.differentialWatch} instead.
71+
* consider using {@link Query#differentialWatch} instead.
7272
*/
73-
watch(options?: StandardWatchedQueryOptions<RowType>): ComparisonWatchedQuery<RowType[]>;
73+
watch(options?: StandardWatchedQueryOptions<RowType>): ComparisonWatchedQuery<ReadonlyArray<Readonly<RowType>>>;
7474

7575
/**
7676
* Creates a {@link WatchedQuery} which watches and emits results of the linked query.
@@ -81,8 +81,8 @@ export interface Query<RowType> {
8181
* If the result set is different, the watched query will emit the new result set and provide a detailed diff of the changes.
8282
*
8383
* The deep differentiation allows maintaining result set object references between result emissions.
84-
* The {@link DifferentialWatchedQuery.state.data} array will contain the previous row references for unchanged rows.
85-
* A detailed diff of the changes can be accessed via {@link DifferentialWatchedQuery.state.diff}.
84+
* The {@link DifferentialWatchedQuery#state} `data` array will contain the previous row references for unchanged rows.
85+
* A detailed diff of the changes can be accessed via {@link DifferentialWatchedQuery#state} `diff`.
8686
*/
8787
differentialWatch(options?: DifferentialWatchedQueryOptions<RowType>): DifferentialWatchedQuery<RowType>;
8888
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export interface WatchedQueryRowDifferential<RowType> {
2020
* {@link WatchedQueryState.data} is of the {@link WatchedQueryDifferential} form when using the {@link IncrementalWatchMode.DIFFERENTIAL} mode.
2121
*/
2222
export interface WatchedQueryDifferential<RowType> {
23-
readonly added: ReadonlyArray<RowType>;
23+
readonly added: ReadonlyArray<Readonly<RowType>>;
2424
/**
2525
* The entire current result set.
2626
* Array item object references are preserved between updates if the item is unchanged.
@@ -35,10 +35,10 @@ export interface WatchedQueryDifferential<RowType> {
3535
* the updated result set will be contain the same object reference, to item A, as the previous result set.
3636
* This is regardless of the item A's position in the updated result set.
3737
*/
38-
readonly all: ReadonlyArray<RowType>;
39-
readonly removed: ReadonlyArray<RowType>;
40-
readonly updated: ReadonlyArray<WatchedQueryRowDifferential<RowType>>;
41-
readonly unchanged: ReadonlyArray<RowType>;
38+
readonly all: ReadonlyArray<Readonly<RowType>>;
39+
readonly removed: ReadonlyArray<Readonly<RowType>>;
40+
readonly updated: ReadonlyArray<WatchedQueryRowDifferential<Readonly<RowType>>>;
41+
readonly unchanged: ReadonlyArray<Readonly<RowType>>;
4242
}
4343

4444
/**
@@ -83,7 +83,7 @@ export interface DifferentialWatchedQuerySettings<RowType> extends DifferentialW
8383
query: WatchCompatibleQuery<RowType[]>;
8484
}
8585

86-
export interface DifferentialWatchedQueryState<RowType> extends WatchedQueryState<RowType[]> {
86+
export interface DifferentialWatchedQueryState<RowType> extends WatchedQueryState<ReadonlyArray<Readonly<RowType>>> {
8787
/**
8888
* The difference between the current and previous result set.
8989
*/
@@ -96,7 +96,7 @@ type MutableDifferentialWatchedQueryState<RowType> = MutableWatchedQueryState<Ro
9696
};
9797

9898
export interface DifferentialWatchedQuery<RowType>
99-
extends WatchedQuery<RowType[], DifferentialWatchedQuerySettings<RowType>> {
99+
extends WatchedQuery<ReadonlyArray<Readonly<RowType>>, DifferentialWatchedQuerySettings<RowType>> {
100100
readonly state: DifferentialWatchedQueryState<RowType>;
101101
}
102102

@@ -143,7 +143,7 @@ export const DEFAULT_WATCHED_QUERY_DIFFERENTIATOR: WatchedQueryDifferentiator<an
143143
* @internal
144144
*/
145145
export class DifferentialQueryProcessor<RowType>
146-
extends AbstractQueryProcessor<RowType[], DifferentialWatchedQuerySettings<RowType>>
146+
extends AbstractQueryProcessor<ReadonlyArray<Readonly<RowType>>, DifferentialWatchedQuerySettings<RowType>>
147147
implements DifferentialWatchedQuery<RowType>
148148
{
149149
readonly state: DifferentialWatchedQueryState<RowType>;

packages/react/src/QueryStore.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@ export class QueryStore {
1919

2020
constructor(private db: AbstractPowerSyncDatabase) {}
2121

22-
getQuery<RowType>(
23-
key: string,
24-
query: WatchCompatibleQuery<RowType[]>,
25-
options: AdditionalOptions
26-
): WatchedQuery<RowType[]> {
22+
getQuery<RowType>(key: string, query: WatchCompatibleQuery<RowType[]>, options: AdditionalOptions) {
2723
if (this.cache.has(key)) {
2824
return this.cache.get(key) as WatchedQuery<RowType[]>;
2925
}

packages/react/src/hooks/suspense/useWatchedSuspenseQuery.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,10 @@ export const useWatchedSuspenseQuery = <T = any>(
3030
const store = getQueryStore(powerSync);
3131
const watchedQuery = store.getQuery(key, parsedQuery, options);
3232

33-
return useWatchedQuerySuspenseSubscription(watchedQuery);
33+
const result = useWatchedQuerySuspenseSubscription(watchedQuery);
34+
return {
35+
...result,
36+
// The result above is readonly, but this API expects a mutable array
37+
data: [...result.data]
38+
};
3439
};

packages/react/src/hooks/watched/useWatchedQuery.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,11 @@ export const useWatchedQuery = <RowType = unknown>(
4141
}
4242
}, [queryChanged]);
4343

44-
return useWatchedQuerySubscription(watchedQuery);
44+
const result = useWatchedQuerySubscription(watchedQuery);
45+
return {
46+
...result,
47+
// The Watched Query API returns readonly arrays,
48+
// This allows compatibility with the hook API.
49+
data: [...result.data]
50+
};
4551
};

packages/react/tests/useQuery.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ describe('useQuery', () => {
315315
// This query can be instantiated once and reused.
316316
// The query retains it's state and will not re-fetch the data unless the result changes.
317317
// This is useful for queries that are used in multiple components.
318-
const listsQuery = db.query({ sql: `SELECT * FROM lists`, parameters: [] }).differentialWatch();
318+
const listsQuery = db
319+
.query<{ id: string; name: string }>({ sql: `SELECT * FROM lists`, parameters: [] })
320+
.differentialWatch();
319321

320322
const wrapper = ({ children }) => <PowerSyncContext.Provider value={db}>{children}</PowerSyncContext.Provider>;
321323
const { result } = renderHook(() => useWatchedQuerySubscription(listsQuery), {

packages/vue/src/composables/useWatchedQuery.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ export const useWatchedQuery = <T = any>(
7272
onStateChange: (state) => {
7373
isLoading.value = state.isLoading;
7474
isFetching.value = state.isFetching;
75-
data.value = state.data;
75+
// The watched query state is readonly
76+
data.value = [...state.data];
7677
if (state.error) {
7778
const wrappedError = new Error('PowerSync failed to fetch data: ' + state.error.message);
7879
wrappedError.cause = state.error;

0 commit comments

Comments
 (0)