react-query-firebase 2.3.2 → 2.4.0

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.
Files changed (133) hide show
  1. package/package.json +1 -1
  2. package/react-native/firestore/index.d.ts +5 -0
  3. package/react-native/firestore/index.js +5 -0
  4. package/react-native/firestore/index.ts +5 -0
  5. package/react-native/firestore/useAddDocMutation.d.ts +38 -9
  6. package/react-native/firestore/useAddDocMutation.js +25 -11
  7. package/react-native/firestore/useAddDocMutation.ts +43 -13
  8. package/react-native/firestore/useBatchWrite.d.ts +31 -7
  9. package/react-native/firestore/useBatchWrite.js +22 -4
  10. package/react-native/firestore/useBatchWrite.ts +31 -7
  11. package/react-native/firestore/useCollectionReference.d.ts +30 -9
  12. package/react-native/firestore/useCollectionReference.js +15 -7
  13. package/react-native/firestore/useCollectionReference.ts +34 -25
  14. package/react-native/firestore/useCompositeFilter.d.ts +1 -46
  15. package/react-native/firestore/useCompositeFilter.js +2 -45
  16. package/react-native/firestore/useCompositeFilter.ts +2 -74
  17. package/react-native/firestore/useCountQuery.d.ts +1 -1
  18. package/react-native/firestore/useCountQuery.ts +1 -1
  19. package/react-native/firestore/useDeleteDocMutation.d.ts +39 -10
  20. package/react-native/firestore/useDeleteDocMutation.js +22 -5
  21. package/react-native/firestore/useDeleteDocMutation.ts +38 -15
  22. package/react-native/firestore/useDocReference.d.ts +22 -14
  23. package/react-native/firestore/useDocReference.js +20 -26
  24. package/react-native/firestore/useDocReference.ts +34 -47
  25. package/react-native/firestore/useDocReferences.d.ts +23 -18
  26. package/react-native/firestore/useDocReferences.js +18 -19
  27. package/react-native/firestore/useDocReferences.ts +26 -34
  28. package/react-native/firestore/useFirestore.d.ts +4 -3
  29. package/react-native/firestore/useFirestore.js +4 -3
  30. package/react-native/firestore/useFirestore.ts +4 -3
  31. package/react-native/firestore/useGetDocData.d.ts +33 -14
  32. package/react-native/firestore/useGetDocData.js +21 -6
  33. package/react-native/firestore/useGetDocData.ts +34 -22
  34. package/react-native/firestore/useInfiniteQuery.d.ts +2 -2
  35. package/react-native/firestore/useInfiniteQuery.ts +3 -3
  36. package/react-native/firestore/useQuery.d.ts +1 -1
  37. package/react-native/firestore/useQuery.ts +1 -1
  38. package/react-native/firestore/useQueryConstraints.d.ts +2 -22
  39. package/react-native/firestore/useQueryConstraints.js +1 -39
  40. package/react-native/firestore/useQueryConstraints.ts +3 -43
  41. package/react-native/firestore/useRunTransaction.d.ts +33 -9
  42. package/react-native/firestore/useRunTransaction.js +20 -4
  43. package/react-native/firestore/useRunTransaction.ts +35 -16
  44. package/react-native/firestore/useSetDocMutation.d.ts +43 -11
  45. package/react-native/firestore/useSetDocMutation.js +28 -11
  46. package/react-native/firestore/useSetDocMutation.ts +49 -28
  47. package/react-native/firestore/useUpdateDocMutation.d.ts +40 -18
  48. package/react-native/firestore/useUpdateDocMutation.js +18 -13
  49. package/react-native/firestore/useUpdateDocMutation.ts +40 -31
  50. package/react-native/firestore/utils/buildCompositeFilter.d.ts +47 -0
  51. package/react-native/firestore/utils/buildCompositeFilter.js +45 -0
  52. package/react-native/firestore/utils/buildCompositeFilter.ts +75 -0
  53. package/react-native/firestore/utils/buildQueryConstraint.d.ts +23 -0
  54. package/react-native/firestore/utils/buildQueryConstraint.js +41 -0
  55. package/react-native/firestore/utils/buildQueryConstraint.ts +54 -0
  56. package/react-native/firestore/utils/getDocData.d.ts +11 -11
  57. package/react-native/firestore/utils/getDocData.js +5 -8
  58. package/react-native/firestore/utils/getDocData.ts +12 -16
  59. package/react-native/firestore/utils/getDocRef.d.ts +12 -9
  60. package/react-native/firestore/utils/getDocRef.js +7 -8
  61. package/react-native/firestore/utils/getDocRef.ts +12 -13
  62. package/react-native/firestore/utils/getDocSnap.d.ts +11 -13
  63. package/react-native/firestore/utils/getDocSnap.js +5 -10
  64. package/react-native/firestore/utils/getDocSnap.ts +12 -17
  65. package/types/AppModel.d.ts +4 -1
  66. package/types/AppModel.ts +4 -1
  67. package/web/firestore/index.d.ts +5 -0
  68. package/web/firestore/index.js +5 -0
  69. package/web/firestore/index.ts +5 -0
  70. package/web/firestore/useAddDocMutation.d.ts +41 -13
  71. package/web/firestore/useAddDocMutation.js +27 -14
  72. package/web/firestore/useAddDocMutation.ts +49 -37
  73. package/web/firestore/useBatchWrite.d.ts +34 -7
  74. package/web/firestore/useBatchWrite.js +22 -4
  75. package/web/firestore/useBatchWrite.ts +34 -7
  76. package/web/firestore/useCollectionReference.d.ts +32 -11
  77. package/web/firestore/useCollectionReference.js +15 -7
  78. package/web/firestore/useCollectionReference.ts +34 -13
  79. package/web/firestore/useCompositeFilter.d.ts +2 -47
  80. package/web/firestore/useCompositeFilter.js +2 -42
  81. package/web/firestore/useCompositeFilter.ts +2 -72
  82. package/web/firestore/useCountQuery.d.ts +4 -4
  83. package/web/firestore/useCountQuery.ts +5 -5
  84. package/web/firestore/useDeleteDocMutation.d.ts +41 -11
  85. package/web/firestore/useDeleteDocMutation.js +24 -7
  86. package/web/firestore/useDeleteDocMutation.ts +45 -23
  87. package/web/firestore/useDocReference.d.ts +23 -15
  88. package/web/firestore/useDocReference.js +16 -31
  89. package/web/firestore/useDocReference.ts +28 -46
  90. package/web/firestore/useDocReferences.d.ts +23 -18
  91. package/web/firestore/useDocReferences.js +18 -21
  92. package/web/firestore/useDocReferences.ts +26 -30
  93. package/web/firestore/useFirestore.d.ts +4 -3
  94. package/web/firestore/useFirestore.js +4 -3
  95. package/web/firestore/useFirestore.ts +4 -3
  96. package/web/firestore/useGetDocData.d.ts +32 -13
  97. package/web/firestore/useGetDocData.js +21 -6
  98. package/web/firestore/useGetDocData.ts +34 -23
  99. package/web/firestore/useGetRealtimeDocData.js +2 -2
  100. package/web/firestore/useGetRealtimeDocData.ts +2 -2
  101. package/web/firestore/useInfiniteQuery.d.ts +2 -2
  102. package/web/firestore/useInfiniteQuery.js +1 -1
  103. package/web/firestore/useInfiniteQuery.ts +4 -4
  104. package/web/firestore/useQuery.d.ts +1 -1
  105. package/web/firestore/useQuery.js +1 -1
  106. package/web/firestore/useQuery.ts +2 -2
  107. package/web/firestore/useQueryConstraints.d.ts +0 -21
  108. package/web/firestore/useQueryConstraints.js +1 -39
  109. package/web/firestore/useQueryConstraints.ts +2 -51
  110. package/web/firestore/useRunTransaction.d.ts +33 -8
  111. package/web/firestore/useRunTransaction.js +20 -4
  112. package/web/firestore/useRunTransaction.ts +35 -10
  113. package/web/firestore/useSetDocMutation.d.ts +45 -12
  114. package/web/firestore/useSetDocMutation.js +28 -11
  115. package/web/firestore/useSetDocMutation.ts +51 -26
  116. package/web/firestore/useUpdateDocMutation.d.ts +42 -20
  117. package/web/firestore/useUpdateDocMutation.js +20 -15
  118. package/web/firestore/useUpdateDocMutation.ts +45 -41
  119. package/web/firestore/utils/buildCompositeFilter.d.ts +47 -0
  120. package/web/firestore/utils/buildCompositeFilter.js +42 -0
  121. package/web/firestore/utils/buildCompositeFilter.ts +73 -0
  122. package/web/firestore/utils/buildQueryConstraint.d.ts +23 -0
  123. package/web/firestore/utils/buildQueryConstraint.js +41 -0
  124. package/web/firestore/utils/buildQueryConstraint.ts +55 -0
  125. package/web/firestore/utils/getDocData.d.ts +13 -11
  126. package/web/firestore/utils/getDocData.js +6 -9
  127. package/web/firestore/utils/getDocData.ts +14 -20
  128. package/web/firestore/utils/getDocRef.d.ts +14 -11
  129. package/web/firestore/utils/getDocRef.js +7 -8
  130. package/web/firestore/utils/getDocRef.ts +16 -19
  131. package/web/firestore/utils/getDocSnap.d.ts +11 -13
  132. package/web/firestore/utils/getDocSnap.js +5 -10
  133. package/web/firestore/utils/getDocSnap.ts +14 -21
