PostHook
@tsed/mongoose
Usage
typescript
import { PostHook } from "@tsed/mongoose";
See /packages/orm/mongoose/src/types/decorators/postHook.ts.
Overview
ts
function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>): ClassDecorator;
export function PostHook<T = any>(method: string, fn: MongoosePostHookCB<T>, options: MongooseHookOptions): ClassDecorator;
export function PostHook<T = any>(method: string, options: MongooseHookOptions): StaticMethodDecorator;
Description
We can simply attach a @PostHook
decorator to your model class and define the hook function like you normally would in Mongoose.
typescript
import {Ignore, Required} from "@tsed/platform-http";
import {PostHook, Model} from "@tsed/mongoose";
@Model()
@PostHook("save", (car: CarModel) => {
if (car.topSpeedInKmH > 300) {
console.log(car.model, 'is fast!');
}
})
export class CarModel {
@Ignore()
_id: string;
@Required()
model: string;
@Required()
isFast: boolean;
// or Prehook on static method
@PostHook("save")
static postSave(car: CarModel) {
if (car.topSpeedInKmH > 300) {
console.log(car.model, 'is fast!');
}
}
}
This will execute the post-save hook each time a CarModel
document is saved.