@powersync/react 0.0.0-dev-20251003085035 → 0.0.0-dev-20251111122755

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/lib/QueryStore.js +1 -0
  2. package/lib/QueryStore.js.map +1 -0
  3. package/lib/hooks/PowerSyncContext.js +1 -0
  4. package/lib/hooks/PowerSyncContext.js.map +1 -0
  5. package/lib/hooks/deprecated/usePowerSyncQuery.js +1 -0
  6. package/lib/hooks/deprecated/usePowerSyncQuery.js.map +1 -0
  7. package/lib/hooks/deprecated/usePowerSyncStatus.js +1 -0
  8. package/lib/hooks/deprecated/usePowerSyncStatus.js.map +1 -0
  9. package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js +1 -0
  10. package/lib/hooks/deprecated/usePowerSyncWatchedQuery.js.map +1 -0
  11. package/lib/hooks/streams.js +1 -0
  12. package/lib/hooks/streams.js.map +1 -0
  13. package/lib/hooks/suspense/SuspenseQueryResult.js +1 -0
  14. package/lib/hooks/suspense/SuspenseQueryResult.js.map +1 -0
  15. package/lib/hooks/suspense/suspense-utils.js +1 -0
  16. package/lib/hooks/suspense/suspense-utils.js.map +1 -0
  17. package/lib/hooks/suspense/useSingleSuspenseQuery.js +1 -0
  18. package/lib/hooks/suspense/useSingleSuspenseQuery.js.map +1 -0
  19. package/lib/hooks/suspense/useSuspenseQuery.js +1 -0
  20. package/lib/hooks/suspense/useSuspenseQuery.js.map +1 -0
  21. package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js +1 -0
  22. package/lib/hooks/suspense/useWatchedQuerySuspenseSubscription.js.map +1 -0
  23. package/lib/hooks/suspense/useWatchedSuspenseQuery.js +1 -0
  24. package/lib/hooks/suspense/useWatchedSuspenseQuery.js.map +1 -0
  25. package/lib/hooks/useStatus.js +1 -0
  26. package/lib/hooks/useStatus.js.map +1 -0
  27. package/lib/hooks/watched/useQuery.js +1 -0
  28. package/lib/hooks/watched/useQuery.js.map +1 -0
  29. package/lib/hooks/watched/useSingleQuery.js +1 -0
  30. package/lib/hooks/watched/useSingleQuery.js.map +1 -0
  31. package/lib/hooks/watched/useWatchedQuery.js +1 -0
  32. package/lib/hooks/watched/useWatchedQuery.js.map +1 -0
  33. package/lib/hooks/watched/useWatchedQuerySubscription.js +1 -0
  34. package/lib/hooks/watched/useWatchedQuerySubscription.js.map +1 -0
  35. package/lib/hooks/watched/watch-types.js +1 -0
  36. package/lib/hooks/watched/watch-types.js.map +1 -0
  37. package/lib/hooks/watched/watch-utils.js +1 -0
  38. package/lib/hooks/watched/watch-utils.js.map +1 -0
  39. package/lib/index.js +1 -0
  40. package/lib/index.js.map +1 -0
  41. package/package.json +8 -7
  42. package/src/QueryStore.ts +81 -0
  43. package/src/hooks/PowerSyncContext.ts +22 -0
  44. package/src/hooks/deprecated/usePowerSyncQuery.ts +28 -0
  45. package/src/hooks/deprecated/usePowerSyncStatus.ts +33 -0
  46. package/src/hooks/deprecated/usePowerSyncWatchedQuery.ts +60 -0
  47. package/src/hooks/streams.ts +131 -0
  48. package/src/hooks/suspense/SuspenseQueryResult.ts +4 -0
  49. package/src/hooks/suspense/suspense-utils.ts +86 -0
  50. package/src/hooks/suspense/useSingleSuspenseQuery.ts +81 -0
  51. package/src/hooks/suspense/useSuspenseQuery.ts +76 -0
  52. package/src/hooks/suspense/useWatchedQuerySuspenseSubscription.ts +60 -0
  53. package/src/hooks/suspense/useWatchedSuspenseQuery.ts +36 -0
  54. package/src/hooks/useStatus.ts +17 -0
  55. package/src/hooks/watched/useQuery.ts +98 -0
  56. package/src/hooks/watched/useSingleQuery.ts +67 -0
  57. package/src/hooks/watched/useWatchedQuery.ts +89 -0
  58. package/src/hooks/watched/useWatchedQuerySubscription.ts +52 -0
  59. package/src/hooks/watched/watch-types.ts +93 -0
  60. package/src/hooks/watched/watch-utils.ts +88 -0
  61. package/src/index.ts +12 -0