@@ -1,49 +1,6 @@
1
- import firebase, { and, or, where } from "@react-native-firebase/firestore";
1
+ import { and, or } from "@react-native-firebase/firestore";
2
2
  import { useMemo } from "react";
3
- /**
4
- * Constructs a composite or where query filter based on the provided query structure.
5
- * It recursively builds query constraints using logical "or" or "and" operators.
6
- *
7
- * @group Utility
8
- *
9
- * @param {QueryElement<AppModelType>} query
10
- *
11
- * @returns {QueryFilterConstraint | null}
12
- *
13
- * @example
14
- * ```jsx
15
- * export const MyComponent = () => {
16
- * const filter = buildCompositeFilter({
17
- * operator: "AND",
18
- * children: [
19
- * {
20
- * field: "field",
21
- * value: "value",
22
- * op: "=="
23
- * },
24
- * ...(query ? [query] : [])
25
- * ]
26
- * });
27
- * console.log(filter);
28
- * };
29
- * ```
30
- */
31
- export const buildCompositeFilter = (query) => {
32
- if (query.children) {
33
- const queryConstraints = query.children.map(buildCompositeFilter).filter((constraint) => !!constraint);
34
- if (queryConstraints.length <= 0) {
35
- return null;
36
- }
37
- if (queryConstraints.length <= 1) {
38
- return queryConstraints[0];
39
- }
40
- return query.operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
41
- }
42
- if (query.field && query.op) {
43
- return where(query.field === "documentId" ? firebase.FieldPath.documentId() : query.field, query.op, query.value);
44
- }
45
- return null;
46
- };
3
+ import { buildCompositeFilter } from "./utils/buildCompositeFilter";
47
4
  /**
48
5
  * A custom hook that constructs a composite or where query filter based on the provided query structure.
49
6
  * It recursively builds query constraints using logical "or" or "and" operators.
@@ -1,23 +1,7 @@
1
- import firebase, { FirebaseFirestoreTypes, and, or, where } from "@react-native-firebase/firestore";
1
+ import { and, or } from "@react-native-firebase/firestore";
2
2
  import { useMemo } from "react";
3
3
  import { AppModel } from "../../types";
4
-
5
- export type QueryFilterConstraint =
6
- | FirebaseFirestoreTypes.QueryCompositeFilterConstraint
7
- | FirebaseFirestoreTypes.QueryFilterConstraint;
8
-
9
- export type QueryElement<AppModelType extends AppModel = AppModel> = {
10
- operator?: "OR" | "AND";
11
- children?: QueryElement[];
12
- field?: keyof (AppModelType & { documentId?: string[] });
13
- value?: AppModelType[keyof AppModelType];
14
- op?: FirebaseFirestoreTypes.WhereFilterOp;
15
- };
16
-
17
- export type CompositeFilter<AppModelType extends AppModel = AppModel> = {
18
- operator: "OR" | "AND";
19
- children: QueryElement<AppModelType & { documentId?: string[] }>[];
20
- };
4
+ import { buildCompositeFilter, CompositeFilter, QueryFilterConstraint } from "./utils/buildCompositeFilter";
21
5
 
22
6
  /**
23
7
  * @inline
@@ -26,62 +10,6 @@ export type UseCompositeFilter<AppModelType extends AppModel = AppModel> = {
26
10
  query?: CompositeFilter<AppModelType>;
27
11
  };
28
12
 
29
- /**
30
- * Constructs a composite or where query filter based on the provided query structure.
31
- * It recursively builds query constraints using logical "or" or "and" operators.
32
- *
33
- * @group Utility
34
- *
35
- * @param {QueryElement<AppModelType>} query
36
- *
37
- * @returns {QueryFilterConstraint | null}
38
- *
39
- * @example
40
- * ```jsx
41
- * export const MyComponent = () => {
42
- * const filter = buildCompositeFilter({
43
- * operator: "AND",
44
- * children: [
45
- * {
46
- * field: "field",
47
- * value: "value",
48
- * op: "=="
49
- * },
50
- * ...(query ? [query] : [])
51
- * ]
52
- * });
53
- * console.log(filter);
54
- * };
55
- * ```
56
- */
57
- export const buildCompositeFilter = <AppModelType extends AppModel = AppModel>(
58
- query: QueryElement<AppModelType>
59
- ): QueryFilterConstraint | null => {
60
- if (query.children) {
61
- const queryConstraints = query.children.map(buildCompositeFilter).filter((constraint) => !!constraint);
62
-
63
- if (queryConstraints.length <= 0) {
64
- return null;
65
- }
66
-
67
- if (queryConstraints.length <= 1) {
68
- return queryConstraints[0];
69
- }
70
-
71
- return (query as CompositeFilter).operator === "OR" ? or(...queryConstraints) : and(...queryConstraints);
72
- }
73
-
74
- if (query.field && query.op) {
75
- return where(
76
- query.field === "documentId" ? firebase.FieldPath.documentId() : (query.field as string),
77
- query.op,
78
- query.value
79
- ) as unknown as FirebaseFirestoreTypes.QueryFilterConstraint;
80
- }
81
-
82
- return null;
83
- };
84
-
85
13
  /**
86
14
  * A custom hook that constructs a composite or where query filter based on the provided query structure.
87
15
  * It recursively builds query constraints using logical "or" or "and" operators.
@@ -1,6 +1,6 @@
1
1
  import { FirebaseFirestoreTypes, QueryConstraint, QueryNonFilterConstraint } from "@react-native-firebase/firestore";
2
2
  import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
3
- import { QueryFilterConstraint } from "./useCompositeFilter";
3
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
4
4
  import { AppModel } from "../../types";
5
5
  /**
6
6
  * @inline
@@ -11,7 +11,7 @@ import {
11
11
  useQuery as useReactQuery,
12
12
  UseQueryOptions as UseReactQueryOptions
13
13
  } from "@tanstack/react-query";
14
- import { QueryFilterConstraint } from "./useCompositeFilter";
14
+ import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
15
15
  import { AppModel } from "../../types";
16
16
 
17
17
  /**
@@ -1,18 +1,47 @@
1
1
  import { UseMutationOptions } from "@tanstack/react-query";
2
2
  import { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
3
- import { ReactNativeFirebase } from "@react-native-firebase/app";
4
- export type UseDeleteDocMutationValues<AppModelType> = {
3
+ import { AppModel } from "../../types";
4
+ /**
5
+ * @inline
6
+ */
7
+ export type UseDeleteDocMutationValues<AppModelType extends AppModel = AppModel> = {
5
8
  data: WithFieldValue<AppModelType>;
6
9
  };
