googleapis / nodejs-firestore

Node.js client for Google Cloud Firestore: a NoSQL document database built for automatic scaling, high performance, and ease of application development.
https://cloud.google.com/firestore/
Apache License 2.0
642 stars 149 forks source link

Global converters for different types #1408

Open tomasweigenast opened 3 years ago

tomasweigenast commented 3 years ago

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> ?

wu-hui commented 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.

cedricdg commented 3 years ago

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.

inf0rmatix-old commented 3 years ago

Push

Brucbbro commented 2 years ago

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?

wu-hui commented 2 years ago

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