@@ -31,48 +31,86 @@ export type EnhancedExplicitQueryResults<T extends readonly unknown[]> = {
3131 [ K in keyof T ] : Tanstack . UseQueryResult < T [ K ] [ ] > & { queryKey : Tanstack . QueryKey } ;
3232} ;
3333
34+ export type UseQueriesExplicitWithCombineOptions < T extends readonly unknown [ ] , TCombined > = {
35+ queries : readonly [ ...{ [ K in keyof T ] : PowerSyncQueryOption < T [ K ] > } ] ;
36+ combine : ( results : EnhancedExplicitQueryResults < T > ) => TCombined ;
37+ } ;
38+
39+ export type UseQueriesExplicitWithoutCombineOptions < T extends readonly unknown [ ] > = {
40+ queries : readonly [ ...{ [ K in keyof T ] : PowerSyncQueryOption < T [ K ] > } ] ;
41+ combine ?: undefined ;
42+ } ;
43+
44+ export type UseQueriesAutoInferenceWithCombineOptions < TQueries extends readonly PowerSyncQueryOption [ ] , TCombined > = {
45+ queries : readonly [ ...TQueries ] ;
46+ combine : ( results : EnhancedInferQueryResults < TQueries > ) => TCombined ;
47+ } ;
48+
49+ export type UseQueriesAutoInferenceWithoutCombineOptions < TQueries extends readonly PowerSyncQueryOption [ ] > = {
50+ queries : readonly [ ...TQueries ] ;
51+ combine ?: undefined ;
52+ } ;
53+
54+ export type UseQueriesBaseOptions = {
55+ queries : readonly ( Tanstack . UseQueryOptions & PowerSyncQueryOptions < unknown > ) [ ] ;
56+ combine ?: ( results : ( Tanstack . UseQueryResult < unknown , unknown > & { queryKey : Tanstack . QueryKey } ) [ ] ) => unknown ;
57+ } ;
58+
3459// Explicit generic typing with combine
3560export function useQueries < T extends readonly unknown [ ] , TCombined > (
36- options : {
37- queries : readonly [ ...{ [ K in keyof T ] : PowerSyncQueryOption < T [ K ] > } ] ;
38- combine : ( results : EnhancedExplicitQueryResults < T > ) => TCombined ;
39- } ,
61+ options : UseQueriesExplicitWithCombineOptions < T , TCombined > ,
4062 queryClient ?: Tanstack . QueryClient
4163) : TCombined ;
4264
4365// Explicit generic typing without combine
4466export function useQueries < T extends readonly unknown [ ] > (
45- options : {
46- queries : readonly [ ...{ [ K in keyof T ] : PowerSyncQueryOption < T [ K ] > } ] ;
47- combine ?: undefined ;
48- } ,
67+ options : UseQueriesExplicitWithoutCombineOptions < T > ,
4968 queryClient ?: Tanstack . QueryClient
5069) : ExplicitQueryResults < T > ;
5170
5271// Auto inference with combine
5372export function useQueries < TQueries extends readonly PowerSyncQueryOption [ ] , TCombined > (
54- options : {
55- queries : readonly [ ...TQueries ] ;
56- combine : ( results : EnhancedInferQueryResults < TQueries > ) => TCombined ;
57- } ,
73+ options : UseQueriesAutoInferenceWithCombineOptions < TQueries , TCombined > ,
5874 queryClient ?: Tanstack . QueryClient
5975) : TCombined ;
6076
6177// Auto inference without combine
6278export function useQueries < TQueries extends readonly PowerSyncQueryOption [ ] > (
63- options : {
64- queries : readonly [ ...TQueries ] ;
65- combine ?: undefined ;
66- } ,
79+ options : UseQueriesAutoInferenceWithoutCombineOptions < TQueries > ,
6780 queryClient ?: Tanstack . QueryClient
6881) : InferQueryResults < TQueries > ;
6982
70- // Implementation
83+ /**
84+ * @example
85+ * ```
86+ * const { data, error, isLoading } = useQueries({
87+ * queries: [
88+ * { queryKey: ['lists'], query: 'SELECT * from lists' },
89+ * { queryKey: ['todos'], query: 'SELECT * from todos' }
90+ * ],
91+ * })
92+ * ```
93+ *
94+ * @example
95+ * ```
96+ * const ids = [1, 2, 3];
97+ * const combinedQueries = useQueries({
98+ * queries: ids.map((id) => ({
99+ * queryKey: ['post', id],
100+ * query: 'SELECT * from lists where id = ?',
101+ * parameters: [id],
102+ * })),
103+ * combine: (results) => {
104+ * return {
105+ * data: results.map((result) => result.data),
106+ * pending: results.some((result) => result.isPending),
107+ * }
108+ * },
109+ * });
110+ * ```
111+ */
71112export function useQueries (
72- options : {
73- queries : readonly ( Tanstack . UseQueryOptions & PowerSyncQueryOptions < unknown > ) [ ] ;
74- combine ?: ( results : ( Tanstack . UseQueryResult < unknown , unknown > & { queryKey : Tanstack . QueryKey } ) [ ] ) => unknown ;
75- } ,
113+ options : UseQueriesBaseOptions ,
76114 queryClient : Tanstack . QueryClient = Tanstack . useQueryClient ( )
77115) {
78116 const powerSync = usePowerSync ( ) ;
0 commit comments