From 5e9c209cc8eaa1d38f33bb3ac2de8b8ab33929f2 Mon Sep 17 00:00:00 2001 From: Wesley Reed Date: Sun, 10 Oct 2021 12:53:56 -0400 Subject: [PATCH] fix(types): add missing upsert hooks (#13394) * Missing upsert hooks Upsert hooks are missing in types * fix(types): missing upsert hooks Fixed upsert options type * fix(types): missing upsert hooks Just having a bad day * fix(types): added tests for upsert hooks * fix(types): incorrect attributes types for afterUpsert Co-authored-by: Sergio Bernal Co-authored-by: Constantin Metz <58604248+Keimeno@users.noreply.github.com> --- types/lib/hooks.d.ts | 3 +++ types/test/hooks.ts | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/types/lib/hooks.d.ts b/types/lib/hooks.d.ts index fd0ef7aef816..480495bfa461 100644 --- a/types/lib/hooks.d.ts +++ b/types/lib/hooks.d.ts @@ -6,6 +6,7 @@ import Model, { CreateOptions, DestroyOptions, RestoreOptions, + UpsertOptions, FindOptions, InstanceDestroyOptions, InstanceRestoreOptions, @@ -34,6 +35,8 @@ export interface ModelHooks { afterRestore(instance: M, options: InstanceRestoreOptions): HookReturn; beforeUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; afterUpdate(instance: M, options: InstanceUpdateOptions): HookReturn; + beforeUpsert(attributes: M, options: UpsertOptions): HookReturn; + afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions): HookReturn; beforeSave( instance: M, options: InstanceUpdateOptions | CreateOptions diff --git a/types/test/hooks.ts b/types/test/hooks.ts index cec4d0cdfa4b..340038759c41 100644 --- a/types/test/hooks.ts +++ b/types/test/hooks.ts @@ -1,6 +1,6 @@ import { expectTypeOf } from "expect-type"; import { SemiDeepWritable } from "./type-helpers/deep-writable"; -import { Model, SaveOptions, Sequelize, FindOptions, ModelCtor, ModelType, ModelDefined, ModelStatic } from "sequelize"; +import { Model, SaveOptions, Sequelize, FindOptions, ModelCtor, ModelType, ModelDefined, ModelStatic, UpsertOptions } from "sequelize"; import { ModelHooks } from "../lib/hooks"; import { DeepWriteable } from '../lib/utils'; import { Config } from '../lib/sequelize'; @@ -20,7 +20,15 @@ import { Config } from '../lib/sequelize'; afterFind(m, options) { expectTypeOf(m).toEqualTypeOf(); expectTypeOf(options).toEqualTypeOf(); - } + }, + beforeUpsert(m, options) { + expectTypeOf(m).toEqualTypeOf(); + expectTypeOf(options).toEqualTypeOf(); + }, + afterUpsert(m, options) { + expectTypeOf(m).toEqualTypeOf<[ TestModel, boolean | null ]>(); + expectTypeOf(options).toEqualTypeOf(); + }, }; const sequelize = new Sequelize('uri', { hooks }); @@ -29,6 +37,8 @@ import { Config } from '../lib/sequelize'; TestModel.addHook('beforeSave', hooks.beforeSave!); TestModel.addHook('afterSave', hooks.afterSave!); TestModel.addHook('afterFind', hooks.afterFind!); + TestModel.addHook('beforeUpsert', hooks.beforeUpsert!); + TestModel.addHook('afterUpsert', hooks.afterUpsert!); TestModel.beforeSave(hooks.beforeSave!); TestModel.afterSave(hooks.afterSave!); @@ -60,6 +70,7 @@ import { Config } from '../lib/sequelize'; hooks.beforeFindAfterOptions = (...args) => { expectTypeOf(args).toEqualTypeOf>() }; hooks.beforeSync = (...args) => { expectTypeOf(args).toEqualTypeOf>() }; hooks.beforeBulkSync = (...args) => { expectTypeOf(args).toEqualTypeOf>() }; + hooks.beforeUpsert = (...args) => { expectTypeOf(args).toEqualTypeOf>() }; } {