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

Commit f078f772 by Constantin Metz Committed by GitHub

feat(types): make config type deeply writeable for before connect hook (#13424)

* feat(types): add deep writeable config type for before connect hook

* feat(tests): added typing tests for beforeConnect callback arg types

Co-authored-by: Sascha Depold <sdepold@users.noreply.github.com>
1 parent ca2a11ae
...@@ -15,6 +15,7 @@ import Model, { ...@@ -15,6 +15,7 @@ import Model, {
UpdateOptions, UpdateOptions,
} from './model'; } from './model';
import { Config, Options, Sequelize, SyncOptions } from './sequelize'; import { Config, Options, Sequelize, SyncOptions } from './sequelize';
import { DeepWriteable } from './utils';
export type HookReturn = Promise<void> | void; export type HookReturn = Promise<void> | void;
...@@ -69,7 +70,7 @@ export interface SequelizeHooks< ...@@ -69,7 +70,7 @@ export interface SequelizeHooks<
afterDefine(model: ModelType): void; afterDefine(model: ModelType): void;
beforeInit(config: Config, options: Options): void; beforeInit(config: Config, options: Options): void;
afterInit(sequelize: Sequelize): void; afterInit(sequelize: Sequelize): void;
beforeConnect(config: Config): HookReturn; beforeConnect(config: DeepWriteable<Config>): HookReturn;
afterConnect(connection: unknown, config: Config): HookReturn; afterConnect(connection: unknown, config: Config): HookReturn;
beforeDisconnect(connection: unknown): HookReturn; beforeDisconnect(connection: unknown): HookReturn;
afterDisconnect(connection: unknown): HookReturn; afterDisconnect(connection: unknown): HookReturn;
......
...@@ -26,7 +26,7 @@ import { ModelManager } from './model-manager'; ...@@ -26,7 +26,7 @@ import { ModelManager } from './model-manager';
import { QueryInterface, QueryOptions, QueryOptionsWithModel, QueryOptionsWithType, ColumnsDescription } from './query-interface'; import { QueryInterface, QueryOptions, QueryOptionsWithModel, QueryOptionsWithType, ColumnsDescription } from './query-interface';
import QueryTypes = require('./query-types'); import QueryTypes = require('./query-types');
import { Transaction, TransactionOptions } from './transaction'; import { Transaction, TransactionOptions } from './transaction';
import { Cast, Col, Fn, Json, Literal, Where } from './utils'; import { Cast, Col, DeepWriteable, Fn, Json, Literal, Where } from './utils';
import { ConnectionManager } from './connection-manager'; import { ConnectionManager } from './connection-manager';
/** /**
...@@ -666,8 +666,8 @@ export class Sequelize extends Hooks { ...@@ -666,8 +666,8 @@ export class Sequelize extends Hooks {
* @param name * @param name
* @param fn A callback function that is called with options * @param fn A callback function that is called with options
*/ */
public static beforeConnect(name: string, fn: (options: Config) => void): void; public static beforeConnect(name: string, fn: (options: DeepWriteable<Config>) => void): void;
public static beforeConnect(fn: (options: Config) => void): void; public static beforeConnect(fn: (options: DeepWriteable<Config>) => void): void;
/** /**
* A hook that is run after a connection is established * A hook that is run after a connection is established
......
...@@ -3,6 +3,8 @@ import { Model, ModelCtor, ModelType, WhereOptions } from './model'; ...@@ -3,6 +3,8 @@ import { Model, ModelCtor, ModelType, WhereOptions } from './model';
export type Primitive = 'string' | 'number' | 'boolean'; export type Primitive = 'string' | 'number' | 'boolean';
export type DeepWriteable<T> = { -readonly [P in keyof T]: DeepWriteable<T[P]> };
export interface Inflector { export interface Inflector {
singularize(str: string): string; singularize(str: string): string;
pluralize(str: string): string; pluralize(str: string): string;
......
...@@ -2,6 +2,8 @@ import { expectTypeOf } from "expect-type"; ...@@ -2,6 +2,8 @@ import { expectTypeOf } from "expect-type";
import { SemiDeepWritable } from "./type-helpers/deep-writable"; 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 } from "sequelize";
import { ModelHooks } from "../lib/hooks"; import { ModelHooks } from "../lib/hooks";
import { DeepWriteable } from '../lib/utils';
import { Config } from '../lib/sequelize';
{ {
class TestModel extends Model {} class TestModel extends Model {}
...@@ -59,3 +61,9 @@ import { ModelHooks } from "../lib/hooks"; ...@@ -59,3 +61,9 @@ import { ModelHooks } from "../lib/hooks";
hooks.beforeSync = (...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.beforeBulkSync = (...args) => { expectTypeOf(args).toEqualTypeOf<SemiDeepWritable<typeof args>>() };
} }
{
Sequelize.beforeConnect('name', config => expectTypeOf(config).toEqualTypeOf<DeepWriteable<Config>>());
Sequelize.beforeConnect(config => expectTypeOf(config).toEqualTypeOf<DeepWriteable<Config>>());
Sequelize.addHook('beforeConnect', (...args) => { expectTypeOf(args).toEqualTypeOf<[DeepWriteable<Config>]>(); })
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!