react-query-firebase 2.2.0 → 2.3.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/useCompositeFilter.d.ts +62 -20
- package/react-native/firestore/useCompositeFilter.js +51 -11
- package/react-native/firestore/useCompositeFilter.ts +70 -25
- package/react-native/firestore/useCountQuery.d.ts +35 -9
- package/react-native/firestore/useCountQuery.js +15 -6
- package/react-native/firestore/useCountQuery.ts +38 -14
- package/react-native/firestore/useInfiniteQuery.d.ts +39 -7
- package/react-native/firestore/useInfiniteQuery.js +19 -4
- package/react-native/firestore/useInfiniteQuery.ts +47 -8
- package/react-native/firestore/useQuery.d.ts +37 -12
- package/react-native/firestore/useQuery.js +17 -9
- package/react-native/firestore/useQuery.ts +40 -14
- package/types/index.d.ts +2 -0
- package/types/index.js +2 -0
- package/web/firestore/useCompositeFilter.d.ts +63 -21
- package/web/firestore/useCompositeFilter.js +48 -9
- package/web/firestore/useCompositeFilter.ts +73 -23
- package/web/firestore/useCountQuery.d.ts +37 -11
- package/web/firestore/useCountQuery.js +16 -7
- package/web/firestore/useCountQuery.ts +44 -19
- package/web/firestore/useInfiniteQuery.d.ts +41 -10
- package/web/firestore/useInfiniteQuery.js +21 -6
- package/web/firestore/useInfiniteQuery.ts +51 -23
- package/web/firestore/useQuery.d.ts +39 -15
- package/web/firestore/useQuery.js +19 -11
- package/web/firestore/useQuery.ts +49 -27
|
@@ -3,15 +3,24 @@ import { useQuery as useReactQuery } from "@tanstack/react-query";
|
|
|
3
3
|
/**
|
|
4
4
|
* Executes a query with specified constraints and returns the count of matched documents.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
6
|
+
* @group Hook
|
|
7
|
+
*
|
|
8
|
+
* @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
8
9
|
*
|
|
9
|
-
* @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
10
|
-
* @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
|
|
11
|
-
* @param {unknown} [options.query] - Reference to the query object to be executed.
|
|
12
|
-
* @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
|
|
13
|
-
* @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
|
|
14
10
|
* @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* export const MyComponent = () => {
|
|
15
|
+
* const count = useCountQuery({
|
|
16
|
+
* options: {
|
|
17
|
+
* queryKey: ['key']
|
|
18
|
+
* },
|
|
19
|
+
* collectionReference: collection(),
|
|
20
|
+
* });
|
|
21
|
+
* console.log(count);
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
15
24
|
*/
|
|
16
25
|
export const useCountQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
|
|
17
26
|
return useReactQuery({
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import {
|
|
2
|
-
DocumentData,
|
|
3
2
|
getCountFromServer,
|
|
4
3
|
CollectionReference,
|
|
5
4
|
query,
|
|
@@ -13,36 +12,58 @@ import {
|
|
|
13
12
|
useQuery as useReactQuery,
|
|
14
13
|
UseQueryOptions as UseReactQueryOptions
|
|
15
14
|
} from "@tanstack/react-query";
|
|
15
|
+
import { QueryFilterConstraint } from "./useCompositeFilter";
|
|
16
|
+
import { AppModel } from "../../types";
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
> = {
|
|
18
|
+
/**
|
|
19
|
+
* @inline
|
|
20
|
+
*/
|
|
21
|
+
type UseCountQueryOptions<AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel> = {
|
|
22
|
+
/**
|
|
23
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
24
|
+
*/
|
|
21
25
|
options: Omit<UseReactQueryOptions<number, Error, number>, "queryFn"> &
|
|
22
26
|
Required<Pick<UseReactQueryOptions<number, Error, number>, "queryKey">>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Reference to a Firestore collection
|
|
30
|
+
*/
|
|
23
31
|
collectionReference: CollectionReference<AppModelType, DbModelType>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Non composite filter constraints such as limit, order, where
|
|
35
|
+
*/
|
|
24
36
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
25
|
-
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Composite filter
|
|
40
|
+
*/
|
|
41
|
+
compositeFilter?: QueryFilterConstraint;
|
|
26
42
|
};
|
|
27
43
|
|
|
28
44
|
/**
|
|
29
45
|
* Executes a query with specified constraints and returns the count of matched documents.
|
|
30
46
|
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
47
|
+
* @group Hook
|
|
48
|
+
*
|
|
49
|
+
* @param {UseCountQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
33
50
|
*
|
|
34
|
-
* @param {UseCountQueryOptions<AppModelType, DbModelType>} options - Configuration options for the query.
|
|
35
|
-
* @param {AppModelType extends DocumentData = DocumentData} [options.options] - Additional options for the React Query.
|
|
36
|
-
* @param {unknown} [options.query] - Reference to the query object to be executed.
|
|
37
|
-
* @param {unknown[]} [options.queryConstraints=[]] - An array of constraints to apply to the query.
|
|
38
|
-
* @param {unknown} [options.compositeFilter] - An optional composite filter to apply to the query.
|
|
39
51
|
* @returns {UseQueryResult<number>} An object containing the number of documents that match the query.
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```jsx
|
|
55
|
+
* export const MyComponent = () => {
|
|
56
|
+
* const count = useCountQuery({
|
|
57
|
+
* options: {
|
|
58
|
+
* queryKey: ['key']
|
|
59
|
+
* },
|
|
60
|
+
* collectionReference: collection(),
|
|
61
|
+
* });
|
|
62
|
+
* console.log(count);
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
40
65
|
*/
|
|
41
|
-
|
|
42
|
-
export const useCountQuery = <
|
|
43
|
-
AppModelType extends DocumentData = DocumentData,
|
|
44
|
-
DbModelType extends DocumentData = DocumentData
|
|
45
|
-
>({
|
|
66
|
+
export const useCountQuery = <AppModelType extends AppModel = AppModel, DbModelType extends AppModel = AppModel>({
|
|
46
67
|
options,
|
|
47
68
|
collectionReference,
|
|
48
69
|
queryConstraints = [],
|
|
@@ -52,7 +73,11 @@ export const useCountQuery = <
|
|
|
52
73
|
...options,
|
|
53
74
|
queryFn: async () => {
|
|
54
75
|
const queryToExecute = compositeFilter
|
|
55
|
-
? query(
|
|
76
|
+
? query(
|
|
77
|
+
collectionReference,
|
|
78
|
+
compositeFilter as QueryCompositeFilterConstraint,
|
|
79
|
+
...(queryConstraints as QueryNonFilterConstraint[])
|
|
80
|
+
)
|
|
56
81
|
: query(collectionReference, ...queryConstraints);
|
|
57
82
|
|
|
58
83
|
const querySnapshot = await getCountFromServer(queryToExecute);
|
|
@@ -1,18 +1,49 @@
|
|
|
1
|
-
import { CollectionReference,
|
|
1
|
+
import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
2
|
import { UseInfiniteQueryOptions as UseReactInfiniteQueryOptions, QueryKey, UseInfiniteQueryResult, InfiniteData } from "@tanstack/react-query";
|
|
3
|
-
|
|
3
|
+
import { QueryFilterConstraint } from "./useCompositeFilter";
|
|
4
|
+
import { AppModel } from "../../types";
|
|
5
|
+
/**
|
|
6
|
+
* @inline
|
|
7
|
+
*/
|
|
8
|
+
type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey> = {
|
|
9
|
+
/**
|
|
10
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
11
|
+
*/
|
|
4
12
|
options: Omit<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryFn"> & Required<Pick<UseReactInfiniteQueryOptions<AppModelType[], Error, InfiniteData<AppModelType[]>, AppModelType[], TQueryKey, QueryConstraint>, "queryKey">>;
|
|
5
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Reference to a Firestore collection
|
|
15
|
+
*/
|
|
16
|
+
collectionReference: CollectionReference<AppModelType, AppModelType>;
|
|
17
|
+
/**
|
|
18
|
+
* Non composite filter constraints such as limit, order, where
|
|
19
|
+
*/
|
|
6
20
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
-
|
|
8
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Composite filter
|
|
23
|
+
*/
|
|
24
|
+
compositeFilter?: QueryFilterConstraint;
|
|
9
25
|
};
|
|
10
26
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
27
|
+
* Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
|
|
28
|
+
*
|
|
29
|
+
* @group Hook
|
|
30
|
+
*
|
|
31
|
+
* @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
|
|
32
|
+
*
|
|
33
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
|
|
13
34
|
*
|
|
14
|
-
* @
|
|
15
|
-
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```jsx
|
|
37
|
+
* export const MyComponent = () => {
|
|
38
|
+
* const docs = useInfiniteQuery({
|
|
39
|
+
* options: {
|
|
40
|
+
* queryKey: ['key']
|
|
41
|
+
* },
|
|
42
|
+
* collectionReference: collection(),
|
|
43
|
+
* });
|
|
44
|
+
* console.log(docs);
|
|
45
|
+
* };
|
|
46
|
+
* ```
|
|
16
47
|
*/
|
|
17
|
-
export declare const useInfiniteQuery: <AppModelType extends
|
|
48
|
+
export declare const useInfiniteQuery: <AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey>({ options, collectionReference, queryConstraints, compositeFilter }: UseInfiniteQueryOptions<AppModelType, TQueryKey>) => UseInfiniteQueryResult<InfiniteData<AppModelType[]>>;
|
|
18
49
|
export {};
|
|
@@ -1,13 +1,28 @@
|
|
|
1
1
|
import { getDocs, query } from "firebase/firestore";
|
|
2
2
|
import { useInfiniteQuery as useInfiniteReactQuery } from "@tanstack/react-query";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
* It fetches data in pages and can load more as required.
|
|
4
|
+
* Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
|
|
6
5
|
*
|
|
7
|
-
* @
|
|
8
|
-
*
|
|
6
|
+
* @group Hook
|
|
7
|
+
*
|
|
8
|
+
* @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
|
|
9
|
+
*
|
|
10
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* export const MyComponent = () => {
|
|
15
|
+
* const docs = useInfiniteQuery({
|
|
16
|
+
* options: {
|
|
17
|
+
* queryKey: ['key']
|
|
18
|
+
* },
|
|
19
|
+
* collectionReference: collection(),
|
|
20
|
+
* });
|
|
21
|
+
* console.log(docs);
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
9
24
|
*/
|
|
10
|
-
export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter
|
|
25
|
+
export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
|
|
11
26
|
return useInfiniteReactQuery({
|
|
12
27
|
...options,
|
|
13
28
|
queryFn: async ({ pageParam }) => {
|
|
@@ -15,7 +30,7 @@ export const useInfiniteQuery = ({ options, collectionReference, queryConstraint
|
|
|
15
30
|
const queryToExecute = compositeFilter
|
|
16
31
|
? query(collectionReference, compositeFilter, ...allQueryConstraints)
|
|
17
32
|
: query(collectionReference, ...allQueryConstraints);
|
|
18
|
-
const querySnapshot = await getDocs(
|
|
33
|
+
const querySnapshot = await getDocs(queryToExecute);
|
|
19
34
|
const docs = [];
|
|
20
35
|
if (querySnapshot) {
|
|
21
36
|
querySnapshot.forEach((doc) => {
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CollectionReference,
|
|
3
|
-
DocumentData,
|
|
4
|
-
FirestoreDataConverter,
|
|
5
3
|
getDocs,
|
|
6
4
|
query,
|
|
7
5
|
QueryCompositeFilterConstraint,
|
|
@@ -16,12 +14,16 @@ import {
|
|
|
16
14
|
UseInfiniteQueryResult,
|
|
17
15
|
InfiniteData
|
|
18
16
|
} from "@tanstack/react-query";
|
|
17
|
+
import { QueryFilterConstraint } from "./useCompositeFilter";
|
|
18
|
+
import { AppModel } from "../../types";
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
/**
|
|
21
|
+
* @inline
|
|
22
|
+
*/
|
|
23
|
+
type UseInfiniteQueryOptions<AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey> = {
|
|
24
|
+
/**
|
|
25
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
26
|
+
*/
|
|
25
27
|
options: Omit<
|
|
26
28
|
UseReactInfiniteQueryOptions<
|
|
27
29
|
AppModelType[],
|
|
@@ -46,38 +48,64 @@ type UseInfiniteQueryOptions<
|
|
|
46
48
|
"queryKey"
|
|
47
49
|
>
|
|
48
50
|
>;
|
|
49
|
-
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Reference to a Firestore collection
|
|
54
|
+
*/
|
|
55
|
+
collectionReference: CollectionReference<AppModelType, AppModelType>;
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Non composite filter constraints such as limit, order, where
|
|
59
|
+
*/
|
|
50
60
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
51
|
-
|
|
52
|
-
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Composite filter
|
|
64
|
+
*/
|
|
65
|
+
compositeFilter?: QueryFilterConstraint;
|
|
53
66
|
};
|
|
54
67
|
|
|
55
68
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
69
|
+
* Executes an infinite query on a Firestore data source and returns the resulting documents as an array.
|
|
70
|
+
*
|
|
71
|
+
* @group Hook
|
|
72
|
+
*
|
|
73
|
+
* @param {UseInfiniteQueryOptions<AppModelType, TQueryKey>} options - Configuration options for the query.
|
|
74
|
+
*
|
|
75
|
+
* @returns {UseInfiniteQueryResult<InfiniteData<AppModelType[]>>} An object containing documents that match the query.
|
|
58
76
|
*
|
|
59
|
-
* @
|
|
60
|
-
*
|
|
77
|
+
* @example
|
|
78
|
+
* ```jsx
|
|
79
|
+
* export const MyComponent = () => {
|
|
80
|
+
* const docs = useInfiniteQuery({
|
|
81
|
+
* options: {
|
|
82
|
+
* queryKey: ['key']
|
|
83
|
+
* },
|
|
84
|
+
* collectionReference: collection(),
|
|
85
|
+
* });
|
|
86
|
+
* console.log(docs);
|
|
87
|
+
* };
|
|
88
|
+
* ```
|
|
61
89
|
*/
|
|
62
|
-
export const useInfiniteQuery = <
|
|
63
|
-
AppModelType extends DocumentData = DocumentData,
|
|
64
|
-
DbModelType extends DocumentData = DocumentData
|
|
65
|
-
>({
|
|
90
|
+
export const useInfiniteQuery = <AppModelType extends AppModel = AppModel, TQueryKey extends QueryKey = QueryKey>({
|
|
66
91
|
options,
|
|
67
92
|
collectionReference,
|
|
68
93
|
queryConstraints = [],
|
|
69
|
-
compositeFilter
|
|
70
|
-
|
|
71
|
-
}: UseInfiniteQueryOptions<AppModelType, DbModelType>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
|
|
94
|
+
compositeFilter
|
|
95
|
+
}: UseInfiniteQueryOptions<AppModelType, TQueryKey>): UseInfiniteQueryResult<InfiniteData<AppModelType[]>> => {
|
|
72
96
|
return useInfiniteReactQuery({
|
|
73
97
|
...options,
|
|
74
98
|
queryFn: async ({ pageParam }) => {
|
|
75
99
|
const allQueryConstraints = [...queryConstraints, ...(pageParam ? [pageParam] : [])];
|
|
76
100
|
const queryToExecute = compositeFilter
|
|
77
|
-
? query(
|
|
101
|
+
? query(
|
|
102
|
+
collectionReference,
|
|
103
|
+
compositeFilter as QueryCompositeFilterConstraint,
|
|
104
|
+
...(allQueryConstraints as QueryNonFilterConstraint[])
|
|
105
|
+
)
|
|
78
106
|
: query(collectionReference, ...allQueryConstraints);
|
|
79
107
|
|
|
80
|
-
const querySnapshot = await getDocs(
|
|
108
|
+
const querySnapshot = await getDocs(queryToExecute);
|
|
81
109
|
const docs: AppModelType[] = [];
|
|
82
110
|
|
|
83
111
|
if (querySnapshot) {
|
|
@@ -1,25 +1,49 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CollectionReference, QueryConstraint, QueryNonFilterConstraint } from "firebase/firestore";
|
|
2
2
|
import { UseQueryResult, UseQueryOptions as UseReactQueryOptions } from "@tanstack/react-query";
|
|
3
|
-
|
|
3
|
+
import { QueryFilterConstraint } from "./useCompositeFilter";
|
|
4
|
+
import { AppModel } from "../../types";
|
|
5
|
+
/**
|
|
6
|
+
* @inline
|
|
7
|
+
*/
|
|
8
|
+
type UseQueryOptions<AppModelType extends AppModel = AppModel> = {
|
|
9
|
+
/**
|
|
10
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
11
|
+
*/
|
|
4
12
|
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> & Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
5
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Reference to a Firestore collection
|
|
15
|
+
*/
|
|
16
|
+
collectionReference: CollectionReference<AppModelType, AppModelType>;
|
|
17
|
+
/**
|
|
18
|
+
* Non composite filter constraints such as limit, order, where
|
|
19
|
+
*/
|
|
6
20
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
7
|
-
|
|
8
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Composite filter
|
|
23
|
+
*/
|
|
24
|
+
compositeFilter?: QueryFilterConstraint;
|
|
9
25
|
};
|
|
10
26
|
/**
|
|
11
|
-
* Executes a query on a Firestore
|
|
27
|
+
* Executes a query on a Firestore data source and returns the resulting documents as an array.
|
|
28
|
+
*
|
|
29
|
+
* @group Hook
|
|
12
30
|
*
|
|
13
|
-
*
|
|
14
|
-
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
31
|
+
* @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
15
32
|
*
|
|
16
|
-
* @
|
|
17
|
-
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
18
|
-
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
19
|
-
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
20
|
-
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
33
|
+
* @returns {UseQueryResult<AppModelType[]>} An object containing documents that match the query.
|
|
21
34
|
*
|
|
22
|
-
* @
|
|
35
|
+
* @example
|
|
36
|
+
* ```jsx
|
|
37
|
+
* export const MyComponent = () => {
|
|
38
|
+
* const docs = useQuery({
|
|
39
|
+
* options: {
|
|
40
|
+
* queryKey: ['key']
|
|
41
|
+
* },
|
|
42
|
+
* collectionReference: collection(),
|
|
43
|
+
* });
|
|
44
|
+
* console.log(docs);
|
|
45
|
+
* };
|
|
46
|
+
* ```
|
|
23
47
|
*/
|
|
24
|
-
export declare const useQuery: <AppModelType extends
|
|
48
|
+
export declare const useQuery: <AppModelType extends AppModel = AppModel>({ options, collectionReference, queryConstraints, compositeFilter }: UseQueryOptions<AppModelType>) => UseQueryResult<AppModelType[]>;
|
|
25
49
|
export {};
|
|
@@ -1,27 +1,35 @@
|
|
|
1
1
|
import { getDocs, query } from "firebase/firestore";
|
|
2
2
|
import { useQuery as useReactQuery } from "@tanstack/react-query";
|
|
3
3
|
/**
|
|
4
|
-
* Executes a query on a Firestore
|
|
4
|
+
* Executes a query on a Firestore data source and returns the resulting documents as an array.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
6
|
+
* @group Hook
|
|
8
7
|
*
|
|
9
|
-
* @param {UseQueryOptions<AppModelType
|
|
10
|
-
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
11
|
-
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
12
|
-
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
13
|
-
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
8
|
+
* @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
14
9
|
*
|
|
15
|
-
* @returns {UseQueryResult<AppModelType[]>}
|
|
10
|
+
* @returns {UseQueryResult<AppModelType[]>} An object containing documents that match the query.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* export const MyComponent = () => {
|
|
15
|
+
* const docs = useQuery({
|
|
16
|
+
* options: {
|
|
17
|
+
* queryKey: ['key']
|
|
18
|
+
* },
|
|
19
|
+
* collectionReference: collection(),
|
|
20
|
+
* });
|
|
21
|
+
* console.log(docs);
|
|
22
|
+
* };
|
|
23
|
+
* ```
|
|
16
24
|
*/
|
|
17
|
-
export const useQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter
|
|
25
|
+
export const useQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
|
|
18
26
|
return useReactQuery({
|
|
19
27
|
...options,
|
|
20
28
|
queryFn: async () => {
|
|
21
29
|
const queryToExecute = compositeFilter
|
|
22
30
|
? query(collectionReference, compositeFilter, ...queryConstraints)
|
|
23
31
|
: query(collectionReference, ...queryConstraints);
|
|
24
|
-
const querySnapshot = await getDocs(
|
|
32
|
+
const querySnapshot = await getDocs(queryToExecute);
|
|
25
33
|
const docs = [];
|
|
26
34
|
if (querySnapshot) {
|
|
27
35
|
querySnapshot.forEach((doc) => {
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import {
|
|
2
|
-
DocumentData,
|
|
3
|
-
FirestoreDataConverter,
|
|
4
2
|
getDocs,
|
|
5
3
|
CollectionReference,
|
|
6
4
|
query,
|
|
@@ -14,51 +12,75 @@ import {
|
|
|
14
12
|
useQuery as useReactQuery,
|
|
15
13
|
UseQueryOptions as UseReactQueryOptions
|
|
16
14
|
} from "@tanstack/react-query";
|
|
15
|
+
import { QueryFilterConstraint } from "./useCompositeFilter";
|
|
16
|
+
import { AppModel } from "../../types";
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
> = {
|
|
18
|
+
/**
|
|
19
|
+
* @inline
|
|
20
|
+
*/
|
|
21
|
+
type UseQueryOptions<AppModelType extends AppModel = AppModel> = {
|
|
22
|
+
/**
|
|
23
|
+
* Reqct-query options that must include queryKey and shall not define queryFn
|
|
24
|
+
*/
|
|
22
25
|
options: Omit<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryFn"> &
|
|
23
26
|
Required<Pick<UseReactQueryOptions<AppModelType[], Error, AppModelType[]>, "queryKey">>;
|
|
24
|
-
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Reference to a Firestore collection
|
|
30
|
+
*/
|
|
31
|
+
collectionReference: CollectionReference<AppModelType, AppModelType>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Non composite filter constraints such as limit, order, where
|
|
35
|
+
*/
|
|
25
36
|
queryConstraints?: QueryConstraint[] | QueryNonFilterConstraint[];
|
|
26
|
-
|
|
27
|
-
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Composite filter
|
|
40
|
+
*/
|
|
41
|
+
compositeFilter?: QueryFilterConstraint;
|
|
28
42
|
};
|
|
29
43
|
|
|
30
44
|
/**
|
|
31
|
-
* Executes a query on a Firestore
|
|
45
|
+
* Executes a query on a Firestore data source and returns the resulting documents as an array.
|
|
46
|
+
*
|
|
47
|
+
* @group Hook
|
|
32
48
|
*
|
|
33
|
-
*
|
|
34
|
-
* reference and constraints. It supports optional filtering, conversion, and additional query constraints.
|
|
49
|
+
* @param {UseQueryOptions<AppModelType>} options - Configuration options for the query.
|
|
35
50
|
*
|
|
36
|
-
* @
|
|
37
|
-
* @param {DocumentReference<AppModelType>} queryReference - The reference to the query to be executed.
|
|
38
|
-
* @param {QueryConstraint[]} queryConstraints - Additional constraints to fine-tune the query.
|
|
39
|
-
* @param {QueryConstraint} compositeFilter - Optional composite filter to apply to the query.
|
|
40
|
-
* @param {FirestoreDataConverter<AppModelType>} converter - Optional data converter for transforming snapshots.
|
|
51
|
+
* @returns {UseQueryResult<AppModelType[]>} An object containing documents that match the query.
|
|
41
52
|
*
|
|
42
|
-
* @
|
|
53
|
+
* @example
|
|
54
|
+
* ```jsx
|
|
55
|
+
* export const MyComponent = () => {
|
|
56
|
+
* const docs = useQuery({
|
|
57
|
+
* options: {
|
|
58
|
+
* queryKey: ['key']
|
|
59
|
+
* },
|
|
60
|
+
* collectionReference: collection(),
|
|
61
|
+
* });
|
|
62
|
+
* console.log(docs);
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
43
65
|
*/
|
|
44
|
-
export const useQuery = <
|
|
45
|
-
AppModelType extends DocumentData = DocumentData,
|
|
46
|
-
DbModelType extends DocumentData = DocumentData
|
|
47
|
-
>({
|
|
66
|
+
export const useQuery = <AppModelType extends AppModel = AppModel>({
|
|
48
67
|
options,
|
|
49
68
|
collectionReference,
|
|
50
69
|
queryConstraints = [],
|
|
51
|
-
compositeFilter
|
|
52
|
-
|
|
53
|
-
}: UseQueryOptions<AppModelType, DbModelType>): UseQueryResult<AppModelType[]> => {
|
|
70
|
+
compositeFilter
|
|
71
|
+
}: UseQueryOptions<AppModelType>): UseQueryResult<AppModelType[]> => {
|
|
54
72
|
return useReactQuery({
|
|
55
73
|
...options,
|
|
56
74
|
queryFn: async () => {
|
|
57
75
|
const queryToExecute = compositeFilter
|
|
58
|
-
? query(
|
|
76
|
+
? query(
|
|
77
|
+
collectionReference,
|
|
78
|
+
compositeFilter as QueryCompositeFilterConstraint,
|
|
79
|
+
...(queryConstraints as QueryNonFilterConstraint[])
|
|
80
|
+
)
|
|
59
81
|
: query(collectionReference, ...queryConstraints);
|
|
60
82
|
|
|
61
|
-
const querySnapshot = await getDocs(
|
|
83
|
+
const querySnapshot = await getDocs(queryToExecute);
|
|
62
84
|
const docs: AppModelType[] = [];
|
|
63
85
|
|
|
64
86
|
if (querySnapshot) {
|