Firestore does not support JavaScript objects with custom prototypes, such as those created via the new operator. This limitation causes issues when trying to save instances of classes with custom prototypes to Firestore. A workaround is needed to serialize these objects correctly before storing them in Firestore.
Steps to Reproduce
Create a class with custom prototypes.
Attempt to save an instance of this class to Firestore using Fireorm.
Observe the error stating that Firestore does not support objects with custom prototypes.
Expected Behavior
Ability to save instances of classes with custom prototypes to Fireorm, with proper serialization to comply with Firestore's requirements.
Actual Behavior
Firestore throws an error when attempting to save objects with custom prototypes.
Acceptance Criteria
Implement a serialization mechanism to convert objects with custom prototypes to plain objects before saving to Firestore.
Ensure compatibility with Fireorm's existing functionality.
Add unit tests to validate the correct serialization of complex data types.
Additional Context
March 2, 2021: Initial issue raised about problems with complex data types.
March 8, 2021: Acknowledgment of the issue and request for a minimal reproduction repo.
March 11, 2021: Discussion about Firestore's limitation and suggestion to serialize objects to plain objects.
April 27, 2021: Mention of a pull request potentially solving the issue.
May 11, 2021: Clarification on the need for a generic mapping between Firestore "primitives" and fields in custom models.
Proposed API Changes
Serialize Entities to Plain Objects:
Implement a mechanism to serialize entities to plain objects before saving to Firestore.
import { plainToClass, classToPlain } from 'class-transformer';
async function saveEntity(entity: any) {
const plainObject = classToPlain(entity);
await firestore.collection('collectionName').add(plainObject);
}
Support for Custom Prototypes:
Ensure the serialization mechanism supports custom prototypes and complex data types.
class GeoP {
latitude: number;
longitude: number;
}
@Collection()
class SomeModel {
@Type(() => GeoP)
geop: GeoP;
}
const someGeoPInstance = new GeoP();
someGeoPInstance.latitude = 19.3753433;
someGeoPInstance.longitude = -99.0438667;
const someModelInstance = new SomeModel();
someModelInstance.geop = someGeoPInstance;
saveEntity(someModelInstance);
Unit Tests:
Add unit tests to validate the serialization of complex data types with custom prototypes.
Description
Firestore does not support JavaScript objects with custom prototypes, such as those created via the
new
operator. This limitation causes issues when trying to save instances of classes with custom prototypes to Firestore. A workaround is needed to serialize these objects correctly before storing them in Firestore.Steps to Reproduce
Expected Behavior
Ability to save instances of classes with custom prototypes to Fireorm, with proper serialization to comply with Firestore's requirements.
Actual Behavior
Firestore throws an error when attempting to save objects with custom prototypes.
Acceptance Criteria
Additional Context
Proposed API Changes
Serialize Entities to Plain Objects:
Support for Custom Prototypes:
Unit Tests:
Example Implementation
Original Issue