Skip to content

Commit 93f5c24

Browse files
Merge pull request #101 from natesilva/feature/hooks
feature(hooks): add decorators for declaring hooks
2 parents b1cfa77 + 2fa0702 commit 93f5c24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+998
-0
lines changed

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Decorators and some other extras for sequelize (v3 + v4).
2121
- [Multiple relations of same models](#multiple-relations-of-same-models)
2222
- [Model validation](#model-validation)
2323
- [Scopes](#scopes)
24+
- [Hooks](#hooks)
2425
- [Why `() => Model`?](#user-content-why---model)
2526
- [Recommendations and limitations](#recommendations-and-limitations)
2627

@@ -552,6 +553,34 @@ export class ShoeWithScopes extends Model<ShoeWithScopes> {
552553
}
553554
```
554555

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+
export class Person extends Model<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+
555584
## Why `() => Model`?
556585
`@ForeignKey(Model)` is much easier to read, so why is `@ForeignKey(() => Model)` so important? When it
557586
comes to circular-dependencies (which are in general solved by node for you) `Model` can be `undefined`

index.ts

+34
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,40 @@ export {NotIn} from "./lib/annotations/validation/NotIn";
5151
export {NotNull} from "./lib/annotations/validation/NotNull";
5252
export {Validate} from "./lib/annotations/validation/Validate";
5353

54+
// hooks
55+
export {BeforeValidate} from "./lib/annotations/hooks/BeforeValidate";
56+
export {AfterValidate} from "./lib/annotations/hooks/AfterValidate";
57+
export {ValidationFailed} from "./lib/annotations/hooks/ValidationFailed";
58+
export {BeforeCreate} from "./lib/annotations/hooks/BeforeCreate";
59+
export {AfterCreate} from "./lib/annotations/hooks/AfterCreate";
60+
export {BeforeDestroy} from "./lib/annotations/hooks/BeforeDestroy";
61+
export {AfterDestroy} from "./lib/annotations/hooks/AfterDestroy";
62+
export {BeforeRestore} from "./lib/annotations/hooks/BeforeRestore";
63+
export {AfterRestore} from "./lib/annotations/hooks/AfterRestore";
64+
export {BeforeUpdate} from "./lib/annotations/hooks/BeforeUpdate";
65+
export {AfterUpdate} from "./lib/annotations/hooks/AfterUpdate";
66+
export {BeforeSave} from "./lib/annotations/hooks/BeforeSave";
67+
export {AfterSave} from "./lib/annotations/hooks/AfterSave";
68+
export {BeforeUpsert} from "./lib/annotations/hooks/BeforeUpsert";
69+
export {AfterUpsert} from "./lib/annotations/hooks/AfterUpsert";
70+
export {BeforeBulkCreate} from "./lib/annotations/hooks/BeforeBulkCreate";
71+
export {AfterBulkCreate} from "./lib/annotations/hooks/AfterBulkCreate";
72+
export {BeforeBulkDestroy} from "./lib/annotations/hooks/BeforeBulkDestroy";
73+
export {AfterBulkDestroy} from "./lib/annotations/hooks/AfterBulkDestroy";
74+
export {BeforeBulkRestore} from "./lib/annotations/hooks/BeforeBulkRestore";
75+
export {AfterBulkRestore} from "./lib/annotations/hooks/AfterBulkRestore";
76+
export {BeforeBulkUpdate} from "./lib/annotations/hooks/BeforeBulkUpdate";
77+
export {AfterBulkUpdate} from "./lib/annotations/hooks/AfterBulkUpdate";
78+
export {BeforeFind} from "./lib/annotations/hooks/BeforeFind";
79+
export {BeforeFindAfterExpandIncludeAll} from "./lib/annotations/hooks/BeforeFindAfterExpandIncludeAll";
80+
export {BeforeFindAfterOptions} from "./lib/annotations/hooks/BeforeFindAfterOptions";
81+
export {AfterFind} from "./lib/annotations/hooks/AfterFind";
82+
export {BeforeCount} from "./lib/annotations/hooks/BeforeCount";
83+
export {BeforeDelete} from "./lib/annotations/hooks/BeforeDelete";
84+
export {AfterDelete} from "./lib/annotations/hooks/AfterDelete";
85+
export {BeforeBulkDelete} from "./lib/annotations/hooks/BeforeBulkDelete";
86+
export {AfterBulkDelete} from "./lib/annotations/hooks/AfterBulkDelete";
87+
5488
// interfaces
5589
export {IAssociationActionOptions} from "./lib/interfaces/IAssociationActionOptions";
5690
export {IBuildOptions} from "./lib/interfaces/IBuildOptions";
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkCreate(target: any, propertyName: string): void;
5+
export function AfterBulkCreate(options: IHookOptions): Function;
6+
export function AfterBulkCreate(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkCreate', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkDelete(target: any, propertyName: string): void;
5+
export function AfterBulkDelete(options: IHookOptions): Function;
6+
export function AfterBulkDelete(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkDelete', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkDestroy(target: any, propertyName: string): void;
5+
export function AfterBulkDestroy(options: IHookOptions): Function;
6+
export function AfterBulkDestroy(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkDestroy', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkRestore(target: any, propertyName: string): void;
5+
export function AfterBulkRestore(options: IHookOptions): Function;
6+
export function AfterBulkRestore(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkRestore', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkSync(target: any, propertyName: string): void;
5+
export function AfterBulkSync(options: IHookOptions): Function;
6+
export function AfterBulkSync(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkSync', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterBulkUpdate(target: any, propertyName: string): void;
5+
export function AfterBulkUpdate(options: IHookOptions): Function;
6+
export function AfterBulkUpdate(...args: any[]): void|Function {
7+
return implementHookDecorator('afterBulkUpdate', args);
8+
}

lib/annotations/hooks/AfterConnect.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterConnect(target: any, propertyName: string): void;
5+
export function AfterConnect(options: IHookOptions): Function;
6+
export function AfterConnect(...args: any[]): void|Function {
7+
return implementHookDecorator('afterConnect', args);
8+
}

lib/annotations/hooks/AfterCreate.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterCreate(target: any, propertyName: string): void;
5+
export function AfterCreate(options: IHookOptions): Function;
6+
export function AfterCreate(...args: any[]): void|Function {
7+
return implementHookDecorator('afterCreate', args);
8+
}

lib/annotations/hooks/AfterDefine.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterDefine(target: any, propertyName: string): void;
5+
export function AfterDefine(options: IHookOptions): Function;
6+
export function AfterDefine(...args: any[]): void|Function {
7+
return implementHookDecorator('afterDefine', args);
8+
}

lib/annotations/hooks/AfterDelete.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterDelete(target: any, propertyName: string): void;
5+
export function AfterDelete(options: IHookOptions): Function;
6+
export function AfterDelete(...args: any[]): void|Function {
7+
return implementHookDecorator('afterDelete', args);
8+
}

lib/annotations/hooks/AfterDestroy.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterDestroy(target: any, propertyName: string): void;
5+
export function AfterDestroy(options: IHookOptions): Function;
6+
export function AfterDestroy(...args: any[]): void|Function {
7+
return implementHookDecorator('afterDestroy', args);
8+
}

lib/annotations/hooks/AfterFind.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterFind(target: any, propertyName: string): void;
5+
export function AfterFind(options: IHookOptions): Function;
6+
export function AfterFind(...args: any[]): void|Function {
7+
return implementHookDecorator('afterFind', args);
8+
}

lib/annotations/hooks/AfterInit.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterInit(target: any, propertyName: string): void;
5+
export function AfterInit(options: IHookOptions): Function;
6+
export function AfterInit(...args: any[]): void|Function {
7+
return implementHookDecorator('afterInit', args);
8+
}

lib/annotations/hooks/AfterRestore.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterRestore(target: any, propertyName: string): void;
5+
export function AfterRestore(options: IHookOptions): Function;
6+
export function AfterRestore(...args: any[]): void|Function {
7+
return implementHookDecorator('afterRestore', args);
8+
}

lib/annotations/hooks/AfterSave.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterSave(target: any, propertyName: string): void;
5+
export function AfterSave(options: IHookOptions): Function;
6+
export function AfterSave(...args: any[]): void|Function {
7+
return implementHookDecorator('afterSave', args);
8+
}

lib/annotations/hooks/AfterSync.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterSync(target: any, propertyName: string): void;
5+
export function AfterSync(options: IHookOptions): Function;
6+
export function AfterSync(...args: any[]): void|Function {
7+
return implementHookDecorator('afterSync', args);
8+
}

lib/annotations/hooks/AfterUpdate.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterUpdate(target: any, propertyName: string): void;
5+
export function AfterUpdate(options: IHookOptions): Function;
6+
export function AfterUpdate(...args: any[]): void|Function {
7+
return implementHookDecorator('afterUpdate', args);
8+
}

lib/annotations/hooks/AfterUpsert.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterUpsert(target: any, propertyName: string): void;
5+
export function AfterUpsert(options: IHookOptions): Function;
6+
export function AfterUpsert(...args: any[]): void|Function {
7+
return implementHookDecorator('afterUpsert', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function AfterValidate(target: any, propertyName: string): void;
5+
export function AfterValidate(options: IHookOptions): Function;
6+
export function AfterValidate(...args: any[]): void|Function {
7+
return implementHookDecorator('afterValidate', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkCreate(target: any, propertyName: string): void;
5+
export function BeforeBulkCreate(options: IHookOptions): Function;
6+
export function BeforeBulkCreate(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkCreate', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkDelete(target: any, propertyName: string): void;
5+
export function BeforeBulkDelete(options: IHookOptions): Function;
6+
export function BeforeBulkDelete(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkDelete', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkDestroy(target: any, propertyName: string): void;
5+
export function BeforeBulkDestroy(options: IHookOptions): Function;
6+
export function BeforeBulkDestroy(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkDestroy', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkRestore(target: any, propertyName: string): void;
5+
export function BeforeBulkRestore(options: IHookOptions): Function;
6+
export function BeforeBulkRestore(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkRestore', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkSync(target: any, propertyName: string): void;
5+
export function BeforeBulkSync(options: IHookOptions): Function;
6+
export function BeforeBulkSync(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkSync', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeBulkUpdate(target: any, propertyName: string): void;
5+
export function BeforeBulkUpdate(options: IHookOptions): Function;
6+
export function BeforeBulkUpdate(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeBulkUpdate', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeConnect(target: any, propertyName: string): void;
5+
export function BeforeConnect(options: IHookOptions): Function;
6+
export function BeforeConnect(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeConnect', args);
8+
}

lib/annotations/hooks/BeforeCount.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeCount(target: any, propertyName: string): void;
5+
export function BeforeCount(options: IHookOptions): Function;
6+
export function BeforeCount(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeCount', args);
8+
}

lib/annotations/hooks/BeforeCreate.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeCreate(target: any, propertyName: string): void;
5+
export function BeforeCreate(options: IHookOptions): Function;
6+
export function BeforeCreate(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeCreate', args);
8+
}

lib/annotations/hooks/BeforeDefine.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeDefine(target: any, propertyName: string): void;
5+
export function BeforeDefine(options: IHookOptions): Function;
6+
export function BeforeDefine(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeDefine', args);
8+
}

lib/annotations/hooks/BeforeDelete.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeDelete(target: any, propertyName: string): void;
5+
export function BeforeDelete(options: IHookOptions): Function;
6+
export function BeforeDelete(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeDelete', args);
8+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeDestroy(target: any, propertyName: string): void;
5+
export function BeforeDestroy(options: IHookOptions): Function;
6+
export function BeforeDestroy(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeDestroy', args);
8+
}

lib/annotations/hooks/BeforeFind.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeFind(target: any, propertyName: string): void;
5+
export function BeforeFind(options: IHookOptions): Function;
6+
export function BeforeFind(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeFind', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeFindAfterExpandIncludeAll(target: any, propertyName: string): void;
5+
export function BeforeFindAfterExpandIncludeAll(options: IHookOptions): Function;
6+
export function BeforeFindAfterExpandIncludeAll(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeFindAfterExpandIncludeAll', args);
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeFindAfterOptions(target: any, propertyName: string): void;
5+
export function BeforeFindAfterOptions(options: IHookOptions): Function;
6+
export function BeforeFindAfterOptions(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeFindAfterOptions', args);
8+
}

lib/annotations/hooks/BeforeInit.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import {IHookOptions} from "../../interfaces/IHookOptions";
2+
import {implementHookDecorator} from "../../services/hooks";
3+
4+
export function BeforeInit(target: any, propertyName: string): void;
5+
export function BeforeInit(options: IHookOptions): Function;
6+
export function BeforeInit(...args: any[]): void|Function {
7+
return implementHookDecorator('beforeInit', args);
8+
}

0 commit comments

Comments
 (0)