Skip to content

Commit 505b29d

Browse files
committed
Make similar changes to #80 for the realtime database
1 parent b826d61 commit 505b29d

File tree

4 files changed

+25
-34
lines changed

4 files changed

+25
-34
lines changed

database/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ The `useListVals` hook takes the following parameters:
9898

9999
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
100100
- `options`: (optional) `Object` with the following parameters:
101-
- `keyField`: (optional) `string` field name that should be populated with the `firebase.firestore.QuerySnapshot.id` property in the returned values
101+
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.id` property in the returned values.
102+
- `refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
102103

103104
Returns:
104105

@@ -158,6 +159,7 @@ The `useObjectVal` hook takes the following parameters:
158159
- `reference`: (optional) `firebase.database.Reference` for the data you would like to load
159160
- `options`: (optional) `Object` with the following parameters:
160161
- `keyField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.key` property in the returned value.
162+
- `refField`: (optional) `string` field name that should be populated with the `firebase.database.DataSnapshot.ref` property.
161163

162164
Returns:
163165

database/helpers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const isObject = (val: any) =>
55

66
export const snapshotToData = (
77
snapshot: firebase.database.DataSnapshot,
8-
keyField?: string
8+
keyField?: string,
9+
refField?: string
910
) => {
1011
if (!snapshot.exists) {
1112
return undefined;
@@ -16,6 +17,7 @@ export const snapshotToData = (
1617
return {
1718
...val,
1819
...(keyField ? { [keyField]: snapshot.key } : null),
20+
...(refField ? { [refField]: snapshot.ref } : null),
1921
};
2022
}
2123
return val;

database/useList.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,52 +68,45 @@ export const useList = (query?: firebase.database.Query | null): ListHook => {
6868
}, [ref.current]);
6969

7070
const resArray: ListHook = [state.value.values, state.loading, state.error];
71-
return useMemo(
72-
() => resArray,
73-
resArray,
74-
);
71+
return useMemo(() => resArray, resArray);
7572
};
7673

7774
export const useListKeys = (
7875
query?: firebase.database.Query | null
7976
): ListKeysHook => {
8077
const [snapshots, loading, error] = useList(query);
8178
const values = useMemo(
82-
() => (snapshots ? snapshots.map(snapshot => snapshot.key as string) : undefined),
79+
() =>
80+
snapshots
81+
? snapshots.map((snapshot) => snapshot.key as string)
82+
: undefined,
8383
[snapshots]
8484
);
85-
const resArray: ListKeysHook = [
86-
values,
87-
loading,
88-
error,
89-
];
90-
91-
return useMemo(
92-
() => resArray,
93-
resArray,
94-
);
85+
const resArray: ListKeysHook = [values, loading, error];
86+
87+
return useMemo(() => resArray, resArray);
9588
};
9689

9790
export const useListVals = <T>(
9891
query?: firebase.database.Query | null,
9992
options?: {
10093
keyField?: string;
94+
refField?: string;
10195
}
10296
): ListValsHook<T> => {
97+
const keyField = options ? options.keyField : undefined;
98+
const refField = options ? options.refField : undefined;
10399
const [snapshots, loading, error] = useList(query);
104100
const values = useMemo(
105101
() =>
106102
snapshots
107103
? snapshots.map((snapshot) =>
108-
snapshotToData(snapshot, options ? options.keyField : undefined)
104+
snapshotToData(snapshot, keyField, refField)
109105
)
110106
: undefined,
111107
[snapshots, options && options.keyField]
112108
);
113109

114110
const resArray: ListValsHook<T> = [values, loading, error];
115-
return useMemo(
116-
() => resArray,
117-
resArray,
118-
);
111+
return useMemo(() => resArray, resArray);
119112
};

database/useObject.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,24 @@ export const useObject = (
3333
}, [ref.current]);
3434

3535
const resArray: ObjectHook = [value, loading, error];
36-
return useMemo(
37-
() => resArray,
38-
resArray,
39-
);
36+
return useMemo(() => resArray, resArray);
4037
};
4138

4239
export const useObjectVal = <T>(
4340
query?: firebase.database.Query | null,
4441
options?: {
4542
keyField?: string;
43+
refField?: string;
4644
}
4745
): ObjectValHook<T> => {
46+
const keyField = options ? options.keyField : undefined;
47+
const refField = options ? options.refField : undefined;
4848
const [snapshot, loading, error] = useObject(query);
4949
const value = useMemo(
50-
() =>
51-
snapshot
52-
? snapshotToData(snapshot, options ? options.keyField : undefined)
53-
: undefined,
50+
() => (snapshot ? snapshotToData(snapshot, keyField, refField) : undefined),
5451
[snapshot, options && options.keyField]
5552
);
5653

5754
const resArray: ObjectValHook<T> = [value, loading, error];
58-
return useMemo(
59-
() => resArray,
60-
resArray,
61-
);
55+
return useMemo(() => resArray, resArray);
6256
};

0 commit comments

Comments
 (0)