I’m trying to figure out the nightmare of dates with Firebase.
I declare my objects with types as follows:
export type FirebaseTimestamp = { _seconds: number, _nanoseconds: number }
type MyObject = {
createdAt: FirebaseTimestamp
}
These type configs are used both by my frontend (NextJS) and Cloud Functions from Firebase.
Please note, that all objects I retrieve from firestore
I convert to my type with the following:
const fetch = firestore.col().get()
const myObject: MyObject = {
id: fetch.id,
...fetch.data(),
}
If I change the FirebaseTimestamp
to any of the following like suggested by other questions:
import * as admin from "firebase-admin"
type FirebaseTimestamp = admin.firestore.Timestamp
It doesn’t work because the Firestore value is a number, not a function, so doesn’t work when I retrieve it from Firestore.
If I do it with the SDK
import {firestore} from "firestore"
type FirebaseTimestamp = admin.firestore.Timestamp
Same problem along with my backend complaining that it doesn’t know the library.
I just can’t get this to work. How can I define a basic date!?!
How can I type cast the timestamps? And how do I generate them both in the frontend using SDK and backend cloud functions?