react-query-firebase 2.1.0 → 2.1.2
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 +2 -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/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/AppModel.ts +7 -0
- package/types/QueryConstraints.d.ts +49 -0
- package/types/QueryConstraints.js +1 -0
- package/types/QueryConstraints.ts +58 -0
- package/types/index.ts +2 -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,17 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes, QueryConstraint, QueryNonFilterConstraint } from "@react-native-firebase/firestore";
|
|
2
|
+
import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
|
|
3
|
+
type UseInfiniteQueryOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TQueryKey extends QueryKey = QueryKey, TPageParam = unknown> = {
|
|
4
|
+
options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, TPageParam>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, TPageParam>, "queryKey">>;
|
|
5
|
+
collectionReference: FirebaseFirestoreTypes.CollectionReference<AppModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Custom hook that creates an infinite query using Firestore, allowing for query constraints, composite filters, and converters.
|
|
11
|
+
* It fetches data in pages and can load more as required.
|
|
12
|
+
*
|
|
13
|
+
* @param {UseInfiniteQueryOptions<AppModelType, DbModelType>} options - Configuration options for the infinite query, including Firestore query reference, query constraints, composite filter, and data converter.
|
|
14
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
15
|
+
*/
|
|
16
|
+
export declare const useInfiniteQuery: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ options, collectionReference, queryConstraints, compositeFilter }: UseInfiniteQueryOptions<AppModelType>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { getDocs, query } from "@react-native-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 }) => {
|
|
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(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,24 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes, QueryConstraint, QueryNonFilterConstraint } from "@react-native-firebase/firestore";
|
|
2
|
+
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
+
type UseQueryOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData> = {
|
|
4
|
+
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> & Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
5
|
+
collectionReference: FirebaseFirestoreTypes.CollectionReference<AppModelType>;
|
|
6
|
+
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
+
compositeFilter?: FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* Executes a query on a Firestore-like data source and returns the resulting documents as an array.
|
|
11
|
+
*
|
|
12
|
+
* This hook utilizes an abstraction over React Query to asynchronously fetch data based on the provided query
|
|
13
|
+
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
14
|
+
*
|
|
15
|
+
* @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
16
|
+
* @param {FirebaseFirestoreTypes.DocumentReference<AppModelType>} collectionReference - The reference to the query to be executed.
|
|
17
|
+
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
18
|
+
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
19
|
+
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
20
|
+
*
|
|
21
|
+
* @returns {UseQueryResult<AppModelType[]>} Result containing an array of documents that match the query criteria.
|
|
22
|
+
*/
|
|
23
|
+
export declare const useQuery: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ options, collectionReference, queryConstraints, compositeFilter }: UseQueryOptions<AppModelType>) => UseQueryResult<AppModelType[]>;
|
|
24
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getDocs, query } from "@react-native-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>} options - Configuration options for the query.
|
|
10
|
+
* @param {FirebaseFirestoreTypes.DocumentReference<AppModelType>} collectionReference - 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 }) => {
|
|
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(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,54 @@
|
|
|
1
|
+
import { AppModel } from "../../types/AppModel";
|
|
2
|
+
import { type NonFilterQueryConstraint } from "../../types/QueryConstraints";
|
|
3
|
+
/**
|
|
4
|
+
* @inline
|
|
5
|
+
*/
|
|
6
|
+
export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
|
|
7
|
+
/**
|
|
8
|
+
* A list of constraints such as limit, order, offset.
|
|
9
|
+
*/
|
|
10
|
+
constraints: NonFilterQueryConstraint<AppModelType>[];
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* A generic mothod to build query constraints for firebase
|
|
14
|
+
*
|
|
15
|
+
* @group Utility
|
|
16
|
+
*
|
|
17
|
+
* @param {NonFilterQueryConstraint<AppModelType>} constraint
|
|
18
|
+
*
|
|
19
|
+
* @returns {FirebaseFirestoreTypes.QueryNonFilterConstraint}
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```jsx
|
|
23
|
+
* const firebaseConfig = {};
|
|
24
|
+
* export const constraint = useMemo(() => {
|
|
25
|
+
* return buildQueryConstraint({
|
|
26
|
+
* type: 'limit',
|
|
27
|
+
* limit: 1
|
|
28
|
+
* });
|
|
29
|
+
* };
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
export declare const buildQueryConstraint: <AppModelType extends AppModel = AppModel>(constraint: NonFilterQueryConstraint<AppModelType>) => import("@react-native-firebase/firestore").QueryOrderByConstraint | import("@react-native-firebase/firestore").QueryLimitConstraint | import("@react-native-firebase/firestore").QueryStartAtConstraint;
|
|
33
|
+
/**
|
|
34
|
+
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
35
|
+
* It uses buildQueryConstraint method and useMemo hook.
|
|
36
|
+
*
|
|
37
|
+
* @group Hook
|
|
38
|
+
*
|
|
39
|
+
* @param {UseQueryConstraints<AppModelType>} options
|
|
40
|
+
*
|
|
41
|
+
* @returns {FirebaseFirestoreTypes.QueryNonFilterConstraint}
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```jsx
|
|
45
|
+
* const firebaseConfig = {};
|
|
46
|
+
* export const constraint = useQueryConstraints([
|
|
47
|
+
* {
|
|
48
|
+
* type: 'limit',
|
|
49
|
+
* limit: 1
|
|
50
|
+
* }
|
|
51
|
+
* ]};
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const useQueryConstraints: <AppModelType extends AppModel = AppModel>({ constraints }: UseQueryConstraints<AppModelType>) => (import("@react-native-firebase/firestore").QueryOrderByConstraint | import("@react-native-firebase/firestore").QueryLimitConstraint | import("@react-native-firebase/firestore").QueryStartAtConstraint)[];
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { startAt, orderBy, limit, limitToLast, startAfter, endAt, endBefore } from "@react-native-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 {FirebaseFirestoreTypes.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>} options
|
|
48
|
+
*
|
|
49
|
+
* @returns {FirebaseFirestoreTypes.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 { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseRunTransactionValues = <AppModelType = unknown>(transaction: FirebaseFirestoreTypes.Transaction) => AppModelType;
|
|
5
|
+
export type UseRunTransactionOptions<AppModelType = unknown, TContext = unknown> = {
|
|
6
|
+
options?: Omit<UseMutationOptions<AppModelType, ReactNativeFirebase.NativeFirebaseError, 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, ReactNativeFirebase.NativeFirebaseError, UseRunTransactionValues, TContext>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { runTransaction } from "@react-native-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 { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseSetDocMutationValues<AppModelType> = {
|
|
5
|
+
data: WithFieldValue<AppModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseSetDocMutationOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
|
|
9
|
+
options?: Omit<UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, 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, 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 extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseSetDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, ReactNativeFirebase.NativeFirebaseError, UseSetDocMutationValues<AppModelType>, TContext>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { setDoc } from "@react-native-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, 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,30 @@
|
|
|
1
|
+
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
+
import { FirebaseFirestoreTypes, UpdateData } from "@react-native-firebase/firestore";
|
|
3
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
export type UseUpdateDocMutationValues<DbModelType> = {
|
|
5
|
+
data: UpdateData<DbModelType>;
|
|
6
|
+
};
|
|
7
|
+
export type UseUpdateDocMutationOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown> = {
|
|
8
|
+
reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
|
|
9
|
+
options?: Omit<UseMutationOptions<AppModelType, ReactNativeFirebase.NativeFirebaseError, UseUpdateDocMutationValues<AppModelType>, TContext>, "mutationFn" | "mutationKey">;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Custom hook that sets up a mutation for updating a document in a Firestore database.
|
|
13
|
+
*
|
|
14
|
+
* This hook utilizes `useMutation` for performing asynchronous operations to update the document
|
|
15
|
+
* and retrieve the latest data snapshot. The update functionality can be configured with a custom
|
|
16
|
+
* converter if needed.
|
|
17
|
+
*
|
|
18
|
+
* @param {UseUpdateDocMutationOptions<AppModelType, TContext>} options - Configuration options for the mutation,
|
|
19
|
+
* including Firestore reference, an optional Firestore data converter, and additional mutation options.
|
|
20
|
+
*
|
|
21
|
+
* `reference` - The Firestore document reference that identifies the document to be updated.
|
|
22
|
+
*
|
|
23
|
+
* `converter` - An optional Firestore converter for transforming the database response into a custom type.
|
|
24
|
+
*
|
|
25
|
+
* `options` - Additional options that customize the mutation's behavior.
|
|
26
|
+
*
|
|
27
|
+
* @returns {UseMutationResult<AppModelType, Error, {data: AppModelType}, TContext>} An object returned by `useMutation`
|
|
28
|
+
* which includes functions to start the mutation and properties that represent the different states of the mutation.
|
|
29
|
+
*/
|
|
30
|
+
export declare const useUpdateDocMutation: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseUpdateDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, ReactNativeFirebase.NativeFirebaseError, UseUpdateDocMutationValues<AppModelType>, TContext>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { useMutation } from "@tanstack/react-query";
|
|
2
|
+
import { updateDoc, getDoc } from "@react-native-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, 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, 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(reference);
|
|
33
|
+
return docSnap.data();
|
|
34
|
+
},
|
|
35
|
+
mutationKey
|
|
36
|
+
});
|
|
37
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
import { GetDocSnapOptions } from "./getDocSnap";
|
|
3
|
+
export type GetDocDataOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = GetDocSnapOptions<AppModelType>;
|
|
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 FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocDataOptions<AppModelType>) => Promise<AppModelType | null | undefined>;
|
|
@@ -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 { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
export type GetDocRefOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
|
|
3
|
+
db: FirebaseFirestoreTypes.Module;
|
|
4
|
+
reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
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 {FirebaseFirestoreTypes.Module} options.db - The Firestore database instance.
|
|
14
|
+
* @param {FirebaseFirestoreTypes.DocumentReference | FirebaseFirestoreTypes.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 {FirebaseFirestoreTypes.DocumentReference<AppModelType>} The document reference.
|
|
18
|
+
*/
|
|
19
|
+
export declare const getDocRef: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocRefOptions<AppModelType>) => Promise<FirebaseFirestoreTypes.DocumentReference<AppModelType>>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { doc } from "@react-native-firebase/firestore";
|
|
2
|
+
/**
|
|
3
|
+
* Retrieves a document reference based on provided database options.
|
|
4
|
+
* Either `reference` or `path` must be provided, not both.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} options - The options for getting a document reference.
|
|
7
|
+
* @param {FirebaseFirestoreTypes.Module} options.db - The Firestore database instance.
|
|
8
|
+
* @param {FirebaseFirestoreTypes.DocumentReference | FirebaseFirestoreTypes.CollectionReference | null} [options.reference] - Reference to a document or collection.
|
|
9
|
+
* @param {string | null} [options.path] - Path to the document.
|
|
10
|
+
* @param {string[] | null} [options.pathSegments] - Additional path segments if any.
|
|
11
|
+
* @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType>} The document reference.
|
|
12
|
+
*/
|
|
13
|
+
export const getDocRef = async ({ db, reference, path, pathSegments }) => {
|
|
14
|
+
if (!reference && !path) {
|
|
15
|
+
throw new Error("One of the options must be provided: path or reference.");
|
|
16
|
+
}
|
|
17
|
+
const docRef = !reference
|
|
18
|
+
? doc(db, path, ...(pathSegments || []))
|
|
19
|
+
: doc(reference, path, ...(pathSegments || []));
|
|
20
|
+
return docRef;
|
|
21
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
|
+
import { GetDocRefOptions } from "./getDocRef";
|
|
3
|
+
export type GetDocSnapOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = GetDocRefOptions<AppModelType>;
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronously retrieves a document snapshot from Firestore using a specified document reference
|
|
6
|
+
* or path information. This function handles getting the appropriate document reference based on
|
|
7
|
+
* the parameters provided, and then fetches the document data from Firestore.
|
|
8
|
+
*
|
|
9
|
+
* @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
|
|
10
|
+
* for retrieving the document
|
|
11
|
+
* snapshot.
|
|
12
|
+
* @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
|
|
13
|
+
* @param {string} options.reference - A string representing a direct Firestore document reference.
|
|
14
|
+
* @param {string} options.path - A Firestore path string leading to the document.
|
|
15
|
+
* @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
|
|
16
|
+
* @returns {Promise<FirebaseFirestoreTypes.DocumentData | null>} A promise that resolves to the document data if the
|
|
17
|
+
* document exists, or `null` if the document does not exist
|
|
18
|
+
* or if the reference could not be obtained.
|
|
19
|
+
*/
|
|
20
|
+
export declare const getDocSnap: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocSnapOptions<AppModelType>) => Promise<FirebaseFirestoreTypes.DocumentSnapshot<AppModelType> | null>;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { getDoc as firestoreGetDoc } from "@react-native-firebase/firestore";
|
|
2
|
+
import { getDocRef } from "./getDocRef";
|
|
3
|
+
/**
|
|
4
|
+
* Asynchronously retrieves a document snapshot from Firestore using a specified document reference
|
|
5
|
+
* or path information. This function handles getting the appropriate document reference based on
|
|
6
|
+
* the parameters provided, and then fetches the document data from Firestore.
|
|
7
|
+
*
|
|
8
|
+
* @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
|
|
9
|
+
* for retrieving the document
|
|
10
|
+
* snapshot.
|
|
11
|
+
* @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
|
|
12
|
+
* @param {string} options.reference - A string representing a direct Firestore document reference.
|
|
13
|
+
* @param {string} options.path - A Firestore path string leading to the document.
|
|
14
|
+
* @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
|
|
15
|
+
* @returns {Promise<FirebaseFirestoreTypes.DocumentData | null>} A promise that resolves to the document data if the
|
|
16
|
+
* document exists, or `null` if the document does not exist
|
|
17
|
+
* or if the reference could not be obtained.
|
|
18
|
+
*/
|
|
19
|
+
export const getDocSnap = async ({ db, reference, path, pathSegments }) => {
|
|
20
|
+
const docRef = await getDocRef({ db, reference, path, pathSegments });
|
|
21
|
+
if (docRef) {
|
|
22
|
+
return await firestoreGetDoc(docRef);
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook to fetch and activate remote configuration settings.
|
|
3
|
+
* Initializes remote configuration, fetches, activates it, and tracks the fetch status.
|
|
4
|
+
* @returns {Object} An object containing:
|
|
5
|
+
* - {Function} fetchAndActivate - Callback function to fetch and activate remote configuration.
|
|
6
|
+
* - {Boolean} isFetched - Boolean representing whether the fetch-and-activate process completed.
|
|
7
|
+
*/
|
|
8
|
+
export declare const useFetchAndActivate: () => {
|
|
9
|
+
fetchAndActivate: () => Promise<void>;
|
|
10
|
+
isFetched: boolean;
|
|
11
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { useRemoteConfig } from "./useRemoteConfig";
|
|
2
|
+
import { ensureInitialized, fetchAndActivate } from "@react-native-firebase/remote-config";
|
|
3
|
+
import { useCallback, useMemo, useState } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook to fetch and activate remote configuration settings.
|
|
6
|
+
* Initializes remote configuration, fetches, activates it, and tracks the fetch status.
|
|
7
|
+
* @returns {Object} An object containing:
|
|
8
|
+
* - {Function} fetchAndActivate - Callback function to fetch and activate remote configuration.
|
|
9
|
+
* - {Boolean} isFetched - Boolean representing whether the fetch-and-activate process completed.
|
|
10
|
+
*/
|
|
11
|
+
export const useFetchAndActivate = () => {
|
|
12
|
+
const remoteConfig = useRemoteConfig();
|
|
13
|
+
const [isFetched, setIsFetched] = useState(false);
|
|
14
|
+
const fetchAndActivateCallback = useCallback(async () => {
|
|
15
|
+
try {
|
|
16
|
+
if (remoteConfig) {
|
|
17
|
+
await ensureInitialized(remoteConfig);
|
|
18
|
+
await fetchAndActivate(remoteConfig);
|
|
19
|
+
setIsFetched(true);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
setIsFetched(true);
|
|
24
|
+
console.log(`Cannot read remote config: ${e?.message}`);
|
|
25
|
+
}
|
|
26
|
+
}, [remoteConfig]);
|
|
27
|
+
return useMemo(() => ({
|
|
28
|
+
fetchAndActivate: fetchAndActivateCallback,
|
|
29
|
+
isFetched
|
|
30
|
+
}), [isFetched, fetchAndActivateCallback]);
|
|
31
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom hook to retrieve a value associated with a specified key from remote configuration.
|
|
3
|
+
* Utilizes memoization to optimize performance by recalculating the result only if dependencies change.
|
|
4
|
+
* @param {string} key - The key for which the corresponding value is to be fetched from the remote configuration.
|
|
5
|
+
* @returns {any | null} The value associated with the specified key if available, otherwise null.
|
|
6
|
+
*/
|
|
7
|
+
export declare const useGetValue: (key: string) => import("@react-native-firebase/remote-config").FirebaseRemoteConfigTypes.ConfigValue | null;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getValue } from "@react-native-firebase/remote-config";
|
|
2
|
+
import { useRemoteConfig } from "./useRemoteConfig";
|
|
3
|
+
import { useMemo } from "react";
|
|
4
|
+
/**
|
|
5
|
+
* Custom hook to retrieve a value associated with a specified key from remote configuration.
|
|
6
|
+
* Utilizes memoization to optimize performance by recalculating the result only if dependencies change.
|
|
7
|
+
* @param {string} key - The key for which the corresponding value is to be fetched from the remote configuration.
|
|
8
|
+
* @returns {any | null} The value associated with the specified key if available, otherwise null.
|
|
9
|
+
*/
|
|
10
|
+
export const useGetValue = (key) => {
|
|
11
|
+
const remoteConfig = useRemoteConfig();
|
|
12
|
+
return useMemo(() => {
|
|
13
|
+
return remoteConfig ? getValue(remoteConfig, key) : null;
|
|
14
|
+
}, [remoteConfig, key]);
|
|
15
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hook to access the Firebase Remote Config instance from the context.
|
|
3
|
+
* @returns {RemoteConfig} The remoteConfig object from the Firebase context.
|
|
4
|
+
*/
|
|
5
|
+
export declare const useRemoteConfig: () => import("@react-native-firebase/remote-config").FirebaseRemoteConfigTypes.Module;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
|
+
/**
|
|
4
|
+
* Hook to access the Firebase Remote Config instance from the context.
|
|
5
|
+
* @returns {RemoteConfig} The remoteConfig object from the Firebase context.
|
|
6
|
+
*/
|
|
7
|
+
export const useRemoteConfig = () => {
|
|
8
|
+
const { remoteConfig } = useContext(FirebaseContext);
|
|
9
|
+
return remoteConfig;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|