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
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { useContext } from "react";
|
|
2
2
|
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
4
|
+
* Returns an instance of firestore bound to the closest context.
|
|
6
5
|
*
|
|
7
|
-
* @
|
|
6
|
+
* @group Hook
|
|
7
|
+
*
|
|
8
|
+
* @returns {Firestore}
|
|
8
9
|
*/
|
|
9
10
|
export const useFirestore = () => {
|
|
10
11
|
const { firestore } = useContext(FirebaseContext);
|
|
@@ -2,10 +2,11 @@ import { useContext } from "react";
|
|
|
2
2
|
import { FirebaseContext } from "../context/FirebaseContext";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* It extracts the Firestore object from the Firebase context, allowing components to interact with Firestore services.
|
|
5
|
+
* Returns an instance of firestore bound to the closest context.
|
|
7
6
|
*
|
|
8
|
-
* @
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @returns {Firestore}
|
|
9
10
|
*/
|
|
10
11
|
export const useFirestore = () => {
|
|
11
12
|
const { firestore } = useContext(FirebaseContext);
|
|
@@ -1,18 +1,37 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { GetDocDataOptions } from "./utils/getDocData";
|
|
2
2
|
import { UseQueryOptions } from "@tanstack/react-query";
|
|
3
|
-
|
|
3
|
+
import { AppModel } from "../../types";
|
|
4
|
+
/**
|
|
5
|
+
* @inline
|
|
6
|
+
*/
|
|
7
|
+
type UseGetDocOptions<AppModelType extends AppModel = AppModel> = {
|
|
8
|
+
/**
|
|
9
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
10
|
+
*/
|
|
4
11
|
options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> & Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
5
|
-
|
|
6
|
-
pathSegments?: string[];
|
|
7
|
-
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
8
|
-
};
|
|
12
|
+
} & Omit<GetDocDataOptions<AppModelType>, "db">;
|
|
9
13
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
*
|
|
14
|
-
* @param {
|
|
15
|
-
*
|
|
14
|
+
* Executes a query on a Firestore data source and returns the resulting document.
|
|
15
|
+
*
|
|
16
|
+
* @group Hook
|
|
17
|
+
*
|
|
18
|
+
* @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
|
|
19
|
+
*
|
|
20
|
+
* @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```jsx
|
|
24
|
+
* export const MyComponent = () => {
|
|
25
|
+
* const doc = useGetDocData({
|
|
26
|
+
* options: {
|
|
27
|
+
* queryKey: ['key']
|
|
28
|
+
* },
|
|
29
|
+
* reference: collection(),
|
|
30
|
+
* path: 'id'
|
|
31
|
+
* });
|
|
32
|
+
* console.log(doc);
|
|
33
|
+
* };
|
|
34
|
+
* ```
|
|
16
35
|
*/
|
|
17
|
-
export declare const useGetDocData: <AppModelType extends
|
|
36
|
+
export declare const useGetDocData: <AppModelType extends AppModel = AppModel>({ options, reference, path, pathSegments }: UseGetDocOptions<AppModelType>) => import("@tanstack/react-query").UseQueryResult<AppModelType, Error>;
|
|
18
37
|
export {};
|
|
@@ -2,12 +2,27 @@ import { useFirestore } from "./useFirestore";
|
|
|
2
2
|
import { getDocData } from "./utils/getDocData";
|
|
3
3
|
import { useQuery } from "@tanstack/react-query";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
9
|
-
* @param {
|
|
10
|
-
*
|
|
5
|
+
* Executes a query on a Firestore data source and returns the resulting document.
|
|
6
|
+
*
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
|
|
10
|
+
*
|
|
11
|
+
* @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
* export const MyComponent = () => {
|
|
16
|
+
* const doc = useGetDocData({
|
|
17
|
+
* options: {
|
|
18
|
+
* queryKey: ['key']
|
|
19
|
+
* },
|
|
20
|
+
* reference: collection(),
|
|
21
|
+
* path: 'id'
|
|
22
|
+
* });
|
|
23
|
+
* console.log(doc);
|
|
24
|
+
* };
|
|
25
|
+
* ```
|
|
11
26
|
*/
|
|
12
27
|
export const useGetDocData = ({ options, reference, path, pathSegments }) => {
|
|
13
28
|
const db = useFirestore();
|
|
@@ -1,41 +1,52 @@
|
|
|
1
|
-
import { CollectionReference, DocumentData, DocumentReference } from "firebase/firestore";
|
|
2
|
-
|
|
3
1
|
import { useFirestore } from "./useFirestore";
|
|
4
|
-
import { getDocData } from "./utils/getDocData";
|
|
2
|
+
import { getDocData, GetDocDataOptions } from "./utils/getDocData";
|
|
5
3
|
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
|
|
4
|
+
import { AppModel } from "../../types";
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
> = {
|
|
6
|
+
/**
|
|
7
|
+
* @inline
|
|
8
|
+
*/
|
|
9
|
+
type UseGetDocOptions<AppModelType extends AppModel = AppModel> = {
|
|
10
|
+
/**
|
|
11
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
12
|
+
*/
|
|
11
13
|
options: Omit<UseQueryOptions<AppModelType | null, Error, AppModelType>, "queryFn"> &
|
|
12
14
|
Required<Pick<UseQueryOptions<AppModelType, Error, AppModelType>, "queryKey">>;
|
|
13
|
-
|
|
14
|
-
pathSegments?: string[];
|
|
15
|
-
reference: CollectionReference<AppModelType, DbModelType> | DocumentReference<AppModelType, DbModelType>;
|
|
16
|
-
};
|
|
15
|
+
} & Omit<GetDocDataOptions<AppModelType>, "db">;
|
|
17
16
|
|
|
18
17
|
/**
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
* @
|
|
22
|
-
*
|
|
23
|
-
* @param {
|
|
24
|
-
*
|
|
18
|
+
* Executes a query on a Firestore data source and returns the resulting document.
|
|
19
|
+
*
|
|
20
|
+
* @group Hook
|
|
21
|
+
*
|
|
22
|
+
* @param {UseGetDocOptions<AppModelType>} options - Configuration options for the query.
|
|
23
|
+
*
|
|
24
|
+
* @returns {UseQueryResult<AppModelType, Error>} An object containing document's data
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```jsx
|
|
28
|
+
* export const MyComponent = () => {
|
|
29
|
+
* const doc = useGetDocData({
|
|
30
|
+
* options: {
|
|
31
|
+
* queryKey: ['key']
|
|
32
|
+
* },
|
|
33
|
+
* reference: collection(),
|
|
34
|
+
* path: 'id'
|
|
35
|
+
* });
|
|
36
|
+
* console.log(doc);
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
25
39
|
*/
|
|
26
|
-
export const useGetDocData = <
|
|
27
|
-
AppModelType extends DocumentData = DocumentData,
|
|
28
|
-
DbModelType extends DocumentData = DocumentData
|
|
29
|
-
>({
|
|
40
|
+
export const useGetDocData = <AppModelType extends AppModel = AppModel>({
|
|
30
41
|
options,
|
|
31
42
|
reference,
|
|
32
43
|
path,
|
|
33
44
|
pathSegments
|
|
34
|
-
}: UseGetDocOptions<AppModelType
|
|
45
|
+
}: UseGetDocOptions<AppModelType>) => {
|
|
35
46
|
const db = useFirestore();
|
|
36
47
|
|
|
37
48
|
return useQuery({
|
|
38
49
|
...options,
|
|
39
|
-
queryFn: () => getDocData<AppModelType
|
|
50
|
+
queryFn: () => getDocData<AppModelType>({ db, reference, path, pathSegments })
|
|
40
51
|
});
|
|
41
52
|
};
|
|
@@ -48,9 +48,9 @@ export const useGetRealtimeDocData = ({ path, pathSegments, reference, onError }
|
|
|
48
48
|
return () => unsubscribe();
|
|
49
49
|
}, [ref, doc, isError, onError, isFetching, error]);
|
|
50
50
|
return useMemo(() => ({
|
|
51
|
-
data: doc,
|
|
51
|
+
data: { ...(doc || {}), uid: ref?.id ?? "" },
|
|
52
52
|
isError,
|
|
53
53
|
isFetching,
|
|
54
54
|
error
|
|
55
|
-
}), [doc, isError, error, isFetching]);
|
|
55
|
+
}), [doc, isError, error, ref, isFetching]);
|
|
56
56
|
};
|
|
@@ -95,11 +95,11 @@ export const useGetRealtimeDocData = <AppModelType extends AppModel = AppModel>(
|
|
|
95
95
|
|
|
96
96
|
return useMemo(
|
|
97
97
|
() => ({
|
|
98
|
-
data: doc,
|
|
98
|
+
data: { ...(doc || {}), uid: ref?.id ?? "" } as AppModelType,
|
|
99
99
|
isError,
|
|
100
100
|
isFetching,
|
|
101
101
|
error
|
|
102
102
|
}),
|
|
103
|
-
[doc, isError, error, isFetching]
|
|
103
|
+
[doc, isError, error, ref, isFetching]
|
|
104
104
|
);
|
|
105
105
|
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
2
|
import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
|
|
3
|
-
import { QueryFilterConstraint } from "./
|
|
3
|
+
import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
|
|
4
4
|
import { AppModel } from "../../types";
|
|
5
5
|
/**
|
|
6
6
|
* @inline
|
|
@@ -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;
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
UseInfiniteQueryResult,
|
|
15
15
|
InfiniteData
|
|
16
16
|
} from "@tanstack/react-query";
|
|
17
|
-
import { QueryFilterConstraint } from "./
|
|
17
|
+
import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
|
|
18
18
|
import { AppModel } from "../../types";
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -110,7 +110,7 @@ export const useInfiniteQuery = <AppModelType extends AppModel = AppModel, TQuer
|
|
|
110
110
|
|
|
111
111
|
if (querySnapshot) {
|
|
112
112
|
querySnapshot.forEach((doc) => {
|
|
113
|
-
docs.push(doc.data());
|
|
113
|
+
docs.push({ ...doc.data(), uid: doc.id });
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
116
|
return docs;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
2
|
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
-
import { QueryFilterConstraint } from "./
|
|
3
|
+
import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
|
|
4
4
|
import { AppModel } from "../../types";
|
|
5
5
|
/**
|
|
6
6
|
* @inline
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
useQuery as useReactQuery,
|
|
13
13
|
UseQueryOptions as UseReactQueryOptions
|
|
14
14
|
} from "@tanstack/react-query";
|
|
15
|
-
import { QueryFilterConstraint } from "./
|
|
15
|
+
import { QueryFilterConstraint } from "./utils/buildCompositeFilter";
|
|
16
16
|
import { AppModel } from "../../types";
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -85,7 +85,7 @@ export const useQuery = <AppModelType extends AppModel = AppModel>({
|
|
|
85
85
|
|
|
86
86
|
if (querySnapshot) {
|
|
87
87
|
querySnapshot.forEach((doc) => {
|
|
88
|
-
docs.push(doc.data());
|
|
88
|
+
docs.push({ ...doc.data(), uid: doc.id });
|
|
89
89
|
});
|
|
90
90
|
}
|
|
91
91
|
return docs;
|
|
@@ -10,27 +10,6 @@ export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
|
|
|
10
10
|
*/
|
|
11
11
|
constraints: NonFilterQueryConstraint<AppModelType>[];
|
|
12
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 {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 declare const buildQueryConstraint: <AppModelType extends AppModel = AppModel>(constraint: NonFilterQueryConstraint<AppModelType>) => QueryNonFilterConstraint;
|
|
34
13
|
/**
|
|
35
14
|
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
36
15
|
* It uses buildQueryConstraint method and useMemo hook.
|
|
@@ -1,43 +1,5 @@
|
|
|
1
|
-
import { startAt, orderBy, limit, limitToLast, startAfter, endAt, endBefore } from "firebase/firestore";
|
|
2
1
|
import { useMemo } from "react";
|
|
3
|
-
|
|
4
|
-
* A generic mothod to build query constraints for firebase
|
|
5
|
-
*
|
|
6
|
-
* @group Utility
|
|
7
|
-
*
|
|
8
|
-
* @param {NonFilterQueryConstraint<AppModelType>} constraint
|
|
9
|
-
*
|
|
10
|
-
* @returns {QueryNonFilterConstraint}
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
* ```jsx
|
|
14
|
-
* const firebaseConfig = {};
|
|
15
|
-
* export const constraint = useMemo(() => {
|
|
16
|
-
* return buildQueryConstraint({
|
|
17
|
-
* type: 'limit',
|
|
18
|
-
* limit: 1
|
|
19
|
-
* });
|
|
20
|
-
* };
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export const buildQueryConstraint = (constraint) => {
|
|
24
|
-
switch (constraint.type) {
|
|
25
|
-
case "orderBy":
|
|
26
|
-
return orderBy(constraint.fieldPath, constraint.directionStr);
|
|
27
|
-
case "startAt":
|
|
28
|
-
return startAt(...constraint.arguments);
|
|
29
|
-
case "startAfter":
|
|
30
|
-
return startAfter(...constraint.arguments);
|
|
31
|
-
case "endAt":
|
|
32
|
-
return endAt(constraint.value, constraint.key);
|
|
33
|
-
case "endBefore":
|
|
34
|
-
return endBefore(constraint.value, constraint.key);
|
|
35
|
-
case "limit":
|
|
36
|
-
return limit(constraint.limit);
|
|
37
|
-
case "limitToLast":
|
|
38
|
-
return limitToLast(constraint.limit);
|
|
39
|
-
}
|
|
40
|
-
};
|
|
2
|
+
import { buildQueryConstraint } from "./utils/buildQueryConstraint";
|
|
41
3
|
/**
|
|
42
4
|
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
43
5
|
* It uses buildQueryConstraint method and useMemo hook.
|
|
@@ -1,16 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
QueryNonFilterConstraint,
|
|
3
|
-
startAt,
|
|
4
|
-
orderBy,
|
|
5
|
-
limit,
|
|
6
|
-
limitToLast,
|
|
7
|
-
startAfter,
|
|
8
|
-
endAt,
|
|
9
|
-
endBefore
|
|
10
|
-
} from "firebase/firestore";
|
|
1
|
+
import { QueryNonFilterConstraint } from "firebase/firestore";
|
|
11
2
|
import { AppModel } from "../../types/AppModel";
|
|
12
3
|
import { NonFilterQueryConstraint } from "../../types/QueryConstraints";
|
|
13
4
|
import { useMemo } from "react";
|
|
5
|
+
import { buildQueryConstraint } from "./utils/buildQueryConstraint";
|
|
14
6
|
|
|
15
7
|
/**
|
|
16
8
|
* @inline
|
|
@@ -22,47 +14,6 @@ export type UseQueryConstraints<AppModelType extends AppModel = AppModel> = {
|
|
|
22
14
|
constraints: NonFilterQueryConstraint<AppModelType>[];
|
|
23
15
|
};
|
|
24
16
|
|
|
25
|
-
/**
|
|
26
|
-
* A generic mothod to build query constraints for firebase
|
|
27
|
-
*
|
|
28
|
-
* @group Utility
|
|
29
|
-
*
|
|
30
|
-
* @param {NonFilterQueryConstraint<AppModelType>} constraint
|
|
31
|
-
*
|
|
32
|
-
* @returns {QueryNonFilterConstraint}
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```jsx
|
|
36
|
-
* const firebaseConfig = {};
|
|
37
|
-
* export const constraint = useMemo(() => {
|
|
38
|
-
* return buildQueryConstraint({
|
|
39
|
-
* type: 'limit',
|
|
40
|
-
* limit: 1
|
|
41
|
-
* });
|
|
42
|
-
* };
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
export const buildQueryConstraint = <AppModelType extends AppModel = AppModel>(
|
|
46
|
-
constraint: NonFilterQueryConstraint<AppModelType>
|
|
47
|
-
): QueryNonFilterConstraint => {
|
|
48
|
-
switch (constraint.type) {
|
|
49
|
-
case "orderBy":
|
|
50
|
-
return orderBy(constraint.fieldPath as string, constraint.directionStr);
|
|
51
|
-
case "startAt":
|
|
52
|
-
return startAt(...constraint.arguments);
|
|
53
|
-
case "startAfter":
|
|
54
|
-
return startAfter(...constraint.arguments);
|
|
55
|
-
case "endAt":
|
|
56
|
-
return endAt(constraint.value, constraint.key);
|
|
57
|
-
case "endBefore":
|
|
58
|
-
return endBefore(constraint.value, constraint.key);
|
|
59
|
-
case "limit":
|
|
60
|
-
return limit(constraint.limit);
|
|
61
|
-
case "limitToLast":
|
|
62
|
-
return limitToLast(constraint.limit);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
17
|
/**
|
|
67
18
|
* A hook to build a list of non-filter query constraints for firebase queries.
|
|
68
19
|
* It uses buildQueryConstraint method and useMemo hook.
|
|
@@ -1,15 +1,40 @@
|
|
|
1
1
|
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
2
|
import { Transaction } from "firebase/firestore";
|
|
3
3
|
import { FirebaseError } from "firebase/app";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @inline
|
|
6
|
+
*/
|
|
7
|
+
export type UseRunTransactionValues = <T = unknown>(transaction: Transaction) => T;
|
|
8
|
+
/**
|
|
9
|
+
* @inline
|
|
10
|
+
*/
|
|
11
|
+
export type UseRunTransactionOptions<T = unknown, TContext = unknown> = {
|
|
12
|
+
/**
|
|
13
|
+
* Reqct-mutation options that shall omit mutationFn
|
|
14
|
+
*/
|
|
15
|
+
options?: Omit<UseMutationOptions<T, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
|
|
7
16
|
};
|
|
8
17
|
/**
|
|
9
|
-
*
|
|
18
|
+
* Executes a Firestore transaction
|
|
19
|
+
*
|
|
20
|
+
* @group Hook
|
|
21
|
+
*
|
|
22
|
+
* @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
|
|
23
|
+
*
|
|
24
|
+
* @returns {UseMutationResult<T, Error>} An object representing mutation
|
|
10
25
|
*
|
|
11
|
-
* @
|
|
12
|
-
*
|
|
13
|
-
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```jsx
|
|
28
|
+
* export const MyComponent = () => {
|
|
29
|
+
* const doc = useGetDocData({
|
|
30
|
+
* options: {
|
|
31
|
+
* queryKey: ['key']
|
|
32
|
+
* },
|
|
33
|
+
* reference: collection(),
|
|
34
|
+
* path: 'id'
|
|
35
|
+
* });
|
|
36
|
+
* console.log(doc);
|
|
37
|
+
* };
|
|
38
|
+
* ```
|
|
14
39
|
*/
|
|
15
|
-
export declare const useRunTransaction: <
|
|
40
|
+
export declare const useRunTransaction: <T = unknown, TContext = unknown>({ options }: UseRunTransactionOptions<T, TContext>) => import("@tanstack/react-query").UseMutationResult<T, FirebaseError, UseRunTransactionValues, TContext>;
|
|
@@ -2,11 +2,27 @@ import { useMutation } from "@tanstack/react-query";
|
|
|
2
2
|
import { runTransaction } from "firebase/firestore";
|
|
3
3
|
import { useFirestore } from "./useFirestore";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Executes a Firestore transaction
|
|
6
6
|
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
|
|
10
|
+
*
|
|
11
|
+
* @returns {UseMutationResult<T, Error>} An object representing mutation
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```jsx
|
|
15
|
+
* export const MyComponent = () => {
|
|
16
|
+
* const doc = useGetDocData({
|
|
17
|
+
* options: {
|
|
18
|
+
* queryKey: ['key']
|
|
19
|
+
* },
|
|
20
|
+
* reference: collection(),
|
|
21
|
+
* path: 'id'
|
|
22
|
+
* });
|
|
23
|
+
* console.log(doc);
|
|
24
|
+
* };
|
|
25
|
+
* ```
|
|
10
26
|
*/
|
|
11
27
|
export const useRunTransaction = ({ options = {} }) => {
|
|
12
28
|
const db = useFirestore();
|
|
@@ -4,28 +4,53 @@ import { runTransaction, Transaction } from "firebase/firestore";
|
|
|
4
4
|
import { FirebaseError } from "firebase/app";
|
|
5
5
|
import { useFirestore } from "./useFirestore";
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
/**
|
|
8
|
+
* @inline
|
|
9
|
+
*/
|
|
10
|
+
export type UseRunTransactionValues = <T = unknown>(transaction: Transaction) => T;
|
|
8
11
|
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
/**
|
|
13
|
+
* @inline
|
|
14
|
+
*/
|
|
15
|
+
export type UseRunTransactionOptions<T = unknown, TContext = unknown> = {
|
|
16
|
+
/**
|
|
17
|
+
* Reqct-mutation options that shall omit mutationFn
|
|
18
|
+
*/
|
|
19
|
+
options?: Omit<UseMutationOptions<T, FirebaseError, UseRunTransactionValues, TContext>, "mutationFn">;
|
|
11
20
|
};
|
|
12
21
|
|
|
13
22
|
/**
|
|
14
|
-
*
|
|
23
|
+
* Executes a Firestore transaction
|
|
24
|
+
*
|
|
25
|
+
* @group Hook
|
|
26
|
+
*
|
|
27
|
+
* @param {UseRunTransactionOptions<T>} options - Configuration options for the mutation.
|
|
28
|
+
*
|
|
29
|
+
* @returns {UseMutationResult<T, Error>} An object representing mutation
|
|
15
30
|
*
|
|
16
|
-
* @
|
|
17
|
-
*
|
|
18
|
-
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```jsx
|
|
33
|
+
* export const MyComponent = () => {
|
|
34
|
+
* const doc = useGetDocData({
|
|
35
|
+
* options: {
|
|
36
|
+
* queryKey: ['key']
|
|
37
|
+
* },
|
|
38
|
+
* reference: collection(),
|
|
39
|
+
* path: 'id'
|
|
40
|
+
* });
|
|
41
|
+
* console.log(doc);
|
|
42
|
+
* };
|
|
43
|
+
* ```
|
|
19
44
|
*/
|
|
20
|
-
export const useRunTransaction = <
|
|
45
|
+
export const useRunTransaction = <T = unknown, TContext = unknown>({
|
|
21
46
|
options = {}
|
|
22
|
-
}: UseRunTransactionOptions<
|
|
47
|
+
}: UseRunTransactionOptions<T, TContext>) => {
|
|
23
48
|
const db = useFirestore();
|
|
24
49
|
|
|
25
50
|
return useMutation({
|
|
26
51
|
...options,
|
|
27
52
|
mutationFn: async (transactionFn) => {
|
|
28
|
-
return runTransaction<
|
|
53
|
+
return runTransaction<T>(db, transactionFn);
|
|
29
54
|
}
|
|
30
55
|
});
|
|
31
56
|
};
|
|
@@ -1,20 +1,53 @@
|
|
|
1
1
|
import { UseMutationOptions } from "@tanstack/react-query";
|
|
2
|
-
import {
|
|
2
|
+
import { DocumentReference, WithFieldValue } from "firebase/firestore";
|
|
3
3
|
import { FirebaseError } from "firebase/app";
|
|
4
|
-
|
|
4
|
+
import { AppModel } from "../../types";
|
|
5
|
+
/**
|
|
6
|
+
* @inline
|
|
7
|
+
*/
|
|
8
|
+
export type UseSetDocMutationValues<AppModelType extends AppModel = AppModel> = {
|
|
9
|
+
/**
|
|
10
|
+
* Data to write
|
|
11
|
+
*/
|
|
5
12
|
data: WithFieldValue<AppModelType>;
|
|
6
13
|
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
14
|
+
/**
|
|
15
|
+
* @inline
|
|
16
|
+
*/
|
|
17
|
+
export type UseSetDocMutationOptions<AppModelType extends AppModel = AppModel, TContext = unknown> = {
|
|
18
|
+
/**
|
|
19
|
+
* Reference to a document that must be written
|
|
20
|
+
*/
|
|
21
|
+
reference: DocumentReference<AppModelType, AppModelType> | null;
|
|
22
|
+
/**
|
|
23
|
+
* Options for useMutation hook excluding mutationFn.
|
|
24
|
+
*/
|
|
25
|
+
options?: Omit<UseMutationOptions<AppModelType, FirebaseError, UseSetDocMutationValues<AppModelType>, TContext>, "mutationFn">;
|
|
10
26
|
};
|
|
11
27
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
28
|
+
* Executes a mutation and returns added document
|
|
29
|
+
*
|
|
30
|
+
* @group Hook
|
|
31
|
+
*
|
|
32
|
+
* @param {UseSetDocMutationOptions<AppModelType>} options - Configuration options for mutation.
|
|
33
|
+
*
|
|
34
|
+
* @returns {UseMutationResult<AppModelType, Error, UseAddDocMutationValues<AppModelType>, TContext>} A mutation result
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```jsx
|
|
38
|
+
* export const MyComponent = () => {
|
|
39
|
+
* const {mutate} = useSetDocMutation({
|
|
40
|
+
* options: {
|
|
41
|
+
* },
|
|
42
|
+
* reference: collection().doc(),
|
|
43
|
+
* });
|
|
14
44
|
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
45
|
+
* // ....
|
|
46
|
+
* mutate({data: {test: 'value'}});
|
|
47
|
+
* // ....
|
|
48
|
+
* };
|
|
49
|
+
* ```
|
|
19
50
|
*/
|
|
20
|
-
export declare const useSetDocMutation: <AppModelType
|
|
51
|
+
export declare const useSetDocMutation: <AppModelType extends AppModel = AppModel, TContext = unknown>({ reference, options }: UseSetDocMutationOptions<AppModelType, TContext>) => import("@tanstack/react-query").UseMutationResult<AppModelType & {
|
|
52
|
+
uid: string;
|
|
53
|
+
}, FirebaseError, UseSetDocMutationValues<AppModelType>, TContext>;
|