react-query-firestore
react-query-firestore copied to clipboard
Timestamp converstions
It would be sick if this lib could output Timestamp as a Timestamp instead of as a string like what swr-firestore does.
My current work around is to use the select option within my useCollection query:
const { data, status } = useCollection<Message, Message>(
path,
{
refetchOnMount: 'always',
select: (x) =>
pipe(
x,
A.map((y) => ({
...y,
...pipe(
y.time,
O.fromNullable,
O.map((time) => ({
time: Timestamp.fromDate(
dayjs(time as unknown as string).toDate(),
),
})),
O.getOrElse(() => ({})),
),
})),
),
},
{
orderBy: ['time', 'desc'],
},
)
@aminerol I ended up just using patch-package to remove parseDates(docData);. I was running into strange issues where collections with older dates would just crash. Taking this out resolved to of my issues, the crashing and wanting timestamp. Is there a reason parseDates is needed? Does react-query not play well with the timestamp?
I also noticed that fetchNextPage within useCollection wasn't calling parseDates which seems like a bug. Maybe we should figure out how to share this code between both instances since you probably want it to be the same:
querySnapshot.forEach((doc) => {
const docData = doc.data() ?? empty.object;
parseDates(docData);
const docToAdd = {
...docData,
id: doc.id,
exists: doc.exists,
hasPendingWrites: doc.metadata.hasPendingWrites,
__snapshot: ignoreFirestoreDocumentSnapshotField
? undefined
: doc,
} as Doc;
// update individual docs in the cache
queryClient.setQueryData(doc.ref.path, docToAdd);
data.push(docToAdd);
});
Hi @izakfilmalter , parseDates was just added recently (v0.3.1) and not mentioned in the readme. but i will for sure update readme to cover all the fixes and new features.
parseDates will automatically parses any field on your document that is a TimeStamp type to JS Date using toDate() by firestore so you dont need to utilize select option to do the parsing, as for the crash it might be due to fact you trying to re-parse the date again, but not sure why its only for older dates.
react-query only fetch, cache and update data and does not care about what date types are.
and yes fetchNextPage should call parseDates as well, you are welcomed to open a PR for that