Skip to content

Commit

Permalink
fix(types): add missing upsert hooks (#13394)
Browse files Browse the repository at this point in the history
* 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 <sergioguillot@gmail.com>
Co-authored-by: Constantin Metz <58604248+Keimeno@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 10, 2021
1 parent d685a9a commit 5e9c209
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions types/lib/hooks.d.ts
Expand Up @@ -6,6 +6,7 @@ import Model, {
CreateOptions,
DestroyOptions,
RestoreOptions,
UpsertOptions,
FindOptions,
InstanceDestroyOptions,
InstanceRestoreOptions,
Expand Down Expand Up @@ -34,6 +35,8 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> {
afterRestore(instance: M, options: InstanceRestoreOptions): HookReturn;
beforeUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
afterUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
beforeUpsert(attributes: M, options: UpsertOptions<TAttributes>): HookReturn;
afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions<TAttributes>): HookReturn;
beforeSave(
instance: M,
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>
Expand Down
15 changes: 13 additions & 2 deletions 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';
Expand All @@ -20,7 +20,15 @@ import { Config } from '../lib/sequelize';
afterFind(m, options) {
expectTypeOf(m).toEqualTypeOf<readonly TestModel[] | TestModel | null>();
expectTypeOf(options).toEqualTypeOf<FindOptions>();
}
},
beforeUpsert(m, options) {
expectTypeOf(m).toEqualTypeOf<TestModel>();
expectTypeOf(options).toEqualTypeOf<UpsertOptions>();
},
afterUpsert(m, options) {
expectTypeOf(m).toEqualTypeOf<[ TestModel, boolean | null ]>();
expectTypeOf(options).toEqualTypeOf<UpsertOptions>();
},
};

const sequelize = new Sequelize('uri', { hooks });
Expand All @@ -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!);
Expand Down Expand Up @@ -60,6 +70,7 @@ import { Config } from '../lib/sequelize';
hooks.beforeFindAfterOptions = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
hooks.beforeSync = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
hooks.beforeBulkSync = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
hooks.beforeUpsert = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
}

{
Expand Down

0 comments on commit 5e9c209

Please sign in to comment.