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
@@ -0,0 +1,41 @@
1
+ import { orderBy, startAt, startAfter, endAt, endBefore, limit, limitToLast } from "@react-native-firebase/firestore";
2
+ /**
3
+ * A generic mothod to build query constraints for firebase
4
+ *
5
+ * @group Utility
6
+ *
7
+ * @param {NonFilterQueryConstraint<AppModelType>} constraint
8
+ *
9
+ * @returns {FirebaseFirestoreTypes.QueryNonFilterConstraint}
10
+ *
11
+ * @example
12
+ * ```jsx
13
+ * const firebaseConfig = {};
14
+ * export const constraint = useMemo(() => {
15
+ * return buildQueryConstraint({
16
+ * type: 'limit',
17
+ * limit: 1
18
+ * });
19
+ * };
20
+ * ```
21
+ */
22
+ export const buildQueryConstraint = (constraint) => {
23
+ switch (constraint.type) {
24
+ case "orderBy":
25
+ return orderBy(constraint.fieldPath, constraint.directionStr);
26
+ case "startAt":
27
+ return startAt(...constraint.arguments);
28
+ case "startAfter":
29
+ return startAfter(...constraint.arguments);
30
+ case "endAt":
31
+ return endAt(constraint.value, constraint.key);
32
+ case "endBefore":
33
+ return endBefore(constraint.value, constraint.key);
34
+ case "limit":
35
+ return limit(constraint.limit);
36
+ case "limitToLast":
37
+ return limitToLast(constraint.limit);
38
+ default:
39
+ throw new Error(`Unsupported constaint ${JSON.stringify(constraint)}`);
40
+ }
41
+ };
@@ -0,0 +1,54 @@
1
+ import {
2
+ type QueryNonFilterConstraint,
3
+ orderBy,
4
+ startAt,
5
+ startAfter,
6
+ endAt,
7
+ endBefore,
8
+ limit,
9
+ limitToLast
10
+ } from "@react-native-firebase/firestore";
11
+ import { AppModel, type NonFilterQueryConstraint } from "../../../types";
12
+
13
+ /**
14
+ * A generic mothod to build query constraints for firebase
15
+ *
16
+ * @group Utility
17
+ *
18
+ * @param {NonFilterQueryConstraint<AppModelType>} constraint
19
+ *
20
+ * @returns {FirebaseFirestoreTypes.QueryNonFilterConstraint}
21
+ *
22
+ * @example
23
+ * ```jsx
24
+ * const firebaseConfig = {};
25
+ * export const constraint = useMemo(() => {
26
+ * return buildQueryConstraint({
27
+ * type: 'limit',
28
+ * limit: 1
29
+ * });
30
+ * };
31
+ * ```
32
+ */
33
+ export const buildQueryConstraint = <AppModelType extends AppModel = AppModel>(
34
+ constraint: NonFilterQueryConstraint<AppModelType>
35
+ ): QueryNonFilterConstraint => {
36
+ switch (constraint.type) {
37
+ case "orderBy":
38
+ return orderBy(constraint.fieldPath as string, constraint.directionStr);
39
+ case "startAt":
40
+ return startAt(...constraint.arguments);
41
+ case "startAfter":
42
+ return startAfter(...constraint.arguments);
43
+ case "endAt":
44
+ return endAt(constraint.value, constraint.key);
45
+ case "endBefore":
46
+ return endBefore(constraint.value, constraint.key);
47
+ case "limit":
48
+ return limit(constraint.limit);
49
+ case "limitToLast":
50
+ return limitToLast(constraint.limit);
51
+ default:
52
+ throw new Error(`Unsupported constaint ${JSON.stringify(constraint)}`);
53
+ }
54
+ };
@@ -1,18 +1,18 @@
1
- import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
1
  import { GetDocSnapOptions } from "./getDocSnap";
