ziflex/compose-record

Not supporting nested object records of same type.

venk120soft opened this issue · 2 comments

We are working on implementing the n-level scenario. where in we have a property of same type as parent record .

Ex:

const UserA= compose({
   name:"Users",
  properties: {
        users: {
            type: List,
            items: {
                type: UserB,
            },
        },
    },
}

const UserB= compose({
   name:"Users",
  properties: {
        users: {
            type: List,
            items: {
                type: UserA,
            },
        },
    },
}

The above scenario is working well until the users list in UserB is empty. once the users are added into UserB its throwing factory error, seems its not working recursively.

If we change the type with any by keeping the single object. its working well.

I think it's because at the time of creation UserA type, variable UserB is undefined.

Try this

            const UserA = compose({
                name: 'Users',
                properties: {
                    users: {
                        type: List,
                        items: {
                            type: compose.factory((i) => new UserB(i)),
                        },
                    },
                },
            });

            const UserB: any = compose({
                name: 'Users',
                properties: {
                    users: {
                        type: List,
                        items: {
                            type: UserA,
                        },
                    },
                },
            });

It will do a late binding, so at the time of instance initialization, variable UserB will be already defined.

Thank you its worked, in my case, not even required the userB model. Instead creating the new userA object inside the composer factory is working.

const UserA = compose({
                name: 'Users',
                properties: {
                    users: {
                        type: List,
                        items: {
                            type: compose.factory((i) => new UserA(i)),
                        },
                    },
                },
> });