Just curious, the documentation for mapToClass() states:
"Given constructor function wont be invoked for instances returned from database."
What is the reason for this?
It would put requirements on the classes to always support a 'default constructor' which could make it impossible to map to classes that expect arguments to its constructor. Also, I couldn't find a valid use case for it given that the db object already contains all properties so there would be nothing that a default constructor would contribute with in practice.
I could be missing something, since the more JavaScript I learn, the less I know :) ... but for my use case, I needed the returned object to be actual instances so that I could call their methods do some further initialization. I just instantiated them manually, but wondered why mapToClass() wouldn't do it for me. I'll rethink my approach though, maybe there's another way.
Thanks for taking the time to explain!
To explain a bit more... these particular objects each contain an array of another object which also needs to inherit the prototype of a different model. I thought a convenience place to instantiate those objects was in the constructor of the parent, but as mentioned, the constructor isn't called.
I'll just stick with instantiating them in the toArray() promise callback.
Ok, I get it. mapToClass lacks some OO features such as aggregation of other classes as well as polymorfism (mapping to multiple classes for same table). And in your case this forces you to manually accomplish the aggregation part. If it would help your case, you could force constructor invokation using the following piece of code:
table.hook('reading', obj => ClassConstructor.call(obj));
Thanks for sharing your use case!
Most helpful comment
Ok, I get it. mapToClass lacks some OO features such as aggregation of other classes as well as polymorfism (mapping to multiple classes for same table). And in your case this forces you to manually accomplish the aggregation part. If it would help your case, you could force constructor invokation using the following piece of code:
Thanks for sharing your use case!