wovalle / fireorm

ORM for firestore 🔥
https://fireorm.js.org
MIT License
558 stars 75 forks source link

Complex data types couldn't serialize #241

Open sc-cmendoza opened 3 years ago

sc-cmendoza commented 3 years ago

I had read the documentation about complex data types, however I can't make it works.

I have this:


class GeoP{
    latitude: number;
    longitude: number;
}

@Collection()
class SomeModel{
    @Type(() => GeoP)
    geop: GeoP;
}

And then I do this:

const someGeoPInstance = new GeoP();
someGeoPInstance.latitude = 19.3753433;
someGeoPInstance.longitud = -99.0438667;

const someModelInstance = new SomeModel();
someModelInstance.geop = someGeoPInstance;

getRepository(SomeModel).create(someModelInstance);

And then I get this

Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "GeoP" (found in field "geop"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).

Is there something I'm misunderstanding?

wovalle commented 3 years ago

Hey! I think tit should work.

Can you create a minimal repo reproducing the issue so I can check it out?

skalashnyk commented 3 years ago

This error is thrown by firestore sdk

import * as admin from 'firebase-admin';

class Hello {
}

admin.firestore()
  .collection('foo')
  .doc('test_1')
  .set({
    prop: new Hello()
  })
  .then(console.log)
  .catch(console.error);

Output:

Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "Hello" (found in field "prop"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
    at validateUserInput ...

@wovalle , it can be fixed by serializing entity to plain object before passing to firestore SDK, e.g. JSON.parse(JSON.stringify(doc))

leandrogehlen commented 3 years ago

This is not necessary.

Please read docs: https://fireorm.js.org/#/README?id=usage

wovalle commented 3 years ago

I'll take a look at your suggestion @skalashnyk

wovalle commented 3 years ago

I'll take a look at your suggestion @skalashnyk

danieleisenhardt commented 3 years ago

If I'm not mistaken, my pull request (https://github.com/wovalle/fireorm/pull/251) solves this issue.

wovalle commented 3 years ago

Does it? My initial idea with this issue was to create a generic mapping between firestore "primitives" and fields in custom models.

Can you provide an example of how would you map a native Reference or GeoPoint with the serialization included in the PR? @danieleisenhardt