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

Types for date fields differ in input and output. #124

Open
jckw opened this issue Jul 9, 2021 · 3 comments
Open

Types for date fields differ in input and output. #124

jckw opened this issue Jul 9, 2021 · 3 comments

Comments

@jckw
Copy link
Contributor

jckw commented Jul 9, 2021

The input type is different to the output type for a date field, however the types in functions etc do not reflect this.

E.g.

type Profile = {
  createdAt: Date
}

const { add, data } = useCollection<Profile>('profiles')


add({
  // works and correctly stored as a timestamp in Firestore
  createdAt: new Date()
})

console.log(data[0].createdAt instance Date) // false
// actually an instance of FirestoreTimestamp

Since the type used is the one passed as a the generic Profile, the type of createdAt in data is wrong.

There is also one exception to this rule: if the cache is being used after add. In that case, there will be two potential types being used... since the cached items that are created via add use the Date types, whereas the cached items from firebase have the FirestoreTimestamp type.

This type mismatch also means that sorting does not work correctly.

@nandorojo
Copy link
Owner

This is due to how firestore handles dates. Use the parseDates field

@nandorojo
Copy link
Owner

Also, I believe instead of new Date() you should use a Firestore server time stamp, but maybe someone else can confirm that

@jckw
Copy link
Contributor Author

jckw commented Jul 9, 2021

Aha! I missed parseDates, thank you!

There still the problem of how the cache is organised. It seems unrelated to this issue though, since inserting at the end of the cache is hard-coded into:

return [...state, ...docsToAdd]

Perhaps best to move to another issue to discuss how cache updating options can be customised?

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