Mixins
Mixins are the perfect tools to apply functionality and are very similar to mongoose plugins.
Let's say we want to add createDate
and updateDate
columns to our Customer
model:
The straight-forward approach would ne to use the @GtTimestampCreated
& @GtTimestampUpdated
decorators in the class:
tip
@GtTimestampCreated
& @GtTimestampUpdated
are utility decorators that expose mongoose's built-in timestamps
This is ok but we most probably want to add this capability to other models in our application but not all of them. We can use inheritance but this will limit our possibility of composing capabilities together...
Instead, we define a class (does not have to be a document or sub-document...) with the capability we want:
Now we can mix it into our models where needed:
Ohh, we forgot, the Customer
model also needs an Owner
mixin:
Now we can mix it into our models where needed: