# PostHook Decorator

Module
import { PostHook } from "@tsed/mongoose"
Source/packages/mongoose/src/decorators/postHook.ts

# Overview

function PostHook(method: string, fn?: MongoosePostHookCB<any> | MongoosePostErrorHookCB<any>): Function;

# Description

We can simply attach a @PostHook decorator to your model class and define the hook function like you normally would in Mongoose.

import {IgnoreProperty, Required} from "@tsed/common";
import {PostHook, Model} from "@tsed/mongoose";

@Model()
@PostHook("save", (car: CarModel) => {
   if (car.topSpeedInKmH > 300) {
       console.log(car.model, 'is fast!');
   }
})
export class CarModel {
   @IgnoreProperty()
   _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!');
      }
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

This will execute the post-save hook each time a CarModel document is saved.