不要怂,就是干,撸起袖子干!

Commit 0569f42a by Vitaliy Zaytsev Committed by Simon Schick

fix(types): add beforeSave and afterSave typings (#10987)

1 parent b3d28490
...@@ -215,8 +215,8 @@ The following hooks will emit whenever you're editing a single object ...@@ -215,8 +215,8 @@ The following hooks will emit whenever you're editing a single object
```text ```text
beforeValidate beforeValidate
afterValidate or validationFailed afterValidate or validationFailed
beforeCreate / beforeUpdate / beforeDestroy beforeCreate / beforeUpdate / beforeSave / beforeDestroy
afterCreate / afterUpdate / afterDestroy afterCreate / afterUpdate / afterSave / afterDestroy
``` ```
```js ```js
......
...@@ -909,7 +909,7 @@ class Model { ...@@ -909,7 +909,7 @@ class Model {
* @param {string} [options.comment] Specify comment for model's table * @param {string} [options.comment] Specify comment for model's table
* @param {string} [options.collate] Specify collation for model's table * @param {string} [options.collate] Specify collation for model's table
* @param {string} [options.initialAutoIncrement] Set the initial AUTO_INCREMENT value for the table in MySQL. * @param {string} [options.initialAutoIncrement] Set the initial AUTO_INCREMENT value for the table in MySQL.
* @param {Object} [options.hooks] An object of hook function that are called before and after certain lifecycle events. The possible hooks are: beforeValidate, afterValidate, validationFailed, beforeBulkCreate, beforeBulkDestroy, beforeBulkUpdate, beforeCreate, beforeDestroy, beforeUpdate, afterCreate, afterDestroy, afterUpdate, afterBulkCreate, afterBulkDestroy and afterBulkUpdate. See Hooks for more information about hook functions and their signatures. Each property can either be a function, or an array of functions. * @param {Object} [options.hooks] An object of hook function that are called before and after certain lifecycle events. The possible hooks are: beforeValidate, afterValidate, validationFailed, beforeBulkCreate, beforeBulkDestroy, beforeBulkUpdate, beforeCreate, beforeDestroy, beforeUpdate, afterCreate, beforeSave, afterDestroy, afterUpdate, afterBulkCreate, afterSave, afterBulkDestroy and afterBulkUpdate. See Hooks for more information about hook functions and their signatures. Each property can either be a function, or an array of functions.
* @param {Object} [options.validate] An object of model wide validations. Validations have access to all model values via `this`. If the validator function takes an argument, it is assumed to be async, and is called with a callback that accepts an optional error. * @param {Object} [options.validate] An object of model wide validations. Validations have access to all model values via `this`. If the validator function takes an argument, it is assumed to be async, and is called with a callback that accepts an optional error.
* *
* @returns {Model} * @returns {Model}
......
...@@ -28,6 +28,8 @@ export interface ModelHooks<M extends Model = Model> { ...@@ -28,6 +28,8 @@ export interface ModelHooks<M extends Model = Model> {
afterDestroy(instance: M, options: InstanceDestroyOptions): HookReturn; afterDestroy(instance: M, options: InstanceDestroyOptions): HookReturn;
beforeUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; beforeUpdate(instance: M, options: InstanceUpdateOptions): HookReturn;
afterUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; afterUpdate(instance: M, options: InstanceUpdateOptions): HookReturn;
beforeSave(instance: M, options: InstanceUpdateOptions | CreateOptions): HookReturn;
afterSave(instance: M, options: InstanceUpdateOptions | CreateOptions): HookReturn;
beforeBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn; beforeBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn;
afterBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn; afterBulkCreate(instances: M[], options: BulkCreateOptions): HookReturn;
beforeBulkDestroy(options: DestroyOptions): HookReturn; beforeBulkDestroy(options: DestroyOptions): HookReturn;
......
...@@ -2165,6 +2165,38 @@ export abstract class Model<T = any, T2 = any> extends Hooks { ...@@ -2165,6 +2165,38 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
): void; ): void;
/** /**
* A hook that is run before creating or updating a single instance, It proxies `beforeCreate` and `beforeUpdate`
*
* @param name
* @param fn A callback function that is called with instance, options
*/
public static beforeSave<M extends Model>(
this: { new (): M } & typeof Model,
name: string,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void;
public static beforeSave<M extends Model>(
this: { new (): M } & typeof Model,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void;
/**
* A hook that is run after creating or updating a single instance, It proxies `afterCreate` and `afterUpdate`
*
* @param name
* @param fn A callback function that is called with instance, options
*/
public static afterSave<M extends Model>(
this: { new (): M } & typeof Model,
name: string,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void;
public static afterSave<M extends Model>(
this: { new (): M } & typeof Model,
fn: (instance: M, options: UpdateOptions | SaveOptions) => HookReturn
): void;
/**
* A hook that is run before creating instances in bulk * A hook that is run before creating instances in bulk
* *
* @param name * @param name
......
...@@ -507,6 +507,24 @@ export class Sequelize extends Hooks { ...@@ -507,6 +507,24 @@ export class Sequelize extends Hooks {
public static afterUpdate(fn: (instance: Model, options: UpdateOptions) => void): void; public static afterUpdate(fn: (instance: Model, options: UpdateOptions) => void): void;
/** /**
* A hook that is run before creating or updating a single instance, It proxies `beforeCreate` and `beforeUpdate`
*
* @param name
* @param fn A callback function that is called with instance, options
*/
public static beforeSave(name: string, fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
public static beforeSave(fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
/**
* A hook that is run after creating or updating a single instance, It proxies `afterCreate` and `afterUpdate`
*
* @param name
* @param fn A callback function that is called with instance, options
*/
public static afterSave(name: string, fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
public static afterSave(fn: (instance: Model, options: UpdateOptions | CreateOptions) => void): void;
/**
* A hook that is run before creating instances in bulk * A hook that is run before creating instances in bulk
* *
* @param name * @param name
......
import {Model, SaveOptions, Sequelize} from "sequelize"
import { ModelHooks } from "../lib/hooks";
/*
* covers types/lib/sequelize.d.ts
*/
Sequelize.beforeSave((t: TestModel, options: SaveOptions) => {});
Sequelize.afterSave((t: TestModel, options: SaveOptions) => {});
/*
* covers types/lib/hooks.d.ts
*/
export const sequelize = new Sequelize('uri', {
hooks: {
beforeSave (m: Model, options: SaveOptions) {},
afterSave (m: Model, options: SaveOptions) {},
}
});
class TestModel extends Model {
}
const hooks: Partial<ModelHooks> = {
beforeSave(t: TestModel, options: SaveOptions) { },
afterSave(t: TestModel, options: SaveOptions) { },
};
TestModel.init({}, {sequelize, hooks })
TestModel.addHook('beforeSave', (t: TestModel, options: SaveOptions) => { });
TestModel.addHook('afterSave', (t: TestModel, options: SaveOptions) => { });
/*
* covers types/lib/model.d.ts
*/
TestModel.beforeSave((t: TestModel, options: SaveOptions) => { });
TestModel.afterSave((t: TestModel, options: SaveOptions) => { });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!