Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How are people dealing with the Timestamp values that come from Firestore? #381

devth started this conversation in Ideas
Discussion options

In my experience I always need to convert firebase.firestore.Timestamps to Dates so I can work with JS date libs, like date-fns. That means I end up with annoying/gross pollutions like:

 // in a component
 const commentsRef = post.ref.collection("comments");
 const { data: commentsData } = useFirestoreCollectionData<PostComment>(
 commentsRef,
 {
 idField: "id",
 }
 );
 const comments = commentsData ? commentsData.map(shallowTSToDates) : [];
// utils
export const firestoreTsToDate = ({
 seconds,
 _seconds,
 nanoseconds,
}: SerializedTs): Date =>
 new Timestamp(seconds || _seconds || 0, nanoseconds).toDate();
// for every value in a map, if the value looks like a Firestore Timestamp,
// convert it to a JS Date
export const shallowTSToDates = <T extends Record<string, unknown>>(
 object?: T
): T =>
 mapValues(object, (v) =>
 isNumber((v as any)?.seconds) && isNumber((v as any)?.nanoseconds)
 ? firestoreTsToDate(v as SerializedTs)
 : v
 ) as T;

💡 Would it make sense for reactfire's hooks to have the option to do this automatically?

Maybe something like:

 // in a component
 const commentsRef = post.ref.collection("comments");
 const { data: commentsData } = useFirestoreCollectionData<PostComment>(
 commentsRef,
 {
 idField: "id",
 convertTimestamps: true,
 }
 );

(Sidenote: I really want to be able to specify those ReactFireOptions globally once, because I'd always use the exact same options everywhere).

You must be logged in to vote

Replies: 2 comments

Comment options

Anyone have thoughts on this? I'd be happy to brainstorm, contrib something, etc. Seems like low hanging fruit with a potential big increase in developer experience.

You must be logged in to vote
0 replies
Comment options

100% would be nice to have a nice approach to do this

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Ideas
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /