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