I'm wondering if there is some best-practice for how to give behavior to object received from the outside (a db, the network, etc) in javascript (nodejs).
For instance, assume I have some getById(<Id>) function which fetch the right object from a db, how would you add a savemethod on the object returned by this function?
Here is what I would like to have:
var getById = function (id) {
var obj = callSomeExternalCode(id);
return obj;
};
var myObj = getById('1234');
myObj.aProperty = 'a_new_value';
myObj.save();
I would prefer to avoid pseudo-classical solution (anyway, I may adapt it to my style if you can provide me with a good one)
Edit: I forgot to mention, it would be very nice to have these methods defined in a separate 'parent' object (even if, I suppose, proto-inheritance is not the answer)
Thanks