react-query-firebase 2.3.3 → 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.
- package/package.json +1 -1
- package/react-native/firestore/index.d.ts +5 -0
- package/react-native/firestore/index.js +5 -0
- package/react-native/firestore/index.ts +5 -0
- package/react-native/firestore/useAddDocMutation.d.ts +38 -9
- package/react-native/firestore/useAddDocMutation.js +25 -11
- package/react-native/firestore/useAddDocMutation.ts +43 -13
- package/react-native/firestore/useBatchWrite.d.ts +31 -7
- package/react-native/firestore/useBatchWrite.js +22 -4
- package/react-native/firestore/useBatchWrite.ts +31 -7
- package/react-native/firestore/useCollectionReference.d.ts +30 -9
- package/react-native/firestore/useCollectionReference.js +15 -7
- package/react-native/firestore/useCollectionReference.ts +34 -25
- package/react-native/firestore/useCompositeFilter.d.ts +1 -46
- package/react-native/firestore/useCompositeFilter.js +2 -45
- package/react-native/firestore/useCompositeFilter.ts +2 -74
- package/react-native/firestore/useCountQuery.d.ts +1 -1
- package/react-native/firestore/useCountQuery.ts +1 -1
- package/react-native/firestore/useDeleteDocMutation.d.ts +39 -10
- package/react-native/firestore/useDeleteDocMutation.js +22 -5
- package/react-native/firestore/useDeleteDocMutation.ts +38 -15
- package/react-native/firestore/useDocReference.d.ts +22 -14
- package/react-native/firestore/useDocReference.js +20 -26
- package/react-native/firestore/useDocReference.ts +34 -47
- package/react-native/firestore/useDocReferences.d.ts +23 -18
- package/react-native/firestore/useDocReferences.js +18 -19
- package/react-native/firestore/useDocReferences.ts +26 -34
- package/react-native/firestore/useFirestore.d.ts +4 -3
- package/react-native/firestore/useFirestore.js +4 -3
- package/react-native/firestore/useFirestore.ts +4 -3
- package/react-native/firestore/useGetDocData.d.ts +33 -14
- package/react-native/firestore/useGetDocData.js +21 -6
- package/react-native/firestore/useGetDocData.ts +34 -22
- package/react-native/firestore/useInfiniteQuery.d.ts +1 -1
- package/react-native/firestore/useInfiniteQuery.ts +1 -1
- package/react-native/firestore/useQuery.d.ts +1 -1
- package/react-native/firestore/useQuery.ts +1 -1
- package/react-native/firestore/useQueryConstraints.d.ts +0 -21
- package/react-native/firestore/useQueryConstraints.js +1 -39
- package/react-native/firestore/useQueryConstraints.ts +2 -51
- package/react-native/firestore/useRunTransaction.d.ts +33 -9
- package/react-native/firestore/useRunTransaction.js +20 -4
- package/react-native/firestore/useRunTransaction.ts +35 -16
- package/react-native/firestore/useSetDocMutation.d.ts +43 -11
- package/react-native/firestore/useSetDocMutation.js +28 -11
- package/react-native/firestore/useSetDocMutation.ts +49 -28
- package/react-native/firestore/useUpdateDocMutation.d.ts +40 -18
- package/react-native/firestore/useUpdateDocMutation.js +18 -13
- package/react-native/firestore/useUpdateDocMutation.ts +40 -31
- package/react-native/firestore/utils/buildCompositeFilter.d.ts +47 -0
- package/react-native/firestore/utils/buildCompositeFilter.js +45 -0
- package/react-native/firestore/utils/buildCompositeFilter.ts +75 -0
- package/react-native/firestore/utils/buildQueryConstraint.d.ts +23 -0
- package/react-native/firestore/utils/buildQueryConstraint.js +41 -0
- package/react-native/firestore/utils/buildQueryConstraint.ts +54 -0
- package/react-native/firestore/utils/getDocData.d.ts +11 -11
- package/react-native/firestore/utils/getDocData.js +5 -8
- package/react-native/firestore/utils/getDocData.ts +12 -16
- package/react-native/firestore/utils/getDocRef.d.ts +12 -9
- package/react-native/firestore/utils/getDocRef.js +7 -8
- package/react-native/firestore/utils/getDocRef.ts +12 -13
- package/react-native/firestore/utils/getDocSnap.d.ts +11 -13
- package/react-native/firestore/utils/getDocSnap.js +5 -10
- package/react-native/firestore/utils/getDocSnap.ts +12 -17
- package/types/AppModel.d.ts +4 -1
- package/types/AppModel.ts +4 -1
- package/web/firestore/index.d.ts +5 -0
- package/web/firestore/index.js +5 -0
- package/web/firestore/index.ts +5 -0
- package/web/firestore/useAddDocMutation.d.ts +41 -13
- package/web/firestore/useAddDocMutation.js +27 -14
- package/web/firestore/useAddDocMutation.ts +49 -37
- package/web/firestore/useBatchWrite.d.ts +34 -7
- package/web/firestore/useBatchWrite.js +22 -4
- package/web/firestore/useBatchWrite.ts +34 -7
- package/web/firestore/useCollectionReference.d.ts +32 -11
- package/web/firestore/useCollectionReference.js +15 -7
- package/web/firestore/useCollectionReference.ts +34 -13
- package/web/firestore/useCompositeFilter.d.ts +2 -47
- package/web/firestore/useCompositeFilter.js +2 -42
- package/web/firestore/useCompositeFilter.ts +2 -72
- package/web/firestore/useCountQuery.d.ts +4 -4
- package/web/firestore/useCountQuery.ts +5 -5
- package/web/firestore/useDeleteDocMutation.d.ts +41 -11
- package/web/firestore/useDeleteDocMutation.js +24 -7
- package/web/firestore/useDeleteDocMutation.ts +45 -23
- package/web/firestore/useDocReference.d.ts +23 -15
- package/web/firestore/useDocReference.js +16 -31
- package/web/firestore/useDocReference.ts +28 -46
- package/web/firestore/useDocReferences.d.ts +23 -18
- package/web/firestore/useDocReferences.js +18 -21
- package/web/firestore/useDocReferences.ts +26 -30
- package/web/firestore/useFirestore.d.ts +4 -3
- package/web/firestore/useFirestore.js +4 -3
- package/web/firestore/useFirestore.ts +4 -3
- package/web/firestore/useGetDocData.d.ts +32 -13
- package/web/firestore/useGetDocData.js +21 -6
- package/web/firestore/useGetDocData.ts +34 -23
- package/web/firestore/useGetRealtimeDocData.js +2 -2
- package/web/firestore/useGetRealtimeDocData.ts +2 -2
- package/web/firestore/useInfiniteQuery.d.ts +1 -1
- package/web/firestore/useInfiniteQuery.js +1 -1
- package/web/firestore/useInfiniteQuery.ts +2 -2
- package/web/firestore/useQuery.d.ts +1 -1
- package/web/firestore/useQuery.js +1 -1
- package/web/firestore/useQuery.ts +2 -2
- package/web/firestore/useQueryConstraints.d.ts +0 -21
- package/web/firestore/useQueryConstraints.js +1 -39
- package/web/firestore/useQueryConstraints.ts +2 -51
- package/web/firestore/useRunTransaction.d.ts +33 -8
- package/web/firestore/useRunTransaction.js +20 -4
- package/web/firestore/useRunTransaction.ts +35 -10
- package/web/firestore/useSetDocMutation.d.ts +45 -12
- package/web/firestore/useSetDocMutation.js +28 -11
- package/web/firestore/useSetDocMutation.ts +51 -26
- package/web/firestore/useUpdateDocMutation.d.ts +42 -20
- package/web/firestore/useUpdateDocMutation.js +20 -15
- package/web/firestore/useUpdateDocMutation.ts +45 -41
- package/web/firestore/utils/buildCompositeFilter.d.ts +47 -0
- package/web/firestore/utils/buildCompositeFilter.js +42 -0
- package/web/firestore/utils/buildCompositeFilter.ts +73 -0
- package/web/firestore/utils/buildQueryConstraint.d.ts +23 -0
- package/web/firestore/utils/buildQueryConstraint.js +41 -0
- package/web/firestore/utils/buildQueryConstraint.ts +55 -0
- package/web/firestore/utils/getDocData.d.ts +13 -11
- package/web/firestore/utils/getDocData.js +6 -9
- package/web/firestore/utils/getDocData.ts +14 -20
- package/web/firestore/utils/getDocRef.d.ts +14 -11
- package/web/firestore/utils/getDocRef.js +7 -8
- package/web/firestore/utils/getDocRef.ts +16 -19
- package/web/firestore/utils/getDocSnap.d.ts +11 -13
- package/web/firestore/utils/getDocSnap.js +5 -10
- 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
|
-
|
|
2
|
+
import { AppModel } from "../../../types";
|
|
4
3
|
/**
|
|
5
|
-
*
|
|
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
|
-
* @
|
|
10
|
-
*
|
|
11
|
-
* @param {
|
|
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
|
|
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
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
14
|
-
*
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
|
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
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
* @param {
|
|
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
|
-
|
|
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
|
|
14
|
+
* Either `reference` or `path`.
|
|
15
|
+
*
|
|
16
|
+
* @group Utility
|
|
17
|
+
*
|
|
18
|
+
* @param {GetDocRefOptions<AppModelType>} options - The options for building doc reference.
|
|
11
19
|
*
|
|
12
|
-
* @
|
|
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
|
|
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
|
|
4
|
+
* Either `reference` or `path`.
|
|
5
5
|
*
|
|
6
|
-
* @
|
|
7
|
-
*
|
|
8
|
-
* @param {
|
|
9
|
-
*
|
|
10
|
-
* @
|
|
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 =
|
|
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
|
-
|
|
4
|
-
|
|
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
|
|
18
|
+
* Either `reference` or `path`.
|
|
19
|
+
*
|
|
20
|
+
* @group Utility
|
|
21
|
+
*
|
|
22
|
+
* @param {GetDocRefOptions<AppModelType>} options - The options for building doc reference.
|
|
17
23
|
*
|
|
18
|
-
* @
|
|
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 =
|
|
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
|
-
|
|
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
|
-
* @
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
* @
|
|
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
|
|
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
|
-
* @
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
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 {
|
|
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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
* @
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
* @
|
|
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,
|
package/types/AppModel.d.ts
CHANGED
package/types/AppModel.ts
CHANGED
package/web/firestore/index.d.ts
CHANGED
|
@@ -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";
|
package/web/firestore/index.js
CHANGED
|
@@ -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";
|
package/web/firestore/index.ts
CHANGED
|
@@ -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 {
|
|
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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
*
|
|
14
|
-
*
|
|
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
|
-
* @
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```jsx
|
|
38
|
+
* export const MyComponent = () => {
|
|
39
|
+
* const {mutate} = useAddDocMutation({
|
|
40
|
+
* options: {
|
|
41
|
+
* },
|
|
42
|
+
* reference: collection(),
|
|
43
|
+
* });
|
|
20
44
|
*
|
|
21
|
-
*
|
|
45
|
+
* // ....
|
|
46
|
+
* mutate({data: {test: 'value'}});
|
|
47
|
+
* // ....
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
22
50
|
*/
|
|
23
|
-
export declare const useAddDocMutation: <AppModelType extends
|
|
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
|
-
*
|
|
6
|
-
* It handles addition and optional conversion of the document data in Firestore.
|
|
5
|
+
* Executes a mutation and returns added document
|
|
7
6
|
*
|
|
8
|
-
* @
|
|
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
|
-
* @
|
|
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 = ({
|
|
16
|
-
const mutationKey = useMemo(() => [
|
|
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(
|
|
21
|
-
const docSnap = await getDoc(
|
|
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
|
};
|