Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp converstions #7

Open
izakfilmalter opened this issue Nov 10, 2021 · 3 comments
Open

Timestamp converstions #7

izakfilmalter opened this issue Nov 10, 2021 · 3 comments

Comments

@izakfilmalter
Copy link

It would be sick if this lib could output Timestamp as a Timestamp instead of as a string like what swr-firestore does.

@izakfilmalter
Copy link
Author

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'],
    },
  )

@izakfilmalter
Copy link
Author

@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);
                    });

@aminerol
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants