You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+29
Original file line number
Diff line number
Diff line change
@@ -21,6 +21,7 @@ Decorators and some other extras for sequelize (v3 + v4).
21
21
-[Multiple relations of same models](#multiple-relations-of-same-models)
22
22
-[Model validation](#model-validation)
23
23
-[Scopes](#scopes)
24
+
-[Hooks](#hooks)
24
25
-[Why `() => Model`?](#user-content-why---model)
25
26
-[Recommendations and limitations](#recommendations-and-limitations)
26
27
@@ -552,6 +553,34 @@ export class ShoeWithScopes extends Model<ShoeWithScopes> {
552
553
}
553
554
```
554
555
556
+
## Hooks
557
+
Hooks can be attached to your models. All Model-level hooks are supported. See [the related unit tests](test/models/Hook.ts) for a summary.
558
+
559
+
Each hook must be a `static` method. Multiple hooks can be attached to a single method, and you can define multiple methods for a given hook.
560
+
561
+
The name of the method cannot be the same as the name of the hook (for example, a `@BeforeCreate` hook method cannot be named `beforeCreate`). That’s because Sequelize has pre-defined methods with those names.
562
+
563
+
```typescript
564
+
@Table
565
+
exportclassPersonextendsModel<Person> {
566
+
@Column
567
+
name:string;
568
+
569
+
@BeforeUpdate
570
+
@BeforeCreate
571
+
static makeUpperCase(instance:Person) {
572
+
// this will be called when an instance is created or updated
573
+
instance.name=instance.name.toLocaleUpperCase();
574
+
}
575
+
576
+
@BeforeCreate
577
+
static addUnicorn(instance:Person) {
578
+
// this will also be called when an instance is created
579
+
instance.name+=' 🦄';
580
+
}
581
+
}
582
+
```
583
+
555
584
## Why `() => Model`?
556
585
`@ForeignKey(Model)` is much easier to read, so why is `@ForeignKey(() => Model)` so important? When it
557
586
comes to circular-dependencies (which are in general solved by node for you) `Model` can be `undefined`
0 commit comments