Open tomasweigenast opened 3 years ago
Hey there,
Sorry for responding so late.
We definitely understand the pain you have, and want to address this. However, we currently don't have a good idea of how to implement this gracefully.
We will keep this issue open while we search for solutions. In the meantime, if you have some concrete ideas, please share.
Like @TomasWeg said, also in my projects I only use converters to convert Timestamp into Date. I believe this is the most common use case for this kind of problem.
From the top of my head here are two options:
1. Timestamp - Date Conversion: Why can't we simply add a setting like convertTimestampToDate
to enable the conversion of Timestamp to Date? From a technical perspective I understand the advantages of using Timestamp, but firebase is so awesome because of its developer friendliness and usability. Therefore I'd love to see an easy way to work with Dates.
admin.firestore().settings({
convertTimestampToDate: true;
})
2. Converter for Firebase types: Setting global converters for firebase types like Timestamp, GeoPoint etc.
admin.firestore().settings({
converters: [{
type: 'Timestamp',
to: (timestamp: Timestamp) => timestamp.toDate(),
from: (date: Date) => Timestamp.fromDate(date)
}]
})
Incompatibility of Firebase Types: When we try to share backend and frontend types it's a big hassle right now when using firebase specific types. This is because of the incompatibility between nodejs and js-client library types.
Push
Strongly agree with @cedricdg . I'm about to write converters just about anywhere in my database abstraction for firebase: having this out of the box would have saved me a lot of time, and frankly I don't understand how this issue had so little traffic. Feels like just about every dev using firestore would stumble upon this at some point? What would be the recommended approach to dealing with the native type differences?
Hi all,
This is something we would like to do if we have capacity. The team is short handed at the moment, and unfortunately we do not know when we can work on this.
OTOH, PRs are always welcome if anyone feeling like helping other users :-)
The idea is to set global converters, maybe using
That can be reused along with all the requests and for individual types, for example, for
Timestamp
:I have many models where I use the Javascript Date object which gets converted to a
Timestamp
object, and when I get from the database, I cannot usesnapshot.data() as MyModel
, because ifMyModel
contains a Date property, it will be undefined since Timestamp cannot be converted directly to Date. This can be solved using a converter andwithConverter
, but it makes sense to have many converters only for a Date property? Why we can make it global usingFirestoreDataConverter<Date>
?