Hey, I am building a REST API in a OOP way
I have a Product entity. This entity contains currently only variables that are really needed for the Product. So stuff like: price, content, title, ... Relations like "owner" are not included to keep things clean.
Now when I fetch a list of products, that don't contain a "owner" relation, everything is fine and works. I create the objects and they get returned, serialized through zod and returned correctly.
But when I have a "get single product" route, the owner should return with it. Like I need to build a product object, that contains an owner (not owner: User | undefined like before, now it's: owner: User). I can easily extend the object, yes. And that works, yes. But that might not be the only relation that I have to another object. Maybe there are pictures in the future, maybe variants, ...
Best case would be, that I call setOwner(owner: User) and then the object itself knows that it contains an owner.
I am using typescript. How to deal with relations like that?
Builder (without returning Object): https://pastebin.com/3tNyjhrU