react-query-firebase 2.6.1 → 2.6.3
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 +15 -15
- package/react-native/auth/useIdToken.ts +1 -1
- package/react-native/firestore/useAddDocMutation.d.ts +3 -2
- package/react-native/firestore/useAddDocMutation.js +1 -1
- package/react-native/firestore/useAddDocMutation.ts +4 -9
- package/react-native/firestore/useEnsureDoc.js +1 -1
- package/react-native/firestore/useEnsureDoc.ts +1 -1
- package/react-native/firestore/useGetRealtimeDocData.js +2 -1
- package/react-native/firestore/useGetRealtimeDocData.ts +2 -1
- package/react-native/firestore/useInfiniteQuery.js +1 -1
- package/react-native/firestore/useInfiniteQuery.ts +1 -1
- package/react-native/firestore/useQuery.js +1 -1
- package/react-native/firestore/useQuery.ts +1 -1
- package/react-native/firestore/utils/getDocData.js +2 -2
- package/react-native/firestore/utils/getDocData.ts +2 -2
- package/web/firestore/useEnsureDoc.js +21 -9
- package/web/firestore/useEnsureDoc.ts +27 -14
package/package.json
CHANGED
|
@@ -5,13 +5,13 @@
|
|
|
5
5
|
"url": "https://github.com/vpishuk/react-query-firebase/issues"
|
|
6
6
|
},
|
|
7
7
|
"peerDependencies": {
|
|
8
|
-
"@react-native-firebase/analytics": "^
|
|
9
|
-
"@react-native-firebase/app": "^
|
|
10
|
-
"@react-native-firebase/auth": "^
|
|
11
|
-
"@react-native-firebase/crashlytics": "^
|
|
12
|
-
"@react-native-firebase/firestore": "^
|
|
13
|
-
"@react-native-firebase/installations": "^
|
|
14
|
-
"@react-native-firebase/remote-config": "^
|
|
8
|
+
"@react-native-firebase/analytics": "^22.1.0",
|
|
9
|
+
"@react-native-firebase/app": "^22.1.0",
|
|
10
|
+
"@react-native-firebase/auth": "^22.1.0",
|
|
11
|
+
"@react-native-firebase/crashlytics": "^22.1.0",
|
|
12
|
+
"@react-native-firebase/firestore": "^22.1.0",
|
|
13
|
+
"@react-native-firebase/installations": "^22.1.0",
|
|
14
|
+
"@react-native-firebase/remote-config": "^22.1.0",
|
|
15
15
|
"@tanstack/react-query": "^5.0.0",
|
|
16
16
|
"firebase": "^11.0.0",
|
|
17
17
|
"react": "^18.0.0 || ^19.0.0"
|
|
@@ -22,13 +22,13 @@
|
|
|
22
22
|
"description": "This module offers react hooks to work with Firebase on react-native and web platforms",
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@laverve/eslint-utils": "^5.1.1",
|
|
25
|
-
"@react-native-firebase/analytics": "^
|
|
26
|
-
"@react-native-firebase/app": "^
|
|
27
|
-
"@react-native-firebase/auth": "^
|
|
28
|
-
"@react-native-firebase/crashlytics": "^
|
|
29
|
-
"@react-native-firebase/firestore": "^
|
|
30
|
-
"@react-native-firebase/installations": "^
|
|
31
|
-
"@react-native-firebase/remote-config": "^
|
|
25
|
+
"@react-native-firebase/analytics": "^22.1.0",
|
|
26
|
+
"@react-native-firebase/app": "^22.1.0",
|
|
27
|
+
"@react-native-firebase/auth": "^22.1.0",
|
|
28
|
+
"@react-native-firebase/crashlytics": "^22.1.0",
|
|
29
|
+
"@react-native-firebase/firestore": "^22.1.0",
|
|
30
|
+
"@react-native-firebase/installations": "^22.1.0",
|
|
31
|
+
"@react-native-firebase/remote-config": "^22.1.0",
|
|
32
32
|
"@types/react": "^19.0.2",
|
|
33
33
|
"husky": "^9.1.1",
|
|
34
34
|
"lint-staged": "^15.1.0",
|
|
@@ -74,5 +74,5 @@
|
|
|
74
74
|
"docs:build": "vitepress build docs",
|
|
75
75
|
"docs:preview": "vitepress preview docs"
|
|
76
76
|
},
|
|
77
|
-
"version": "2.6.
|
|
77
|
+
"version": "2.6.3"
|
|
78
78
|
}
|
|
@@ -63,7 +63,7 @@ export const useIdToken = (): UseIdTokenResult => {
|
|
|
63
63
|
}, [callback]);
|
|
64
64
|
|
|
65
65
|
useEffect(() => {
|
|
66
|
-
const unsubscribe = onIdTokenChanged(auth, (user: FirebaseAuthTypes.User) => {
|
|
66
|
+
const unsubscribe = onIdTokenChanged(auth, (user: FirebaseAuthTypes.User | null) => {
|
|
67
67
|
if (user) {
|
|
68
68
|
user.getIdToken().then((idToken) => {
|
|
69
69
|
setIdToken(idToken);
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
2
|
import { FirebaseFirestoreTypes, WithFieldValue } from "@react-native-firebase/firestore";
|
|
3
3
|
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
4
|
+
import { AppModel } from "../../types";
|
|
4
5
|
/**
|
|
5
6
|
* @inline
|
|
6
7
|
*/
|
|
@@ -13,7 +14,7 @@ export type UseAddDocMutationValues<AppModelType> = {
|
|
|
13
14
|
/**
|
|
14
15
|
* @inline
|
|
15
16
|
*/
|
|
16
|
-
export type UseAddDocMutationOptions<AppModelType extends
|
|
17
|
+
export type UseAddDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
|
|
17
18
|
/**
|
|
18
19
|
* Reference to a collection where document must be added
|
|
19
20
|
*/
|
|
@@ -47,4 +48,4 @@ export type UseAddDocMutationOptions<AppModelType extends FirebaseFirestoreTypes
|
|
|
47
48
|
* };
|
|
48
49
|
* ```
|
|
49
50
|
*/
|
|
50
|
-
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, ReactNativeFirebase.NativeFirebaseError, UseAddDocMutationValues<AppModelType>, TContext>;
|
|
@@ -33,7 +33,7 @@ export const useAddDocMutation = ({ collectionReference, options = {} }) => {
|
|
|
33
33
|
mutationFn: async ({ data }) => {
|
|
34
34
|
const docRef = await addDoc(collectionReference, data);
|
|
35
35
|
const docSnap = await getDoc(docRef);
|
|
36
|
-
return docSnap.data();
|
|
36
|
+
return { ...docSnap.data(), uid: docRef.id };
|
|
37
37
|
}
|
|
38
38
|
});
|
|
39
39
|
};
|
|
@@ -3,6 +3,7 @@ import { FirebaseFirestoreTypes, addDoc, WithFieldValue, getDoc } from "@react-n
|
|
|
3
3
|
|
|
4
4
|
import { ReactNativeFirebase } from "@react-native-firebase/app";
|
|
5
5
|
import { useMemo } from "react";
|
|
6
|
+
import { AppModel } from "../../types";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* @inline
|
|
@@ -17,10 +18,7 @@ export type UseAddDocMutationValues<AppModelType> = {
|
|
|
17
18
|
/**
|
|
18
19
|
* @inline
|
|
19
20
|
*/
|
|
20
|
-
export type UseAddDocMutationOptions<
|
|
21
|
-
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
22
|
-
TContext = unknown
|
|
23
|
-
> = {
|
|
21
|
+
export type UseAddDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
|
|
24
22
|
/**
|
|
25
23
|
* Reference to a collection where document must be added
|
|
26
24
|
*/
|
|
@@ -64,10 +62,7 @@ export type UseAddDocMutationOptions<
|
|
|
64
62
|
* };
|
|
65
63
|
* ```
|
|
66
64
|
*/
|
|
67
|
-
export const useAddDocMutation = <
|
|
68
|
-
AppModelType extends FirebaseFirestoreTypes.DocumentData = FirebaseFirestoreTypes.DocumentData,
|
|
69
|
-
TContext = unknown
|
|
70
|
-
>({
|
|
65
|
+
export const useAddDocMutation = <AppModelType extends AppModel = AppModel, TContext = unknown>({
|
|
71
66
|
collectionReference,
|
|
72
67
|
options = {}
|
|
73
68
|
}: UseAddDocMutationOptions<AppModelType, TContext>) => {
|
|
@@ -79,7 +74,7 @@ export const useAddDocMutation = <
|
|
|
79
74
|
mutationFn: async ({ data }) => {
|
|
80
75
|
const docRef = await addDoc<AppModelType>(collectionReference, data);
|
|
81
76
|
const docSnap = await getDoc(docRef);
|
|
82
|
-
return docSnap.data() as AppModelType;
|
|
77
|
+
return { ...docSnap.data(), uid: docRef.id } as AppModelType;
|
|
83
78
|
}
|
|
84
79
|
});
|
|
85
80
|
};
|
|
@@ -32,7 +32,7 @@ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options
|
|
|
32
32
|
...options,
|
|
33
33
|
queryFn: async () => {
|
|
34
34
|
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
35
|
-
if (existingDocSnap?.exists) {
|
|
35
|
+
if (existingDocSnap?.exists()) {
|
|
36
36
|
return { ...existingDocSnap.data(), uid: existingDocSnap.id };
|
|
37
37
|
}
|
|
38
38
|
const docRef = getDocRef({ db, reference, path, pathSegments });
|
|
@@ -60,7 +60,7 @@ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
|
|
|
60
60
|
queryFn: async () => {
|
|
61
61
|
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
62
62
|
|
|
63
|
-
if (existingDocSnap?.exists) {
|
|
63
|
+
if (existingDocSnap?.exists()) {
|
|
64
64
|
return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
|
|
65
65
|
}
|
|
66
66
|
|
|
@@ -34,12 +34,13 @@ export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }
|
|
|
34
34
|
? onSnapshot(ref, {
|
|
35
35
|
next: async (snapshot) => {
|
|
36
36
|
setIsFetching(false);
|
|
37
|
-
setDoc(snapshot.data()
|
|
37
|
+
setDoc({ ...snapshot.data(), uid: snapshot.id });
|
|
38
38
|
setError(null);
|
|
39
39
|
setIsError(false);
|
|
40
40
|
},
|
|
41
41
|
error: (e) => {
|
|
42
42
|
setIsError(true);
|
|
43
|
+
setDoc(null);
|
|
43
44
|
setError(e);
|
|
44
45
|
onError?.(e);
|
|
45
46
|
}
|
|
@@ -78,12 +78,13 @@ export const useGetRealtimeDocData = <AppModelType extends AppModel = AppModel>(
|
|
|
78
78
|
? onSnapshot<AppModelType>(ref, {
|
|
79
79
|
next: async (snapshot) => {
|
|
80
80
|
setIsFetching(false);
|
|
81
|
-
setDoc(snapshot.data()
|
|
81
|
+
setDoc({ ...snapshot.data(), uid: snapshot.id } as AppModelType);
|
|
82
82
|
setError(null);
|
|
83
83
|
setIsError(false);
|
|
84
84
|
},
|
|
85
85
|
error: (e) => {
|
|
86
86
|
setIsError(true);
|
|
87
|
+
setDoc(null);
|
|
87
88
|
setError(e);
|
|
88
89
|
onError?.(e);
|
|
89
90
|
}
|
|
@@ -34,7 +34,7 @@ export const useInfiniteQuery = ({ options, collectionReference, queryConstraint
|
|
|
34
34
|
const docs = [];
|
|
35
35
|
if (querySnapshot) {
|
|
36
36
|
querySnapshot.forEach((doc) => {
|
|
37
|
-
docs.push(doc.data());
|
|
37
|
+
docs.push({ ...doc.data(), uid: doc.id });
|
|
38
38
|
});
|
|
39
39
|
}
|
|
40
40
|
return docs;
|
|
@@ -12,8 +12,8 @@ import { getDocSnap } from "./getDocSnap";
|
|
|
12
12
|
*/
|
|
13
13
|
export const getDocData = async ({ db, reference, path, pathSegments }) => {
|
|
14
14
|
const docSnap = await getDocSnap({ db, reference, path, pathSegments });
|
|
15
|
-
if (docSnap && docSnap?.exists) {
|
|
16
|
-
return docSnap.data();
|
|
15
|
+
if (docSnap && docSnap?.exists()) {
|
|
16
|
+
return { ...docSnap.data(), uid: docSnap.id };
|
|
17
17
|
}
|
|
18
18
|
return null;
|
|
19
19
|
};
|
|
@@ -25,8 +25,8 @@ export const getDocData = async <AppModelType extends AppModel = AppModel>({
|
|
|
25
25
|
}: GetDocDataOptions<AppModelType>) => {
|
|
26
26
|
const docSnap = await getDocSnap<AppModelType>({ db, reference, path, pathSegments });
|
|
27
27
|
|
|
28
|
-
if (docSnap && docSnap?.exists) {
|
|
29
|
-
return docSnap.data() as AppModelType;
|
|
28
|
+
if (docSnap && docSnap?.exists()) {
|
|
29
|
+
return { ...docSnap.data(), uid: docSnap.id } as AppModelType;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
return null;
|
|
@@ -31,17 +31,29 @@ export const useEnsureDoc = ({ reference, path, pathSegments, defaults, options
|
|
|
31
31
|
return useQuery({
|
|
32
32
|
...options,
|
|
33
33
|
queryFn: async () => {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
34
|
+
const createDoc = async () => {
|
|
35
|
+
const docRef = getDocRef({ db, reference, path, pathSegments });
|
|
36
|
+
if (!docRef) {
|
|
37
|
+
throw new Error(`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`);
|
|
38
|
+
}
|
|
39
|
+
await setDoc(docRef, defaults);
|
|
40
|
+
const docSnap = await getDoc(docRef);
|
|
41
|
+
return { ...docSnap.data(), uid: docSnap.id };
|
|
42
|
+
};
|
|
43
|
+
try {
|
|
44
|
+
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
45
|
+
if (existingDocSnap?.exists()) {
|
|
46
|
+
return { ...existingDocSnap.data(), uid: existingDocSnap.id };
|
|
47
|
+
}
|
|
48
|
+
return createDoc();
|
|
37
49
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
50
|
+
catch (e) {
|
|
51
|
+
// permission denied error may arise because of a security rule
|
|
52
|
+
if (e.code === "permission-denied") {
|
|
53
|
+
return createDoc();
|
|
54
|
+
}
|
|
55
|
+
throw e;
|
|
41
56
|
}
|
|
42
|
-
await setDoc(docRef, defaults);
|
|
43
|
-
const docSnap = await getDoc(docRef);
|
|
44
|
-
return { ...docSnap.data(), uid: docSnap.id };
|
|
45
57
|
}
|
|
46
58
|
});
|
|
47
59
|
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
|
|
2
|
-
import { getDoc, setDoc } from "firebase/firestore";
|
|
2
|
+
import { FirestoreError, getDoc, setDoc } from "firebase/firestore";
|
|
3
3
|
|
|
4
4
|
import { AppModel } from "../../types";
|
|
5
5
|
import { GetDocDataOptions } from "./utils/getDocData";
|
|
@@ -58,22 +58,35 @@ export const useEnsureDoc = <AppModelType extends AppModel = AppModel>({
|
|
|
58
58
|
return useQuery({
|
|
59
59
|
...options,
|
|
60
60
|
queryFn: async () => {
|
|
61
|
-
const
|
|
61
|
+
const createDoc = async () => {
|
|
62
|
+
const docRef = getDocRef({ db, reference, path, pathSegments });
|
|
63
|
+
if (!docRef) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Cannot fetch document reference using data: ${reference?.path}, ${path}, ${pathSegments?.join("/")}`
|
|
66
|
+
);
|
|
67
|
+
}
|
|
62
68
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
69
|
+
await setDoc<AppModelType, AppModelType>(docRef, defaults);
|
|
70
|
+
const docSnap = await getDoc(docRef);
|
|
71
|
+
return { ...(docSnap.data() as AppModelType), uid: docSnap.id };
|
|
72
|
+
};
|
|
66
73
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
74
|
+
try {
|
|
75
|
+
const existingDocSnap = await getDocSnap({ db, path, pathSegments, reference });
|
|
76
|
+
|
|
77
|
+
if (existingDocSnap?.exists()) {
|
|
78
|
+
return { ...(existingDocSnap.data() as AppModelType), uid: existingDocSnap.id };
|
|
79
|
+
}
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
return createDoc();
|
|
82
|
+
} catch (e) {
|
|
83
|
+
// permission denied error may arise because of a security rule
|
|
84
|
+
if ((e as FirestoreError).code === "permission-denied") {
|
|
85
|
+
return createDoc();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
77
90
|
}
|
|
78
91
|
});
|
|
79
92
|
};
|