@@ -0,0 +1,93 @@
1
+ import { DifferentialWatchedQueryComparator, SQLOnChangeOptions, SyncSubscriptionDescription } from '@powersync/common';
2
+ import { UseSyncStreamOptions } from '../streams.js';
3
+
4
+ export interface HookWatchOptions extends Omit<SQLOnChangeOptions, 'signal'> {
5
+ /**
6
+ * An optional array of sync streams (with names and parameters) backing the query.
7
+ *
8
+ * When set, `useQuery` will subscribe to those streams (and automatically handle unsubscribing from them, too).
9
+ *
10
+ * If {@link QuerySyncStreamOptions} is set on a stream, `useQuery` will remain in a loading state until that stream
11
+ * is marked as {@link SyncSubscriptionDescription.hasSynced}. This ensures the query is not missing rows that haven't
12
+ * been downloaded.
13
+ * Note however that after an initial sync, the query will not block itself while new rows are downloading. Instead,
14
+ * consistent sync snapshots will be made available as they've been processed by PowerSync.
15
+ *
16
+ * @experimental Sync streams are currently in alpha.
17
+ */
18
+ streams?: QuerySyncStreamOptions[];
19
+ reportFetching?: boolean;
20
+ }
21
+
22
+ /**
23
+ * Additional options to control how `useQuery` behaves when subscribing to a stream.
24
+ */
25
+ export interface QuerySyncStreamOptions extends UseSyncStreamOptions {
26
+ /**
27
+ * When set to `true`, a `useQuery` hook will remain in a loading state as long as the stream is resolving or
28
+ * downloading for the first time (in other words, until {@link SyncSubscriptionDescription.hasSynced} is true).
29
+ */
30
+ waitForStream?: boolean;
31
+ }
32
+
33
+ export interface AdditionalOptions extends HookWatchOptions {
34
+ runQueryOnce?: boolean;
35
+ }
36
+
37
+ export interface DifferentialHookOptions<RowType> extends HookWatchOptions {
38
+ /**
39
+ * Used to compare result sets.
40
+ *
41
+ * By default the hook will requery on any dependent table change. This will
42
+ * emit a new hook result even if the result set has not changed.
43
+ *
44
+ * Specifying a {@link DifferentialWatchedQueryComparator} will remove emissions for
45
+ * unchanged result sets.
46
+ * Furthermore, emitted `data` arrays will preserve object references between result set emissions
47
+ * for unchanged rows.
48
+ * @example
49
+ * ```javascript
50
+ * {
51
+ * rowComparator: {
52
+ * keyBy: (item) => item.id,
53
+ * compareBy: (item) => JSON.stringify(item)
54
+ * }
55
+ * }
56
+ * ```
57
+ */
58
+ rowComparator?: DifferentialWatchedQueryComparator<RowType>;
59
+ }
60
+
61
+ export type ReadonlyQueryResult<RowType> = {
62
+ readonly data: ReadonlyArray<Readonly<RowType>>;
63
+ /**
64
+ * Indicates the initial loading state (hard loading). Loading becomes false once the first set of results from the watched query is available or an error occurs.
65
+ */
66
+ readonly isLoading: boolean;
67
+ /**
68
+ * Indicates whether the query is currently fetching data, is true during the initial load and any time when the query is re-evaluating (useful for large queries).
69
+ */
70
+ readonly isFetching: boolean;
71
+ readonly error: Error | undefined;
72
+ /**
73
+ * Function used to run the query again.
74
+ */
75
+ refresh?: (signal?: AbortSignal) => Promise<void>;
76
+ };
77
+
78
+ export type QueryResult<RowType> = {
79
+ data: RowType[];
80
+ /**
81
+ * Indicates the initial loading state (hard loading). Loading becomes false once the first set of results from the watched query is available or an error occurs.
82
+ */
83
+ isLoading: boolean;
84
+ /**
85
+ * Indicates whether the query is currently fetching data, is true during the initial load and any time when the query is re-evaluating (useful for large queries).
86
+ */
87
+ isFetching: boolean;
88
+ error: Error | undefined;
89
+ /**
90
+ * Function used to run the query again.
91
+ */
92
+ refresh?: (signal?: AbortSignal) => Promise<void>;
93
+ };
@@ -0,0 +1,88 @@
1
+ import { AbstractPowerSyncDatabase, CompilableQuery, CompiledQuery, WatchCompatibleQuery } from '@powersync/common';
2
+ import React from 'react';
3
+ import { usePowerSync } from '../PowerSyncContext.js';
4
+ import { AdditionalOptions } from './watch-types.js';
5
+
6
+ export type InternalHookOptions<DataType> = {
7
+ query: WatchCompatibleQuery<DataType>;
8
+ powerSync: AbstractPowerSyncDatabase;
9
+ queryChanged: boolean;
10
+ active: boolean;
11
+ };
12
+
13
+ interface WatchCompatibleQueryWithParams<T> extends WatchCompatibleQuery<T> {
14
+ stringifiedParameters?: string;
15
+ }
16
+
17
+ export const checkQueryChanged = <T>(query: WatchCompatibleQueryWithParams<T>, options: AdditionalOptions) => {
18
+ let _compiled: CompiledQuery;
19
+ try {
20
+ _compiled = query.compile();
21
+ } catch (error) {
22
+ return false; // If compilation fails, we assume the query has changed
23
+ }
24
+ const compiled = _compiled!;
25
+
26
+ const stringifiedParams = query.stringifiedParameters ?? JSON.stringify(compiled.parameters);
27
+ const stringifiedOptions = JSON.stringify(options);
28
+
29
+ const previousQueryRef = React.useRef({ sqlStatement: compiled.sql, stringifiedParams, stringifiedOptions });
30
+
31
+ if (
32
+ previousQueryRef.current.sqlStatement !== compiled.sql ||
33
+ previousQueryRef.current.stringifiedParams != stringifiedParams ||
34
+ previousQueryRef.current.stringifiedOptions != stringifiedOptions
35
+ ) {
36
+ previousQueryRef.current.sqlStatement = compiled.sql;
37
+ previousQueryRef.current.stringifiedParams = stringifiedParams;
38
+ previousQueryRef.current.stringifiedOptions = stringifiedOptions;
39
+
40
+ return true;
41
+ }
42
+
43
+ return false;
44
+ };
45
+
46
+ export const constructCompatibleQuery = <RowType>(
47
+ query: string | CompilableQuery<RowType>,
48
+ parameters: any[] = [],
49
+ options: AdditionalOptions
50
+ ) => {
51
+ const powerSync = usePowerSync();
52
+ const stringifiedParameters = React.useMemo(() => JSON.stringify(parameters), [parameters]);
53
+
54
+ const parsedQuery = React.useMemo<WatchCompatibleQueryWithParams<RowType[]>>(() => {
55
+ if (typeof query == 'string') {
56
+ return {
57
+ compile: () => ({
58
+ sql: query,
59
+ parameters
60
+ }),
61
+ execute: () => powerSync.getAll(query, parameters),
62
+ // Setting this is a small optimization that avoids checkQueryChanged recomputing the JSON representation.
63
+ stringifiedParameters
64
+ };
65
+ } else {
66
+ return {
67
+ // Generics differ a bit but holistically this is the same
68
+ compile: () => {
69
+ const compiled = query.compile();
70
+ return {
71
+ sql: compiled.sql,
72
+ parameters: [...compiled.parameters]
73
+ };
74
+ },
75
+ execute: () => query.execute()
76
+ // Note that we can't set stringifiedParameters here because we only know parameters after the query has been
77
+ // compiled.
78
+ };
79
+ }
80
+ }, [query, powerSync, stringifiedParameters]);
81
+
82
+ const queryChanged = checkQueryChanged(parsedQuery, options);
83
+
84
+ return {
85
+ parsedQuery,
86
+ queryChanged
87
+ };
88
+ };
package/src/index.ts ADDED
@@ -0,0 +1,12 @@
1
+ export { usePowerSyncQuery } from './hooks/deprecated/usePowerSyncQuery.js';
2
+ export { usePowerSyncStatus } from './hooks/deprecated/usePowerSyncStatus.js';
3
+ export { usePowerSyncWatchedQuery } from './hooks/deprecated/usePowerSyncWatchedQuery.js';
4
+ export * from './hooks/PowerSyncContext.js';
5
+ export { SuspenseQueryResult } from './hooks/suspense/SuspenseQueryResult.js';
6
+ export { useSuspenseQuery } from './hooks/suspense/useSuspenseQuery.js';
7
+ export { useWatchedQuerySuspenseSubscription } from './hooks/suspense/useWatchedQuerySuspenseSubscription.js';
8
+ export { useSyncStream, UseSyncStreamOptions } from './hooks/streams.js';
9
+ export { useStatus } from './hooks/useStatus.js';
10
+ export { useQuery } from './hooks/watched/useQuery.js';
11
+ export { useWatchedQuerySubscription } from './hooks/watched/useWatchedQuerySubscription.js';
12
+ export { AdditionalOptions } from './hooks/watched/watch-types.js';