wovalle / fireorm

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

Serialization doesn't work when a document contains a reference #95

Closed cjmyles closed 5 years ago

cjmyles commented 5 years ago

I know that there is a plan to add relationship support to Fireorm (https://github.com/wovalle/fireorm/issues/32), however I noticed that adding a reference to a document causes an error.

I copied the example from this Medium article as follows which works fine:

@Collection()
class Band {
    id!: string;
    name!: string;
    formationYear!: number;
    genres!: Array<string>;
}

// Create a band
const band = new Band();
band.name = "A Perfect Circle";
band.formationYear = 1999;
band.genres = ['alternative-rock', 'hard-rock']

const bandDocument = await bandRepository.create(band);
console.log(`Created band with id: ${band.id}`);

As soon as I go into the Firebase Console, locate this document and then add a new field which is a reference to another collection I get the following error:

TypeError: Cannot read property '_settings' of undefined
    at new Serializer (/node_modules/@google-cloud/firestore/build/src/serializer.js:46:23)
    at TransformOperationExecutor.transform 

Is this right, or is there any easy way to fix it? I have an existing Firestore database that contains references so I need Fireorm to fail gracefully with references until it's fully supported.

P.S I assume you are using TypeORM as a reference for adding entity relations?

wovalle commented 5 years ago

Hey!

Yes, I actually found a similar bug when working on #82 which itself is contained in #58. The problem is that fireorm doesn't know how to instantiate the custom Reference class returned by firestore and it throws an error.

The temporary/hack solution is to do something similar of what I did to support geopoints (which is using the @Type Decorator to tell fireorm how to parse the class, that's explained here). The long term solution is that fireorm know how to parse every firestore class and provide you a way to tell how to parse the things that it doesn't know, but that must wait until #58.

I have limited internet connectivity until the weekend, I'll check deeper then.

cjmyles commented 5 years ago

Hola! Thanks for the quick reply.

I'm happy with the temporary/hack solution, and seems like it should be easy enough to implement, however I'm still getting the same serialisation error with this code:

class Pillar { }

@Collection()
class Band {
    id!: string;
    name!: string;
    formationYear!: number;
    genres!: Array<string>;
    @Type(() => Pillar) pillar!: Pillar;
}

Where pillar is my Firebase reference to another collection. Any idea what I'm doing wrong?

wovalle commented 5 years ago

Hey! I was quite busy last week, I'll try to take a look at this in the coming days :)

wovalle commented 5 years ago

Hey!

I took a look at this issue and I'm afraid it'll a bit more cumbersome that I was expecting.

1st question: How do you add a "reference" field? I was looking in Firebase Console but "reference" is not inside the field types see image.

2nd Q: Adding a reference via the console makes the query to return a CollectionReference or a [Document Reference]()?

cjmyles commented 5 years ago

Hey, thanks for taking a look at this.

The "reference" type is actually underneath the "geopoint" entry in that dropdown - you just have to scroll down. Because the scrollbar isn't visible on chrome it isn't entirely obvious that it exists!

With regards to the reference, it's a DocumentReference type.

wovalle commented 5 years ago

Oh, didn't see the scrollbar. Will check it again, thanks!

wovalle commented 5 years ago

Hey! This was closed in #105. Now fireorm wont throw if it finds a DocumentReference! See the integration test to see how to use it!

PD. Don't forget to use @Type! (L11-12)

cjmyles commented 5 years ago

Amazing thanks dude! Quick work. Will check it out and let you know if I experience any issues.

anhzf commented 3 years ago

Hey! This was closed in #105. Now fireorm wont throw if it finds a DocumentReference! See the integration test to see how to use it!

PD. Don't forget to use @type! (L11-12)

I'm sorry but I can't see it, please explain it in the documentation too. Because my problem isn't still solved, I'm working with vue then it says Maximum call stack size exceeded