Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typings): queryInterface.addIndex #11844

Merged
merged 3 commits into from
Jan 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion types/lib/query-interface.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Promise } from './promise';
import QueryTypes = require('./query-types');
import { Sequelize, RetryOptions } from './sequelize';
import { Transaction } from './transaction';
import { SetRequired } from './../type-helpers/set-required';

type BindOrReplacements = { [key: string]: unknown } | unknown[];
type FieldMap = { [key: string]: string };
Expand Down Expand Up @@ -403,7 +404,7 @@ export class QueryInterface {
): Promise<void>;
public addIndex(
tableName: string,
options: QueryInterfaceIndexOptions & { fields: string[] },
options: SetRequired<QueryInterfaceIndexOptions, 'fields'>,
rawTablename?: string
): Promise<void>;

Expand Down
9 changes: 9 additions & 0 deletions types/test/query-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ queryInterface.addIndex('Person', ['firstname', 'lastname'], {
type: 'UNIQUE',
});

queryInterface.addIndex('Foo', {
name: 'foo_a',
fields: [
{ name: 'foo_b', order: 'DESC' },
'foo_c',
{ name: 'foo_d', order: 'ASC', collate: 'foobar', length: 42 }
],
});

queryInterface.removeIndex('Person', 'SuperDuperIndex');

// or
Expand Down
16 changes: 16 additions & 0 deletions types/type-helpers/set-required.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Full credits to sindresorhus/type-fest
*
* https://github.com/sindresorhus/type-fest/blob/v0.8.1/source/set-required.d.ts
*
* Thank you!
*/
export type SetRequired<BaseType, Keys extends keyof BaseType = keyof BaseType> =
// Pick just the keys that are not required from the base type.
Pick<BaseType, Exclude<keyof BaseType, Keys>> &
// Pick the keys that should be required from the base type and make them required.
Required<Pick<BaseType, Keys>> extends
// If `InferredType` extends the previous, then for each key, use the inferred type key.
infer InferredType
? {[KeyType in keyof InferredType]: InferredType[KeyType]}
: never;