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
|
@@ -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("@firebase/firestore").Firestore;
|
|
@@ -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 { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseGetDocOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
|
|
4
|
+
options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
5
|
+
path?: string;
|
|
6
|
+
pathSegments?: string[];
|
|
7
|
+
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
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 DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType, DbModelType>) => 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
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
+
import { FirebaseError } from "firebase/app";
|
|
3
|
+
/**
|
|
4
|
+
* @inline
|
|
5
|
+
*/
|
|
6
|
+
export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends DocumentData = DocumentData> = {
|
|
7
|
+
/**
|
|
8
|
+
* A slash-separated path to a document. Has to be omitted to use
|
|
9
|
+
*/
|
|
10
|
+
path?: string;
|
|
11
|
+
/**
|
|
12
|
+
* A reference to a collection.
|
|
13
|
+
*/
|
|
14
|
+
reference?: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
15
|
+
/**
|
|
16
|
+
* Additional path segments that will be applied relative
|
|
17
|
+
* to the first argument.
|
|
18
|
+
*/
|
|
19
|
+
pathSegments?: string[];
|
|
20
|
+
/**
|
|
21
|
+
* A callback to be called if the listen fails or is
|
|
22
|
+
* cancelled. No further callbacks will occur.
|
|
23
|
+
*/
|
|
24
|
+
onError?: (error: FirebaseError) => unknown;
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* @inline
|
|
28
|
+
*/
|
|
29
|
+
export type UseGetRealtimeDocDataResult<AppModelType> = {
|
|
30
|
+
data: AppModelType | null;
|
|
31
|
+
isError: boolean;
|
|
32
|
+
error: FirebaseError | null;
|
|
33
|
+
isFetching: boolean;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* A hook to get realtime updates to a firestore document.
|
|
37
|
+
*
|
|
38
|
+
* @group Hook
|
|
39
|
+
*
|
|
40
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
41
|
+
*
|
|
42
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```jsx
|
|
46
|
+
* const firebaseConfig = {};
|
|
47
|
+
* export const MyComponent = () => {
|
|
48
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
49
|
+
* return (
|
|
50
|
+
* <div>
|
|
51
|
+
* {JSON.stringify(result)}
|
|
52
|
+
* </div>
|
|
53
|
+
* );
|
|
54
|
+
* };
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare const useGetRealtimeDocData: <AppModelType, DbModelType extends DocumentData = DocumentData>({ path, pathSegments, reference, onError }: UseGetRealtimeDocDataOptions<AppModelType, DbModelType>) => UseGetRealtimeDocDataResult<AppModelType>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { onSnapshot } from "firebase/firestore";
|
|
2
|
+
import { useEffect, useMemo, useState } from "react";
|
|
3
|
+
import { useDocReference } from "./useDocReference";
|
|
4
|
+
/**
|
|
5
|
+
* A hook to get realtime updates to a firestore document.
|
|
6
|
+
*
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @param {UseGetRealtimeDocDataOptions<AppModelType, DbModelType>} options
|
|
10
|
+
*
|
|
11
|
+
* @returns {UseGetRealtimeDocDataResult<AppModelType>}
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
* const firebaseConfig = {};
|
|
16
|
+
* export const MyComponent = () => {
|
|
17
|
+
* const result = useGetRealtimeDocData('collection/documentId');
|
|
18
|
+
* return (
|
|
19
|
+
* <div>
|
|
20
|
+
* {JSON.stringify(result)}
|
|
21
|
+
* </div>
|
|
22
|
+
* );
|
|
23
|
+
* };
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }) => {
|
|
27
|
+
const ref = useDocReference({ path, reference, pathSegments });
|
|
28
|
+
const [doc, setDoc] = useState(null);
|
|
29
|
+
const [isError, setIsError] = useState(false);
|
|
30
|
+
const [isFetching, setIsFetching] = useState(true);
|
|
31
|
+
const [error, setError] = useState(null);
|
|
32
|
+
useEffect(() => {
|
|
33
|
+
const unsubscribe = ref
|
|
34
|
+
? onSnapshot(ref, {
|
|
35
|
+
next: async (snapshot) => {
|
|
36
|
+
setIsFetching(false);
|
|
37
|
+
setDoc(snapshot.data() || null);
|
|
38
|
+
setError(null);
|
|
39
|
+
setIsError(false);
|
|
40
|
+
},
|
|
41
|
+
error: (e) => {
|
|
42
|
+
setIsError(true);
|
|
43
|
+
setError(e);
|
|
44
|
+
onError?.(e);
|
|
45
|
+
}
|
|
46
|
+
})
|
|
47
|
+
: () => { };
|
|
48
|
+
return () => unsubscribe();
|
|
49
|
+
}, [ref, doc, isError, onError, isFetching, error]);
|
|
50
|
+
return useMemo(() => ({
|
|
51
|
+
data: doc,
|
|
52
|
+
isError,
|
|
53
|
+
isFetching,
|
|
54
|
+
error
|
|
55
|
+
}), [doc, isError, error, isFetching]);
|
|
56
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { CollectionReference, DocumentData, FirestoreDataConverter, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
|
+
import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
|
|
3
|
+
type UseInfiniteQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TQueryKey extends QueryKey = QueryKey> = {
|
|
4
|
+
options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryKey">>;
|
|
5
|
+
collectionReference: CollectionReference<AppModelType, DbModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
8
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
12
|
+
* It fetches data in pages and can load more as required.
|
|
13
|
+
*
|
|
14
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
15
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
16
|
+
*/
|
|
17
|
+
export declare const useInfiniteQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, collectionReference, queryConstraints, compositeFilter, converter }: UseInfiniteQueryOptions<AppModelType, DbModelType>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getDocs, query } from "firebase/firestore";
|
|
2
|
+
import { useInfiniteQuery as useInfiniteReactQuery } from "@tanstack/react-query";
|
|
3
|
+
/**
|
|
4
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
5
|
+
* It fetches data in pages and can load more as required.
|
|
6
|
+
*
|
|
7
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
8
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
9
|
+
*/
|
|
10
|
+
export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter, converter }) => {
|
|
11
|
+
return useInfiniteReactQuery({
|
|
12
|
+
...options,
|
|
13
|
+
queryFn: async ({ pageParam }) => {
|
|
14
|
+
const allQueryConstraints = [...queryConstraints, ...(pageParam ? [pageParam] : [])];
|
|
15
|
+
const queryToExecute = compositeFilter
|
|
16
|
+
? query(collectionReference, compositeFilter, ...allQueryConstraints)
|
|
17
|
+
: query(collectionReference, ...allQueryConstraints);
|
|
18
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
19
|
+
const docs = [];
|
|
20
|
+
if (querySnapshot) {
|
|
21
|
+
querySnapshot.forEach((doc) => {
|
|
22
|
+
docs.push(doc.data());
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return docs;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { DocumentData, FirestoreDataConverter, CollectionReference, QueryCompositeFilterConstraint, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
|
+
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseQueryOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData> = {
|
|
4
|
+
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> & Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
5
|
+
collectionReference: CollectionReference<AppModelType, DbModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: QueryCompositeFilterConstraint;
|
|
8
|
+
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
12
|
+
*
|
|
13
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
14
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
15
|
+
*
|
|
16
|
+
* @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
17
|
+
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
18
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
19
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
20
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
21
|
+
*
|
|
22
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
23
|
+
*/
|
|
24
|
+
export declare const useQuery: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData>({ options, collectionReference, queryConstraints, compositeFilter, converter }: UseQueryOptions<AppModelType, DbModelType>) => UseQueryResult<AppModelType[]>;
|
|
25
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getDocs, query } from "firebase/firestore";
|
|
2
|
+
import { useQuery as useReactQuery } from "@tanstack/react-query";
|
|
3
|
+
/**
|
|
4
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
5
|
+
*
|
|
6
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
7
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
8
|
+
*
|
|
9
|
+
* @param {UseQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
10
|
+
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
11
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
12
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
13
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
14
|
+
*
|
|
15
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
16
|
+
*/
|
|
17
|
+
export const useQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter, converter }) => {
|
|
18
|
+
return useReactQuery({
|
|
19
|
+
...options,
|
|
20
|
+
queryFn: async () => {
|
|
21
|
+
const queryToExecute = compositeFilter
|
|
22
|
+
? query(collectionReference, compositeFilter, ...queryConstraints)
|
|
23
|
+
: query(collectionReference, ...queryConstraints);
|
|
24
|
+
const querySnapshot = await getDocs(converter ? queryToExecute.withConverter(converter) : queryToExecute);
|
|
25
|
+
const docs = [];
|
|
26
|
+
if (querySnapshot) {
|
|
27
|
+
querySnapshot.forEach((doc) => {
|
|
28
|
+
docs.push(doc.data());
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return docs;
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
|
+
import { AppModel } from "../../types/AppModel";
|
|
3
|
+
import { NonFilterQueryConstraint } from "../../types/QueryConstraints";
|
|
4
|
+
/**
|
|
5
|
+
* @inline
|
|
6
|
+
*/
|
|
7
|
+
export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
|
|
8
|
+
/**
|
|
9
|
+
* A list of constraints such as limit, order, offset.
|
|
10
|
+
*/
|
|
11
|
+
constraints: NonFilterQueryConstraint<AppModelType>[];
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* A generic mothod to build query constraints for firebase
|
|
15
|
+
*
|
|
16
|
+
* @group Utility
|
|
17
|
+
*
|
|
18
|
+
* @param {NonFilterQueryConstraint<AppModelType>} constraint
|
|
19
|
+
*
|
|
20
|
+
* @returns {QueryNonFilterConstraint}
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```jsx
|
|
24
|
+
* const firebaseConfig = {};
|
|
25
|
+
* export const constraint = useMemo(() => {
|
|
26
|
+
* return buildQueryConstraint({
|
|
27
|
+
* type: 'limit',
|
|
28
|
+
* limit: 1
|
|
29
|
+
* });
|
|
30
|
+
* };
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const buildQueryConstraint: <AppModelType extends AppModel = AppModel>(constraint: NonFilterQueryConstraint<AppModelType>) => QueryNonFilterConstraint;
|
|
34
|
+
/**
|
|
35
|
+
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
36
|
+
* It uses buildQueryConstraint method and useMemo hook.
|
|
37
|
+
*
|
|
38
|
+
* @group Hook
|
|
39
|
+
*
|
|
40
|
+
* @param {UseQueryConstraints<AppModelType>} constraints
|
|
41
|
+
*
|
|
42
|
+
* @returns {QueryNonFilterConstraint[]}
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```jsx
|
|
46
|
+
* const firebaseConfig = {};
|
|
47
|
+
* export const constraint = useQueryConstraints([
|
|
48
|
+
* {
|
|
49
|
+
* type: 'limit',
|
|
50
|
+
* limit: 1
|
|
51
|
+
* }
|
|
52
|
+
* ]};
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const useQueryConstraints: <AppModelType extends AppModel = AppModel>({ constraints }: UseQueryConstraints<AppModelType>) => QueryNonFilterConstraint[];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { startAt, orderBy, limit, limitToLast, startAfter, endAt, endBefore } from "firebase/firestore";
|
|
2
|
+
import { useMemo } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* A generic mothod to build query constraints for firebase
|
|
5
|
+
*
|
|
6
|
+
* @group Utility
|
|
7
|
+
*
|
|
8
|
+
* @param {NonFilterQueryConstraint<AppModelType>} constraint
|
|
9
|
+
*
|
|
10
|
+
* @returns {QueryNonFilterConstraint}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* const firebaseConfig = {};
|
|
15
|
+
* export const constraint = useMemo(() => {
|
|
16
|
+
* return buildQueryConstraint({
|
|
17
|
+
* type: 'limit',
|
|
18
|
+
* limit: 1
|
|
19
|
+
* });
|
|
20
|
+
* };
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export const buildQueryConstraint = (constraint) => {
|
|
24
|
+
switch (constraint.type) {
|
|
25
|
+
case "orderBy":
|
|
26
|
+
return orderBy(constraint.fieldPath, constraint.directionStr);
|
|
27
|
+
case "startAt":
|
|
28
|
+
return startAt(...constraint.arguments);
|
|
29
|
+
case "startAfter":
|
|
30
|
+
return startAfter(...constraint.arguments);
|
|
31
|
+
case "endAt":
|
|
32
|
+
return endAt(constraint.value, constraint.key);
|
|
33
|
+
case "endBefore":
|
|
34
|
+
return endBefore(constraint.value, constraint.key);
|
|
35
|
+
case "limit":
|
|
36
|
+
return limit(constraint.limit);
|
|
37
|
+
case "limitToLast":
|
|
38
|
+
return limitToLast(constraint.limit);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
43
|
+
* It uses buildQueryConstraint method and useMemo hook.
|
|
44
|
+
*
|
|
45
|
+
* @group Hook
|
|
46
|
+
*
|
|
47
|
+
* @param {UseQueryConstraints<AppModelType>} constraints
|
|
48
|
+
*
|
|
49
|
+
* @returns {QueryNonFilterConstraint[]}
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```jsx
|
|
53
|
+
* const firebaseConfig = {};
|
|
54
|
+
* export const constraint = useQueryConstraints([
|
|
55
|
+
* {
|
|
56
|
+
* type: 'limit',
|
|
57
|
+
* limit: 1
|
|
58
|
+
* }
|
|
59
|
+
* ]};
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export const useQueryConstraints = ({ constraints }) => {
|
|
63
|
+
return useMemo(() => {
|
|
64
|
+
return constraints.map(buildQueryConstraint);
|
|
65
|
+
}, [constraints]);
|
|
66
|
+
};
|
|
@@ -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,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,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,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,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>>;
|