Skip to content

PreHook

@tsed/mongoose

Usage

typescript
import { PreHook } from "@tsed/mongoose";

See /packages/orm/mongoose/src/types/decorators/preHook.ts.

Overview

ts
function PreHook<T = any>(method: MongooseMethods, fn: MongoosePreHookCB<T>, options?: MongooseHookOptions): ClassDecorator;
export function PreHook<T = any>(method: MongooseMethods, options?: MongooseHookOptions): StaticMethodDecorator;

Description

We can simply attach a @PreHook 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 {PreHook, Model} from "@tsed/mongoose";

@Model()
@PreHook("save", (car: CarModel, next) => {
   if (car.model === 'Tesla') {
       car.isFast = true;
     }
     next();
})
export class CarModel {

   @Ignore()
   _id: string;

   @Required()
   model: string;

   @Required()
   isFast: boolean;

   // or Prehook on static method
   @PreHook("save")
   static preSave(car: CarModel, next) {
      if (car.model === 'Tesla') {
          car.isFast = true;
      }
      next();
   }
}

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

Released under the MIT License.