7
- export type UseDeleteDocMutationOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown> = {
10
+ /**
11
+ * @inline
12
+ */
13
+ export type UseDeleteDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
14
+ /**
15
+ * A reference to a firestore document
16
+ */
8
17
  reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
9
- options?: Omit<UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>, "mutationFn" | "mutationKey">;
18
+ /**
19
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
20
+ */
21
+ options?: Omit<UseMutationOptions<void, Error, void, TContext>, "mutationFn">;
10
22
  };
11
23
  /**
12
- * A custom hook that provides a mutation function to delete a document from the database.
13
- * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
14
- * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
15
- * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
16
- * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
24
+ * Executes a query with specified constraints and returns the count of matched documents.
25
+ *
26
+ * @group Hook
27
+ *
28
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
29
+ *
30
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
31
+ *
32
+ * @example
33
+ * ```jsx
34
+ * export const MyComponent = () => {
35
+ * const {mutate} = useDeleteDocMutation({
36
+ * options: {
37
+ * },
38
+ * reference: document(),
39
+ * });
40
+ *
41
+ * // ....
42
+ * mutate();
43
+ * // ....
44
+ * };
45
+ * ```
17
46
  */
18
- export declare const useDeleteDocMutation: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseDeleteDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>;
47
+ export declare const useDeleteDocMutation: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData, TContext = unknown>({ reference, options }: UseDeleteDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<void, Error, void, TContext>;
@@ -2,11 +2,28 @@ import { useMutation } from "@tanstack/react-query";
2
2
  import { deleteDoc } from "@react-native-firebase/firestore";
3
3
  import { useMemo } from "react";
4
4
  /**
5
- * A custom hook that provides a mutation function to delete a document from the database.
6
- * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
7
- * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
8
- * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
9
- * @returns {UseMutationResult} An object returned by the `useMutation` hook which includes properties and methods to control the mutation's execution and track its state.
5
+ * Executes a query with specified constraints and returns the count of matched documents.
6
+ *
7
+ * @group Hook
8
+ *
9
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
10
+ *
11
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const {mutate} = useDeleteDocMutation({
17
+ * options: {
18
+ * },
19
+ * reference: document(),
20
+ * });
21
+ *
22
+ * // ....
23
+ * mutate();
24
+ * // ....
25
+ * };
26
+ * ```
10
27
  */
11
28
  export const useDeleteDocMutation = ({ reference, options = {} }) => {
12
29
  const mutationKey = useMemo(() => [reference?.path], [reference?.path]);
@@ -1,30 +1,53 @@
1
1
  import { useMutation, UseMutationOptions } from "@tanstack/react-query";
2
2
  import { FirebaseFirestoreTypes, deleteDoc, WithFieldValue } from "@react-native-firebase/firestore";
3
3
 
4
- import { ReactNativeFirebase } from "@react-native-firebase/app";
5
4
  import { useMemo } from "react";
5
+ import { AppModel } from "../../types";
6
6
 
7
- export type UseDeleteDocMutationValues<AppModelType> = {
7
+ /**
8
+ * @inline
9
+ */
10
+ export type UseDeleteDocMutationValues<AppModelType extends AppModel = AppModel> = {
8
11
  data: WithFieldValue<AppModelType>;
9
12
  };
10
13
 
11
- export type UseDeleteDocMutationOptions<
12
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
13
- TContext = unknown
14
- > = {
14
+ /**
15
+ * @inline
16
+ */
17
+ export type UseDeleteDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
18
+ /**
19
+ * A reference to a firestore document
20
+ */
15
21
  reference: FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
16
- options?: Omit<
17
- UseMutationOptions<void, ReactNativeFirebase.NativeFirebaseError, void, TContext>,
18
- "mutationFn" | "mutationKey"
19
- >;
22
+ /**
23
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
24
+ */
25
+ options?: Omit<UseMutationOptions<void, Error, void, TContext>, "mutationFn">;
20
26
  };
21
27
 
22
28
  /**
23
- * A custom hook that provides a mutation function to delete a document from the database.
24
- * @param {UseDeleteDocMutationOptions<AppModelType, DbModelType, TContext>} options - An object containing the reference to the document and additional options for the mutation.
25
- * @param {FirestoreReference<AppModelType, DbModelType>} options.reference - The reference to the document that needs to be deleted.
26
- * @param {object} options.options - Additional options for the mutation, if any (default is an empty object).
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.
29
+ * Executes a query with specified constraints and returns the count of matched documents.
30
+ *
31
+ * @group Hook
32
+ *
33
+ * @param {UseDeleteDocMutationOptions<AppModelType>} options - Configuration options for the query.
34
+ *
35
+ * @returns {UseMutationResult<void, Error, UseDeleteDocMutationValues<AppModelType>, TContext>} A mutation result
36
+ *
37
+ * @example
38
+ * ```jsx
39
+ * export const MyComponent = () => {
40
+ * const {mutate} = useDeleteDocMutation({
41
+ * options: {
42
+ * },
43
+ * reference: document(),
44
+ * });
45
+ *
46
+ * // ....
47
+ * mutate();
48
+ * // ....
49
+ * };
50
+ * ```
28
51
  */
29
52
  export const useDeleteDocMutation = <
30
53
  AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
@@ -1,19 +1,27 @@
1
1
  import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
- export type UseDocReferenceOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
3
- reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
4
- path?: string;
5
- pathSegments?: string[];
6
- };
2
+ import { AppModel } from "../../types";
3
+ import { GetDocRefOptions } from "./utils/getDocRef";
7
4
  /**
8
- * Custom hook to generate and manage a Firestore document reference.
9
- * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
10
- * The reference is updated whenever the associated path, reference, or path segments change.
5
+ * @inline
6
+ */
7
+ export type UseDocReferenceOptions<AppModelType extends AppModel = AppModel> = Omit<GetDocRefOptions<AppModelType>, "db">;
8
+ /**
9
+ * Gets a `CollectionReference` instance.
10
+ *
11
+ * @group Hook
12
+ *
13
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
11
14
  *
12
- * @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
13
- * @param {string} options.path - The path to the Firestore document.
14
- * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
15
- * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
15
+ * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
16
16
  *
17
- * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
17
+ * @example
18
+ * ```jsx
19
+ * export const MyComponent = () => {
20
+ * const ref = useDocReference({
21
+ * reference: collection('todos')
22
+ * path: 'first'
23
+ * });
24
+ * };
25
+ * ```
18
26
  */
19
- export declare const useDocReference: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType>) => FirebaseFirestoreTypes.DocumentReference<AppModelType>;
27
+ export declare const useDocReference: <AppModelType extends AppModel = AppModel>({ path, reference, pathSegments }: UseDocReferenceOptions<AppModelType>) => FirebaseFirestoreTypes.DocumentReference<AppModelType> | null;
@@ -1,36 +1,30 @@
1
- import { doc } from "@react-native-firebase/firestore";
2
- import { useMemo } from "react";
1
+ import { useEffect, useRef } from "react";
3
2
  import { useFirestore } from "./useFirestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
4
  /**
5
- * Generates a document reference for a specified path or reference in Firestore.
6
- * If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
5
+ * Gets a `CollectionReference` instance.
7
6
  *
8
- * @param {Firestore} db - The Firestore database instance used to create the document reference.
9
- * @param {UseDocReferenceOptions<AppModelType>} options - An object containing the path, path segments, and optional reference.
10
- * @param {string} options.path - The path to the document in the Firestore database.
11
- * @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
12
- * @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
7
+ * @group Hook
13
8
  *
14
- * @returns {DocumentReference<AppModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
15
- */
16
- const getDocReference = (db, { path, pathSegments, reference }) => {
17
- return (!reference ? doc(db, path || "", ...(pathSegments || [])) : doc(reference, path, ...(pathSegments || [])));
18
- };
19
- /**
20
- * Custom hook to generate and manage a Firestore document reference.
21
- * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
22
- * The reference is updated whenever the associated path, reference, or path segments change.
9
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
23
10
  *
24
- * @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
25
- * @param {string} options.path - The path to the Firestore document.
26
- * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
27
- * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
11
+ * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
28
12
  *
29
- * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const ref = useDocReference({
17
+ * reference: collection('todos')
18
+ * path: 'first'
19
+ * });
20
+ * };
21
+ * ```
30
22
  */
31
23
  export const useDocReference = ({ path, reference, pathSegments }) => {
32
24
  const db = useFirestore();
33
- return useMemo(() => {
34
- return getDocReference(db, { path, pathSegments, reference });
35
- }, [db, reference, path, pathSegments]);
25
+ const ref = useRef(getDocRef({ db, path, pathSegments, reference }));
26
+ useEffect(() => {
27
+ ref.current = getDocRef({ db, path, pathSegments, reference });
28
+ }, [path, reference, pathSegments, db]);
29
+ return ref.current;
36
30
  };
@@ -1,63 +1,50 @@
1
- import { doc, FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
1
+ import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
2
 
3
- import { useMemo } from "react";
3
+ import { useEffect, useRef } from "react";
4
4
  import { useFirestore } from "./useFirestore";
5
-
6
- export type UseDocReferenceOptions<
7
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
8
- > = {
9
- reference?:
10
- | FirebaseFirestoreTypes.CollectionReference<AppModelType>
11
- | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
12
- path?: string;
13
- pathSegments?: string[];
14
- };
5
+ import { AppModel } from "../../types";
6
+ import { getDocRef, GetDocRefOptions } from "./utils/getDocRef";
15
7
 
16
8
  /**
17
- * Generates a document reference for a specified path or reference in Firestore.
18
- * If a reference is not provided, it constructs a document reference using the Firestore instance, path, and path segments.
19
- *
20
- * @param {Firestore} db - The Firestore database instance used to create the document reference.
21
- * @param {UseDocReferenceOptions<AppModelType>} options - An object containing the path, path segments, and optional reference.
22
- * @param {string} options.path - The path to the document in the Firestore database.
23
- * @param {string[]} [options.pathSegments] - Optional additional segments to join with the path.
24
- * @param {DocumentReference | CollectionReference} [options.reference] - Optional Firestore reference object that influences how the document reference is constructed.
25
- *
26
- * @returns {DocumentReference<AppModelType> | null} A Firestore document reference if the path is specified; otherwise, returns null if path is not provided.
9
+ * @inline
27
10
  */
28
- const getDocReference = <
29
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
30
- >(
31
- db: FirebaseFirestoreTypes.Module,
32
- { path, pathSegments, reference }: UseDocReferenceOptions<AppModelType>
33
- ) => {
34
- return (
35
- !reference ? doc(db, path || "", ...(pathSegments || [])) : doc(reference, path, ...(pathSegments || []))
36
- ) as FirebaseFirestoreTypes.DocumentReference<AppModelType>;
37
- };
11
+ export type UseDocReferenceOptions<AppModelType extends AppModel = AppModel> = Omit<
12
+ GetDocRefOptions<AppModelType>,
13
+ "db"
14
+ >;
38
15
 
39
16
  /**
40
- * Custom hook to generate and manage a Firestore document reference.
41
- * This hook facilitates the retrieval of a Firestore document reference based on given options such as path and reference data.
42
- * The reference is updated whenever the associated path, reference, or path segments change.
17
+ * Gets a `CollectionReference` instance.
18
+ *
19
+ * @group Hook
43
20
  *
44
- * @param {UseDocReferenceOptions<AppModelType>} options - Configuration options for setting up the document reference.
45
- * @param {string} options.path - The path to the Firestore document.
46
- * @param {DbModelType} options.reference - Reference data for the document, providing additional context or specifics.
47
- * @param {string[]} options.pathSegments - Parts of the path to construct the full document path dynamically.
21
+ * @param {UseDocReferenceOptions<AppModelType>} options - Options
48
22
  *
49
- * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType> | null} The Firestore document reference corresponding to the provided path and options, or null if not initialized.
23
+ * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType, AppModelType> | null} A reference to a Firestore document
24
+ *
25
+ * @example
26
+ * ```jsx
27
+ * export const MyComponent = () => {
28
+ * const ref = useDocReference({
29
+ * reference: collection('todos')
30
+ * path: 'first'
31
+ * });
32
+ * };
33
+ * ```
50
34
  */
51
- export const useDocReference = <
52
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
53
- >({
35
+ export const useDocReference = <AppModelType extends AppModel = AppModel>({
54
36
  path,
55
37
  reference,
56
38
  pathSegments
57
- }: UseDocReferenceOptions<AppModelType>): FirebaseFirestoreTypes.DocumentReference<AppModelType> => {
39
+ }: UseDocReferenceOptions<AppModelType>) => {
58
40
  const db = useFirestore();
41
+ const ref = useRef<FirebaseFirestoreTypes.DocumentReference<AppModelType> | null>(
42
+ getDocRef({ db, path, pathSegments, reference })
43
+ );
44
+
45
+ useEffect(() => {
46
+ ref.current = getDocRef({ db, path, pathSegments, reference });
47
+ }, [path, reference, pathSegments, db]);
59
48
 
60
- return useMemo(() => {
61
- return getDocReference(db, { path, pathSegments, reference });
62
- }, [db, reference, path, pathSegments]);
49
+ return ref.current;
63
50
  };
@@ -1,23 +1,28 @@
1
- import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
- export type UseDocReferencesOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
3
- reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
4
- path: string;
5
- pathSegments?: string[];
1
+ import { AppModel } from "../../types";
2
+ import { GetDocRefOptions } from "./utils/getDocRef";
3
+ /**
4
+ * @inline
5
+ */
6
+ export type UseDocReferencesOptions<AppModelType extends AppModel = AppModel> = {
7
+ references: Omit<GetDocRefOptions<AppModelType>, "db">[];
6
8
  };
7
9
  /**
8
- * A custom hook to generate document references for Firebase Firestore documents using given
9
- * reference options.
10
+ * Gets `CollectionReference` instances
11
+ *
12
+ * @group Hook
13
+ *
14
+ * @param {UseDocReferencesOptions<AppModelType>} options - Options
10
15
  *
11
- * The hook accepts an array of options, where each option may contain a path,
12
- * an existing reference, or path segments, and returns an array of generated
13
- * Firestore document references based on those options. The references can be used
14
- * to interact with the Firestore database documents. It uses memoization to optimize
15
- * reference generation.
16
+ * @returns {Array<FirebaseFirestoreTypes.DocumentReference<AppModelType> | null>} A reference to a Firestore document
16
17
  *
17
- * @param {UseDocReferencesOptions<AppModelType>[]} references - An array of options
18
- * specifying how to generate document references. Each option may include a `path`
19
- * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
20
- * @returns {DocumentReference<AppModelType>[]} An array of Firestore document references
21
- * generated from the options provided.
18
+ * @example
19
+ * ```jsx
20
+ * export const MyComponent = () => {
21
+ * const ref = useDocReference({
22
+ * reference: collection('todos')
23
+ * path: 'first'
24
+ * });
25
+ * };
26
+ * ```
22
27
  */
23
- export declare const useDocReferences: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>(references: UseDocReferencesOptions<AppModelType>[]) => FirebaseFirestoreTypes.DocumentReference<AppModelType>[];
28
+ export declare const useDocReferences: <AppModelType extends AppModel = AppModel>({ references }: UseDocReferencesOptions<AppModelType>) => import("@react-native-firebase/firestore").FirebaseFirestoreTypes.DocumentReference<AppModelType>[];
@@ -1,29 +1,28 @@
1
- import { doc } from "@react-native-firebase/firestore";
2
1
  import { useMemo } from "react";
3
2
  import { useFirestore } from "./useFirestore";
3
+ import { getDocRef } from "./utils/getDocRef";
4
4
  /**
5
- * A custom hook to generate document references for Firebase Firestore documents using given
6
- * reference options.
5
+ * Gets `CollectionReference` instances
7
6
  *
8
- * The hook accepts an array of options, where each option may contain a path,
9
- * an existing reference, or path segments, and returns an array of generated
10
- * Firestore document references based on those options. The references can be used
11
- * to interact with the Firestore database documents. It uses memoization to optimize
12
- * reference generation.
7
+ * @group Hook
13
8
  *
14
- * @param {UseDocReferencesOptions<AppModelType>[]} references - An array of options
15
- * specifying how to generate document references. Each option may include a `path`
16
- * as a string, a `reference` as a Firestore reference, and `pathSegments` as an array of strings.
17
- * @returns {DocumentReference<AppModelType>[]} An array of Firestore document references
18
- * generated from the options provided.
9
+ * @param {UseDocReferencesOptions<AppModelType>} options - Options
10
+ *
11
+ * @returns {Array<FirebaseFirestoreTypes.DocumentReference<AppModelType> | null>} A reference to a Firestore document
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const ref = useDocReference({
17
+ * reference: collection('todos')
18
+ * path: 'first'
19
+ * });
20
+ * };
21
+ * ```
19
22
  */
20
- export const useDocReferences = (references) => {
23
+ export const useDocReferences = ({ references }) => {
21
24
  const db = useFirestore();
22
25
  return useMemo(() => {
23
- return references.map(({ path, reference, pathSegments }) => {
24
- return (!reference
25
- ? doc(db, path || "", ...(pathSegments || []))
26
- : doc(reference, path, ...(pathSegments || [])));
27
- });
26
+ return references.map(({ path, reference, pathSegments }) => getDocRef({ db, reference, path, pathSegments }));
28
27
  }, [references, db]);
29
28
  };