react-query-firebase 2.1.4 → 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/auth/index.d.ts +1 -0
- package/react-native/auth/index.js +1 -0
- package/react-native/auth/index.ts +1 -0
- package/react-native/auth/useAuthStateReady.d.ts +17 -0
- package/react-native/auth/useAuthStateReady.js +33 -0
- package/react-native/auth/useAuthStateReady.ts +38 -0
- 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/auth/useAuthStateReady.d.ts +12 -1
- package/web/auth/useAuthStateReady.js +12 -1
- package/web/auth/useAuthStateReady.ts +12 -1
- 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
|
@@ -1,11 +1,26 @@
|
|
|
1
1
|
import { getDocs, query } from "@react-native-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
25
|
export const useInfiniteQuery = ({ options, collectionReference, queryConstraints = [], compositeFilter }) => {
|
|
11
26
|
return useInfiniteReactQuery({
|
|
@@ -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,6 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A custom hook that determines if the Firebase authentication state is ready.
|
|
3
3
|
* It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
|
|
4
|
-
*
|
|
4
|
+
*
|
|
5
|
+
* @group Hook
|
|
6
|
+
*
|
|
7
|
+
* @returns {boolean}
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```jsx
|
|
11
|
+
* export const MyComponent = () => {
|
|
12
|
+
* const isAuthStateReady = useAuthStateReady();
|
|
13
|
+
* console.log(isAuthStateReady);
|
|
14
|
+
* };
|
|
15
|
+
* ```
|
|
5
16
|
*/
|
|
6
17
|
export declare const useAuthStateReady: () => boolean;
|
|
@@ -3,7 +3,18 @@ import { useCallback, useEffect, useState } from "react";
|
|
|
3
3
|
/**
|
|
4
4
|
* A custom hook that determines if the Firebase authentication state is ready.
|
|
5
5
|
* It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
|
|
6
|
-
*
|
|
6
|
+
*
|
|
7
|
+
* @group Hook
|
|
8
|
+
*
|
|
9
|
+
* @returns {boolean}
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```jsx
|
|
13
|
+
* export const MyComponent = () => {
|
|
14
|
+
* const isAuthStateReady = useAuthStateReady();
|
|
15
|
+
* console.log(isAuthStateReady);
|
|
16
|
+
* };
|
|
17
|
+
* ```
|
|
7
18
|
*/
|
|
8
19
|
export const useAuthStateReady = () => {
|
|
9
20
|
const firebaseAuth = useAuth();
|
|
@@ -4,7 +4,18 @@ import { useCallback, useEffect, useState } from "react";
|
|
|
4
4
|
/**
|
|
5
5
|
* A custom hook that determines if the Firebase authentication state is ready.
|
|
6
6
|
* It uses Firebase authentication to check if the auth state is ready and updates the state accordingly.
|
|
7
|
-
*
|
|
7
|
+
*
|
|
8
|
+
* @group Hook
|
|
9
|
+
*
|
|
10
|
+
* @returns {boolean}
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```jsx
|
|
14
|
+
* export const MyComponent = () => {
|
|
15
|
+
* const isAuthStateReady = useAuthStateReady();
|
|
16
|
+
* console.log(isAuthStateReady);
|
|
17
|
+
* };
|
|
18
|
+
* ```
|
|
8
19
|
*/
|
|
9
20
|
export const useAuthStateReady = () => {
|
|
10
21
|
const firebaseAuth = useAuth();
|
|
@@ -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(() => {
|