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

Commit 6b0b532a by JacobLey Committed by GitHub

fix(typings): allow `schema` for queryInterface methods (#13223)

1 parent 63ceb738
...@@ -4,7 +4,7 @@ import { DataType } from './data-types'; ...@@ -4,7 +4,7 @@ import { DataType } from './data-types';
import { Deferrable } from './deferrable'; import { Deferrable } from './deferrable';
import { HookReturn, Hooks, ModelHooks } from './hooks'; import { HookReturn, Hooks, ModelHooks } from './hooks';
import { ValidationOptions } from './instance-validator'; import { ValidationOptions } from './instance-validator';
import { QueryOptions, IndexesOptions } from './query-interface'; import { QueryOptions, IndexesOptions, TableName } from './query-interface';
import { Sequelize, SyncOptions } from './sequelize'; import { Sequelize, SyncOptions } from './sequelize';
import { Transaction, LOCK } from './transaction'; import { Transaction, LOCK } from './transaction';
import { Col, Fn, Literal, Where } from './utils'; import { Col, Fn, Literal, Where } from './utils';
...@@ -1232,7 +1232,7 @@ export interface ModelAttributeColumnReferencesOptions { ...@@ -1232,7 +1232,7 @@ export interface ModelAttributeColumnReferencesOptions {
/** /**
* If this column references another table, provide it here as a Model, or a string * If this column references another table, provide it here as a Model, or a string
*/ */
model?: string | ModelType; model?: TableName | ModelType;
/** /**
* The column of the foreign table that this column references * The column of the foreign table that this column references
......
...@@ -238,7 +238,7 @@ export interface AddPrimaryKeyConstraintOptions extends BaseConstraintOptions { ...@@ -238,7 +238,7 @@ export interface AddPrimaryKeyConstraintOptions extends BaseConstraintOptions {
export interface AddForeignKeyConstraintOptions extends BaseConstraintOptions { export interface AddForeignKeyConstraintOptions extends BaseConstraintOptions {
type: 'foreign key'; type: 'foreign key';
references?: { references?: {
table: string; table: TableName;
field: string; field: string;
}; };
onDelete: string; onDelete: string;
...@@ -336,7 +336,7 @@ export class QueryInterface { ...@@ -336,7 +336,7 @@ export class QueryInterface {
* @param options Table options. * @param options Table options.
*/ */
public createTable<M extends Model>( public createTable<M extends Model>(
tableName: string | { schema?: string; tableName?: string }, tableName: TableName,
attributes: ModelAttributes<M, M['_creationAttributes']>, attributes: ModelAttributes<M, M['_creationAttributes']>,
options?: QueryInterfaceCreateTableOptions options?: QueryInterfaceCreateTableOptions
): Promise<void>; ): Promise<void>;
...@@ -347,7 +347,7 @@ export class QueryInterface { ...@@ -347,7 +347,7 @@ export class QueryInterface {
* @param tableName Table name. * @param tableName Table name.
* @param options Query options, particularly "force". * @param options Query options, particularly "force".
*/ */
public dropTable(tableName: string, options?: QueryInterfaceDropTableOptions): Promise<void>; public dropTable(tableName: TableName, options?: QueryInterfaceDropTableOptions): Promise<void>;
/** /**
* Drops all tables. * Drops all tables.
...@@ -366,7 +366,7 @@ export class QueryInterface { ...@@ -366,7 +366,7 @@ export class QueryInterface {
/** /**
* Renames a table * Renames a table
*/ */
public renameTable(before: string, after: string, options?: QueryInterfaceOptions): Promise<void>; public renameTable(before: TableName, after: TableName, options?: QueryInterfaceOptions): Promise<void>;
/** /**
* Returns all tables * Returns all tables
...@@ -377,7 +377,7 @@ export class QueryInterface { ...@@ -377,7 +377,7 @@ export class QueryInterface {
* Describe a table * Describe a table
*/ */
public describeTable( public describeTable(
tableName: string | { schema?: string; tableName?: string }, tableName: TableName,
options?: string | { schema?: string; schemaDelimiter?: string } & Logging options?: string | { schema?: string; schemaDelimiter?: string } & Logging
): Promise<ColumnsDescription>; ): Promise<ColumnsDescription>;
...@@ -385,7 +385,7 @@ export class QueryInterface { ...@@ -385,7 +385,7 @@ export class QueryInterface {
* Adds a new column to a table * Adds a new column to a table
*/ */
public addColumn( public addColumn(
table: string | { schema?: string; tableName?: string }, table: TableName,
key: string, key: string,
attribute: ModelAttributeColumnOptions | DataType, attribute: ModelAttributeColumnOptions | DataType,
options?: QueryInterfaceOptions options?: QueryInterfaceOptions
...@@ -395,7 +395,7 @@ export class QueryInterface { ...@@ -395,7 +395,7 @@ export class QueryInterface {
* Removes a column from a table * Removes a column from a table
*/ */
public removeColumn( public removeColumn(
table: string | { schema?: string; tableName?: string }, table: TableName,
attribute: string, attribute: string,
options?: QueryInterfaceOptions options?: QueryInterfaceOptions
): Promise<void>; ): Promise<void>;
...@@ -404,7 +404,7 @@ export class QueryInterface { ...@@ -404,7 +404,7 @@ export class QueryInterface {
* Changes a column * Changes a column
*/ */
public changeColumn( public changeColumn(
tableName: string | { schema?: string; tableName?: string }, tableName: TableName,
attributeName: string, attributeName: string,
dataTypeOrOptions?: DataType | ModelAttributeColumnOptions, dataTypeOrOptions?: DataType | ModelAttributeColumnOptions,
options?: QueryInterfaceOptions options?: QueryInterfaceOptions
...@@ -414,7 +414,7 @@ export class QueryInterface { ...@@ -414,7 +414,7 @@ export class QueryInterface {
* Renames a column * Renames a column
*/ */
public renameColumn( public renameColumn(
tableName: string | { schema?: string; tableName?: string }, tableName: TableName,
attrNameBefore: string, attrNameBefore: string,
attrNameAfter: string, attrNameAfter: string,
options?: QueryInterfaceOptions options?: QueryInterfaceOptions
...@@ -424,13 +424,13 @@ export class QueryInterface { ...@@ -424,13 +424,13 @@ export class QueryInterface {
* Adds a new index to a table * Adds a new index to a table
*/ */
public addIndex( public addIndex(
tableName: string, tableName: TableName,
attributes: string[], attributes: string[],
options?: QueryInterfaceIndexOptions, options?: QueryInterfaceIndexOptions,
rawTablename?: string rawTablename?: string
): Promise<void>; ): Promise<void>;
public addIndex( public addIndex(
tableName: string, tableName: TableName,
options: SetRequired<QueryInterfaceIndexOptions, 'fields'>, options: SetRequired<QueryInterfaceIndexOptions, 'fields'>,
rawTablename?: string rawTablename?: string
): Promise<void>; ): Promise<void>;
...@@ -438,21 +438,21 @@ export class QueryInterface { ...@@ -438,21 +438,21 @@ export class QueryInterface {
/** /**
* Removes an index of a table * Removes an index of a table
*/ */
public removeIndex(tableName: string, indexName: string, options?: QueryInterfaceIndexOptions): Promise<void>; public removeIndex(tableName: TableName, indexName: string, options?: QueryInterfaceIndexOptions): Promise<void>;
public removeIndex(tableName: string, attributes: string[], options?: QueryInterfaceIndexOptions): Promise<void>; public removeIndex(tableName: TableName, attributes: string[], options?: QueryInterfaceIndexOptions): Promise<void>;
/** /**
* Adds constraints to a table * Adds constraints to a table
*/ */
public addConstraint( public addConstraint(
tableName: string, tableName: TableName,
options?: AddConstraintOptions & QueryInterfaceOptions options?: AddConstraintOptions & QueryInterfaceOptions
): Promise<void>; ): Promise<void>;
/** /**
* Removes constraints from a table * Removes constraints from a table
*/ */
public removeConstraint(tableName: string, constraintName: string, options?: QueryInterfaceOptions): Promise<void>; public removeConstraint(tableName: TableName, constraintName: string, options?: QueryInterfaceOptions): Promise<void>;
/** /**
* Shows the index of a table * Shows the index of a table
...@@ -472,7 +472,7 @@ export class QueryInterface { ...@@ -472,7 +472,7 @@ export class QueryInterface {
/** /**
* Get foreign key references details for the table * Get foreign key references details for the table
*/ */
public getForeignKeyReferencesForTable(tableName: string, options?: QueryInterfaceOptions): Promise<object>; public getForeignKeyReferencesForTable(tableName: TableName, options?: QueryInterfaceOptions): Promise<object>;
/** /**
* Inserts a new record * Inserts a new record
......
...@@ -25,6 +25,15 @@ async function test() { ...@@ -25,6 +25,15 @@ async function test() {
}, },
type: DataTypes.INTEGER, type: DataTypes.INTEGER,
}, },
attr5: {
onDelete: 'cascade',
onUpdate: 'cascade',
references: {
key: 'id',
model: { schema: '<schema>', tableName: 'another_table_name' },
},
type: DataTypes.INTEGER,
},
createdAt: { createdAt: {
type: DataTypes.DATE, type: DataTypes.DATE,
}, },
...@@ -49,8 +58,10 @@ async function test() { ...@@ -49,8 +58,10 @@ async function test() {
} }
} }
); );
await queryInterface.createTable({ tableName: '<table-name>' }, {});
await queryInterface.dropTable('nameOfTheExistingTable'); await queryInterface.dropTable('nameOfTheExistingTable');
await queryInterface.dropTable({ schema: '<schema>', tableName: 'nameOfTheExistingTable' });
await queryInterface.bulkDelete({ tableName: 'foo', schema: 'bar' }, {}, {}); await queryInterface.bulkDelete({ tableName: 'foo', schema: 'bar' }, {}, {});
...@@ -65,6 +76,10 @@ async function test() { ...@@ -65,6 +76,10 @@ async function test() {
await queryInterface.dropAllTables(); await queryInterface.dropAllTables();
await queryInterface.renameTable('Person', 'User'); await queryInterface.renameTable('Person', 'User');
await queryInterface.renameTable(
{ schema: '<schema>', tableName: 'Person' },
{ schema: '<schema>', tableName: 'User' },
);
const tableNames: string[] = await queryInterface.showAllTables(); const tableNames: string[] = await queryInterface.showAllTables();
...@@ -128,9 +143,11 @@ async function test() { ...@@ -128,9 +143,11 @@ async function test() {
); );
await queryInterface.renameColumn('Person', 'signature', 'sig'); await queryInterface.renameColumn('Person', 'signature', 'sig');
await queryInterface.renameColumn({ schema: '<schema>', tableName: 'Person' }, 'signature', 'sig');
// This example will create the index person_firstname_lastname // This example will create the index person_firstname_lastname
await queryInterface.addIndex('Person', ['firstname', 'lastname']); await queryInterface.addIndex('Person', ['firstname', 'lastname']);
await queryInterface.addIndex({ schema: '<schema>', tableName: 'Person' }, ['firstname', 'lastname']);
// This example will create a unique index with the name SuperDuperIndex using the optional 'options' field. // This example will create a unique index with the name SuperDuperIndex using the optional 'options' field.
// Possible options: // Possible options:
...@@ -167,6 +184,7 @@ async function test() { ...@@ -167,6 +184,7 @@ async function test() {
}) })
await queryInterface.removeIndex('Person', 'SuperDuperIndex'); await queryInterface.removeIndex('Person', 'SuperDuperIndex');
await queryInterface.removeIndex({ schema: '<schema>', tableName: 'Person' }, 'SuperDuperIndex');
// or // or
...@@ -180,12 +198,18 @@ async function test() { ...@@ -180,12 +198,18 @@ async function test() {
})) }))
await queryInterface.removeConstraint('Person', 'firstnamexlastname'); await queryInterface.removeConstraint('Person', 'firstnamexlastname');
await queryInterface.removeConstraint({ schema: '<schema>', tableName: 'Person' }, 'firstnamexlastname');
await queryInterface.select(null, 'Person', { await queryInterface.select(null, 'Person', {
where: { where: {
a: 1, a: 1,
}, },
}); });
await queryInterface.select(null, { schema: '<schema>', tableName: 'Person' }, {
where: {
a: 1,
},
});
await queryInterface.delete(null, 'Person', { await queryInterface.delete(null, 'Person', {
where: { where: {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!