# PreHook Decorator class
Module | import { PreHook } from "@tsed/mongoose" |
---|---|
Source | /packages/mongoose/src/decorators/preHook.ts |
# Overview
function PreHook(method: string, fn?: MongoosePreHookSyncCB<any> | MongoosePreHookAsyncCB<any> | PreHookOptions, options?: PreHookOptions): Function;
# Description
We can simply attach a @PreHook
decorator to your model class and
define the hook function like you normally would in Mongoose.
import {Ignore, Required} from "@tsed/common";
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();
}
}
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
28
29
30
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
28
29
30
This will execute the pre-save hook each time a CarModel
document is saved.
- Session & cookies
- Passport.js
- TypeORM
- Mongoose
- GraphQL
- Socket.io
- Swagger
- AJV
- Multer
- Serve static files
- Templating
- Throw HTTP Exceptions
- Customize 404
- AWS
- Jest
- Seq
- Controllers
- Providers
- Model
- Converters
- Middlewares
- Pipes
- Interceptors
- Authentication
- Hooks
- Injection scopes
- Custom providers
- Custom endpoint decorator
- Testing