Skip to main content

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:

import { GtDocument, GtModel, GtTimestampCreated, GtTimestampUpdated } from '@pebula/goosetyped';
@GtDocument()
export class Customer extends GtModel() {
@GtColumn()
name: string;
@GtColumn()
age: number;
@GtTimestampCreated()
createDate: Date;
@GtTimestampUpdated()
updateDate: Date;
}
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:

export class TimestampMixin {
@GtTimestampCreated()
createDate: Date;
@GtTimestampUpdated()
updateDate: Date;
}

Now we can mix it into our models where needed:

@GtDocument()
export class Customer extends GtModel(TimestampMixin) {
@GtColumn()
name: string;
@GtColumn()
age: number;
updateDate: Date;
}

Ohh, we forgot, the Customer model also needs an Owner mixin:

export class OwnerMixin {
@GtColumn()
owner: string;
}

Now we can mix it into our models where needed:

@GtDocument()
export class Customer extends GtModel(TimestampMixin, OwnerMixin) {
@GtColumn()
name: string;
@GtColumn()
age: number;
updateDate: Date;
}