3
- export type GetDocDataOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = GetDocSnapOptions<AppModelType>;
2
+ import { AppModel } from "../../../types";
4
3
  /**
5
- * Asynchronously retrieves document data from a specified database and reference.
4
+ * @inline
5
+ */
6
+ export type GetDocDataOptions<AppModelType extends AppModel = AppModel> = GetDocSnapOptions<AppModelType>;
7
+ /**
8
+ * Asynchronously retrieves document data from a specified database reference.
6
9
  * Utilizes the helper function `getDocSnap` to fetch the document snapshot and
7
10
  * checks if the document exists before returning its data.
8
11
  *
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.
12
+ * @group Utility
13
+ *
14
+ * @param {GetDocDataOptions<AppModelType>} options - The options for fetching document data.
14
15
  *
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.
16
+ * @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists, or null if the document does not exist.
17
17
  */
18
- export declare const getDocData: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocDataOptions<AppModelType>) => Promise<AppModelType | null | undefined>;
18
+ export declare const getDocData: <AppModelType extends AppModel = AppModel>({ db, reference, path, pathSegments }: GetDocDataOptions<AppModelType>) => Promise<AppModelType | null>;
@@ -1,17 +1,14 @@
1
1
  import { getDocSnap } from "./getDocSnap";
2
2
  /**
3
- * Asynchronously retrieves document data from a specified database and reference.
3
+ * Asynchronously retrieves document data from a specified database reference.
4
4
  * Utilizes the helper function `getDocSnap` to fetch the document snapshot and
5
5
  * checks if the document exists before returning its data.
6
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.
7
+ * @group Utility
12
8
  *
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.
9
+ * @param {GetDocDataOptions<AppModelType>} options - The options for fetching document data.
10
+ *
11
+ * @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists, or null if the document does not exist.
15
12
  */
16
13
  export const getDocData = async ({ db, reference, path, pathSegments }) => {
17
14
  const docSnap = await getDocSnap({ db, reference, path, pathSegments });
@@ -1,27 +1,23 @@
1
- import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
1
  import { getDocSnap, GetDocSnapOptions } from "./getDocSnap";
2
+ import { AppModel } from "../../../types";
3
3
 
4
- export type GetDocDataOptions<
5
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
6
- > = GetDocSnapOptions<AppModelType>;
4
+ /**
5
+ * @inline
6
+ */
7
+ export type GetDocDataOptions<AppModelType extends AppModel = AppModel> = GetDocSnapOptions<AppModelType>;
7
8
 
8
9
  /**
9
- * Asynchronously retrieves document data from a specified database and reference.
10
+ * Asynchronously retrieves document data from a specified database reference.
10
11
  * Utilizes the helper function `getDocSnap` to fetch the document snapshot and
11
12
  * checks if the document exists before returning its data.
12
13
  *
13
- * @param {Object} options - The options for fetching document data.
14
- * @param {FirebaseFirestore} options.db - The Firestore database instance.
15
- * @param {DocumentReference=} options.reference - The document reference. This is an optional parameter.
16
- * @param {string=} options.path - The path to the document in the database. This is an optional parameter.
17
- * @param {Array<string>=} options.pathSegments - The path segments for the document's path. This is an optional parameter.
14
+ * @group Utility
15
+ *
16
+ * @param {GetDocDataOptions<AppModelType>} options - The options for fetching document data.
18
17
  *
19
- * @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists,
20
- * or null if the document does not exist.
18
+ * @returns {Promise<AppModelType | null>} Returns a promise that resolves to the document data if it exists, or null if the document does not exist.
21
19
  */
22
- export const getDocData = async <
23
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
24
- >({
20
+ export const getDocData = async <AppModelType extends AppModel = AppModel>({
25
21
  db,
26
22
  reference,
27
23
  path,
@@ -30,7 +26,7 @@ export const getDocData = async <
30
26
  const docSnap = await getDocSnap<AppModelType>({ db, reference, path, pathSegments });
31
27
 
32
28
  if (docSnap && docSnap?.exists) {
33
- return docSnap.data();
29
+ return docSnap.data() as AppModelType;
34
30
  }
35
31
 
36
32
  return null;
@@ -1,5 +1,9 @@
1
1
  import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
- export type GetDocRefOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = {
2
+ import { AppModel } from "../../../types";
3
+ /**
4
+ * @inline
5
+ */
6
+ export type GetDocRefOptions<AppModelType extends AppModel = AppModel> = {
3
7
  db: FirebaseFirestoreTypes.Module;
4
8
  reference?: FirebaseFirestoreTypes.CollectionReference<AppModelType> | FirebaseFirestoreTypes.DocumentReference<AppModelType>;
5
9
  path?: string;
@@ -7,13 +11,12 @@ export type GetDocRefOptions<AppModelType extends FirebaseFirestoreTypes.Documen
7
11
  };
8
12
  /**
9
13
  * Retrieves a document reference based on provided database options.
10
- * Either `reference` or `path` must be provided, not both.
14
+ * Either `reference` or `path`.
15
+ *
16
+ * @group Utility
17
+ *
18
+ * @param {GetDocRefOptions<AppModelType>} options - The options for building doc reference.
11
19
  *
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.
20
+ * @returns {DocumentReference<AppModelType, AppModelType>} Returns a document reference
18
21
  */
19
- export declare const getDocRef: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocRefOptions<AppModelType>) => Promise<FirebaseFirestoreTypes.DocumentReference<AppModelType>>;
22
+ export declare const getDocRef: <AppModelType extends AppModel = AppModel>({ db, reference, path, pathSegments }: GetDocRefOptions<AppModelType>) => FirebaseFirestoreTypes.DocumentReference<AppModelType>;
@@ -1,16 +1,15 @@
1
1
  import { doc } from "@react-native-firebase/firestore";
2
2
  /**
3
3
  * Retrieves a document reference based on provided database options.
4
- * Either `reference` or `path` must be provided, not both.
4
+ * Either `reference` or `path`.
5
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.
6
+ * @group Utility
7
+ *
8
+ * @param {GetDocRefOptions<AppModelType>} options - The options for building doc reference.
9
+ *
10
+ * @returns {DocumentReference<AppModelType, AppModelType>} Returns a document reference
12
11
  */
13
- export const getDocRef = async ({ db, reference, path, pathSegments }) => {
12
+ export const getDocRef = ({ db, reference, path, pathSegments }) => {
14
13
  if (!reference && !path) {
15
14
  throw new Error("One of the options must be provided: path or reference.");
16
15
  }
@@ -1,8 +1,10 @@
1
1
  import { doc, FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
+ import { AppModel } from "../../../types";
2
3
 
3
- export type GetDocRefOptions<
4
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
5
- > = {
4
+ /**
5
+ * @inline
6
+ */
7
+ export type GetDocRefOptions<AppModelType extends AppModel = AppModel> = {
6
8
  db: FirebaseFirestoreTypes.Module;
7
9
  reference?:
8
10
  | FirebaseFirestoreTypes.CollectionReference<AppModelType>
@@ -13,18 +15,15 @@ export type GetDocRefOptions<
13
15
 
14
16
  /**
15
17
  * Retrieves a document reference based on provided database options.
16
- * Either `reference` or `path` must be provided, not both.
18
+ * Either `reference` or `path`.
19
+ *
20
+ * @group Utility
21
+ *
22
+ * @param {GetDocRefOptions<AppModelType>} options - The options for building doc reference.
17
23
  *
18
- * @param {Object} options - The options for getting a document reference.
19
- * @param {FirebaseFirestoreTypes.Module} options.db - The Firestore database instance.
20
- * @param {FirebaseFirestoreTypes.DocumentReference | FirebaseFirestoreTypes.CollectionReference | null} [options.reference] - Reference to a document or collection.
21
- * @param {string | null} [options.path] - Path to the document.
22
- * @param {string[] | null} [options.pathSegments] - Additional path segments if any.
23
- * @returns {FirebaseFirestoreTypes.DocumentReference<AppModelType>} The document reference.
24
+ * @returns {DocumentReference<AppModelType, AppModelType>} Returns a document reference
24
25
  */
25
- export const getDocRef = async <
26
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
27
- >({
26
+ export const getDocRef = <AppModelType extends AppModel = AppModel>({
28
27
  db,
29
28
  reference,
30
29
  path,
@@ -1,20 +1,18 @@
1
- import { FirebaseFirestoreTypes } from "@react-native-firebase/firestore";
2
1
  import { GetDocRefOptions } from "./getDocRef";
3
- export type GetDocSnapOptions<AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData> = GetDocRefOptions<AppModelType>;
2
+ import { AppModel } from "../../../types";
3
+ /**
4
+ * @inline
5
+ */
6
+ export type GetDocSnapOptions<AppModelType extends AppModel = AppModel> = GetDocRefOptions<AppModelType>;
4
7
  /**
5
8
  * Asynchronously retrieves a document snapshot from Firestore using a specified document reference
6
9
  * or path information. This function handles getting the appropriate document reference based on
7
10
  * the parameters provided, and then fetches the document data from Firestore.
8
11
  *
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.
12
+ * @group Utility
13
+ *
14
+ * @param {GetDocSnapOptions<AppModelType>} options - The options for retrieving the document snapshot
15
+ *
16
+ * @returns {Promise<DocumentSnapshot<AppModelType, AppModelType> | null>} Returns a document reference
19
17
  */
20
- export declare const getDocSnap: <AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData>({ db, reference, path, pathSegments }: GetDocSnapOptions<AppModelType>) => Promise<FirebaseFirestoreTypes.DocumentSnapshot<AppModelType> | null>;
18
+ export declare const getDocSnap: <AppModelType extends AppModel = AppModel>({ db, reference, path, pathSegments }: GetDocSnapOptions<AppModelType>) => Promise<import("@react-native-firebase/firestore").FirebaseFirestoreTypes.DocumentSnapshot<AppModelType> | null>;
@@ -5,16 +5,11 @@ import { getDocRef } from "./getDocRef";
5
5
  * or path information. This function handles getting the appropriate document reference based on
6
6
  * the parameters provided, and then fetches the document data from Firestore.
7
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.
8
+ * @group Utility
9
+ *
10
+ * @param {GetDocSnapOptions<AppModelType>} options - The options for retrieving the document snapshot
11
+ *
12
+ * @returns {Promise<DocumentSnapshot<AppModelType, AppModelType> | null>} Returns a document reference
18
13
  */
19
14
  export const getDocSnap = async ({ db, reference, path, pathSegments }) => {
20
15
  const docRef = await getDocRef({ db, reference, path, pathSegments });
@@ -1,29 +1,24 @@
1
- import { FirebaseFirestoreTypes, getDoc as firestoreGetDoc } from "@react-native-firebase/firestore";
1
+ import { getDoc as firestoreGetDoc } from "@react-native-firebase/firestore";
2
2
  import { getDocRef, GetDocRefOptions } from "./getDocRef";
3
+ import { AppModel } from "../../../types";
3
4
 
4
- export type GetDocSnapOptions<
5
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
6
- > = GetDocRefOptions<AppModelType>;
5
+ /**
6
+ * @inline
7
+ */
8
+ export type GetDocSnapOptions<AppModelType extends AppModel = AppModel> = GetDocRefOptions<AppModelType>;
7
9
 
8
10
  /**
9
11
  * Asynchronously retrieves a document snapshot from Firestore using a specified document reference
10
12
  * or path information. This function handles getting the appropriate document reference based on
11
13
  * the parameters provided, and then fetches the document data from Firestore.
12
14
  *
13
- * @param {GetDocSnapOptions<AppModelType, DbModelType>} options - An object containing options
14
- * for retrieving the document
15
- * snapshot.
16
- * @param {DocumentData} options.db - The Firestore database instance to perform the operation on.
17
- * @param {string} options.reference - A string representing a direct Firestore document reference.
18
- * @param {string} options.path - A Firestore path string leading to the document.
19
- * @param {string[]} options.pathSegments - An array of path segments to build the Firestore path.
20
- * @returns {Promise<FirebaseFirestoreTypes.DocumentData | null>} A promise that resolves to the document data if the
21
- * document exists, or `null` if the document does not exist
22
- * or if the reference could not be obtained.
15
+ * @group Utility
16
+ *
17
+ * @param {GetDocSnapOptions<AppModelType>} options - The options for retrieving the document snapshot
18
+ *
19
+ * @returns {Promise<DocumentSnapshot<AppModelType, AppModelType> | null>} Returns a document reference
23
20
  */
24
- export const getDocSnap = async <
25
- AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData
26
- >({
21
+ export const getDocSnap = async <AppModelType extends AppModel = AppModel>({
27
22
  db,
28
23
  reference,
29
24
  path,
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Generic definition for a data model
3
- * @group Models
4
3
  */
5
4
  export type AppModel = {
5
+ /**
6
+ * Unique identifier equl to documentId() from Firestore
7
+ */
8
+ uid?: string;
6
9
  [key: string]: unknown;
7
10
  };
package/types/AppModel.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  /**
2
2
  * Generic definition for a data model
3
- * @group Models
4
3
  */
5
4
  export type AppModel = {
5
+ /**
6
+ * Unique identifier equl to documentId() from Firestore
7
+ */
8
+ uid?: string;
6
9
  [key: string]: unknown;
7
10
  };
@@ -15,3 +15,8 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./utils/getDocData";
19
+ export * from "./utils/getDocSnap";
20
+ export * from "./utils/getDocRef";
21
+ export * from "./utils/buildQueryConstraint";
22
+ export * from "./utils/buildCompositeFilter";
@@ -15,3 +15,8 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./utils/getDocData";
19
+ export * from "./utils/getDocSnap";
20
+ export * from "./utils/getDocRef";
21
+ export * from "./utils/buildQueryConstraint";
22
+ export * from "./utils/buildCompositeFilter";
@@ -15,3 +15,8 @@ export * from "./useSetDocMutation";
15
15
  export * from "./useUpdateDocMutation";
16
16
  export * from "./useGetRealtimeDocData";
17
17
  export * from "./useQueryConstraints";
18
+ export * from "./utils/getDocData";
19
+ export * from "./utils/getDocSnap";
20
+ export * from "./utils/getDocRef";
21
+ export * from "./utils/buildQueryConstraint";
22
+ export * from "./utils/buildCompositeFilter";
@@ -1,23 +1,51 @@
1
1
  import { UseMutationOptions } from "@tanstack/react-query";
2
- import { DocumentData, WithFieldValue, CollectionReference, FirestoreDataConverter } from "firebase/firestore";
2
+ import { WithFieldValue, CollectionReference } from "firebase/firestore";
3
3
  import { FirebaseError } from "firebase/app";
4
+ import { AppModel } from "../../types";
5
+ /**
6
+ * @inline
7
+ */
4
8
  export type UseAddDocMutationValues<AppModelType> = {
9
+ /**
10
+ * Data to write
11
+ */
5
12
  data: WithFieldValue<AppModelType>;
6
13
  };
7
- export type UseAddDocMutationOptions<AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown> = {
8
- reference: CollectionReference<AppModelType, DbModelType>;
9
- converter?: FirestoreDataConverter<AppModelType, DbModelType>;
10
- options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseAddDocMutationValues<AppModelType>, TContext>, "mutationFn" | "mutationKey">;
14
+ /**
15
+ * @inline
16
+ */
17
+ export type UseAddDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
18
+ /**
19
+ * Reference to a collection where document must be added
20
+ */
21
+ collectionReference: CollectionReference<AppModelType, AppModelType>;
22
+ /**
23
+ * Options for useMutation hook excluding mutationFn. MutationKey will be equal to reference.path by default.
24
+ */
25
+ options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseAddDocMutationValues<AppModelType>, TContext>, "mutationFn">;
11
26
  };
12
27
  /**
13
- * Provides a mutation hook to add a document to a Firestore collection utilizing React Query's `useMutation`.
14
- * It handles addition and optional conversion of the document data in Firestore.
28
+ * Executes a mutation and returns added document
29
+ *
30
+ * @group Hook
31
+ *
32
+ * @param {UseAddDocMutationOptions<AppModelType>} options - Configuration options for the mutation.
33
+ *
34
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
15
35
  *
16
- * @param {Object} options - Options for the mutation hook
17
- * @param {FirebaseFirestore.CollectionReference<AppModelType>} options.reference - Firestore collection reference where the document should be added.
18
- * @param {FirebaseFirestore.FirestoreDataConverter<DbModelType>} [options.converter] - Optional data converter for reading and writing Firestore documents.
19
- * @param {UseMutationOptions<AppModelType, Error, { data: DbModelType }, TContext>} [options.options={}] - Optional configuration for the mutation.
36
+ * @example
37
+ * ```jsx
38
+ * export const MyComponent = () => {
39
+ * const {mutate} = useAddDocMutation({
40
+ * options: {
41
+ * },
42
+ * reference: collection(),
43
+ * });
20
44
  *
21
- * @returns {UseMutationResult<AppModelType, Error, { data: DbModelType }, TContext>} The mutation hook result containing status, error, and data of the mutation process.
45
+ * // ....
46
+ * mutate({data: {test: 'value'}});
47
+ * // ....
48
+ * };
49
+ * ```
22
50
  */
23
- export declare const useAddDocMutation: <AppModelType extends DocumentData = DocumentData, DbModelType extends DocumentData = DocumentData, TContext = unknown>({ reference, converter, options }: UseAddDocMutationOptions<AppModelType, DbModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, FirebaseError, UseAddDocMutationValues<AppModelType>, TContext>;
51
+ export declare const useAddDocMutation: <AppModelType extends AppModel = AppModel, TContext = unknown>({ collectionReference, options }: UseAddDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType, FirebaseError, UseAddDocMutationValues<AppModelType>, TContext>;
@@ -2,25 +2,38 @@ import { useMutation } from "@tanstack/react-query";
2
2
  import { addDoc, getDoc } from "firebase/firestore";
3
3
  import { useMemo } from "react";
4
4
  /**
5
- * Provides a mutation hook to add a document to a Firestore collection utilizing React Query's `useMutation`.
6
- * It handles addition and optional conversion of the document data in Firestore.
5
+ * Executes a mutation and returns added document
7
6
  *
8
- * @param {Object} options - Options for the mutation hook
9
- * @param {FirebaseFirestore.CollectionReference<AppModelType>} options.reference - Firestore collection reference where the document should be added.
10
- * @param {FirebaseFirestore.FirestoreDataConverter<DbModelType>} [options.converter] - Optional data converter for reading and writing Firestore documents.
11
- * @param {UseMutationOptions<AppModelType, Error, { data: DbModelType }, TContext>} [options.options={}] - Optional configuration for the mutation.
7
+ * @group Hook
12
8
  *
13
- * @returns {UseMutationResult<AppModelType, Error, { data: DbModelType }, TContext>} The mutation hook result containing status, error, and data of the mutation process.
9
+ * @param {UseAddDocMutationOptions<AppModelType>} options - Configuration options for the mutation.
10
+ *
11
+ * @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
12
+ *
13
+ * @example
14
+ * ```jsx
15
+ * export const MyComponent = () => {
16
+ * const {mutate} = useAddDocMutation({
17
+ * options: {
18
+ * },
19
+ * reference: collection(),
20
+ * });
21
+ *
22
+ * // ....
23
+ * mutate({data: {test: 'value'}});
24
+ * // ....
25
+ * };
26
+ * ```
14
27
  */
15
- export const useAddDocMutation = ({ reference, converter, options = {} }) => {
16
- const mutationKey = useMemo(() => [reference.path], [reference.path]);
28
+ export const useAddDocMutation = ({ collectionReference, options = {} }) => {
29
+ const mutationKey = useMemo(() => [collectionReference.path], [collectionReference.path]);
17
30
  return useMutation({
31
+ mutationKey,
18
32
  ...options,
19
33
  mutationFn: async ({ data }) => {
20
- const docRef = await addDoc(reference, data);
21
- const docSnap = await getDoc(converter ? docRef.withConverter(converter) : docRef);
22
- return docSnap.data();
23
- },
24
- mutationKey
34
+ const docRef = await addDoc(collectionReference, data);
35
+ const docSnap = await getDoc(docRef);
36
+ return { ...docSnap.data(), uid: docSnap.id };
37
+ }
25
38
  });
26
39
  };