Skip to content

Commit 5e9c209

Browse files
reediculous456imsergiobernalKeimeno
authored
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 <sergioguillot@gmail.com> Co-authored-by: Constantin Metz <58604248+Keimeno@users.noreply.github.com>
1 parent d685a9a commit 5e9c209

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

types/lib/hooks.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Model, {
66
CreateOptions,
77
DestroyOptions,
88
RestoreOptions,
9+
UpsertOptions,
910
FindOptions,
1011
InstanceDestroyOptions,
1112
InstanceRestoreOptions,
@@ -34,6 +35,8 @@ export interface ModelHooks<M extends Model = Model, TAttributes = any> {
3435
afterRestore(instance: M, options: InstanceRestoreOptions): HookReturn;
3536
beforeUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
3637
afterUpdate(instance: M, options: InstanceUpdateOptions<TAttributes>): HookReturn;
38+
beforeUpsert(attributes: M, options: UpsertOptions<TAttributes>): HookReturn;
39+
afterUpsert(attributes: [ M, boolean | null ], options: UpsertOptions<TAttributes>): HookReturn;
3740
beforeSave(
3841
instance: M,
3942
options: InstanceUpdateOptions<TAttributes> | CreateOptions<TAttributes>

types/test/hooks.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expectTypeOf } from "expect-type";
22
import { SemiDeepWritable } from "./type-helpers/deep-writable";
3-
import { Model, SaveOptions, Sequelize, FindOptions, ModelCtor, ModelType, ModelDefined, ModelStatic } from "sequelize";
3+
import { Model, SaveOptions, Sequelize, FindOptions, ModelCtor, ModelType, ModelDefined, ModelStatic, UpsertOptions } from "sequelize";
44
import { ModelHooks } from "../lib/hooks";
55
import { DeepWriteable } from '../lib/utils';
66
import { Config } from '../lib/sequelize';
@@ -20,7 +20,15 @@ import { Config } from '../lib/sequelize';
2020
afterFind(m, options) {
2121
expectTypeOf(m).toEqualTypeOf<readonly TestModel[] | TestModel | null>();
2222
expectTypeOf(options).toEqualTypeOf<FindOptions>();
23-
}
23+
},
24+
beforeUpsert(m, options) {
25+
expectTypeOf(m).toEqualTypeOf<TestModel>();
26+
expectTypeOf(options).toEqualTypeOf<UpsertOptions>();
27+
},
28+
afterUpsert(m, options) {
29+
expectTypeOf(m).toEqualTypeOf<[ TestModel, boolean | null ]>();
30+
expectTypeOf(options).toEqualTypeOf<UpsertOptions>();
31+
},
2432
};
2533

2634
const sequelize = new Sequelize('uri', { hooks });
@@ -29,6 +37,8 @@ import { Config } from '../lib/sequelize';
2937
TestModel.addHook('beforeSave', hooks.beforeSave!);
3038
TestModel.addHook('afterSave', hooks.afterSave!);
3139
TestModel.addHook('afterFind', hooks.afterFind!);
40+
TestModel.addHook('beforeUpsert', hooks.beforeUpsert!);
41+
TestModel.addHook('afterUpsert', hooks.afterUpsert!);
3242

3343
TestModel.beforeSave(hooks.beforeSave!);
3444
TestModel.afterSave(hooks.afterSave!);
@@ -60,6 +70,7 @@ import { Config } from '../lib/sequelize';
6070
hooks.beforeFindAfterOptions = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
6171
hooks.beforeSync = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
6272
hooks.beforeBulkSync = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
73+
hooks.beforeUpsert = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
6374
}
6475

6576
{

0 commit comments

Comments
 (0)