firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firestore doesn't support JavaScript objects with custom prototypes

Open nomagick opened this issue 1 year ago • 2 comments

Firebase SDK Product:

Firestore

Describe the problem

Firestore doesn't support JavaScript objects with custom prototypes.

"Doesn't support JavaScript objects with custom prototypes" means it would not work with custom classes.

This disability had caused mass regression in code quality.

Could you simply call toJSON() of any unknown object or pick its own properties like a normal person would do?

nomagick avatar Jun 26 '24 08:06 nomagick

We explicitly decided to not support serialization of custom classes for the Web and Node.JS client as we won't be able to deserialize back into these types. I will happily bring up any suggestions on how to improve this for team discussion. I think allowing writes of custom classes will create unexpected results for some developers when trying to use data read back from Firestore.

The functionality you are looking for can be found in converters. See: Custom objects

tom-andersen avatar Jun 26 '24 17:06 tom-andersen

As a library it needs to be neutral to developer expertise level. It should not expect that the developer would not really know what they are doing and dictate a "safe" way.

More importantly, it needs to be upward compatible with the project, if someone tries to improve code quality and manage the code towards better maintainability and readability.

In the case of Javascript, the native JSON.stringify and JSON.parse already demonstrated the way of doing serialization and deserialization in Javascript world, also setting a baseline of doing so. People would expect similar behavior when they see a serialize / deserialize feature.

The custom classes are all subclasses of Object, you should treat them the same way as plain Objects, instead of refusing to process them.

That is, only export the enumerable ownProperties of an object when serializing, and create simple object when deserializing. You can also refer to MongoDB driver and see how they serialize and deserialize objects.

nomagick avatar Jun 27 '24 04:06 nomagick

I'll take your suggestion up for discussion with the team.

tom-andersen avatar Jul 02 '24 13:07 tom-andersen

This has been discussed before:

https://github.com/firebase/firebase-js-sdk/issues/476 https://github.com/googleapis/nodejs-firestore/issues/143

Our reasoning remains, in particular what you proposed does not solve the concerns raised in https://github.com/firebase/firebase-js-sdk/issues/476.

What works for other tools might not be a good fit for our product, and we will not support this feature unless we have good answers to the concerns we had. Also, you can do achieve you want with converters and customize however you want to serialize/deserialize custom prototypes, you are not limited AFAICT.

One more note: you are welcome to make feature request and propose designs to help us implement this feature. But please keep your comments respectful with your future communications. Please refer to our code of conduct: https://github.com/firebase/.github/blob/c189bc7785a73039b6002050777a2dbea624f38c/CODE_OF_CONDUCT.md

wu-hui avatar Jul 02 '24 18:07 wu-hui