react-query-firebase 1.3.2 → 2.0.0-rc1
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/dist/src/analytics/useAnalytics.d.ts +1 -1
- package/dist/src/analytics/useLogEvent.js +1 -1
- package/dist/src/analytics/useSetAnalyticsCollectionEnabled.js +1 -1
- package/dist/src/auth/index.d.ts +0 -1
- package/dist/src/auth/index.js +0 -1
- package/dist/src/auth/useAuth.d.ts +1 -1
- package/dist/src/auth/useCreateUserWitEmailAndPasswordMutation.d.ts +5 -5
- package/dist/src/auth/useCreateUserWitEmailAndPasswordMutation.js +3 -3
- package/dist/src/auth/useCurrentUser.d.ts +1 -1
- package/dist/src/auth/useCurrentUser.js +4 -4
- package/dist/src/auth/useIdToken.d.ts +26 -8
- package/dist/src/auth/useIdToken.js +21 -7
- package/dist/src/auth/useReauthenticateWitCredentialMutation.d.ts +7 -7
- package/dist/src/auth/useReauthenticateWitCredentialMutation.js +3 -3
- package/dist/src/auth/useReauthenticateWitRedirectMutation.d.ts +7 -7
- package/dist/src/auth/useReauthenticateWitRedirectMutation.js +3 -3
- package/dist/src/auth/useSendEmailVerificationMutation.d.ts +6 -6
- package/dist/src/auth/useSendEmailVerificationMutation.js +3 -3
- package/dist/src/auth/useSignInWitEmailAndPasswordMutation.d.ts +4 -4
- package/dist/src/auth/useSignInWitEmailAndPasswordMutation.js +2 -2
- package/dist/src/auth/useSignInWitRedirectMutation.d.ts +6 -6
- package/dist/src/auth/useSignInWitRedirectMutation.js +3 -3
- package/dist/src/auth/useSignOutMutation.d.ts +4 -4
- package/dist/src/auth/useSignOutMutation.js +3 -3
- package/dist/src/auth/useUpdateProfileMutation.d.ts +6 -6
- package/dist/src/auth/useUpdateProfileMutation.js +3 -3
- package/dist/src/context/FirebaseContext.d.ts +6 -10
- package/dist/src/context/FirebaseContextProvider.d.ts +25 -7
- package/dist/src/context/FirebaseContextProvider.js +41 -41
- package/dist/src/firestore/useAddDocMutation.d.ts +7 -9
- package/dist/src/firestore/useAddDocMutation.js +4 -5
- package/dist/src/firestore/useBatchWrite.d.ts +5 -5
- package/dist/src/firestore/useBatchWrite.js +1 -1
- package/dist/src/firestore/useCollectionReference.d.ts +5 -5
- package/dist/src/firestore/useCollectionReference.js +3 -5
- package/dist/src/firestore/useCompositeFilter.d.ts +6 -6
- package/dist/src/firestore/useCompositeFilter.js +12 -9
- package/dist/src/firestore/useCountQuery.d.ts +7 -7
- package/dist/src/firestore/useCountQuery.js +3 -3
- package/dist/src/firestore/useDeleteDocMutation.d.ts +6 -6
- package/dist/src/firestore/useDeleteDocMutation.js +1 -1
- package/dist/src/firestore/useDocReference.d.ts +6 -6
- package/dist/src/firestore/useDocReference.js +6 -10
- package/dist/src/firestore/useDocReferences.d.ts +6 -6
- package/dist/src/firestore/useDocReferences.js +4 -6
- package/dist/src/firestore/useFirestore.d.ts +1 -1
- package/dist/src/firestore/useGetDocData.d.ts +5 -5
- package/dist/src/firestore/useGetRealtimeDocData.d.ts +7 -7
- package/dist/src/firestore/useGetRealtimeDocData.js +1 -1
- package/dist/src/firestore/useInfiniteQuery.d.ts +5 -6
- package/dist/src/firestore/useInfiniteQuery.js +3 -3
- package/dist/src/firestore/useQuery.d.ts +7 -8
- package/dist/src/firestore/useQuery.js +5 -5
- package/dist/src/firestore/useRunTransaction.d.ts +5 -5
- package/dist/src/firestore/useRunTransaction.js +1 -1
- package/dist/src/firestore/useSetDocMutation.d.ts +7 -7
- package/dist/src/firestore/useSetDocMutation.js +2 -2
- package/dist/src/firestore/useUpdateDocMutation.d.ts +7 -8
- package/dist/src/firestore/useUpdateDocMutation.js +4 -4
- package/dist/src/firestore/utils/getDocData.d.ts +3 -3
- package/dist/src/firestore/utils/getDocData.js +1 -1
- package/dist/src/firestore/utils/getDocRef.d.ts +8 -8
- package/dist/src/firestore/utils/getDocRef.js +5 -9
- package/dist/src/firestore/utils/getDocSnap.d.ts +4 -4
- package/dist/src/firestore/utils/getDocSnap.js +2 -2
- package/dist/src/remoteConfig/useFetchAndActivate.js +1 -1
- package/dist/src/remoteConfig/useGetValue.d.ts +1 -1
- package/dist/src/remoteConfig/useGetValue.js +1 -1
- package/dist/src/remoteConfig/useRemoteConfig.d.ts +1 -1
- package/package.json +15 -5
- package/src/analytics/useLogEvent.ts +1 -1
- package/src/analytics/useSetAnalyticsCollectionEnabled.ts +1 -1
- package/src/auth/index.ts +0 -1
- package/src/auth/useCreateUserWitEmailAndPasswordMutation.ts +10 -5
- package/src/auth/useCurrentUser.ts +4 -4
- package/src/auth/useIdToken.ts +31 -9
- package/src/auth/useReauthenticateWitCredentialMutation.ts +12 -7
- package/src/auth/useReauthenticateWitRedirectMutation.ts +12 -7
- package/src/auth/useSendEmailVerificationMutation.ts +11 -6
- package/src/auth/useSignInWitEmailAndPasswordMutation.ts +9 -4
- package/src/auth/useSignInWitRedirectMutation.ts +11 -6
- package/src/auth/useSignOutMutation.ts +5 -5
- package/src/auth/useUpdateProfileMutation.ts +6 -6
- package/src/context/FirebaseContext.ts +6 -10
- package/src/context/FirebaseContextProvider.tsx +71 -44
- package/src/firestore/useAddDocMutation.ts +15 -22
- package/src/firestore/useBatchWrite.ts +7 -4
- package/src/firestore/useCollectionReference.ts +13 -9
- package/src/firestore/useCompositeFilter.ts +22 -27
- package/src/firestore/useCountQuery.ts +9 -13
- package/src/firestore/useDeleteDocMutation.ts +11 -10
- package/src/firestore/useDocReference.ts +23 -19
- package/src/firestore/useDocReferences.ts +15 -11
- package/src/firestore/useGetDocData.ts +14 -15
- package/src/firestore/useGetRealtimeDocData.ts +16 -10
- package/src/firestore/useInfiniteQuery.ts +9 -16
- package/src/firestore/useQuery.ts +11 -20
- package/src/firestore/useRunTransaction.ts +9 -4
- package/src/firestore/useSetDocMutation.ts +14 -11
- package/src/firestore/useUpdateDocMutation.ts +15 -21
- package/src/firestore/utils/getDocData.ts +7 -9
- package/src/firestore/utils/getDocRef.ts +13 -17
- package/src/firestore/utils/getDocSnap.ts +7 -9
- package/src/remoteConfig/useFetchAndActivate.ts +1 -1
- package/src/remoteConfig/useGetValue.ts +1 -1
- package/dist/src/auth/useAuthStateReady.d.ts +0 -6
- package/dist/src/auth/useAuthStateReady.js +0 -19
- package/src/auth/useAuthStateReady.ts +0 -24
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { collection,
|
|
1
|
+
import { collection, FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useMemo } from "react";
|
|
4
4
|
import { useFirestore } from "./useFirestore";
|
|
5
5
|
|
|
6
|
-
export type UseCollectionReferenceOptions<
|
|
7
|
-
|
|
6
|
+
export type UseCollectionReferenceOptions<
|
|
7
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
8
|
+
> = {
|
|
9
|
+
reference?:
|
|
10
|
+
| FirebaseFirestoreTypes.CollectionReference<AppModelType>
|
|
11
|
+
| FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
8
12
|
path: string;
|
|
9
13
|
pathSegments?: string[];
|
|
10
14
|
};
|
|
@@ -14,24 +18,24 @@ export type UseCollectionReferenceOptions<AppModelType, DbModelType extends Docu
|
|
|
14
18
|
*
|
|
15
19
|
* This hook utilizes useMemo for optimization, ensuring the collection reference is recalculated only when its dependencies change.
|
|
16
20
|
*
|
|
17
|
-
* @param {UseCollectionReferenceOptions<AppModelType
|
|
21
|
+
* @param {UseCollectionReferenceOptions<AppModelType>} options - The options including path, reference, and pathSegments to construct the Firestore collection reference.
|
|
18
22
|
* @param {string} options.path - The base path for the collection.
|
|
19
23
|
* @param {FirestoreReference} options.reference - An optional Firestore reference object that should be of type "collection".
|
|
20
24
|
* @param {string[]} options.pathSegments - Additional path segments to append to the base path.
|
|
21
25
|
* @returns {CollectionReference} A Firestore collection reference constructed using the specified path, reference, and path segments.
|
|
22
26
|
*/
|
|
23
|
-
export const useCollectionReference = <
|
|
27
|
+
export const useCollectionReference = <
|
|
28
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
29
|
+
>({
|
|
24
30
|
path,
|
|
25
31
|
reference,
|
|
26
32
|
pathSegments
|
|
27
|
-
}: UseCollectionReferenceOptions<AppModelType
|
|
33
|
+
}: UseCollectionReferenceOptions<AppModelType>) => {
|
|
28
34
|
const db = useFirestore();
|
|
29
35
|
|
|
30
36
|
return useMemo(() => {
|
|
31
37
|
return !reference
|
|
32
38
|
? collection(db, path || "", ...(pathSegments || []))
|
|
33
|
-
: reference
|
|
34
|
-
? collection(reference, path, ...(pathSegments || []))
|
|
35
|
-
: collection(reference, path, ...(pathSegments || []));
|
|
39
|
+
: collection(reference, path, ...(pathSegments || []));
|
|
36
40
|
}, [path, reference?.path, pathSegments]);
|
|
37
41
|
};
|
|
@@ -1,27 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
DocumentData,
|
|
3
|
-
QueryCompositeFilterConstraint,
|
|
4
|
-
QueryFilterConstraint,
|
|
5
|
-
WhereFilterOp,
|
|
6
|
-
and,
|
|
7
|
-
documentId,
|
|
8
|
-
or,
|
|
9
|
-
where
|
|
10
|
-
} from "firebase/firestore";
|
|
1
|
+
import firebase, { FirebaseFirestoreTypes, and, or, where } from "@react-native-firebase/firestore";
|
|
11
2
|
import { useMemo } from "react";
|
|
12
3
|
|
|
13
|
-
type CompositeFilterDocumentData = DocumentData;
|
|
4
|
+
type CompositeFilterDocumentData = FirebaseFirestoreTypes.DocumentData;
|
|
14
5
|
|
|
15
6
|
export type QueryElement<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> =
|
|
16
|
-
Partial<QueryCompositeFilterConstraint> & {
|
|
7
|
+
Partial<FirebaseFirestoreTypes.QueryCompositeFilterConstraint> & {
|
|
17
8
|
children?: QueryElement[];
|
|
18
9
|
field?: keyof (DbModelType & { documentId?: string[] });
|
|
19
10
|
value?: DbModelType[keyof DbModelType];
|
|
20
|
-
op?: WhereFilterOp;
|
|
11
|
+
op?: FirebaseFirestoreTypes.WhereFilterOp;
|
|
21
12
|
};
|
|
22
13
|
|
|
23
14
|
export type CompositeFilter<DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData> =
|
|
24
|
-
QueryCompositeFilterConstraint & {
|
|
15
|
+
FirebaseFirestoreTypes.QueryCompositeFilterConstraint & {
|
|
25
16
|
children: QueryElement<DbModelType & { documentId?: string[] }>[];
|
|
26
17
|
};
|
|
27
18
|
|
|
@@ -34,26 +25,31 @@ export type UseCompositeFilter<DbModelType extends CompositeFilterDocumentData =
|
|
|
34
25
|
* It recursively builds query constraints using logical "or" or "and" operators.
|
|
35
26
|
*
|
|
36
27
|
* @param {QueryElement<DbModelType>} query - The query element or structure to be evaluated and transformed into filter constraints.
|
|
37
|
-
* @returns {
|
|
28
|
+
* @returns {QueryFieldFilterConstraint | null} A constructed query filter constraint based on the input query, or null if no valid constraints can be derived.
|
|
38
29
|
*/
|
|
39
30
|
|
|
40
31
|
const buildCompositeQuery = <DbModelType extends CompositeFilterDocumentData = CompositeFilterDocumentData>(
|
|
41
32
|
query: QueryElement<DbModelType>
|
|
42
|
-
):
|
|
33
|
+
): FirebaseFirestoreTypes.QueryFieldFilterConstraint | FirebaseFirestoreTypes.QueryCompositeFilterConstraint | null => {
|
|
43
34
|
if (query.children) {
|
|
44
35
|
const queryConstraints = query.children
|
|
45
|
-
.map((subQuery) =>
|
|
46
|
-
|
|
36
|
+
.map((subQuery) => {
|
|
37
|
+
if (subQuery.field && subQuery.op) {
|
|
38
|
+
return where(
|
|
39
|
+
query.field === "documentId" ? firebase.FieldPath.documentId() : (query.field as string),
|
|
40
|
+
subQuery.op,
|
|
41
|
+
query.value
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return buildCompositeQuery(subQuery);
|
|
45
|
+
})
|
|
46
|
+
.filter((constraint) => !!constraint);
|
|
47
47
|
|
|
48
48
|
if (queryConstraints.length <= 0) {
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
-
return (query as CompositeFilter).
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (query.field && query.op) {
|
|
56
|
-
return where(query.field === "documentId" ? documentId() : (query.field as string), query.op, query.value);
|
|
52
|
+
return (query as CompositeFilter).operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
return null;
|
|
@@ -74,13 +70,12 @@ export const useCompositeFilter = <DbModelType extends CompositeFilterDocumentDa
|
|
|
74
70
|
}: UseCompositeFilter<DbModelType>) => {
|
|
75
71
|
return useMemo(() => {
|
|
76
72
|
const queryConstraints =
|
|
77
|
-
query?.children?.map?.((subQuery) => buildCompositeQuery(subQuery))
|
|
78
|
-
(constraint) => !!constraint
|
|
79
|
-
) ?? [];
|
|
73
|
+
query?.children?.map?.((subQuery) => buildCompositeQuery(subQuery))
|
|
74
|
+
?.filter<FirebaseFirestoreTypes.QueryFilterConstraint>?.((constraint) => !!constraint) ?? [];
|
|
80
75
|
|
|
81
76
|
if (queryConstraints.length <= 0) {
|
|
82
77
|
return undefined;
|
|
83
78
|
}
|
|
84
|
-
return query?.
|
|
79
|
+
return query?.operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
|
|
85
80
|
}, [query]);
|
|
86
81
|
};
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
2
|
+
FirebaseFirestoreTypes,
|
|
3
3
|
getCountFromServer,
|
|
4
|
-
Query,
|
|
5
4
|
query,
|
|
6
|
-
QueryCompositeFilterConstraint,
|
|
7
5
|
QueryConstraint,
|
|
8
6
|
QueryNonFilterConstraint
|
|
9
|
-
} from "firebase/firestore";
|
|
7
|
+
} from "@react-native-firebase/firestore";
|
|
10
8
|
|
|
11
9
|
import {
|
|
12
10
|
UseQueryResult,
|
|
@@ -15,14 +13,13 @@ import {
|
|
|
15
13
|
} from "@tanstack/react-query";
|
|
16
14
|
|
|
17
15
|
type UseCountQueryOptions<
|
|
18
|
-
AppModelType extends DocumentData = DocumentData
|
|
19
|
-
DbModelType extends DocumentData = DocumentData
|
|
16
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
20
17
|
> = {
|
|
21
18
|
options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> &
|
|
22
19
|
Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
|
|
23
|
-
query: Query<AppModelType
|
|
20
|
+
query: FirebaseFirestoreTypes.Query<AppModelType>;
|
|
24
21
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
25
|
-
compositeFilter?: QueryCompositeFilterConstraint;
|
|
22
|
+
compositeFilter?: FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
|
|
26
23
|
};
|
|
27
24
|
|
|
28
25
|
/**
|
|
@@ -31,8 +28,8 @@ type UseCountQueryOptions<
|
|
|
31
28
|
* This function utilizes React Query to asynchronously fetch the count of documents from a server database
|
|
32
29
|
* that match the provided query constraints and an optional composite filter.
|
|
33
30
|
*
|
|
34
|
-
* @param {UseCountQueryOptions<AppModelType
|
|
35
|
-
* @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
|
|
31
|
+
* @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
32
|
+
* @param {AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData} [options.options] - Additional options for the React Query.
|
|
36
33
|
* @param {unknown} [options.query] - Reference to the query object to be executed.
|
|
37
34
|
* @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
|
|
38
35
|
* @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
|
|
@@ -40,14 +37,13 @@ type UseCountQueryOptions<
|
|
|
40
37
|
*/
|
|
41
38
|
|
|
42
39
|
export const useCountQuery = <
|
|
43
|
-
AppModelType extends DocumentData = DocumentData
|
|
44
|
-
DbModelType extends DocumentData = DocumentData
|
|
40
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
45
41
|
>({
|
|
46
42
|
options,
|
|
47
43
|
query: queryReference,
|
|
48
44
|
queryConstraints = [],
|
|
49
45
|
compositeFilter
|
|
50
|
-
}: UseCountQueryOptions<AppModelType
|
|
46
|
+
}: UseCountQueryOptions<AppModelType>): UseQueryResult<number> => {
|
|
51
47
|
return useReactQuery({
|
|
52
48
|
...options,
|
|
53
49
|
queryFn: async () => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
-
import {
|
|
2
|
+
import { FirebaseFirestoreTypes, deleteDoc, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
5
5
|
import { useMemo } from "react";
|
|
6
6
|
|
|
7
7
|
export type UseDeleteDocMutationValues<AppModelType> = {
|
|
@@ -9,12 +9,14 @@ export type UseDeleteDocMutationValues<AppModelType> = {
|
|
|
9
9
|
};
|
|
10
10
|
|
|
11
11
|
export type UseDeleteDocMutationOptions<
|
|
12
|
-
AppModelType extends DocumentData = DocumentData,
|
|
13
|
-
DbModelType extends DocumentData = DocumentData,
|
|
12
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
14
13
|
TContext = unknown
|
|
15
14
|
> = {
|
|
16
|
-
reference: DocumentReference<AppModelType
|
|
17
|
-
options?: Omit<
|
|
15
|
+
reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
|
|
16
|
+
options?: Omit<
|
|
17
|
+
UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>,
|
|
18
|
+
"mutationFn" | "mutationKey"
|
|
19
|
+
>;
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
/**
|
|
@@ -25,13 +27,12 @@ export type UseDeleteDocMutationOptions<
|
|
|
25
27
|
* @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
|
|
26
28
|
*/
|
|
27
29
|
export const useDeleteDocMutation = <
|
|
28
|
-
AppModelType extends DocumentData = DocumentData,
|
|
29
|
-
DbModelType extends DocumentData = DocumentData,
|
|
30
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
30
31
|
TContext = unknown
|
|
31
32
|
>({
|
|
32
33
|
reference,
|
|
33
34
|
options = {}
|
|
34
|
-
}: UseDeleteDocMutationOptions<AppModelType,
|
|
35
|
+
}: UseDeleteDocMutationOptions<AppModelType, TContext>) => {
|
|
35
36
|
const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
|
|
36
37
|
|
|
37
38
|
return useMutation({
|
|
@@ -40,7 +41,7 @@ export const useDeleteDocMutation = <
|
|
|
40
41
|
if (!reference) {
|
|
41
42
|
throw new Error("Reference is undefined");
|
|
42
43
|
}
|
|
43
|
-
await deleteDoc
|
|
44
|
+
await deleteDoc(reference);
|
|
44
45
|
},
|
|
45
46
|
mutationKey
|
|
46
47
|
});
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import { doc,
|
|
1
|
+
import { doc, FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useEffect, useRef } from "react";
|
|
4
4
|
import { useFirestore } from "./useFirestore";
|
|
5
5
|
|
|
6
|
-
export type UseDocReferenceOptions<
|
|
7
|
-
|
|
6
|
+
export type UseDocReferenceOptions<
|
|
7
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
8
|
+
> = {
|
|
9
|
+
reference?:
|
|
10
|
+
| FirebaseFirestoreTypes.CollectionReference<AppModelType>
|
|
11
|
+
| FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
8
12
|
path?: string;
|
|
9
13
|
pathSegments?: string[];
|
|
10
14
|
};
|
|
@@ -14,28 +18,26 @@ export type UseDocReferenceOptions<AppModelType, DbModelType extends DocumentDat
|
|
|
14
18
|
* If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
|
|
15
19
|
*
|
|
16
20
|
* @param {Firestore} db - The Firestore database instance used to create the document reference.
|
|
17
|
-
* @param {UseDocReferenceOptions<AppModelType
|
|
21
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - An object containing the path, path segments, and optional reference.
|
|
18
22
|
* @param {string} options.path - The path to the document in the Firestore database.
|
|
19
23
|
* @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
|
|
20
24
|
* @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
|
|
21
25
|
*
|
|
22
|
-
* @returns {DocumentReference<AppModelType
|
|
26
|
+
* @returns {DocumentReference<AppModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
|
|
23
27
|
*/
|
|
24
|
-
const getDocReference = <
|
|
25
|
-
|
|
26
|
-
|
|
28
|
+
const getDocReference = <
|
|
29
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
30
|
+
>(
|
|
31
|
+
db: FirebaseFirestoreTypes.Module,
|
|
32
|
+
{ path, pathSegments, reference }: UseDocReferenceOptions<AppModelType>
|
|
27
33
|
) => {
|
|
28
34
|
if (!path) {
|
|
29
35
|
return null;
|
|
30
36
|
}
|
|
31
37
|
|
|
32
38
|
return (
|
|
33
|
-
!reference
|
|
34
|
-
|
|
35
|
-
: reference.type === "collection"
|
|
36
|
-
? doc(reference, path, ...(pathSegments || []))
|
|
37
|
-
: doc(reference, path, ...(pathSegments || []))
|
|
38
|
-
) as DocumentReference<AppModelType, DbModelType>;
|
|
39
|
+
!reference ? doc(db, path || "", ...(pathSegments || [])) : doc(reference, path, ...(pathSegments || []))
|
|
40
|
+
) as FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
39
41
|
};
|
|
40
42
|
|
|
41
43
|
/**
|
|
@@ -43,20 +45,22 @@ const getDocReference = <AppModelType, DbModelType extends DocumentData = Docume
|
|
|
43
45
|
* This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
|
|
44
46
|
* The reference is updated whenever the associated path, reference, or path segments change.
|
|
45
47
|
*
|
|
46
|
-
* @param {UseDocReferenceOptions<AppModelType
|
|
48
|
+
* @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
|
|
47
49
|
* @param {string} options.path - The path to the Firestore document.
|
|
48
50
|
* @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
|
|
49
51
|
* @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
|
|
50
52
|
*
|
|
51
|
-
* @returns {DocumentReference<AppModelType
|
|
53
|
+
* @returns {DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
|
|
52
54
|
*/
|
|
53
|
-
export const useDocReference = <
|
|
55
|
+
export const useDocReference = <
|
|
56
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
57
|
+
>({
|
|
54
58
|
path,
|
|
55
59
|
reference,
|
|
56
60
|
pathSegments
|
|
57
|
-
}: UseDocReferenceOptions<AppModelType
|
|
61
|
+
}: UseDocReferenceOptions<AppModelType>) => {
|
|
58
62
|
const db = useFirestore();
|
|
59
|
-
const ref = useRef<DocumentReference<AppModelType
|
|
63
|
+
const ref = useRef<FirebaseFirestoreTypes.DocumentReference<AppModelType> | null>(
|
|
60
64
|
getDocReference(db, { path, pathSegments, reference })
|
|
61
65
|
);
|
|
62
66
|
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { doc, FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useMemo } from "react";
|
|
4
4
|
import { useFirestore } from "./useFirestore";
|
|
5
5
|
|
|
6
|
-
export type UseDocReferencesOptions<
|
|
7
|
-
|
|
6
|
+
export type UseDocReferencesOptions<
|
|
7
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
8
|
+
> = {
|
|
9
|
+
reference?:
|
|
10
|
+
| FirebaseFirestoreTypes.CollectionReference<AppModelType>
|
|
11
|
+
| FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
8
12
|
path: string;
|
|
9
13
|
pathSegments?: string[];
|
|
10
14
|
};
|
|
@@ -19,14 +23,16 @@ export type UseDocReferencesOptions<AppModelType, DbModelType extends DocumentDa
|
|
|
19
23
|
* to interact with the Firestore database documents. It uses memoization to optimize
|
|
20
24
|
* reference generation.
|
|
21
25
|
*
|
|
22
|
-
* @param {UseDocReferencesOptions<AppModelType
|
|
26
|
+
* @param {UseDocReferencesOptions<AppModelType>[]} references - An array of options
|
|
23
27
|
* specifying how to generate document references. Each option may include a `path`
|
|
24
28
|
* as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
|
|
25
|
-
* @returns {DocumentReference<AppModelType
|
|
29
|
+
* @returns {DocumentReference<AppModelType>[]} An array of Firestore document references
|
|
26
30
|
* generated from the options provided.
|
|
27
31
|
*/
|
|
28
|
-
export const useDocReferences = <
|
|
29
|
-
|
|
32
|
+
export const useDocReferences = <
|
|
33
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
34
|
+
>(
|
|
35
|
+
references: UseDocReferencesOptions<AppModelType>[]
|
|
30
36
|
) => {
|
|
31
37
|
const db = useFirestore();
|
|
32
38
|
|
|
@@ -35,10 +41,8 @@ export const useDocReferences = <AppModelType, DbModelType extends DocumentData
|
|
|
35
41
|
return (
|
|
36
42
|
!reference
|
|
37
43
|
? doc(db, path || "", ...(pathSegments || []))
|
|
38
|
-
: reference
|
|
39
|
-
|
|
40
|
-
: doc(reference, path, ...(pathSegments || []))
|
|
41
|
-
) as DocumentReference<AppModelType, DbModelType>;
|
|
44
|
+
: doc(reference, path, ...(pathSegments || []))
|
|
45
|
+
) as FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
42
46
|
});
|
|
43
47
|
}, [references]);
|
|
44
48
|
};
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useFirestore } from "./useFirestore";
|
|
4
4
|
import { getDocData } from "./utils/getDocData";
|
|
5
5
|
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
|
|
6
6
|
|
|
7
|
-
type UseGetDocOptions<
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
};
|
|
7
|
+
type UseGetDocOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> =
|
|
8
|
+
{
|
|
9
|
+
options: Omit<UseQueryOptions<AppModelType | null | undefined, Error, AppModelType>, "queryFn"> &
|
|
10
|
+
Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
11
|
+
path?: string;
|
|
12
|
+
pathSegments?: string[];
|
|
13
|
+
reference:
|
|
14
|
+
| FirebaseFirestoreTypes.CollectionReference<AppModelType>
|
|
15
|
+
| FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
16
|
+
};
|
|
17
17
|
|
|
18
18
|
/**
|
|
19
19
|
* Custom React Hook to retrieve document data from Firestore using specified parameters.
|
|
@@ -24,18 +24,17 @@ type UseGetDocOptions<
|
|
|
24
24
|
* @returns {Object} Result of the query containing document data and query status.
|
|
25
25
|
*/
|
|
26
26
|
export const useGetDocData = <
|
|
27
|
-
AppModelType extends DocumentData = DocumentData
|
|
28
|
-
DbModelType extends DocumentData = DocumentData
|
|
27
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
29
28
|
>({
|
|
30
29
|
options,
|
|
31
30
|
reference,
|
|
32
31
|
path,
|
|
33
32
|
pathSegments
|
|
34
|
-
}: UseGetDocOptions<AppModelType
|
|
33
|
+
}: UseGetDocOptions<AppModelType>) => {
|
|
35
34
|
const db = useFirestore();
|
|
36
35
|
|
|
37
36
|
return useQuery({
|
|
38
37
|
...options,
|
|
39
|
-
queryFn: () => getDocData<AppModelType
|
|
38
|
+
queryFn: () => getDocData<AppModelType>({ db, reference, path, pathSegments })
|
|
40
39
|
});
|
|
41
40
|
};
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FirebaseFirestoreTypes, onSnapshot } from "@react-native-firebase/firestore";
|
|
2
2
|
|
|
3
3
|
import { useEffect, useMemo, useState } from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
5
5
|
import { useDocReference } from "./useDocReference";
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* @inline
|
|
9
9
|
*/
|
|
10
|
-
export type UseGetRealtimeDocDataOptions<
|
|
10
|
+
export type UseGetRealtimeDocDataOptions<
|
|
11
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
12
|
+
> = {
|
|
11
13
|
/**
|
|
12
14
|
* A slash-separated path to a document. Has to be omitted to use
|
|
13
15
|
*/
|
|
@@ -15,7 +17,9 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
15
17
|
/**
|
|
16
18
|
* A reference to a collection.
|
|
17
19
|
*/
|
|
18
|
-
reference?:
|
|
20
|
+
reference?:
|
|
21
|
+
| FirebaseFirestoreTypes.CollectionReference<AppModelType>
|
|
22
|
+
| FirebaseFirestoreTypes.DocumentReference<AppModelType>;
|
|
19
23
|
/**
|
|
20
24
|
* Additional path segments that will be applied relative
|
|
21
25
|
* to the first argument.
|
|
@@ -25,7 +29,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
25
29
|
* A callback to be called if the listen fails or is
|
|
26
30
|
* cancelled. No further callbacks will occur.
|
|
27
31
|
*/
|
|
28
|
-
onError?: (error:
|
|
32
|
+
onError?: (error: ReactNativeFirebase.NativeFirebaseError | Error) => unknown;
|
|
29
33
|
};
|
|
30
34
|
|
|
31
35
|
/**
|
|
@@ -34,7 +38,7 @@ export type UseGetRealtimeDocDataOptions<AppModelType, DbModelType extends Docum
|
|
|
34
38
|
export type UseGetRealtimeDocDataResult<AppModelType> = {
|
|
35
39
|
data: AppModelType | null;
|
|
36
40
|
isError: boolean;
|
|
37
|
-
error:
|
|
41
|
+
error: ReactNativeFirebase.NativeFirebaseError | Error | null;
|
|
38
42
|
isFetching: boolean;
|
|
39
43
|
};
|
|
40
44
|
|
|
@@ -60,21 +64,23 @@ export type UseGetRealtimeDocDataResult<AppModelType> = {
|
|
|
60
64
|
* };
|
|
61
65
|
* ```
|
|
62
66
|
*/
|
|
63
|
-
export const useGetRealtimeDocData = <
|
|
67
|
+
export const useGetRealtimeDocData = <
|
|
68
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
69
|
+
>({
|
|
64
70
|
path,
|
|
65
71
|
pathSegments,
|
|
66
72
|
reference,
|
|
67
73
|
onError
|
|
68
|
-
}: UseGetRealtimeDocDataOptions<AppModelType
|
|
74
|
+
}: UseGetRealtimeDocDataOptions<AppModelType>): UseGetRealtimeDocDataResult<AppModelType> => {
|
|
69
75
|
const ref = useDocReference({ path, reference, pathSegments });
|
|
70
76
|
const [doc, setDoc] = useState<AppModelType | null>(null);
|
|
71
77
|
const [isError, setIsError] = useState(false);
|
|
72
78
|
const [isFetching, setIsFetching] = useState(true);
|
|
73
|
-
const [error, setError] = useState<
|
|
79
|
+
const [error, setError] = useState<ReactNativeFirebase.NativeFirebaseError | Error | null>(null);
|
|
74
80
|
|
|
75
81
|
useEffect(() => {
|
|
76
82
|
const unsubscribe = ref
|
|
77
|
-
? onSnapshot(ref, {
|
|
83
|
+
? onSnapshot<AppModelType>(ref, {
|
|
78
84
|
next: async (snapshot) => {
|
|
79
85
|
setIsFetching(false);
|
|
80
86
|
setDoc(snapshot.data() || null);
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
FirestoreDataConverter,
|
|
2
|
+
FirebaseFirestoreTypes,
|
|
4
3
|
getDocs,
|
|
5
|
-
Query,
|
|
6
4
|
query,
|
|
7
|
-
QueryCompositeFilterConstraint,
|
|
8
5
|
QueryConstraint,
|
|
9
6
|
QueryNonFilterConstraint
|
|
10
|
-
} from "firebase/firestore";
|
|
7
|
+
} from "@react-native-firebase/firestore";
|
|
11
8
|
|
|
12
9
|
import {
|
|
13
10
|
useInfiniteQuery as useInfiniteReactQuery,
|
|
@@ -18,8 +15,7 @@ import {
|
|
|
18
15
|
} from "@tanstack/react-query";
|
|
19
16
|
|
|
20
17
|
type UseInfiniteQueryOptions<
|
|
21
|
-
AppModelType extends DocumentData = DocumentData,
|
|
22
|
-
DbModelType extends DocumentData = DocumentData,
|
|
18
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
23
19
|
TQueryKey extends QueryKey = QueryKey
|
|
24
20
|
> = {
|
|
25
21
|
options: Omit<
|
|
@@ -46,10 +42,9 @@ type UseInfiniteQueryOptions<
|
|
|
46
42
|
"queryKey"
|
|
47
43
|
>
|
|
48
44
|
>;
|
|
49
|
-
query: Query<AppModelType
|
|
45
|
+
query: FirebaseFirestoreTypes.Query<AppModelType>;
|
|
50
46
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
51
|
-
compositeFilter?: QueryCompositeFilterConstraint;
|
|
52
|
-
converter?: FirestoreDataConverter<AppModelType, DbModelType>;
|
|
47
|
+
compositeFilter?: FirebaseFirestoreTypes.QueryCompositeFilterConstraint;
|
|
53
48
|
};
|
|
54
49
|
|
|
55
50
|
/**
|
|
@@ -60,15 +55,13 @@ type UseInfiniteQueryOptions<
|
|
|
60
55
|
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} Result object containing the infinite data and methods for fetching more pages.
|
|
61
56
|
*/
|
|
62
57
|
export const useInfiniteQuery = <
|
|
63
|
-
AppModelType extends DocumentData = DocumentData
|
|
64
|
-
DbModelType extends DocumentData = DocumentData
|
|
58
|
+
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
|
|
65
59
|
>({
|
|
66
60
|
options,
|
|
67
61
|
query: queryReference,
|
|
68
62
|
queryConstraints = [],
|
|
69
|
-
compositeFilter
|
|
70
|
-
|
|
71
|
-
}: UseInfiniteQueryOptions<AppModelType, DbModelType>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
|
|
63
|
+
compositeFilter
|
|
64
|
+
}: UseInfiniteQueryOptions<AppModelType>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
|
|
72
65
|
return useInfiniteReactQuery({
|
|
73
66
|
...options,
|
|
74
67
|
queryFn: async ({ pageParam }) => {
|
|
@@ -77,7 +70,7 @@ export const useInfiniteQuery = <
|
|
|
77
70
|
? query(queryReference, compositeFilter, ...(allQueryConstraints as QueryNonFilterConstraint[]))
|
|
78
71
|
: query(queryReference, ...allQueryConstraints);
|
|
79
72
|
|
|
80
|
-
const querySnapshot = await getDocs(
|
|
73
|
+
const querySnapshot = await getDocs(queryToExecute);
|
|
81
74
|
const docs: AppModelType[] = [];
|
|
82
75
|
|
|
83
76
|
if (querySnapshot) {
|