react-query-firebase 2.0.0-rc5 → 2.0.0-rc7
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/package.json +16 -13
- package/{dist/src → react-native}/firestore/useCollectionReference.d.ts +1 -1
- package/{src → react-native}/firestore/useCollectionReference.ts +1 -1
- package/{dist/src → react-native}/firestore/useDocReference.d.ts +1 -1
- package/{dist/src → react-native}/firestore/useDocReference.js +4 -9
- package/{src → react-native}/firestore/useDocReference.ts +5 -14
- package/web/analytics/index.d.ts +3 -0
- package/web/analytics/index.js +3 -0
- package/web/analytics/index.ts +3 -0
- package/web/analytics/useAnalytics.d.ts +5 -0
- package/web/analytics/useAnalytics.js +10 -0
- package/web/analytics/useAnalytics.ts +12 -0
- package/web/analytics/useLogEvent.d.ts +17 -0
- package/web/analytics/useLogEvent.js +21 -0
- package/web/analytics/useLogEvent.ts +34 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.d.ts +10 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.js +16 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.ts +22 -0
- package/web/auth/index.d.ts +13 -0
- package/web/auth/index.js +13 -0
- package/web/auth/index.ts +13 -0
- package/web/auth/mutation-keys.d.ts +7 -0
- package/web/auth/mutation-keys.js +19 -0
- package/web/auth/mutation-keys.ts +19 -0
- package/web/auth/useAuth.d.ts +5 -0
- package/web/auth/useAuth.js +10 -0
- package/web/auth/useAuth.ts +12 -0
- package/web/auth/useAuthStateReady.d.ts +6 -0
- package/web/auth/useAuthStateReady.js +19 -0
- package/web/auth/useAuthStateReady.ts +24 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.ts +33 -0
- package/web/auth/useCurrentUser.d.ts +6 -0
- package/web/auth/useCurrentUser.js +20 -0
- package/web/auth/useCurrentUser.ts +24 -0
- package/web/auth/useIdToken.d.ts +30 -0
- package/web/auth/useIdToken.js +66 -0
- package/web/auth/useIdToken.ts +83 -0
- package/web/auth/useReauthenticateWitCredentialMutation.d.ts +16 -0
- package/web/auth/useReauthenticateWitCredentialMutation.js +18 -0
- package/web/auth/useReauthenticateWitCredentialMutation.ts +31 -0
- package/web/auth/useReauthenticateWitRedirectMutation.d.ts +17 -0
- package/web/auth/useReauthenticateWitRedirectMutation.js +18 -0
- package/web/auth/useReauthenticateWitRedirectMutation.ts +37 -0
- package/web/auth/useSendEmailVerificationMutation.d.ts +13 -0
- package/web/auth/useSendEmailVerificationMutation.js +16 -0
- package/web/auth/useSendEmailVerificationMutation.ts +28 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.ts +34 -0
- package/web/auth/useSignInWitRedirectMutation.d.ts +14 -0
- package/web/auth/useSignInWitRedirectMutation.js +18 -0
- package/web/auth/useSignInWitRedirectMutation.ts +33 -0
- package/web/auth/useSignOutMutation.d.ts +10 -0
- package/web/auth/useSignOutMutation.js +19 -0
- package/web/auth/useSignOutMutation.ts +25 -0
- package/web/auth/useUpdateProfileMutation.d.ts +15 -0
- package/web/auth/useUpdateProfileMutation.js +17 -0
- package/web/auth/useUpdateProfileMutation.ts +30 -0
- package/web/context/FirebaseContext.d.ts +19 -0
- package/web/context/FirebaseContext.js +8 -0
- package/web/context/FirebaseContext.ts +25 -0
- package/web/context/FirebaseContextProvider.d.ts +127 -0
- package/web/context/FirebaseContextProvider.js +100 -0
- package/web/context/FirebaseContextProvider.tsx +231 -0
- package/web/context/index.d.ts +1 -0
- package/web/context/index.js +1 -0
- package/web/context/index.ts +1 -0
- package/web/firestore/index.d.ts +16 -0
- package/web/firestore/index.js +16 -0
- package/web/firestore/index.ts +16 -0
- package/web/firestore/useAddDocMutation.d.ts +23 -0
- package/web/firestore/useAddDocMutation.js +26 -0
- package/web/firestore/useAddDocMutation.ts +62 -0
- package/web/firestore/useBatchWrite.d.ts +15 -0
- package/web/firestore/useBatchWrite.js +21 -0
- package/web/firestore/useBatchWrite.ts +31 -0
- package/web/firestore/useCollectionReference.d.ts +18 -0
- package/web/firestore/useCollectionReference.js +24 -0
- package/web/firestore/useCollectionReference.ts +37 -0
- package/web/firestore/useCompositeFilter.d.ts +30 -0
- package/web/firestore/useCompositeFilter.js +43 -0
- package/web/firestore/useCompositeFilter.ts +86 -0
- package/web/firestore/useCountQuery.d.ts +23 -0
- package/web/firestore/useCountQuery.js +30 -0
- package/web/firestore/useCountQuery.ts +65 -0
- package/web/firestore/useDeleteDocMutation.d.ts +18 -0
- package/web/firestore/useDeleteDocMutation.js +23 -0
- package/web/firestore/useDeleteDocMutation.ts +47 -0
- package/web/firestore/useDocReference.d.ts +19 -0
- package/web/firestore/useDocReference.js +45 -0
- package/web/firestore/useDocReference.ts +68 -0
- package/web/firestore/useDocReferences.d.ts +23 -0
- package/web/firestore/useDocReferences.js +31 -0
- package/web/firestore/useDocReferences.ts +44 -0
- package/web/firestore/useFirestore.d.ts +7 -0
- package/web/firestore/useFirestore.js +12 -0
- package/web/firestore/useFirestore.ts +13 -0
- package/web/firestore/useGetDocData.d.ts +18 -0
- package/web/firestore/useGetDocData.js +18 -0
- package/web/firestore/useGetDocData.ts +41 -0
- package/web/firestore/useGetRealtimeDocData.d.ts +57 -0
- package/web/firestore/useGetRealtimeDocData.js +56 -0
- package/web/firestore/useGetRealtimeDocData.ts +104 -0
- package/web/firestore/useInfiniteQuery.d.ts +18 -0
- package/web/firestore/useInfiniteQuery.js +28 -0
- package/web/firestore/useInfiniteQuery.ts +91 -0
- package/web/firestore/useQuery.d.ts +25 -0
- package/web/firestore/useQuery.js +34 -0
- package/web/firestore/useQuery.ts +72 -0
- package/web/firestore/useRunTransaction.d.ts +15 -0
- package/web/firestore/useRunTransaction.js +19 -0
- package/web/firestore/useRunTransaction.ts +31 -0
- package/web/firestore/useSetDocMutation.d.ts +20 -0
- package/web/firestore/useSetDocMutation.js +25 -0
- package/web/firestore/useSetDocMutation.ts +52 -0
- package/web/firestore/useUpdateDocMutation.d.ts +31 -0
- package/web/firestore/useUpdateDocMutation.js +37 -0
- package/web/firestore/useUpdateDocMutation.ts +74 -0
- package/web/firestore/utils/getDocData.d.ts +18 -0
- package/web/firestore/utils/getDocData.js +22 -0
- package/web/firestore/utils/getDocData.ts +39 -0
- package/web/firestore/utils/getDocRef.d.ts +19 -0
- package/web/firestore/utils/getDocRef.js +25 -0
- package/web/firestore/utils/getDocRef.ts +46 -0
- package/web/firestore/utils/getDocSnap.d.ts +20 -0
- package/web/firestore/utils/getDocSnap.js +25 -0
- package/web/firestore/utils/getDocSnap.ts +41 -0
- package/web/index.d.ts +5 -0
- package/web/index.js +5 -0
- package/web/index.ts +5 -0
- package/web/remoteConfig/index.d.ts +3 -0
- package/web/remoteConfig/index.js +3 -0
- package/web/remoteConfig/index.ts +3 -0
- package/web/remoteConfig/useFetchAndActivate.d.ts +11 -0
- package/web/remoteConfig/useFetchAndActivate.js +31 -0
- package/web/remoteConfig/useFetchAndActivate.ts +36 -0
- package/web/remoteConfig/useGetValue.d.ts +7 -0
- package/web/remoteConfig/useGetValue.js +15 -0
- package/web/remoteConfig/useGetValue.ts +16 -0
- package/web/remoteConfig/useRemoteConfig.d.ts +5 -0
- package/web/remoteConfig/useRemoteConfig.js +10 -0
- package/web/remoteConfig/useRemoteConfig.ts +11 -0
- /package/{dist/src → react-native}/analytics/index.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/index.js +0 -0
- /package/{src → react-native}/analytics/index.ts +0 -0
- /package/{dist/src → react-native}/analytics/useAnalytics.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useAnalytics.js +0 -0
- /package/{src → react-native}/analytics/useAnalytics.ts +0 -0
- /package/{dist/src → react-native}/analytics/useLogEvent.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useLogEvent.js +0 -0
- /package/{src → react-native}/analytics/useLogEvent.ts +0 -0
- /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.d.ts +0 -0
- /package/{dist/src → react-native}/analytics/useSetAnalyticsCollectionEnabled.js +0 -0
- /package/{src → react-native}/analytics/useSetAnalyticsCollectionEnabled.ts +0 -0
- /package/{dist/src → react-native}/auth/index.d.ts +0 -0
- /package/{dist/src → react-native}/auth/index.js +0 -0
- /package/{src → react-native}/auth/index.ts +0 -0
- /package/{dist/src → react-native}/auth/mutation-keys.d.ts +0 -0
- /package/{dist/src → react-native}/auth/mutation-keys.js +0 -0
- /package/{src → react-native}/auth/mutation-keys.ts +0 -0
- /package/{dist/src → react-native}/auth/useAuth.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useAuth.js +0 -0
- /package/{src → react-native}/auth/useAuth.ts +0 -0
- /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.js +0 -0
- /package/{src → react-native}/auth/useCreateUserWitEmailAndPasswordMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useCurrentUser.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useCurrentUser.js +0 -0
- /package/{src → react-native}/auth/useCurrentUser.ts +0 -0
- /package/{dist/src → react-native}/auth/useIdToken.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useIdToken.js +0 -0
- /package/{src → react-native}/auth/useIdToken.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitCredentialMutation.js +0 -0
- /package/{src → react-native}/auth/useReauthenticateWitCredentialMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useReauthenticateWitRedirectMutation.js +0 -0
- /package/{src → react-native}/auth/useReauthenticateWitRedirectMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSendEmailVerificationMutation.js +0 -0
- /package/{src → react-native}/auth/useSendEmailVerificationMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitEmailAndPasswordMutation.js +0 -0
- /package/{src → react-native}/auth/useSignInWitEmailAndPasswordMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignInWitRedirectMutation.js +0 -0
- /package/{src → react-native}/auth/useSignInWitRedirectMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignOutMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useSignOutMutation.js +0 -0
- /package/{src → react-native}/auth/useSignOutMutation.ts +0 -0
- /package/{dist/src → react-native}/auth/useUpdateProfileMutation.d.ts +0 -0
- /package/{dist/src → react-native}/auth/useUpdateProfileMutation.js +0 -0
- /package/{src → react-native}/auth/useUpdateProfileMutation.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContext.d.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContext.js +0 -0
- /package/{src → react-native}/context/FirebaseContext.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContextProvider.d.ts +0 -0
- /package/{dist/src → react-native}/context/FirebaseContextProvider.js +0 -0
- /package/{src → react-native}/context/FirebaseContextProvider.tsx +0 -0
- /package/{dist/src → react-native}/context/index.d.ts +0 -0
- /package/{dist/src → react-native}/context/index.js +0 -0
- /package/{src → react-native}/context/index.ts +0 -0
- /package/{dist/src → react-native}/firestore/index.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/index.js +0 -0
- /package/{src → react-native}/firestore/index.ts +0 -0
- /package/{dist/src → react-native}/firestore/useAddDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useAddDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useAddDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useBatchWrite.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useBatchWrite.js +0 -0
- /package/{src → react-native}/firestore/useBatchWrite.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCollectionReference.js +0 -0
- /package/{dist/src → react-native}/firestore/useCompositeFilter.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCompositeFilter.js +0 -0
- /package/{src → react-native}/firestore/useCompositeFilter.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCountQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCountQuery.js +0 -0
- /package/{src → react-native}/firestore/useCountQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDeleteDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDeleteDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useDeleteDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDocReferences.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDocReferences.js +0 -0
- /package/{src → react-native}/firestore/useDocReferences.ts +0 -0
- /package/{dist/src → react-native}/firestore/useFirestore.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useFirestore.js +0 -0
- /package/{src → react-native}/firestore/useFirestore.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetDocData.js +0 -0
- /package/{src → react-native}/firestore/useGetDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useGetRealtimeDocData.js +0 -0
- /package/{src → react-native}/firestore/useGetRealtimeDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/useInfiniteQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useInfiniteQuery.js +0 -0
- /package/{src → react-native}/firestore/useInfiniteQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useQuery.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useQuery.js +0 -0
- /package/{src → react-native}/firestore/useQuery.ts +0 -0
- /package/{dist/src → react-native}/firestore/useRunTransaction.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useRunTransaction.js +0 -0
- /package/{src → react-native}/firestore/useRunTransaction.ts +0 -0
- /package/{dist/src → react-native}/firestore/useSetDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useSetDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useSetDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/useUpdateDocMutation.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useUpdateDocMutation.js +0 -0
- /package/{src → react-native}/firestore/useUpdateDocMutation.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocData.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocData.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocData.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocRef.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocRef.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocRef.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocSnap.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/utils/getDocSnap.js +0 -0
- /package/{src → react-native}/firestore/utils/getDocSnap.ts +0 -0
- /package/{dist/src → react-native}/index.d.ts +0 -0
- /package/{dist/src → react-native}/index.js +0 -0
- /package/{src → react-native}/index.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/index.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/index.js +0 -0
- /package/{src → react-native}/remoteConfig/index.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useFetchAndActivate.js +0 -0
- /package/{src → react-native}/remoteConfig/useFetchAndActivate.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useGetValue.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useGetValue.js +0 -0
- /package/{src → react-native}/remoteConfig/useGetValue.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.d.ts +0 -0
- /package/{dist/src → react-native}/remoteConfig/useRemoteConfig.js +0 -0
- /package/{src → react-native}/remoteConfig/useRemoteConfig.ts +0 -0
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DocumentData,
|
|
3
|
+
FirestoreDataConverter,
|
|
4
|
+
getDocs,
|
|
5
|
+
Query,
|
|
6
|
+
query,
|
|
7
|
+
QueryCompositeFilterConstraint,
|
|
8
|
+
QueryConstraint,
|
|
9
|
+
QueryNonFilterConstraint
|
|
10
|
+
} from "firebase/firestore";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
useInfiniteQuery as useInfiniteReactQuery,
|
|
14
|
+
UseInfiniteQueryOptions as UseReactInfiniteQueryOptions,
|
|
15
|
+
QueryKey,
|
|
16
|
+
UseInfiniteQueryResult,
|
|
17
|
+
InfiniteData
|
|
18
|
+
} from "@tanstack/react-query";
|
|
19
|
+
|
|
20
|
+
type UseInfiniteQueryOptions<
|
|
21
|
+
AppModelType extends DocumentData = DocumentData,
|
|
22
|
+
DbModelType extends DocumentData = DocumentData,
|
|
23
|
+
TQueryKey extends QueryKey = QueryKey
|
|
24
|
+
> = {
|
|
25
|
+
options: Omit<
|
|
26
|
+
UseReactInfiniteQueryOptions<
|
|
27
|
+
AppModelType[],
|
|
28
|
+
Error,
|
|
29
|
+
InfiniteData<AppModelType[]>,
|
|
30
|
+
AppModelType[],
|
|
31
|
+
TQueryKey,
|
|
32
|
+
QueryConstraint
|
|
33
|
+
>,
|
|
34
|
+
"queryFn"
|
|
35
|
+
> &
|
|
36
|
+
Required<
|
|
37
|
+
Pick<
|
|
38
|
+
UseReactInfiniteQueryOptions<
|
|
39
|
+
AppModelType[],
|
|
40
|
+
Error,
|
|
41
|
+
InfiniteData<AppModelType[]>,
|
|
42
|
+
AppModelType[],
|
|
43
|
+
TQueryKey,
|
|
44
|
+
QueryConstraint
|
|
45
|
+
>,
|
|
46
|
+
"queryKey"
|
|
47
|
+
>
|
|
48
|
+
>;
|
|
49
|
+
query: Query<AppModelType, DbModelType>;
|
|
50
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
51
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
52
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
57
|
+
* It fetches data in pages and can load more as required.
|
|
58
|
+
*
|
|
59
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
60
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
61
|
+
*/
|
|
62
|
+
export const useInfiniteQuery = <
|
|
63
|
+
AppModelType extends DocumentData = DocumentData,
|
|
64
|
+
DbModelType extends DocumentData = DocumentData
|
|
65
|
+
>({
|
|
66
|
+
options,
|
|
67
|
+
query: queryReference,
|
|
68
|
+
queryConstraints = [],
|
|
69
|
+
compositeFilter,
|
|
70
|
+
converter
|
|
71
|
+
}: UseInfiniteQueryOptions<AppModelType, DbModelType>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
|
|
72
|
+
return useInfiniteReactQuery({
|
|
73
|
+
...options,
|
|
74
|
+
queryFn: async ({ pageParam }) => {
|
|
75
|
+
const allQueryConstraints = [...queryConstraints, ...(pageParam ? [pageParam] : [])];
|
|
76
|
+
const queryToExecute = compositeFilter
|
|
77
|
+
? query(queryReference, compositeFilter, ...(allQueryConstraints as QueryNonFilterConstraint[]))
|
|
78
|
+
: query(queryReference, ...allQueryConstraints);
|
|
79
|
+
|
|
80
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
81
|
+
const docs: AppModelType[] = [];
|
|
82
|
+
|
|
83
|
+
if (querySnapshot) {
|
|
84
|
+
querySnapshot.forEach((doc) => {
|
|
85
|
+
docs.push(doc.data());
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return docs;
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DocumentData, FirestoreDataConverter, Query, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
|
+
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
|
|
4
|
+
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> & Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
5
|
+
query: Query<AppModelType, DbModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
8
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
12
|
+
*
|
|
13
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
14
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
15
|
+
*
|
|
16
|
+
* @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
17
|
+
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
18
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
19
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
20
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
21
|
+
*
|
|
22
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
23
|
+
*/
|
|
24
|
+
export declare const useQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, query: queryReference, queryConstraints, compositeFilter, converter }: UseQueryOptions<AppModelType, DbModelType>) => UseQueryResult<AppModelType[]>;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getDocs, query } from "firebase/firestore";
|
|
2
|
+
import { useQuery as useReactQuery } from "@tanstack/react-query";
|
|
3
|
+
/**
|
|
4
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
5
|
+
*
|
|
6
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
7
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
8
|
+
*
|
|
9
|
+
* @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
10
|
+
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
11
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
12
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
13
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
14
|
+
*
|
|
15
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
16
|
+
*/
|
|
17
|
+
export const useQuery = ({ options, query: queryReference, queryConstraints = [], compositeFilter, converter }) => {
|
|
18
|
+
return useReactQuery({
|
|
19
|
+
...options,
|
|
20
|
+
queryFn: async () => {
|
|
21
|
+
const queryToExecute = compositeFilter
|
|
22
|
+
? query(queryReference, compositeFilter, ...queryConstraints)
|
|
23
|
+
: query(queryReference, ...queryConstraints);
|
|
24
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
25
|
+
const docs = [];
|
|
26
|
+
if (querySnapshot) {
|
|
27
|
+
querySnapshot.forEach((doc) => {
|
|
28
|
+
docs.push(doc.data());
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return docs;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DocumentData,
|
|
3
|
+
FirestoreDataConverter,
|
|
4
|
+
getDocs,
|
|
5
|
+
Query,
|
|
6
|
+
query,
|
|
7
|
+
QueryCompositeFilterConstraint,
|
|
8
|
+
QueryConstraint,
|
|
9
|
+
QueryNonFilterConstraint
|
|
10
|
+
} from "firebase/firestore";
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
UseQueryResult,
|
|
14
|
+
useQuery as useReactQuery,
|
|
15
|
+
UseQueryOptions as UseReactQueryOptions
|
|
16
|
+
} from "@tanstack/react-query";
|
|
17
|
+
|
|
18
|
+
type UseQueryOptions<
|
|
19
|
+
AppModelType extends DocumentData = DocumentData,
|
|
20
|
+
DbModelType extends DocumentData = DocumentData
|
|
21
|
+
> = {
|
|
22
|
+
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> &
|
|
23
|
+
Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
24
|
+
query: Query<AppModelType, DbModelType>;
|
|
25
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
26
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
27
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
32
|
+
*
|
|
33
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
34
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
35
|
+
*
|
|
36
|
+
* @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
37
|
+
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
38
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
39
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
40
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
41
|
+
*
|
|
42
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
43
|
+
*/
|
|
44
|
+
export const useQuery = <
|
|
45
|
+
AppModelType extends DocumentData = DocumentData,
|
|
46
|
+
DbModelType extends DocumentData = DocumentData
|
|
47
|
+
>({
|
|
48
|
+
options,
|
|
49
|
+
query: queryReference,
|
|
50
|
+
queryConstraints = [],
|
|
51
|
+
compositeFilter,
|
|
52
|
+
converter
|
|
53
|
+
}: UseQueryOptions<AppModelType, DbModelType>): UseQueryResult<AppModelType[]> => {
|
|
54
|
+
return useReactQuery({
|
|
55
|
+
...options,
|
|
56
|
+
queryFn: async () => {
|
|
57
|
+
const queryToExecute = compositeFilter
|
|
58
|
+
? query(queryReference, compositeFilter, ...(queryConstraints as QueryNonFilterConstraint[]))
|
|
59
|
+
: query(queryReference, ...queryConstraints);
|
|
60
|
+
|
|
61
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
62
|
+
const docs: AppModelType[] = [];
|
|
63
|
+
|
|
64
|
+
if (querySnapshot) {
|
|
65
|
+
querySnapshot.forEach((doc) => {
|
|
66
|
+
docs.push(doc.data());
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return docs;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { Transaction } from "firebase/firestore";
|
|
3
|
+
import { FirebaseError } from "firebase/app";
|
|
4
|
+
export type UseRunTransactionValues = <AppModelType = unknown>(transaction: Transaction) => AppModelType;
|
|
5
|
+
export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown> = {
|
|
6
|
+
options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Custom hook to execute a Firestore transaction using the useMutation hook.
|
|
10
|
+
*
|
|
11
|
+
* @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
|
|
12
|
+
* @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
|
|
13
|
+
* @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
|
|
14
|
+
*/
|
|
15
|
+
export declare const useRunTransaction: <AppModelType = unknown, TContext = unknown>({ options }: UseRunTransactionOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, FirebaseError, UseRunTransactionValues, TContext>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { runTransaction } from "firebase/firestore";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook to execute a Firestore transaction using the useMutation hook.
|
|
6
|
+
*
|
|
7
|
+
* @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
|
|
8
|
+
* @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
|
|
9
|
+
* @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
|
|
10
|
+
*/
|
|
11
|
+
export const useRunTransaction = ({ options = {} }) => {
|
|
12
|
+
const db = useFirestore();
|
|
13
|
+
return useMutation({
|
|
14
|
+
...options,
|
|
15
|
+
mutationFn: async (transactionFn) => {
|
|
16
|
+
return runTransaction(db, transactionFn);
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { runTransaction, Transaction } from "firebase/firestore";
|
|
3
|
+
|
|
4
|
+
import { FirebaseError } from "firebase/app";
|
|
5
|
+
import { useFirestore } from "./useFirestore";
|
|
6
|
+
|
|
7
|
+
export type UseRunTransactionValues = <AppModelType = unknown>(transaction: Transaction) => AppModelType;
|
|
8
|
+
|
|
9
|
+
export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown> = {
|
|
10
|
+
options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Custom hook to execute a Firestore transaction using the useMutation hook.
|
|
15
|
+
*
|
|
16
|
+
* @param {UseRunTransactionOptions<AppModelType, TContext>} options - Configuration options for running the transaction.
|
|
17
|
+
* @param {Object} options.options - Options to customize the behavior of useMutation and runTransaction.
|
|
18
|
+
* @returns {UseMutationResult} The result object from the useMutation hook, allowing to track the transaction state and outcome.
|
|
19
|
+
*/
|
|
20
|
+
export const useRunTransaction = <AppModelType = unknown, TContext = unknown>({
|
|
21
|
+
options = {}
|
|
22
|
+
}: UseRunTransactionOptions<AppModelType, TContext>) => {
|
|
23
|
+
const db = useFirestore();
|
|
24
|
+
|
|
25
|
+
return useMutation({
|
|
26
|
+
...options,
|
|
27
|
+
mutationFn: async (transactionFn) => {
|
|
28
|
+
return runTransaction<AppModelType>(db, transactionFn);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { DocumentData, DocumentReference, WithFieldValue } from "firebase/firestore";
|
|
3
|
+
import { FirebaseError } from "firebase/app";
|
|
4
|
+
export type UseSetDocMutationValues<AppModelType> = {
|
|
5
|
+
data: WithFieldValue<AppModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseSetDocMutationOptions<AppModelType = unknown, DbModelType extends DocumentData = DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: DocumentReference<AppModelType, DbModelType> | null;
|
|
9
|
+
options?: Omit<UseMutationOptions<void, FirebaseError, UseSetDocMutationValues<AppModelType>, TContext>, "mutationFn" | "mutationKey">;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Custom hook to create a mutation for setting a document in a Firestore-like database.
|
|
13
|
+
* The mutation can be configured with options and reference to specific document path.
|
|
14
|
+
*
|
|
15
|
+
* @param {UseSetDocMutationOptions<AppModelType, DbModelType, TContext>} param0 - The options for configuring the mutation, including the document reference and additional mutation options.
|
|
16
|
+
* @param {Object} param0.reference - The reference object that contains the path to the document.
|
|
17
|
+
* @param {Object} param0.options - Additional options for the mutation, can configure aspects like onSuccess or onError callbacks.
|
|
18
|
+
* @returns {MutationResult} The result of the mutation operation, which includes states like isLoading, isSuccess, isError, and methods to control the mutation process.
|
|
19
|
+
*/
|
|
20
|
+
export declare const useSetDocMutation: <AppModelType = unknown, DbModelType extends DocumentData = DocumentData, TContext = unknown>({ reference, options }: UseSetDocMutationOptions<AppModelType, DbModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, FirebaseError, UseSetDocMutationValues<AppModelType>, TContext>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { setDoc } from "firebase/firestore";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook to create a mutation for setting a document in a Firestore-like database.
|
|
6
|
+
* The mutation can be configured with options and reference to specific document path.
|
|
7
|
+
*
|
|
8
|
+
* @param {UseSetDocMutationOptions<AppModelType, DbModelType, TContext>} param0 - The options for configuring the mutation, including the document reference and additional mutation options.
|
|
9
|
+
* @param {Object} param0.reference - The reference object that contains the path to the document.
|
|
10
|
+
* @param {Object} param0.options - Additional options for the mutation, can configure aspects like onSuccess or onError callbacks.
|
|
11
|
+
* @returns {MutationResult} The result of the mutation operation, which includes states like isLoading, isSuccess, isError, and methods to control the mutation process.
|
|
12
|
+
*/
|
|
13
|
+
export const useSetDocMutation = ({ reference, options }) => {
|
|
14
|
+
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
15
|
+
return useMutation({
|
|
16
|
+
...options,
|
|
17
|
+
mutationFn: ({ data }) => {
|
|
18
|
+
if (!reference) {
|
|
19
|
+
throw new Error("Reference is undefined");
|
|
20
|
+
}
|
|
21
|
+
return setDoc(reference, data);
|
|
22
|
+
},
|
|
23
|
+
mutationKey
|
|
24
|
+
});
|
|
25
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { DocumentData, DocumentReference, setDoc, WithFieldValue } from "firebase/firestore";
|
|
3
|
+
|
|
4
|
+
import { FirebaseError } from "firebase/app";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
|
|
7
|
+
export type UseSetDocMutationValues<AppModelType> = {
|
|
8
|
+
data: WithFieldValue<AppModelType>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type UseSetDocMutationOptions<
|
|
12
|
+
AppModelType = unknown,
|
|
13
|
+
DbModelType extends DocumentData = DocumentData,
|
|
14
|
+
TContext = unknown
|
|
15
|
+
> = {
|
|
16
|
+
reference: DocumentReference<AppModelType, DbModelType> | null;
|
|
17
|
+
options?: Omit<
|
|
18
|
+
UseMutationOptions<void, FirebaseError, UseSetDocMutationValues<AppModelType>, TContext>,
|
|
19
|
+
"mutationFn" | "mutationKey"
|
|
20
|
+
>;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Custom hook to create a mutation for setting a document in a Firestore-like database.
|
|
25
|
+
* The mutation can be configured with options and reference to specific document path.
|
|
26
|
+
*
|
|
27
|
+
* @param {UseSetDocMutationOptions<AppModelType, DbModelType, TContext>} param0 - The options for configuring the mutation, including the document reference and additional mutation options.
|
|
28
|
+
* @param {Object} param0.reference - The reference object that contains the path to the document.
|
|
29
|
+
* @param {Object} param0.options - Additional options for the mutation, can configure aspects like onSuccess or onError callbacks.
|
|
30
|
+
* @returns {MutationResult} The result of the mutation operation, which includes states like isLoading, isSuccess, isError, and methods to control the mutation process.
|
|
31
|
+
*/
|
|
32
|
+
export const useSetDocMutation = <
|
|
33
|
+
AppModelType = unknown,
|
|
34
|
+
DbModelType extends DocumentData = DocumentData,
|
|
35
|
+
TContext = unknown
|
|
36
|
+
>({
|
|
37
|
+
reference,
|
|
38
|
+
options
|
|
39
|
+
}: UseSetDocMutationOptions<AppModelType, DbModelType, TContext>) => {
|
|
40
|
+
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
41
|
+
|
|
42
|
+
return useMutation({
|
|
43
|
+
...options,
|
|
44
|
+
mutationFn: ({ data }) => {
|
|
45
|
+
if (!reference) {
|
|
46
|
+
throw new Error("Reference is undefined");
|
|
47
|
+
}
|
|
48
|
+
return setDoc<AppModelType, DbModelType>(reference, data);
|
|
49
|
+
},
|
|
50
|
+
mutationKey
|
|
51
|
+
});
|
|
52
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { DocumentData, FirestoreDataConverter, DocumentReference, UpdateData } from "firebase/firestore";
|
|
3
|
+
import { FirebaseError } from "firebase/app";
|
|
4
|
+
export type UseUpdateDocMutationValues<DbModelType> = {
|
|
5
|
+
data: UpdateData<DbModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseUpdateDocMutationOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: DocumentReference<AppModelType, DbModelType> | null;
|
|
9
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
10
|
+
options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseUpdateDocMutationValues<DbModelType>, TContext>, "mutationFn" | "mutationKey">;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Custom hook that sets up a mutation for updating a document in a Firestore database.
|
|
14
|
+
*
|
|
15
|
+
* This hook utilizes `useMutation` for performing asynchronous operations to update the document
|
|
16
|
+
* and retrieve the latest data snapshot. The update functionality can be configured with a custom
|
|
17
|
+
* converter if needed.
|
|
18
|
+
*
|
|
19
|
+
* @param {UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>} options - Configuration options for the mutation,
|
|
20
|
+
* including Firestore reference, an optional Firestore data converter, and additional mutation options.
|
|
21
|
+
*
|
|
22
|
+
* `reference` - The Firestore document reference that identifies the document to be updated.
|
|
23
|
+
*
|
|
24
|
+
* `converter` - An optional Firestore converter for transforming the database response into a custom type.
|
|
25
|
+
*
|
|
26
|
+
* `options` - Additional options that customize the mutation's behavior.
|
|
27
|
+
*
|
|
28
|
+
* @returns {UseMutationResult<AppModelType, Error, {data: AppModelType}, TContext>} An object returned by `useMutation`
|
|
29
|
+
* which includes functions to start the mutation and properties that represent the different states of the mutation.
|
|
30
|
+
*/
|
|
31
|
+
export declare const useUpdateDocMutation: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown>({ reference, converter, options }: UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, FirebaseError, UseUpdateDocMutationValues<DbModelType>, TContext>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { updateDoc, getDoc } from "firebase/firestore";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook that sets up a mutation for updating a document in a Firestore database.
|
|
6
|
+
*
|
|
7
|
+
* This hook utilizes `useMutation` for performing asynchronous operations to update the document
|
|
8
|
+
* and retrieve the latest data snapshot. The update functionality can be configured with a custom
|
|
9
|
+
* converter if needed.
|
|
10
|
+
*
|
|
11
|
+
* @param {UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>} options - Configuration options for the mutation,
|
|
12
|
+
* including Firestore reference, an optional Firestore data converter, and additional mutation options.
|
|
13
|
+
*
|
|
14
|
+
* `reference` - The Firestore document reference that identifies the document to be updated.
|
|
15
|
+
*
|
|
16
|
+
* `converter` - An optional Firestore converter for transforming the database response into a custom type.
|
|
17
|
+
*
|
|
18
|
+
* `options` - Additional options that customize the mutation's behavior.
|
|
19
|
+
*
|
|
20
|
+
* @returns {UseMutationResult<AppModelType, Error, {data: AppModelType}, TContext>} An object returned by `useMutation`
|
|
21
|
+
* which includes functions to start the mutation and properties that represent the different states of the mutation.
|
|
22
|
+
*/
|
|
23
|
+
export const useUpdateDocMutation = ({ reference, converter, options = {} }) => {
|
|
24
|
+
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
25
|
+
return useMutation({
|
|
26
|
+
...options,
|
|
27
|
+
mutationFn: async ({ data }) => {
|
|
28
|
+
if (!reference) {
|
|
29
|
+
throw new Error("Reference is undefined");
|
|
30
|
+
}
|
|
31
|
+
await updateDoc(reference, data);
|
|
32
|
+
const docSnap = await getDoc(converter ? reference.withConverter(converter) : reference);
|
|
33
|
+
return docSnap.data();
|
|
34
|
+
},
|
|
35
|
+
mutationKey
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import {
|
|
3
|
+
DocumentData,
|
|
4
|
+
updateDoc,
|
|
5
|
+
getDoc,
|
|
6
|
+
FirestoreDataConverter,
|
|
7
|
+
DocumentReference,
|
|
8
|
+
UpdateData
|
|
9
|
+
} from "firebase/firestore";
|
|
10
|
+
|
|
11
|
+
import { FirebaseError } from "firebase/app";
|
|
12
|
+
import { useMemo } from "react";
|
|
13
|
+
|
|
14
|
+
export type UseUpdateDocMutationValues<DbModelType> = {
|
|
15
|
+
data: UpdateData<DbModelType>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export type UseUpdateDocMutationOptions<
|
|
19
|
+
AppModelType extends DocumentData = DocumentData,
|
|
20
|
+
DbModelType extends DocumentData = DocumentData,
|
|
21
|
+
TContext = unknown
|
|
22
|
+
> = {
|
|
23
|
+
reference: DocumentReference<AppModelType, DbModelType> | null;
|
|
24
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
25
|
+
options?: Omit<
|
|
26
|
+
UseMutationOptions<AppModelType, FirebaseError, UseUpdateDocMutationValues<DbModelType>, TContext>,
|
|
27
|
+
"mutationFn" | "mutationKey"
|
|
28
|
+
>;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Custom hook that sets up a mutation for updating a document in a Firestore database.
|
|
33
|
+
*
|
|
34
|
+
* This hook utilizes `useMutation` for performing asynchronous operations to update the document
|
|
35
|
+
* and retrieve the latest data snapshot. The update functionality can be configured with a custom
|
|
36
|
+
* converter if needed.
|
|
37
|
+
*
|
|
38
|
+
* @param {UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>} options - Configuration options for the mutation,
|
|
39
|
+
* including Firestore reference, an optional Firestore data converter, and additional mutation options.
|
|
40
|
+
*
|
|
41
|
+
* `reference` - The Firestore document reference that identifies the document to be updated.
|
|
42
|
+
*
|
|
43
|
+
* `converter` - An optional Firestore converter for transforming the database response into a custom type.
|
|
44
|
+
*
|
|
45
|
+
* `options` - Additional options that customize the mutation's behavior.
|
|
46
|
+
*
|
|
47
|
+
* @returns {UseMutationResult<AppModelType, Error, {data: AppModelType}, TContext>} An object returned by `useMutation`
|
|
48
|
+
* which includes functions to start the mutation and properties that represent the different states of the mutation.
|
|
49
|
+
*/
|
|
50
|
+
export const useUpdateDocMutation = <
|
|
51
|
+
AppModelType extends DocumentData = DocumentData,
|
|
52
|
+
DbModelType extends DocumentData = DocumentData,
|
|
53
|
+
TContext = unknown
|
|
54
|
+
>({
|
|
55
|
+
reference,
|
|
56
|
+
converter,
|
|
57
|
+
options = {}
|
|
58
|
+
}: UseUpdateDocMutationOptions<AppModelType, DbModelType, TContext>) => {
|
|
59
|
+
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
60
|
+
|
|
61
|
+
return useMutation({
|
|
62
|
+
...options,
|
|
63
|
+
mutationFn: async ({ data }) => {
|
|
64
|
+
if (!reference) {
|
|
65
|
+
throw new Error("Reference is undefined");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
await updateDoc<AppModelType, DbModelType>(reference, data);
|
|
69
|
+
const docSnap = await getDoc(converter ? reference.withConverter(converter) : reference);
|
|
70
|
+
return docSnap.data() as AppModelType;
|
|
71
|
+
},
|
|
72
|
+
mutationKey
|
|
73
|
+
});
|
|
74
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { DocumentData } from "firebase/firestore";
|
|
2
|
+
import { GetDocSnapOptions } from "./getDocSnap";
|
|
3
|
+
export type GetDocDataOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = GetDocSnapOptions<AppModelType, DbModelType>;
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronously retrieves document data from a specified database and reference.
|
|
6
|
+
* Utilizes the helper function `getDocSnap` to fetch the document snapshot and
|
|
7
|
+
* checks if the document exists before returning its data.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} options - The options for fetching document data.
|
|
10
|
+
* @param {FirebaseFirestore} options.db - The Firestore database instance.
|
|
11
|
+
* @param {DocumentReference=} options.reference - The document reference. This is an optional parameter.
|
|
12
|
+
* @param {string=} options.path - The path to the document in the database. This is an optional parameter.
|
|
13
|
+
* @param {Array<string>=} options.pathSegments - The path segments for the document's path. This is an optional parameter.
|
|
14
|
+
*
|
|
15
|
+
* @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists,
|
|
16
|
+
* or null if the document does not exist.
|
|
17
|
+
*/
|
|
18
|
+
export declare const getDocData: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ db, reference, path, pathSegments }: GetDocDataOptions<AppModelType, DbModelType>) => Promise<AppModelType | null>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getDocSnap } from "./getDocSnap";
|
|
2
|
+
/**
|
|
3
|
+
* Asynchronously retrieves document data from a specified database and reference.
|
|
4
|
+
* Utilizes the helper function `getDocSnap` to fetch the document snapshot and
|
|
5
|
+
* checks if the document exists before returning its data.
|
|
6
|
+
*
|
|
7
|
+
* @param {Object} options - The options for fetching document data.
|
|
8
|
+
* @param {FirebaseFirestore} options.db - The Firestore database instance.
|
|
9
|
+
* @param {DocumentReference=} options.reference - The document reference. This is an optional parameter.
|
|
10
|
+
* @param {string=} options.path - The path to the document in the database. This is an optional parameter.
|
|
11
|
+
* @param {Array<string>=} options.pathSegments - The path segments for the document's path. This is an optional parameter.
|
|
12
|
+
*
|
|
13
|
+
* @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists,
|
|
14
|
+
* or null if the document does not exist.
|
|
15
|
+
*/
|
|
16
|
+
export const getDocData = async ({ db, reference, path, pathSegments }) => {
|
|
17
|
+
const docSnap = await getDocSnap({ db, reference, path, pathSegments });
|
|
18
|
+
if (docSnap && docSnap.exists()) {
|
|
19
|
+
return docSnap.data();
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { DocumentData } from "firebase/firestore";
|
|
2
|
+
import { getDocSnap, GetDocSnapOptions } from "./getDocSnap";
|
|
3
|
+
|
|
4
|
+
export type GetDocDataOptions<
|
|
5
|
+
AppModelType extends DocumentData = DocumentData,
|
|
6
|
+
DbModelType extends DocumentData = DocumentData
|
|
7
|
+
> = GetDocSnapOptions<AppModelType, DbModelType>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Asynchronously retrieves document data from a specified database and reference.
|
|
11
|
+
* Utilizes the helper function `getDocSnap` to fetch the document snapshot and
|
|
12
|
+
* checks if the document exists before returning its data.
|
|
13
|
+
*
|
|
14
|
+
* @param {Object} options - The options for fetching document data.
|
|
15
|
+
* @param {FirebaseFirestore} options.db - The Firestore database instance.
|
|
16
|
+
* @param {DocumentReference=} options.reference - The document reference. This is an optional parameter.
|
|
17
|
+
* @param {string=} options.path - The path to the document in the database. This is an optional parameter.
|
|
18
|
+
* @param {Array<string>=} options.pathSegments - The path segments for the document's path. This is an optional parameter.
|
|
19
|
+
*
|
|
20
|
+
* @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists,
|
|
21
|
+
* or null if the document does not exist.
|
|
22
|
+
*/
|
|
23
|
+
export const getDocData = async <
|
|
24
|
+
AppModelType extends DocumentData = DocumentData,
|
|
25
|
+
DbModelType extends DocumentData = DocumentData
|
|
26
|
+
>({
|
|
27
|
+
db,
|
|
28
|
+
reference,
|
|
29
|
+
path,
|
|
30
|
+
pathSegments
|
|
31
|
+
}: GetDocDataOptions<AppModelType, DbModelType>) => {
|
|
32
|
+
const docSnap = await getDocSnap<AppModelType, DbModelType>({ db, reference, path, pathSegments });
|
|
33
|
+
|
|
34
|
+
if (docSnap && docSnap.exists()) {
|
|
35
|
+
return docSnap.data();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return null;
|
|
39
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
|
|
2
|
+
export type GetDocRefOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
|
|
3
|
+
db: Firestore;
|
|
4
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
5
|
+
path?: string;
|
|
6
|
+
pathSegments?: string[];
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Retrieves a document reference based on provided database options.
|
|
10
|
+
* Either `reference` or `path` must be provided, not both.
|
|
11
|
+
*
|
|
12
|
+
* @param {Object} options - The options for getting a document reference.
|
|
13
|
+
* @param {Firestore} options.db - The Firestore database instance.
|
|
14
|
+
* @param {DocumentReference | CollectionReference | null} [options.reference] - Reference to a document or collection.
|
|
15
|
+
* @param {string | null} [options.path] - Path to the document.
|
|
16
|
+
* @param {string[] | null} [options.pathSegments] - Additional path segments if any.
|
|
17
|
+
* @returns {DocumentReference<AppModelType, DbModelType>} The document reference.
|
|
18
|
+
*/
|
|
19
|
+
export declare const getDocRef: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ db, reference, path, pathSegments }: GetDocRefOptions<AppModelType, DbModelType>) => Promise<DocumentReference<AppModelType, DbModelType>>;
|