Skip to content

feature(hooks): add decorators for declaring hooks #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Decorators and some other extras for sequelize (v3 + v4).
- [Multiple relations of same models](#multiple-relations-of-same-models)
- [Model validation](#model-validation)
- [Scopes](#scopes)
- [Hooks](#hooks)
- [Why `() => Model`?](#user-content-why---model)
- [Recommendations and limitations](#recommendations-and-limitations)

Expand Down Expand Up @@ -552,6 +553,34 @@ export class ShoeWithScopes extends Model<ShoeWithScopes> {
}
```

## Hooks
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.

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.

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.

```typescript
@Table
export class Person extends Model<Person> {
@Column
name: string;

@BeforeUpdate
@BeforeCreate
static makeUpperCase(instance: Person) {
// this will be called when an instance is created or updated
instance.name = instance.name.toLocaleUpperCase();
}

@BeforeCreate
static addUnicorn(instance: Person) {
// this will also be called when an instance is created
instance.name += ' 🦄';
}
}
```

## Why `() => Model`?
`@ForeignKey(Model)` is much easier to read, so why is `@ForeignKey(() => Model)` so important? When it
comes to circular-dependencies (which are in general solved by node for you) `Model` can be `undefined`
Expand Down
34 changes: 34 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,40 @@ export {NotIn} from "./lib/annotations/validation/NotIn";
export {NotNull} from "./lib/annotations/validation/NotNull";
export {Validate} from "./lib/annotations/validation/Validate";

// hooks
export {BeforeValidate} from "./lib/annotations/hooks/BeforeValidate";
export {AfterValidate} from "./lib/annotations/hooks/AfterValidate";
export {ValidationFailed} from "./lib/annotations/hooks/ValidationFailed";
export {BeforeCreate} from "./lib/annotations/hooks/BeforeCreate";
export {AfterCreate} from "./lib/annotations/hooks/AfterCreate";
export {BeforeDestroy} from "./lib/annotations/hooks/BeforeDestroy";
export {AfterDestroy} from "./lib/annotations/hooks/AfterDestroy";
export {BeforeRestore} from "./lib/annotations/hooks/BeforeRestore";
export {AfterRestore} from "./lib/annotations/hooks/AfterRestore";
export {BeforeUpdate} from "./lib/annotations/hooks/BeforeUpdate";
export {AfterUpdate} from "./lib/annotations/hooks/AfterUpdate";
export {BeforeSave} from "./lib/annotations/hooks/BeforeSave";
export {AfterSave} from "./lib/annotations/hooks/AfterSave";
export {BeforeUpsert} from "./lib/annotations/hooks/BeforeUpsert";
export {AfterUpsert} from "./lib/annotations/hooks/AfterUpsert";
export {BeforeBulkCreate} from "./lib/annotations/hooks/BeforeBulkCreate";
export {AfterBulkCreate} from "./lib/annotations/hooks/AfterBulkCreate";
export {BeforeBulkDestroy} from "./lib/annotations/hooks/BeforeBulkDestroy";
export {AfterBulkDestroy} from "./lib/annotations/hooks/AfterBulkDestroy";
export {BeforeBulkRestore} from "./lib/annotations/hooks/BeforeBulkRestore";
export {AfterBulkRestore} from "./lib/annotations/hooks/AfterBulkRestore";
export {BeforeBulkUpdate} from "./lib/annotations/hooks/BeforeBulkUpdate";
export {AfterBulkUpdate} from "./lib/annotations/hooks/AfterBulkUpdate";
export {BeforeFind} from "./lib/annotations/hooks/BeforeFind";
export {BeforeFindAfterExpandIncludeAll} from "./lib/annotations/hooks/BeforeFindAfterExpandIncludeAll";
export {BeforeFindAfterOptions} from "./lib/annotations/hooks/BeforeFindAfterOptions";
export {AfterFind} from "./lib/annotations/hooks/AfterFind";
export {BeforeCount} from "./lib/annotations/hooks/BeforeCount";
export {BeforeDelete} from "./lib/annotations/hooks/BeforeDelete";
export {AfterDelete} from "./lib/annotations/hooks/AfterDelete";
export {BeforeBulkDelete} from "./lib/annotations/hooks/BeforeBulkDelete";
export {AfterBulkDelete} from "./lib/annotations/hooks/AfterBulkDelete";

// interfaces
export {IAssociationActionOptions} from "./lib/interfaces/IAssociationActionOptions";
export {IBuildOptions} from "./lib/interfaces/IBuildOptions";
Expand Down
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkCreate(target: any, propertyName: string): void;
export function AfterBulkCreate(options: IHookOptions): Function;
export function AfterBulkCreate(...args: any[]): void|Function {
return implementHookDecorator('afterBulkCreate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkDelete(target: any, propertyName: string): void;
export function AfterBulkDelete(options: IHookOptions): Function;
export function AfterBulkDelete(...args: any[]): void|Function {
return implementHookDecorator('afterBulkDelete', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkDestroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkDestroy(target: any, propertyName: string): void;
export function AfterBulkDestroy(options: IHookOptions): Function;
export function AfterBulkDestroy(...args: any[]): void|Function {
return implementHookDecorator('afterBulkDestroy', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkRestore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkRestore(target: any, propertyName: string): void;
export function AfterBulkRestore(options: IHookOptions): Function;
export function AfterBulkRestore(...args: any[]): void|Function {
return implementHookDecorator('afterBulkRestore', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkSync(target: any, propertyName: string): void;
export function AfterBulkSync(options: IHookOptions): Function;
export function AfterBulkSync(...args: any[]): void|Function {
return implementHookDecorator('afterBulkSync', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterBulkUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterBulkUpdate(target: any, propertyName: string): void;
export function AfterBulkUpdate(options: IHookOptions): Function;
export function AfterBulkUpdate(...args: any[]): void|Function {
return implementHookDecorator('afterBulkUpdate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterConnect(target: any, propertyName: string): void;
export function AfterConnect(options: IHookOptions): Function;
export function AfterConnect(...args: any[]): void|Function {
return implementHookDecorator('afterConnect', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterCreate(target: any, propertyName: string): void;
export function AfterCreate(options: IHookOptions): Function;
export function AfterCreate(...args: any[]): void|Function {
return implementHookDecorator('afterCreate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterDefine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterDefine(target: any, propertyName: string): void;
export function AfterDefine(options: IHookOptions): Function;
export function AfterDefine(...args: any[]): void|Function {
return implementHookDecorator('afterDefine', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterDelete(target: any, propertyName: string): void;
export function AfterDelete(options: IHookOptions): Function;
export function AfterDelete(...args: any[]): void|Function {
return implementHookDecorator('afterDelete', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterDestroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterDestroy(target: any, propertyName: string): void;
export function AfterDestroy(options: IHookOptions): Function;
export function AfterDestroy(...args: any[]): void|Function {
return implementHookDecorator('afterDestroy', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterFind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterFind(target: any, propertyName: string): void;
export function AfterFind(options: IHookOptions): Function;
export function AfterFind(...args: any[]): void|Function {
return implementHookDecorator('afterFind', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterInit(target: any, propertyName: string): void;
export function AfterInit(options: IHookOptions): Function;
export function AfterInit(...args: any[]): void|Function {
return implementHookDecorator('afterInit', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterRestore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterRestore(target: any, propertyName: string): void;
export function AfterRestore(options: IHookOptions): Function;
export function AfterRestore(...args: any[]): void|Function {
return implementHookDecorator('afterRestore', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterSave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterSave(target: any, propertyName: string): void;
export function AfterSave(options: IHookOptions): Function;
export function AfterSave(...args: any[]): void|Function {
return implementHookDecorator('afterSave', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterSync(target: any, propertyName: string): void;
export function AfterSync(options: IHookOptions): Function;
export function AfterSync(...args: any[]): void|Function {
return implementHookDecorator('afterSync', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterUpdate(target: any, propertyName: string): void;
export function AfterUpdate(options: IHookOptions): Function;
export function AfterUpdate(...args: any[]): void|Function {
return implementHookDecorator('afterUpdate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterUpsert.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterUpsert(target: any, propertyName: string): void;
export function AfterUpsert(options: IHookOptions): Function;
export function AfterUpsert(...args: any[]): void|Function {
return implementHookDecorator('afterUpsert', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/AfterValidate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function AfterValidate(target: any, propertyName: string): void;
export function AfterValidate(options: IHookOptions): Function;
export function AfterValidate(...args: any[]): void|Function {
return implementHookDecorator('afterValidate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkCreate(target: any, propertyName: string): void;
export function BeforeBulkCreate(options: IHookOptions): Function;
export function BeforeBulkCreate(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkCreate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkDelete(target: any, propertyName: string): void;
export function BeforeBulkDelete(options: IHookOptions): Function;
export function BeforeBulkDelete(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkDelete', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkDestroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkDestroy(target: any, propertyName: string): void;
export function BeforeBulkDestroy(options: IHookOptions): Function;
export function BeforeBulkDestroy(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkDestroy', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkRestore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkRestore(target: any, propertyName: string): void;
export function BeforeBulkRestore(options: IHookOptions): Function;
export function BeforeBulkRestore(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkRestore', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkSync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkSync(target: any, propertyName: string): void;
export function BeforeBulkSync(options: IHookOptions): Function;
export function BeforeBulkSync(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkSync', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeBulkUpdate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeBulkUpdate(target: any, propertyName: string): void;
export function BeforeBulkUpdate(options: IHookOptions): Function;
export function BeforeBulkUpdate(...args: any[]): void|Function {
return implementHookDecorator('beforeBulkUpdate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeConnect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeConnect(target: any, propertyName: string): void;
export function BeforeConnect(options: IHookOptions): Function;
export function BeforeConnect(...args: any[]): void|Function {
return implementHookDecorator('beforeConnect', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeCount(target: any, propertyName: string): void;
export function BeforeCount(options: IHookOptions): Function;
export function BeforeCount(...args: any[]): void|Function {
return implementHookDecorator('beforeCount', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeCreate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeCreate(target: any, propertyName: string): void;
export function BeforeCreate(options: IHookOptions): Function;
export function BeforeCreate(...args: any[]): void|Function {
return implementHookDecorator('beforeCreate', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeDefine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeDefine(target: any, propertyName: string): void;
export function BeforeDefine(options: IHookOptions): Function;
export function BeforeDefine(...args: any[]): void|Function {
return implementHookDecorator('beforeDefine', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeDelete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeDelete(target: any, propertyName: string): void;
export function BeforeDelete(options: IHookOptions): Function;
export function BeforeDelete(...args: any[]): void|Function {
return implementHookDecorator('beforeDelete', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeDestroy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeDestroy(target: any, propertyName: string): void;
export function BeforeDestroy(options: IHookOptions): Function;
export function BeforeDestroy(...args: any[]): void|Function {
return implementHookDecorator('beforeDestroy', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeFind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeFind(target: any, propertyName: string): void;
export function BeforeFind(options: IHookOptions): Function;
export function BeforeFind(...args: any[]): void|Function {
return implementHookDecorator('beforeFind', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeFindAfterExpandIncludeAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeFindAfterExpandIncludeAll(target: any, propertyName: string): void;
export function BeforeFindAfterExpandIncludeAll(options: IHookOptions): Function;
export function BeforeFindAfterExpandIncludeAll(...args: any[]): void|Function {
return implementHookDecorator('beforeFindAfterExpandIncludeAll', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeFindAfterOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeFindAfterOptions(target: any, propertyName: string): void;
export function BeforeFindAfterOptions(options: IHookOptions): Function;
export function BeforeFindAfterOptions(...args: any[]): void|Function {
return implementHookDecorator('beforeFindAfterOptions', args);
}
8 changes: 8 additions & 0 deletions lib/annotations/hooks/BeforeInit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IHookOptions} from "../../interfaces/IHookOptions";
import {implementHookDecorator} from "../../services/hooks";

export function BeforeInit(target: any, propertyName: string): void;
export function BeforeInit(options: IHookOptions): Function;
export function BeforeInit(...args: any[]): void|Function {
return implementHookDecorator('beforeInit', args);
}
Loading