nodejs-firestore icon indicating copy to clipboard operation
nodejs-firestore copied to clipboard

Global converters for different types

Open tomasweigenast opened this issue 3 years ago • 5 comments

The idea is to set global converters, maybe using

admin.firestore().settings({
    converters: []
})

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 use snapshot.data() as MyModel, because if MyModel contains a Date property, it will be undefined since Timestamp cannot be converted directly to Date. This can be solved using a converter and withConverter, but it makes sense to have many converters only for a Date property? Why we can make it global using FirestoreDataConverter<Date> ?

tomasweigenast avatar Feb 03 '21 12:02 tomasweigenast

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.

wu-hui avatar Mar 09 '21 19:03 wu-hui

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.

cedricdg avatar Apr 19 '21 11:04 cedricdg

Push

inf0rmatix-old avatar Jul 07 '21 07:07 inf0rmatix-old

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?

Brucbbro avatar Jan 11 '22 08:01 Brucbbro

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 :-)

wu-hui avatar Jan 17 '22 16:01 wu-hui