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

Commit 5e9c209c by Wesley Reed Committed by GitHub

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 d685a9a7
Showing with 16 additions and 2 deletions
......@@ -6,6 +6,7 @@ import Model, {
CreateOptions,
DestroyOptions,
RestoreOptions,
UpsertOptions,
FindOptions,
InstanceDestroyOptions,
InstanceRestoreOptions,
......@@ -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>
......
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<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 });
......@@ -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<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>>() };
}
{
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!