@powersync/tanstack-react-query 0.0.2 → 0.0.3
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.d.ts +19 -0
- package/lib/hooks/useQuery.js +86 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +2 -0
- package/package.json +2 -1
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type CompilableQuery } from '@powersync/common';
|
|
2
|
+
import * as Tanstack from '@tanstack/react-query';
|
|
3
|
+
export type PowerSyncQueryOptions<T> = {
|
|
4
|
+
query?: string | CompilableQuery<T>;
|
|
5
|
+
parameters?: any[];
|
|
6
|
+
};
|
|
7
|
+
export type UseBaseQueryOptions<TQueryOptions> = TQueryOptions & PowerSyncQueryOptions<any>;
|
|
8
|
+
export declare function useQuery<TData = unknown, TError = Tanstack.DefaultError>(options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData, TError>> & {
|
|
9
|
+
query?: undefined;
|
|
10
|
+
}, queryClient?: Tanstack.QueryClient): Tanstack.UseQueryResult<TData, TError>;
|
|
11
|
+
export declare function useQuery<TData = unknown, TError = Tanstack.DefaultError>(options: UseBaseQueryOptions<Tanstack.UseQueryOptions<TData[], TError>> & {
|
|
12
|
+
query: string | CompilableQuery<TData>;
|
|
13
|
+
}, queryClient?: Tanstack.QueryClient): Tanstack.UseQueryResult<TData[], TError>;
|
|
14
|
+
export declare function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData, TError>> & {
|
|
15
|
+
query?: undefined;
|
|
16
|
+
}, queryClient?: Tanstack.QueryClient): Tanstack.UseSuspenseQueryResult<TData, TError>;
|
|
17
|
+
export declare function useSuspenseQuery<TData = unknown, TError = Tanstack.DefaultError>(options: UseBaseQueryOptions<Tanstack.UseSuspenseQueryOptions<TData[], TError>> & {
|
|
18
|
+
query: string | CompilableQuery<TData>;
|
|
19
|
+
}, queryClient?: Tanstack.QueryClient): Tanstack.UseSuspenseQueryResult<TData[], TError>;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { parseQuery } from '@powersync/common';
|
|
2
|
+
import { usePowerSync } from '@powersync/react';
|
|
3
|
+
import React from 'react';
|
|
4
|
+
import * as Tanstack from '@tanstack/react-query';
|
|
5
|
+
export function useQuery(options, queryClient = Tanstack.useQueryClient()) {
|
|
6
|
+
return useQueryCore(options, queryClient, Tanstack.useQuery);
|
|
7
|
+
}
|
|
8
|
+
export function useSuspenseQuery(options, queryClient = Tanstack.useQueryClient()) {
|
|
9
|
+
return useQueryCore(options, queryClient, Tanstack.useSuspenseQuery);
|
|
10
|
+
}
|
|
11
|
+
function useQueryCore(options, queryClient, useQueryFn) {
|
|
12
|
+
const powerSync = usePowerSync();
|
|
13
|
+
if (!powerSync) {
|
|
14
|
+
throw new Error('PowerSync is not available');
|
|
15
|
+
}
|
|
16
|
+
const [error, setError] = React.useState(null);
|
|
17
|
+
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]);
|
|
24
|
+
let sqlStatement = '';
|
|
25
|
+
let queryParameters = [];
|
|
26
|
+
if (query) {
|
|
27
|
+
try {
|
|
28
|
+
const parsedQuery = parseQuery(query, parameters);
|
|
29
|
+
sqlStatement = parsedQuery.sqlStatement;
|
|
30
|
+
queryParameters = parsedQuery.parameters;
|
|
31
|
+
}
|
|
32
|
+
catch (e) {
|
|
33
|
+
setError(e);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const fetchTables = async () => {
|
|
37
|
+
try {
|
|
38
|
+
const tables = await powerSync.resolveTables(sqlStatement, queryParameters);
|
|
39
|
+
setTables(tables);
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
setError(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
React.useEffect(() => {
|
|
46
|
+
if (!query)
|
|
47
|
+
return;
|
|
48
|
+
(async () => {
|
|
49
|
+
await fetchTables();
|
|
50
|
+
})();
|
|
51
|
+
}, [powerSync, sqlStatement, queryParameters]);
|
|
52
|
+
const queryFn = React.useCallback(async () => {
|
|
53
|
+
if (error) {
|
|
54
|
+
return Promise.reject(error);
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
return typeof query == 'string' ? powerSync.getAll(sqlStatement, queryParameters) : query.execute();
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
return Promise.reject(e);
|
|
61
|
+
}
|
|
62
|
+
}, [powerSync, query, parameters, options.queryKey, error]);
|
|
63
|
+
React.useEffect(() => {
|
|
64
|
+
if (error || !query)
|
|
65
|
+
return () => { };
|
|
66
|
+
const abort = new AbortController();
|
|
67
|
+
powerSync.onChangeWithCallback({
|
|
68
|
+
onChange: () => {
|
|
69
|
+
queryClient.invalidateQueries({
|
|
70
|
+
queryKey: options.queryKey
|
|
71
|
+
});
|
|
72
|
+
},
|
|
73
|
+
onError: (e) => {
|
|
74
|
+
setError(e);
|
|
75
|
+
}
|
|
76
|
+
}, {
|
|
77
|
+
tables,
|
|
78
|
+
signal: abort.signal
|
|
79
|
+
});
|
|
80
|
+
return () => abort.abort();
|
|
81
|
+
}, [powerSync, options.queryKey, queryClient, tables, error]);
|
|
82
|
+
return useQueryFn({
|
|
83
|
+
...resolvedOptions,
|
|
84
|
+
queryFn: query ? queryFn : resolvedOptions.queryFn
|
|
85
|
+
}, queryClient);
|
|
86
|
+
}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powersync/tanstack-react-query",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"registry": "https://registry.npmjs.org/",
|
|
6
6
|
"access": "public"
|
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
},
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "tsc -b",
|
|
41
|
+
"build:prod": "tsc -b --sourceMap false",
|
|
41
42
|
"clean": "rm -rf lib tsconfig.tsbuildinfo",
|
|
42
43
|
"watch": "tsc -b -w"
|
|
43
44
|
}
|