react-query-firebase 2.1.1 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/react-native/analytics/index.d.ts +3 -0
- package/react-native/analytics/index.js +3 -0
- package/react-native/analytics/useAnalytics.d.ts +5 -0
- package/react-native/analytics/useAnalytics.js +10 -0
- package/react-native/analytics/useLogEvent.d.ts +17 -0
- package/react-native/analytics/useLogEvent.js +21 -0
- package/react-native/analytics/useSetAnalyticsCollectionEnabled.d.ts +10 -0
- package/react-native/analytics/useSetAnalyticsCollectionEnabled.js +16 -0
- package/react-native/auth/index.d.ts +12 -0
- package/react-native/auth/index.js +12 -0
- package/react-native/auth/mutation-keys.d.ts +7 -0
- package/react-native/auth/mutation-keys.js +19 -0
- package/react-native/auth/useAuth.d.ts +5 -0
- package/react-native/auth/useAuth.js +10 -0
- package/react-native/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +15 -0
- package/react-native/auth/useCreateUserWitEmailAndPasswordMutation.js +19 -0
- package/react-native/auth/useCurrentUser.d.ts +6 -0
- package/react-native/auth/useCurrentUser.js +20 -0
- package/react-native/auth/useIdToken.d.ts +30 -0
- package/react-native/auth/useIdToken.js +66 -0
- package/react-native/auth/useReauthenticateWitCredentialMutation.d.ts +16 -0
- package/react-native/auth/useReauthenticateWitCredentialMutation.js +18 -0
- package/react-native/auth/useReauthenticateWitRedirectMutation.d.ts +17 -0
- package/react-native/auth/useReauthenticateWitRedirectMutation.js +18 -0
- package/react-native/auth/useSendEmailVerificationMutation.d.ts +13 -0
- package/react-native/auth/useSendEmailVerificationMutation.js +16 -0
- package/react-native/auth/useSignInWitEmailAndPasswordMutation.d.ts +15 -0
- package/react-native/auth/useSignInWitEmailAndPasswordMutation.js +19 -0
- package/react-native/auth/useSignInWitRedirectMutation.d.ts +14 -0
- package/react-native/auth/useSignInWitRedirectMutation.js +18 -0
- package/react-native/auth/useSignOutMutation.d.ts +10 -0
- package/react-native/auth/useSignOutMutation.js +19 -0
- package/react-native/auth/useUpdateProfileMutation.d.ts +15 -0
- package/react-native/auth/useUpdateProfileMutation.js +17 -0
- package/react-native/context/FirebaseContextProvider.d.ts +1 -2
- package/react-native/context/FirebaseContextProvider.js +3 -3
- package/react-native/context/FirebaseContextProvider.tsx +3 -5
- package/react-native/firestore/index.d.ts +17 -0
- package/react-native/firestore/index.js +17 -0
- package/react-native/firestore/useAddDocMutation.d.ts +21 -0
- package/react-native/firestore/useAddDocMutation.js +25 -0
- package/react-native/firestore/useBatchWrite.d.ts +15 -0
- package/react-native/firestore/useBatchWrite.js +21 -0
- package/react-native/firestore/useCollectionReference.d.ts +18 -0
- package/react-native/firestore/useCollectionReference.js +22 -0
- package/react-native/firestore/useCompositeFilter.d.ts +40 -0
- package/react-native/firestore/useCompositeFilter.js +42 -0
- package/react-native/firestore/useCountQuery.d.ts +23 -0
- package/react-native/firestore/useCountQuery.js +30 -0
- package/react-native/firestore/useDeleteDocMutation.d.ts +18 -0
- package/react-native/firestore/useDeleteDocMutation.js +23 -0
- package/react-native/firestore/useDocReference.d.ts +19 -0
- package/react-native/firestore/useDocReference.js +36 -0
- package/react-native/firestore/useDocReferences.d.ts +23 -0
- package/react-native/firestore/useDocReferences.js +29 -0
- package/react-native/firestore/useFirestore.d.ts +7 -0
- package/react-native/firestore/useFirestore.js +12 -0
- package/react-native/firestore/useGetDocData.d.ts +18 -0
- package/react-native/firestore/useGetDocData.js +18 -0
- package/react-native/firestore/useGetRealtimeDocData.d.ts +57 -0
- package/react-native/firestore/useGetRealtimeDocData.js +56 -0
- package/react-native/firestore/useInfiniteQuery.d.ts +17 -0
- package/react-native/firestore/useInfiniteQuery.js +28 -0
- package/react-native/firestore/useQuery.d.ts +24 -0
- package/react-native/firestore/useQuery.js +34 -0
- package/react-native/firestore/useQueryConstraints.d.ts +54 -0
- package/react-native/firestore/useQueryConstraints.js +66 -0
- package/react-native/firestore/useRunTransaction.d.ts +15 -0
- package/react-native/firestore/useRunTransaction.js +19 -0
- package/react-native/firestore/useSetDocMutation.d.ts +20 -0
- package/react-native/firestore/useSetDocMutation.js +25 -0
- package/react-native/firestore/useUpdateDocMutation.d.ts +30 -0
- package/react-native/firestore/useUpdateDocMutation.js +37 -0
- package/react-native/firestore/utils/getDocData.d.ts +18 -0
- package/react-native/firestore/utils/getDocData.js +22 -0
- package/react-native/firestore/utils/getDocRef.d.ts +19 -0
- package/react-native/firestore/utils/getDocRef.js +21 -0
- package/react-native/firestore/utils/getDocSnap.d.ts +20 -0
- package/react-native/firestore/utils/getDocSnap.js +25 -0
- package/react-native/remoteConfig/index.d.ts +3 -0
- package/react-native/remoteConfig/index.js +3 -0
- package/react-native/remoteConfig/useFetchAndActivate.d.ts +11 -0
- package/react-native/remoteConfig/useFetchAndActivate.js +31 -0
- package/react-native/remoteConfig/useGetValue.d.ts +7 -0
- package/react-native/remoteConfig/useGetValue.js +15 -0
- package/react-native/remoteConfig/useRemoteConfig.d.ts +5 -0
- package/react-native/remoteConfig/useRemoteConfig.js +10 -0
- package/types/AppModel.d.ts +7 -0
- package/types/AppModel.js +1 -0
- package/types/QueryConstraints.d.ts +49 -0
- package/types/QueryConstraints.js +1 -0
- package/web/analytics/index.d.ts +3 -0
- package/web/analytics/index.js +3 -0
- package/web/analytics/useAnalytics.d.ts +5 -0
- package/web/analytics/useAnalytics.js +10 -0
- package/web/analytics/useLogEvent.d.ts +17 -0
- package/web/analytics/useLogEvent.js +21 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.d.ts +10 -0
- package/web/analytics/useSetAnalyticsCollectionEnabled.js +16 -0
- package/web/auth/index.d.ts +13 -0
- package/web/auth/index.js +13 -0
- package/web/auth/mutation-keys.d.ts +7 -0
- package/web/auth/mutation-keys.js +19 -0
- package/web/auth/useAuth.d.ts +5 -0
- package/web/auth/useAuth.js +10 -0
- package/web/auth/useAuthStateReady.d.ts +6 -0
- package/web/auth/useAuthStateReady.js +19 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useCreateUserWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useCurrentUser.d.ts +6 -0
- package/web/auth/useCurrentUser.js +20 -0
- package/web/auth/useIdToken.d.ts +30 -0
- package/web/auth/useIdToken.js +66 -0
- package/web/auth/useReauthenticateWitCredentialMutation.d.ts +16 -0
- package/web/auth/useReauthenticateWitCredentialMutation.js +18 -0
- package/web/auth/useReauthenticateWitRedirectMutation.d.ts +17 -0
- package/web/auth/useReauthenticateWitRedirectMutation.js +18 -0
- package/web/auth/useSendEmailVerificationMutation.d.ts +13 -0
- package/web/auth/useSendEmailVerificationMutation.js +16 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.d.ts +15 -0
- package/web/auth/useSignInWitEmailAndPasswordMutation.js +19 -0
- package/web/auth/useSignInWitRedirectMutation.d.ts +14 -0
- package/web/auth/useSignInWitRedirectMutation.js +18 -0
- package/web/auth/useSignOutMutation.d.ts +10 -0
- package/web/auth/useSignOutMutation.js +19 -0
- package/web/auth/useUpdateProfileMutation.d.ts +15 -0
- package/web/auth/useUpdateProfileMutation.js +17 -0
- package/web/firestore/index.d.ts +17 -0
- package/web/firestore/index.js +17 -0
- package/web/firestore/useAddDocMutation.d.ts +23 -0
- package/web/firestore/useAddDocMutation.js +26 -0
- package/web/firestore/useBatchWrite.d.ts +15 -0
- package/web/firestore/useBatchWrite.js +21 -0
- package/web/firestore/useCollectionReference.d.ts +18 -0
- package/web/firestore/useCollectionReference.js +24 -0
- package/web/firestore/useCompositeFilter.d.ts +40 -0
- package/web/firestore/useCompositeFilter.js +41 -0
- package/web/firestore/useCountQuery.d.ts +23 -0
- package/web/firestore/useCountQuery.js +30 -0
- package/web/firestore/useDeleteDocMutation.d.ts +18 -0
- package/web/firestore/useDeleteDocMutation.js +23 -0
- package/web/firestore/useDocReference.d.ts +19 -0
- package/web/firestore/useDocReference.js +45 -0
- package/web/firestore/useDocReferences.d.ts +23 -0
- package/web/firestore/useDocReferences.js +31 -0
- package/web/firestore/useFirestore.d.ts +7 -0
- package/web/firestore/useFirestore.js +12 -0
- package/web/firestore/useGetDocData.d.ts +18 -0
- package/web/firestore/useGetDocData.js +18 -0
- package/web/firestore/useGetRealtimeDocData.d.ts +57 -0
- package/web/firestore/useGetRealtimeDocData.js +56 -0
- package/web/firestore/useInfiniteQuery.d.ts +18 -0
- package/web/firestore/useInfiniteQuery.js +28 -0
- package/web/firestore/useQuery.d.ts +25 -0
- package/web/firestore/useQuery.js +34 -0
- package/web/firestore/useQueryConstraints.d.ts +55 -0
- package/web/firestore/useQueryConstraints.js +66 -0
- package/web/firestore/useRunTransaction.d.ts +15 -0
- package/web/firestore/useRunTransaction.js +19 -0
- package/web/firestore/useSetDocMutation.d.ts +20 -0
- package/web/firestore/useSetDocMutation.js +25 -0
- package/web/firestore/useUpdateDocMutation.d.ts +31 -0
- package/web/firestore/useUpdateDocMutation.js +37 -0
- package/web/firestore/utils/getDocData.d.ts +18 -0
- package/web/firestore/utils/getDocData.js +22 -0
- package/web/firestore/utils/getDocRef.d.ts +19 -0
- package/web/firestore/utils/getDocRef.js +25 -0
- package/web/firestore/utils/getDocSnap.d.ts +20 -0
- package/web/firestore/utils/getDocSnap.js +25 -0
- package/web/remoteConfig/index.d.ts +3 -0
- package/web/remoteConfig/index.js +3 -0
- package/web/remoteConfig/useFetchAndActivate.d.ts +11 -0
- package/web/remoteConfig/useFetchAndActivate.js +31 -0
- package/web/remoteConfig/useGetValue.d.ts +7 -0
- package/web/remoteConfig/useGetValue.js +15 -0
- package/web/remoteConfig/useRemoteConfig.d.ts +5 -0
- package/web/remoteConfig/useRemoteConfig.js +10 -0
|
@@ -27,7 +27,7 @@ import firebase from "@react-native-firebase/app";
|
|
|
27
27
|
* };
|
|
28
28
|
* ```
|
|
29
29
|
*/
|
|
30
|
-
export const FirebaseContextProvider = ({ emulators,
|
|
30
|
+
export const FirebaseContextProvider = ({ emulators, children, authEnabled = true, firestoreEnabled = true, analyticsEnabled = true, consentSettings = {}, remoteConfigEnabled = true, remoteConfigSettings, remoteConfigDefaults = {}, firestoreSettings }) => {
|
|
31
31
|
const internalFirebase = useMemo(() => firebase, []);
|
|
32
32
|
useEffect(() => {
|
|
33
33
|
setConsent(firebase.analytics(), {
|
|
@@ -67,11 +67,11 @@ export const FirebaseContextProvider = ({ emulators, options, children, authEnab
|
|
|
67
67
|
return null;
|
|
68
68
|
}, [emulators?.auth, authEnabled, internalFirebase]);
|
|
69
69
|
const internalAnalytics = useMemo(() => {
|
|
70
|
-
if (analyticsEnabled
|
|
70
|
+
if (analyticsEnabled) {
|
|
71
71
|
return internalFirebase.analytics();
|
|
72
72
|
}
|
|
73
73
|
return null;
|
|
74
|
-
}, [analyticsEnabled,
|
|
74
|
+
}, [analyticsEnabled, internalFirebase]);
|
|
75
75
|
const internalRemoteConfig = useMemo(() => {
|
|
76
76
|
if (remoteConfigEnabled) {
|
|
77
77
|
const localRemoteConfig = internalFirebase.remoteConfig();
|
|
@@ -67,7 +67,6 @@ export type FirebaseContextProviderEmulators = {
|
|
|
67
67
|
* @inline
|
|
68
68
|
*/
|
|
69
69
|
export type FirebaseContextProviderProps = PropsWithChildren & {
|
|
70
|
-
platform: "Web" | "ReactNative";
|
|
71
70
|
/**
|
|
72
71
|
* Defines configuration for firebase emulators
|
|
73
72
|
*/
|
|
@@ -75,7 +74,7 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
|
|
|
75
74
|
/**
|
|
76
75
|
* Configuration options for Firebase initialization. {@link https://firebase.google.com/docs/web/setup#config-object | Learn about the Firebase config object}
|
|
77
76
|
*/
|
|
78
|
-
options
|
|
77
|
+
options?: ReactNativeFirebase.FirebaseAppOptions;
|
|
79
78
|
/**
|
|
80
79
|
* Flag indicating whether Firebase Auth should be enabled.
|
|
81
80
|
*/
|
|
@@ -151,7 +150,6 @@ export type FirebaseContextProviderProps = PropsWithChildren & {
|
|
|
151
150
|
*/
|
|
152
151
|
export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = ({
|
|
153
152
|
emulators,
|
|
154
|
-
options,
|
|
155
153
|
children,
|
|
156
154
|
authEnabled = true,
|
|
157
155
|
firestoreEnabled = true,
|
|
@@ -211,11 +209,11 @@ export const FirebaseContextProvider: React.FC<FirebaseContextProviderProps> = (
|
|
|
211
209
|
}, [emulators?.auth, authEnabled, internalFirebase]);
|
|
212
210
|
|
|
213
211
|
const internalAnalytics = useMemo(() => {
|
|
214
|
-
if (analyticsEnabled
|
|
212
|
+
if (analyticsEnabled) {
|
|
215
213
|
return internalFirebase.analytics();
|
|
216
214
|
}
|
|
217
215
|
return null;
|
|
218
|
-
}, [analyticsEnabled,
|
|
216
|
+
}, [analyticsEnabled, internalFirebase]);
|
|
219
217
|
|
|
220
218
|
const internalRemoteConfig = useMemo(() => {
|
|
221
219
|
if (remoteConfigEnabled) {
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./useFirestore";
|
|
2
|
+
export * from "./useAddDocMutation";
|
|
3
|
+
export * from "./useBatchWrite";
|
|
4
|
+
export * from "./useCollectionReference";
|
|
5
|
+
export * from "./useCompositeFilter";
|
|
6
|
+
export * from "./useCountQuery";
|
|
7
|
+
export * from "./useDeleteDocMutation";
|
|
8
|
+
export * from "./useDocReference";
|
|
9
|
+
export * from "./useDocReferences";
|
|
10
|
+
export * from "./useGetDocData";
|
|
11
|
+
export * from "./useInfiniteQuery";
|
|
12
|
+
export * from "./useQuery";
|
|
13
|
+
export * from "./useRunTransaction";
|
|
14
|
+
export * from "./useSetDocMutation";
|
|
15
|
+
export * from "./useUpdateDocMutation";
|
|
16
|
+
export * from "./useGetRealtimeDocData";
|
|
17
|
+
export * from "./useQueryConstraints";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export * from "./useFirestore";
|
|
2
|
+
export * from "./useAddDocMutation";
|
|
3
|
+
export * from "./useBatchWrite";
|
|
4
|
+
export * from "./useCollectionReference";
|
|
5
|
+
export * from "./useCompositeFilter";
|
|
6
|
+
export * from "./useCountQuery";
|
|
7
|
+
export * from "./useDeleteDocMutation";
|
|
8
|
+
export * from "./useDocReference";
|
|
9
|
+
export * from "./useDocReferences";
|
|
10
|
+
export * from "./useGetDocData";
|
|
11
|
+
export * from "./useInfiniteQuery";
|
|
12
|
+
export * from "./useQuery";
|
|
13
|
+
export * from "./useRunTransaction";
|
|
14
|
+
export * from "./useSetDocMutation";
|
|
15
|
+
export * from "./useUpdateDocMutation";
|
|
16
|
+
export * from "./useGetRealtimeDocData";
|
|
17
|
+
export * from "./useQueryConstraints";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseAddDocMutationValues<AppModelType> = {
|
|
5
|
+
data: WithFieldValue<AppModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseAddDocMutationOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: FirebaseFirestoreTypes.CollectionReference<AppModelType>;
|
|
9
|
+
options?: Omit<UseMutationOptions<AppModelType, ReactNativeFirebase.NativeFirebaseError, UseAddDocMutationValues<AppModelType>, TContext>, "mutationFn" | "mutationKey">;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Provides a mutation hook to add a document to a Firestore collection utilizing React Query's `useMutation`.
|
|
13
|
+
* It handles addition and optional conversion of the document data in Firestore.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} options - Options for the mutation hook
|
|
16
|
+
* @param {FirebaseFirestore.FirebaseFirestoreTypes.CollectionReference<AppModelType>} options.reference - Firestore collection reference where the document should be added.
|
|
17
|
+
* @param {UseMutationOptions<AppModelType, Error, { data: DbModelType }, TContext>} [options.options={}] - Optional configuration for the mutation.
|
|
18
|
+
*
|
|
19
|
+
* @returns {UseMutationResult<AppModelType, Error, { data: DbModelType }, TContext>} The mutation hook result containing status, error, and data of the mutation process.
|
|
20
|
+
*/
|
|
21
|
+
export declare const useAddDocMutation: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseAddDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, ReactNativeFirebase.NativeFirebaseError, UseAddDocMutationValues<AppModelType>, TContext>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { addDoc, getDoc } from "@react-native-firebase/firestore";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Provides a mutation hook to add a document to a Firestore collection utilizing React Query's `useMutation`.
|
|
6
|
+
* It handles addition and optional conversion of the document data in Firestore.
|
|
7
|
+
*
|
|
8
|
+
* @param {Object} options - Options for the mutation hook
|
|
9
|
+
* @param {FirebaseFirestore.FirebaseFirestoreTypes.CollectionReference<AppModelType>} options.reference - Firestore collection reference where the document should be added.
|
|
10
|
+
* @param {UseMutationOptions<AppModelType, Error, { data: DbModelType }, TContext>} [options.options={}] - Optional configuration for the mutation.
|
|
11
|
+
*
|
|
12
|
+
* @returns {UseMutationResult<AppModelType, Error, { data: DbModelType }, TContext>} The mutation hook result containing status, error, and data of the mutation process.
|
|
13
|
+
*/
|
|
14
|
+
export const useAddDocMutation = ({ reference, options = {} }) => {
|
|
15
|
+
const mutationKey = useMemo(() => [reference.path], [reference.path]);
|
|
16
|
+
return useMutation({
|
|
17
|
+
...options,
|
|
18
|
+
mutationFn: async ({ data }) => {
|
|
19
|
+
const docRef = await addDoc(reference, data);
|
|
20
|
+
const docSnap = await getDoc(docRef);
|
|
21
|
+
return docSnap.data();
|
|
22
|
+
},
|
|
23
|
+
mutationKey
|
|
24
|
+
});
|
|
25
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseBatchWriteVariables = (batch: FirebaseFirestoreTypes.WriteBatch) => Promise<void> | void;
|
|
5
|
+
export type UseBatchWriteOptions<TContext = unknown> = {
|
|
6
|
+
options?: Omit<UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, UseBatchWriteVariables, TContext>, "mutationFn">;
|
|
7
|
+
};
|
|
8
|
+
/**
|
|
9
|
+
* Custom hook to perform batch write operations using Firestore.
|
|
10
|
+
* Utilizes a mutation to carry out the batch write transaction.
|
|
11
|
+
* @template TContext - The type of context that can be passed into the hook, defaults to unknown.
|
|
12
|
+
* @param {Object} options - The configuration options for the mutation operation.
|
|
13
|
+
* @returns {Object} Returns an object composed of elements returned by useMutation, including properties such as status, and functions to trigger and control the mutation process.
|
|
14
|
+
*/
|
|
15
|
+
export declare const useBatchWrite: <TContext = unknown>({ options }?: UseBatchWriteOptions<TContext>) => import("@tanstack/react-query").UseMutationResult<void, ReactNativeFirebase.NativeFirebaseError, UseBatchWriteVariables, TContext>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { writeBatch } from "@react-native-firebase/firestore";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook to perform batch write operations using Firestore.
|
|
6
|
+
* Utilizes a mutation to carry out the batch write transaction.
|
|
7
|
+
* @template TContext - The type of context that can be passed into the hook, defaults to unknown.
|
|
8
|
+
* @param {Object} options - The configuration options for the mutation operation.
|
|
9
|
+
* @returns {Object} Returns an object composed of elements returned by useMutation, including properties such as status, and functions to trigger and control the mutation process.
|
|
10
|
+
*/
|
|
11
|
+
export const useBatchWrite = ({ options = {} } = {}) => {
|
|
12
|
+
const db = useFirestore();
|
|
13
|
+
return useMutation({
|
|
14
|
+
...options,
|
|
15
|
+
mutationFn: async (batchWriteFn) => {
|
|
16
|
+
const batch = writeBatch(db);
|
|
17
|
+
await batchWriteFn(batch);
|
|
18
|
+
return batch.commit();
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
export type UseCollectionReferenceOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
3
|
+
reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
4
|
+
path: string;
|
|
5
|
+
pathSegments?: string[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
|
|
9
|
+
*
|
|
10
|
+
* This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
|
|
11
|
+
*
|
|
12
|
+
* @param {UseCollectionReferenceOptions<AppModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
|
|
13
|
+
* @param {string} options.path - The base path for the collection.
|
|
14
|
+
* @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
|
|
15
|
+
* @param {string[]} options.pathSegments - Additional path segments to append to the base path.
|
|
16
|
+
* @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
|
|
17
|
+
*/
|
|
18
|
+
export declare const useCollectionReference: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ path, reference, pathSegments }: UseCollectionReferenceOptions<AppModelType>) => FirebaseFirestoreTypes.CollectionReference<AppModelType>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { collection } from "@react-native-firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a reference to a Firestore collection based on the provided path, reference, and path segments.
|
|
6
|
+
*
|
|
7
|
+
* This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
|
|
8
|
+
*
|
|
9
|
+
* @param {UseCollectionReferenceOptions<AppModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
|
|
10
|
+
* @param {string} options.path - The base path for the collection.
|
|
11
|
+
* @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
|
|
12
|
+
* @param {string[]} options.pathSegments - Additional path segments to append to the base path.
|
|
13
|
+
* @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
|
|
14
|
+
*/
|
|
15
|
+
export const useCollectionReference = ({ path, reference, pathSegments }) => {
|
|
16
|
+
const db = useFirestore();
|
|
17
|
+
return useMemo(() => {
|
|
18
|
+
return !reference
|
|
19
|
+
? collection(db, path || "", ...(pathSegments || []))
|
|
20
|
+
: collection(reference, path, ...(pathSegments || []));
|
|
21
|
+
}, [db, reference, path, pathSegments]);
|
|
22
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
type CompositeFilterDocumentData = FirebaseFirestoreTypes.DocumentData;
|
|
3
|
+
export type QueryElement<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
|
|
4
|
+
operator?: "OR" | "AND";
|
|
5
|
+
children?: QueryElement[];
|
|
6
|
+
field?: keyof (DbModelType & {
|
|
7
|
+
documentId?: string[];
|
|
8
|
+
});
|
|
9
|
+
value?: DbModelType[keyof DbModelType];
|
|
10
|
+
op?: FirebaseFirestoreTypes.WhereFilterOp;
|
|
11
|
+
};
|
|
12
|
+
export type CompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
|
|
13
|
+
operator: "OR" | "AND";
|
|
14
|
+
children: QueryElement<DbModelType & {
|
|
15
|
+
documentId?: string[];
|
|
16
|
+
}>[];
|
|
17
|
+
};
|
|
18
|
+
export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> = {
|
|
19
|
+
query?: CompositeFilter<DbModelType>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Constructs a composite query filter based on the provided query structure.
|
|
23
|
+
* It recursively builds query constraints using logical "or" or "and" operators.
|
|
24
|
+
*
|
|
25
|
+
* @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
|
|
26
|
+
* @returns {QueryFieldFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
|
|
27
|
+
*/
|
|
28
|
+
export declare const buildCompositeFilter: <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>(query: QueryElement<DbModelType>) => FirebaseFirestoreTypes.QueryFilterConstraint | null;
|
|
29
|
+
/**
|
|
30
|
+
* A custom hook that generates a composite filter for database queries, using the provided query configuration.
|
|
31
|
+
* It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
|
|
32
|
+
*
|
|
33
|
+
* @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
|
|
34
|
+
* @param {string} query.type - The type of composite operation ('or'/'and').
|
|
35
|
+
* @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
|
|
36
|
+
*
|
|
37
|
+
* @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
|
|
38
|
+
*/
|
|
39
|
+
export declare const useCompositeFilter: <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>({ query }: UseCompositeFilter<DbModelType>) => FirebaseFirestoreTypes.QueryCompositeFilterConstraint | undefined;
|
|
40
|
+
export {};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import firebase, { and, or, where } from "@react-native-firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* Constructs a composite query filter based on the provided query structure.
|
|
5
|
+
* It recursively builds query constraints using logical "or" or "and" operators.
|
|
6
|
+
*
|
|
7
|
+
* @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
|
|
8
|
+
* @returns {QueryFieldFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
|
|
9
|
+
*/
|
|
10
|
+
export const buildCompositeFilter = (query) => {
|
|
11
|
+
if (query.children) {
|
|
12
|
+
const queryConstraints = query.children.map(buildCompositeFilter).filter((constraint) => !!constraint);
|
|
13
|
+
if (queryConstraints.length <= 0) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return query.operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
|
|
17
|
+
}
|
|
18
|
+
if (query.field && query.op) {
|
|
19
|
+
return where(query.field === "documentId" ? firebase.FieldPath.documentId() : query.field, query.op, query.value);
|
|
20
|
+
}
|
|
21
|
+
return null;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* A custom hook that generates a composite filter for database queries, using the provided query configuration.
|
|
25
|
+
* It applies either an 'OR' or 'AND' logical operation based on the type specified in the query.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} query - The query configuration object that contains subqueries and a type for logical combination.
|
|
28
|
+
* @param {string} query.type - The type of composite operation ('or'/'and').
|
|
29
|
+
* @param {Array} query.children - An array of subqueries that will be processed to form the composite filter.
|
|
30
|
+
*
|
|
31
|
+
* @returns {(Function|undefined)} A composite query filter constraint function formed by combining subqueries or undefined if there are no valid constraints.
|
|
32
|
+
*/
|
|
33
|
+
export const useCompositeFilter = ({ query }) => {
|
|
34
|
+
return useMemo(() => {
|
|
35
|
+
const queryConstraints = (query?.children?.map?.((subQuery) => buildCompositeFilter(subQuery))
|
|
36
|
+
?.filter)?.((constraint) => !!constraint) ?? [];
|
|
37
|
+
if (queryConstraints.length <= 0) {
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
return query?.operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
|
|
41
|
+
}, [query]);
|
|
42
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes, QueryConstraint, QueryNonFilterConstraint } from "@react-native-firebase/firestore";
|
|
2
|
+
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseCountQueryOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
4
|
+
options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> & Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
|
|
5
|
+
collectionReference: FirebaseFirestoreTypes.CollectionReference<AppModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Executes a query with specified constraints and returns the count of matched documents.
|
|
11
|
+
*
|
|
12
|
+
* This function utilizes React Query to asynchronously fetch the count of documents from a server database
|
|
13
|
+
* that match the provided query constraints and an optional composite filter.
|
|
14
|
+
*
|
|
15
|
+
* @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
16
|
+
* @param {AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData} [options.options] - Additional options for the React Query.
|
|
17
|
+
* @param {unknown} [options.query] - Reference to the query object to be executed.
|
|
18
|
+
* @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
|
|
19
|
+
* @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
|
|
20
|
+
* @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
|
|
21
|
+
*/
|
|
22
|
+
export declare const useCountQuery: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ options, collectionReference, queryConstraints, compositeFilter }: UseCountQueryOptions<AppModelType>) => UseQueryResult<number>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { getCountFromServer, query } from "@react-native-firebase/firestore";
|
|
2
|
+
import { useQuery as useReactQuery } from "@tanstack/react-query";
|
|
3
|
+
/**
|
|
4
|
+
* Executes a query with specified constraints and returns the count of matched documents.
|
|
5
|
+
*
|
|
6
|
+
* This function utilizes React Query to asynchronously fetch the count of documents from a server database
|
|
7
|
+
* that match the provided query constraints and an optional composite filter.
|
|
8
|
+
*
|
|
9
|
+
* @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
10
|
+
* @param {AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData} [options.options] - Additional options for the React Query.
|
|
11
|
+
* @param {unknown} [options.query] - Reference to the query object to be executed.
|
|
12
|
+
* @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
|
|
13
|
+
* @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
|
|
14
|
+
* @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
|
|
15
|
+
*/
|
|
16
|
+
export const useCountQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
|
|
17
|
+
return useReactQuery({
|
|
18
|
+
...options,
|
|
19
|
+
queryFn: async () => {
|
|
20
|
+
const queryToExecute = compositeFilter
|
|
21
|
+
? query(collectionReference, compositeFilter, ...queryConstraints)
|
|
22
|
+
: query(collectionReference, ...queryConstraints);
|
|
23
|
+
const querySnapshot = await getCountFromServer(queryToExecute);
|
|
24
|
+
if (querySnapshot) {
|
|
25
|
+
return querySnapshot.data().count;
|
|
26
|
+
}
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseDeleteDocMutationValues<AppModelType> = {
|
|
5
|
+
data: WithFieldValue<AppModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseDeleteDocMutationOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
|
|
9
|
+
options?: Omit<UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>, "mutationFn" | "mutationKey">;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* A custom hook that provides a mutation function to delete a document from the database.
|
|
13
|
+
* @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
|
|
14
|
+
* @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
|
|
15
|
+
* @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
|
|
16
|
+
* @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
|
|
17
|
+
*/
|
|
18
|
+
export declare const useDeleteDocMutation: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseDeleteDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { deleteDoc } from "@react-native-firebase/firestore";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* A custom hook that provides a mutation function to delete a document from the database.
|
|
6
|
+
* @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
|
|
7
|
+
* @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
|
|
8
|
+
* @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
|
|
9
|
+
* @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
|
|
10
|
+
*/
|
|
11
|
+
export const useDeleteDocMutation = ({ reference, options = {} }) => {
|
|
12
|
+
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
13
|
+
return useMutation({
|
|
14
|
+
...options,
|
|
15
|
+
mutationFn: async () => {
|
|
16
|
+
if (!reference) {
|
|
17
|
+
throw new Error("Reference is undefined");
|
|
18
|
+
}
|
|
19
|
+
await deleteDoc(reference);
|
|
20
|
+
},
|
|
21
|
+
mutationKey
|
|
22
|
+
});
|
|
23
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
export type UseDocReferenceOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
3
|
+
reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
4
|
+
path?: string;
|
|
5
|
+
pathSegments?: string[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Custom hook to generate and manage a Firestore document reference.
|
|
9
|
+
* This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
|
|
10
|
+
* The reference is updated whenever the associated path, reference, or path segments change.
|
|
11
|
+
*
|
|
12
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
|
|
13
|
+
* @param {string} options.path - The path to the Firestore document.
|
|
14
|
+
* @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
|
|
15
|
+
* @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
|
|
16
|
+
*
|
|
17
|
+
* @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
|
|
18
|
+
*/
|
|
19
|
+
export declare const useDocReference: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType>) => FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { doc } from "@react-native-firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* Generates a document reference for a specified path or reference in Firestore.
|
|
6
|
+
* If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
|
|
7
|
+
*
|
|
8
|
+
* @param {Firestore} db - The Firestore database instance used to create the document reference.
|
|
9
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - An object containing the path, path segments, and optional reference.
|
|
10
|
+
* @param {string} options.path - The path to the document in the Firestore database.
|
|
11
|
+
* @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
|
|
12
|
+
* @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
|
|
13
|
+
*
|
|
14
|
+
* @returns {DocumentReference<AppModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
|
|
15
|
+
*/
|
|
16
|
+
const getDocReference = (db, { path, pathSegments, reference }) => {
|
|
17
|
+
return (!reference ? doc(db, path || "", ...(pathSegments || [])) : doc(reference, path, ...(pathSegments || [])));
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Custom hook to generate and manage a Firestore document reference.
|
|
21
|
+
* This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
|
|
22
|
+
* The reference is updated whenever the associated path, reference, or path segments change.
|
|
23
|
+
*
|
|
24
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
|
|
25
|
+
* @param {string} options.path - The path to the Firestore document.
|
|
26
|
+
* @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
|
|
27
|
+
* @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
|
|
28
|
+
*
|
|
29
|
+
* @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
|
|
30
|
+
*/
|
|
31
|
+
export const useDocReference = ({ path, reference, pathSegments }) => {
|
|
32
|
+
const db = useFirestore();
|
|
33
|
+
return useMemo(() => {
|
|
34
|
+
return getDocReference(db, { path, pathSegments, reference });
|
|
35
|
+
}, [db, reference, path, pathSegments]);
|
|
36
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
export type UseDocReferencesOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
3
|
+
reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
4
|
+
path: string;
|
|
5
|
+
pathSegments?: string[];
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* A custom hook to generate document references for Firebase Firestore documents using given
|
|
9
|
+
* reference options.
|
|
10
|
+
*
|
|
11
|
+
* The hook accepts an array of options, where each option may contain a path,
|
|
12
|
+
* an existing reference, or path segments, and returns an array of generated
|
|
13
|
+
* Firestore document references based on those options. The references can be used
|
|
14
|
+
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
15
|
+
* reference generation.
|
|
16
|
+
*
|
|
17
|
+
* @param {UseDocReferencesOptions<AppModelType>[]} references - An array of options
|
|
18
|
+
* specifying how to generate document references. Each option may include a `path`
|
|
19
|
+
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
20
|
+
* @returns {DocumentReference<AppModelType>[]} An array of Firestore document references
|
|
21
|
+
* generated from the options provided.
|
|
22
|
+
*/
|
|
23
|
+
export declare const useDocReferences: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>(references: UseDocReferencesOptions<AppModelType>[]) => FirebaseFirestoreTypes.DocumentReference<AppModelType>[];
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { doc } from "@react-native-firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
import { useFirestore } from "./useFirestore";
|
|
4
|
+
/**
|
|
5
|
+
* A custom hook to generate document references for Firebase Firestore documents using given
|
|
6
|
+
* reference options.
|
|
7
|
+
*
|
|
8
|
+
* The hook accepts an array of options, where each option may contain a path,
|
|
9
|
+
* an existing reference, or path segments, and returns an array of generated
|
|
10
|
+
* Firestore document references based on those options. The references can be used
|
|
11
|
+
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
12
|
+
* reference generation.
|
|
13
|
+
*
|
|
14
|
+
* @param {UseDocReferencesOptions<AppModelType>[]} references - An array of options
|
|
15
|
+
* specifying how to generate document references. Each option may include a `path`
|
|
16
|
+
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
17
|
+
* @returns {DocumentReference<AppModelType>[]} An array of Firestore document references
|
|
18
|
+
* generated from the options provided.
|
|
19
|
+
*/
|
|
20
|
+
export const useDocReferences = (references) => {
|
|
21
|
+
const db = useFirestore();
|
|
22
|
+
return useMemo(() => {
|
|
23
|
+
return references.map(({ path, reference, pathSegments }) => {
|
|
24
|
+
return (!reference
|
|
25
|
+
? doc(db, path || "", ...(pathSegments || []))
|
|
26
|
+
: doc(reference, path, ...(pathSegments || [])));
|
|
27
|
+
});
|
|
28
|
+
}, [references, db]);
|
|
29
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provides a hook to access the Firestore instance from the Firebase context.
|
|
3
|
+
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
4
|
+
*
|
|
5
|
+
* @returns {firestore.Firestore} The Firestore instance from the Firebase context.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useFirestore: () => import("@react-native-firebase/firestore").FirebaseFirestoreTypes.Module;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
|
+
/**
|
|
4
|
+
* Provides a hook to access the Firestore instance from the Firebase context.
|
|
5
|
+
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
6
|
+
*
|
|
7
|
+
* @returns {firestore.Firestore} The Firestore instance from the Firebase context.
|
|
8
|
+
*/
|
|
9
|
+
export const useFirestore = () => {
|
|
10
|
+
const { firestore } = useContext(FirebaseContext);
|
|
11
|
+
return firestore;
|
|
12
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
import { UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseGetDocOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
4
|
+
options: Omit<UseQueryOptions<AppModelType | null | undefined, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
5
|
+
path?: string;
|
|
6
|
+
pathSegments?: string[];
|
|
7
|
+
reference: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
11
|
+
* @param {Object} options - The options for configuring the Firestore query.
|
|
12
|
+
* @param {string} reference - The reference to the document in Firestore.
|
|
13
|
+
* @param {string} path - The path to the document in Firestore.
|
|
14
|
+
* @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
|
|
15
|
+
* @returns {Object} Result of the query containing document data and query status.
|
|
16
|
+
*/
|
|
17
|
+
export declare const useGetDocData: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useFirestore } from "./useFirestore";
|
|
2
|
+
import { getDocData } from "./utils/getDocData";
|
|
3
|
+
import { useQuery } from "@tanstack/react-query";
|
|
4
|
+
/**
|
|
5
|
+
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
6
|
+
* @param {Object} options - The options for configuring the Firestore query.
|
|
7
|
+
* @param {string} reference - The reference to the document in Firestore.
|
|
8
|
+
* @param {string} path - The path to the document in Firestore.
|
|
9
|
+
* @param {Array<string>} pathSegments - Segments of the path to document in Firestore.
|
|
10
|
+
* @returns {Object} Result of the query containing document data and query status.
|
|
11
|
+
*/
|
|
12
|
+
export const useGetDocData = ({ options, reference, path, pathSegments }) => {
|
|
13
|
+
const db = useFirestore();
|
|
14
|
+
return useQuery({
|
|
15
|
+
...options,
|
|
16
|
+
queryFn: () => getDocData({ db, reference, path, pathSegments })
|
|
17
|
+
});
|
|
18
|
+
};
|