@powersync/tanstack-react-query 0.0.0-dev-20241107150304 → 0.0.0-dev-20241209122448
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.
- package/lib/hooks/useQuery.js +21 -18
- package/package.json +6 -4
package/lib/hooks/useQuery.js
CHANGED
|
@@ -2,10 +2,10 @@ import { parseQuery } from '@powersync/common';
|
|
|
2
2
|
import { usePowerSync } from '@powersync/react';
|
|
3
3
|
import React from 'react';
|
|
4
4
|
import * as Tanstack from '@tanstack/react-query';
|
|
5
|
-
export function useQuery(options, queryClient
|
|
5
|
+
export function useQuery(options, queryClient) {
|
|
6
6
|
return useQueryCore(options, queryClient, Tanstack.useQuery);
|
|
7
7
|
}
|
|
8
|
-
export function useSuspenseQuery(options, queryClient
|
|
8
|
+
export function useSuspenseQuery(options, queryClient) {
|
|
9
9
|
return useQueryCore(options, queryClient, Tanstack.useSuspenseQuery);
|
|
10
10
|
}
|
|
11
11
|
function useQueryCore(options, queryClient, useQueryFn) {
|
|
@@ -13,14 +13,10 @@ function useQueryCore(options, queryClient, useQueryFn) {
|
|
|
13
13
|
if (!powerSync) {
|
|
14
14
|
throw new Error('PowerSync is not available');
|
|
15
15
|
}
|
|
16
|
-
const
|
|
16
|
+
const resolvedQueryClient = queryClient ?? Tanstack.useQueryClient();
|
|
17
|
+
let error = undefined;
|
|
17
18
|
const [tables, setTables] = React.useState([]);
|
|
18
|
-
const { query, parameters, ...resolvedOptions } = options;
|
|
19
|
-
React.useEffect(() => {
|
|
20
|
-
if (error) {
|
|
21
|
-
setError(null);
|
|
22
|
-
}
|
|
23
|
-
}, [powerSync, query, parameters, options.queryKey]);
|
|
19
|
+
const { query, parameters = [], ...resolvedOptions } = options;
|
|
24
20
|
let sqlStatement = '';
|
|
25
21
|
let queryParameters = [];
|
|
26
22
|
if (query) {
|
|
@@ -30,7 +26,7 @@ function useQueryCore(options, queryClient, useQueryFn) {
|
|
|
30
26
|
queryParameters = parsedQuery.parameters;
|
|
31
27
|
}
|
|
32
28
|
catch (e) {
|
|
33
|
-
|
|
29
|
+
error = e;
|
|
34
30
|
}
|
|
35
31
|
}
|
|
36
32
|
const stringifiedParams = JSON.stringify(queryParameters);
|
|
@@ -41,15 +37,22 @@ function useQueryCore(options, queryClient, useQueryFn) {
|
|
|
41
37
|
setTables(tables);
|
|
42
38
|
}
|
|
43
39
|
catch (e) {
|
|
44
|
-
|
|
40
|
+
error = e;
|
|
45
41
|
}
|
|
46
42
|
};
|
|
47
43
|
React.useEffect(() => {
|
|
48
|
-
if (!query)
|
|
49
|
-
return;
|
|
44
|
+
if (error || !query)
|
|
45
|
+
return () => { };
|
|
50
46
|
(async () => {
|
|
51
47
|
await fetchTables();
|
|
52
48
|
})();
|
|
49
|
+
const l = powerSync.registerListener({
|
|
50
|
+
schemaChanged: async () => {
|
|
51
|
+
await fetchTables();
|
|
52
|
+
resolvedQueryClient.invalidateQueries({ queryKey: options.queryKey });
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return () => l?.();
|
|
53
56
|
}, [powerSync, sqlStatement, stringifiedParams]);
|
|
54
57
|
const queryFn = React.useCallback(async () => {
|
|
55
58
|
if (error) {
|
|
@@ -61,28 +64,28 @@ function useQueryCore(options, queryClient, useQueryFn) {
|
|
|
61
64
|
catch (e) {
|
|
62
65
|
return Promise.reject(e);
|
|
63
66
|
}
|
|
64
|
-
}, [powerSync, query, parameters, stringifiedKey
|
|
67
|
+
}, [powerSync, query, parameters, stringifiedKey]);
|
|
65
68
|
React.useEffect(() => {
|
|
66
69
|
if (error || !query)
|
|
67
70
|
return () => { };
|
|
68
71
|
const abort = new AbortController();
|
|
69
72
|
powerSync.onChangeWithCallback({
|
|
70
73
|
onChange: () => {
|
|
71
|
-
|
|
74
|
+
resolvedQueryClient.invalidateQueries({
|
|
72
75
|
queryKey: options.queryKey
|
|
73
76
|
});
|
|
74
77
|
},
|
|
75
78
|
onError: (e) => {
|
|
76
|
-
|
|
79
|
+
error = e;
|
|
77
80
|
}
|
|
78
81
|
}, {
|
|
79
82
|
tables,
|
|
80
83
|
signal: abort.signal
|
|
81
84
|
});
|
|
82
85
|
return () => abort.abort();
|
|
83
|
-
}, [powerSync,
|
|
86
|
+
}, [powerSync, resolvedQueryClient, stringifiedKey, tables]);
|
|
84
87
|
return useQueryFn({
|
|
85
88
|
...resolvedOptions,
|
|
86
89
|
queryFn: query ? queryFn : resolvedOptions.queryFn
|
|
87
|
-
},
|
|
90
|
+
}, resolvedQueryClient);
|
|
88
91
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/tanstack-react-query",
|
|
3
|
-
"version": "0.0.0-dev-
|
|
3
|
+
"version": "0.0.0-dev-20241209122448",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -22,15 +22,16 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://docs.powersync.com",
|
|
24
24
|
"peerDependencies": {
|
|
25
|
-
"@powersync/common": "
|
|
25
|
+
"@powersync/common": "^1.22.0",
|
|
26
26
|
"react": "*"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"@tanstack/react-query": "^5.55.4",
|
|
30
|
-
"@powersync/common": "
|
|
31
|
-
"@powersync/react": "1.5.
|
|
30
|
+
"@powersync/common": "1.22.0",
|
|
31
|
+
"@powersync/react": "1.5.1"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
+
"@testing-library/react": "^15.0.2",
|
|
34
35
|
"@types/react": "^18.2.34",
|
|
35
36
|
"jsdom": "^24.0.0",
|
|
36
37
|
"react": "18.2.0",
|
|
@@ -40,6 +41,7 @@
|
|
|
40
41
|
"build": "tsc -b",
|
|
41
42
|
"build:prod": "tsc -b --sourceMap false",
|
|
42
43
|
"clean": "rm -rf lib tsconfig.tsbuildinfo",
|
|
44
|
+
"test": "vitest",
|
|
43
45
|
"watch": "tsc -b -w"
|
|
44
46
|
}
|
|
45
47
|
}
|