react-query-firebase 2.0.0-rc6 → 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/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.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useCollectionReference.js +0 -0
- /package/{src → react-native}/firestore/useCollectionReference.ts +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/useDocReference.d.ts +0 -0
- /package/{dist/src → react-native}/firestore/useDocReference.js +0 -0
- /package/{src → react-native}/firestore/useDocReference.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,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>>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { doc } from "firebase/firestore";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves a document reference based on provided database options.
|
|
4
|
+
* Either `reference` or `path` must be provided, not both.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} options - The options for getting a document reference.
|
|
7
|
+
* @param {Firestore} options.db - The Firestore database instance.
|
|
8
|
+
* @param {DocumentReference | CollectionReference | null} [options.reference] - Reference to a document or collection.
|
|
9
|
+
* @param {string | null} [options.path] - Path to the document.
|
|
10
|
+
* @param {string[] | null} [options.pathSegments] - Additional path segments if any.
|
|
11
|
+
* @returns {DocumentReference<AppModelType, DbModelType>} The document reference.
|
|
12
|
+
*/
|
|
13
|
+
export const getDocRef = async ({ db, reference, path, pathSegments }) => {
|
|
14
|
+
if (!reference && !path) {
|
|
15
|
+
throw new Error("One of the options must be provided: path or reference.");
|
|
16
|
+
}
|
|
17
|
+
const docRef = !reference
|
|
18
|
+
? doc(db, path, ...(pathSegments || []))
|
|
19
|
+
: reference.type === "collection"
|
|
20
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
21
|
+
: reference.type === "document"
|
|
22
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
23
|
+
: null;
|
|
24
|
+
return docRef;
|
|
25
|
+
};
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { CollectionReference, doc, DocumentData, DocumentReference, Firestore } from "firebase/firestore";
|
|
2
|
+
|
|
3
|
+
export type GetDocRefOptions<
|
|
4
|
+
AppModelType extends DocumentData = DocumentData,
|
|
5
|
+
DbModelType extends DocumentData = DocumentData
|
|
6
|
+
> = {
|
|
7
|
+
db: Firestore;
|
|
8
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
9
|
+
path?: string;
|
|
10
|
+
pathSegments?: string[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Retrieves a document reference based on provided database options.
|
|
15
|
+
* Either `reference` or `path` must be provided, not both.
|
|
16
|
+
*
|
|
17
|
+
* @param {Object} options - The options for getting a document reference.
|
|
18
|
+
* @param {Firestore} options.db - The Firestore database instance.
|
|
19
|
+
* @param {DocumentReference | CollectionReference | null} [options.reference] - Reference to a document or collection.
|
|
20
|
+
* @param {string | null} [options.path] - Path to the document.
|
|
21
|
+
* @param {string[] | null} [options.pathSegments] - Additional path segments if any.
|
|
22
|
+
* @returns {DocumentReference<AppModelType, DbModelType>} The document reference.
|
|
23
|
+
*/
|
|
24
|
+
export const getDocRef = async <
|
|
25
|
+
AppModelType extends DocumentData = DocumentData,
|
|
26
|
+
DbModelType extends DocumentData = DocumentData
|
|
27
|
+
>({
|
|
28
|
+
db,
|
|
29
|
+
reference,
|
|
30
|
+
path,
|
|
31
|
+
pathSegments
|
|
32
|
+
}: GetDocRefOptions<AppModelType, DbModelType>) => {
|
|
33
|
+
if (!reference && !path) {
|
|
34
|
+
throw new Error("One of the options must be provided: path or reference.");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const docRef = !reference
|
|
38
|
+
? doc(db, path as string, ...(pathSegments || []))
|
|
39
|
+
: reference.type === "collection"
|
|
40
|
+
? doc(reference, path, ...(pathSegments || []))
|
|
41
|
+
: reference.type === "document"
|
|
42
|
+
? doc(reference, path as string, ...(pathSegments || []))
|
|
43
|
+
: null;
|
|
44
|
+
|
|
45
|
+
return docRef as DocumentReference<AppModelType, DbModelType>;
|
|
46
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DocumentData } from "firebase/firestore";
|
|
2
|
+
import { GetDocRefOptions } from "./getDocRef";
|
|
3
|
+
export type GetDocSnapOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = GetDocRefOptions<AppModelType, DbModelType>;
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronously retrieves a document snapshot from Firestore using a specified document reference
|
|
6
|
+
* or path information. This function handles getting the appropriate document reference based on
|
|
7
|
+
* the parameters provided, and then fetches the document data from Firestore.
|
|
8
|
+
*
|
|
9
|
+
* @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
|
|
10
|
+
* for retrieving the document
|
|
11
|
+
* snapshot.
|
|
12
|
+
* @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
|
|
13
|
+
* @param {string} options.reference - A string representing a direct Firestore document reference.
|
|
14
|
+
* @param {string} options.path - A Firestore path string leading to the document.
|
|
15
|
+
* @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
|
|
16
|
+
* @returns {Promise<DocumentData | null>} A promise that resolves to the document data if the
|
|
17
|
+
* document exists, or `null` if the document does not exist
|
|
18
|
+
* or if the reference could not be obtained.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getDocSnap: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ db, reference, path, pathSegments }: GetDocSnapOptions<AppModelType, DbModelType>) => Promise<import("@firebase/firestore").DocumentSnapshot<AppModelType, DbModelType> | null>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getDoc as firestoreGetDoc } from "firebase/firestore";
|
|
2
|
+
import { getDocRef } from "./getDocRef";
|
|
3
|
+
/**
|
|
4
|
+
* Asynchronously retrieves a document snapshot from Firestore using a specified document reference
|
|
5
|
+
* or path information. This function handles getting the appropriate document reference based on
|
|
6
|
+
* the parameters provided, and then fetches the document data from Firestore.
|
|
7
|
+
*
|
|
8
|
+
* @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
|
|
9
|
+
* for retrieving the document
|
|
10
|
+
* snapshot.
|
|
11
|
+
* @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
|
|
12
|
+
* @param {string} options.reference - A string representing a direct Firestore document reference.
|
|
13
|
+
* @param {string} options.path - A Firestore path string leading to the document.
|
|
14
|
+
* @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
|
|
15
|
+
* @returns {Promise<DocumentData | null>} A promise that resolves to the document data if the
|
|
16
|
+
* document exists, or `null` if the document does not exist
|
|
17
|
+
* or if the reference could not be obtained.
|
|
18
|
+
*/
|
|
19
|
+
export const getDocSnap = async ({ db, reference, path, pathSegments }) => {
|
|
20
|
+
const docRef = await getDocRef({ db, reference, path, pathSegments });
|
|
21
|
+
if (docRef) {
|
|
22
|
+
return await firestoreGetDoc(docRef);
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { DocumentData, getDoc as firestoreGetDoc } from "firebase/firestore";
|
|
2
|
+
import { getDocRef, GetDocRefOptions } from "./getDocRef";
|
|
3
|
+
|
|
4
|
+
export type GetDocSnapOptions<
|
|
5
|
+
AppModelType extends DocumentData = DocumentData,
|
|
6
|
+
DbModelType extends DocumentData = DocumentData
|
|
7
|
+
> = GetDocRefOptions<AppModelType, DbModelType>;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Asynchronously retrieves a document snapshot from Firestore using a specified document reference
|
|
11
|
+
* or path information. This function handles getting the appropriate document reference based on
|
|
12
|
+
* the parameters provided, and then fetches the document data from Firestore.
|
|
13
|
+
*
|
|
14
|
+
* @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
|
|
15
|
+
* for retrieving the document
|
|
16
|
+
* snapshot.
|
|
17
|
+
* @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
|
|
18
|
+
* @param {string} options.reference - A string representing a direct Firestore document reference.
|
|
19
|
+
* @param {string} options.path - A Firestore path string leading to the document.
|
|
20
|
+
* @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
|
|
21
|
+
* @returns {Promise<DocumentData | null>} A promise that resolves to the document data if the
|
|
22
|
+
* document exists, or `null` if the document does not exist
|
|
23
|
+
* or if the reference could not be obtained.
|
|
24
|
+
*/
|
|
25
|
+
export const getDocSnap = async <
|
|
26
|
+
AppModelType extends DocumentData = DocumentData,
|
|
27
|
+
DbModelType extends DocumentData = DocumentData
|
|
28
|
+
>({
|
|
29
|
+
db,
|
|
30
|
+
reference,
|
|
31
|
+
path,
|
|
32
|
+
pathSegments
|
|
33
|
+
}: GetDocSnapOptions<AppModelType, DbModelType>) => {
|
|
34
|
+
const docRef = await getDocRef<AppModelType, DbModelType>({ db, reference, path, pathSegments });
|
|
35
|
+
|
|
36
|
+
if (docRef) {
|
|
37
|
+
return await firestoreGetDoc(docRef);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return null;
|
|
41
|
+
};
|