Skip to content

Commit

Permalink
meta: simplify usage of rejectInvalidOptions (#17073)
Browse files Browse the repository at this point in the history
  • Loading branch information
ephys committed Feb 10, 2024
1 parent 1fead8a commit c091578
Show file tree
Hide file tree
Showing 19 changed files with 141 additions and 169 deletions.
100 changes: 21 additions & 79 deletions packages/core/src/dialects/abstract/query-generator-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,36 +186,11 @@ export class AbstractQueryGeneratorTypeScript {
}

if (options) {
const CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set<keyof CreateSchemaQueryOptions>();
if (this.dialect.supports.createSchema.authorization) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('authorization');
}

if (this.dialect.supports.createSchema.charset) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('charset');
}

if (this.dialect.supports.createSchema.collate) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('collate');
}

if (this.dialect.supports.createSchema.comment) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('comment');
}

if (this.dialect.supports.createSchema.ifNotExists) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('ifNotExists');
}

if (this.dialect.supports.createSchema.replace) {
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('replace');
}

rejectInvalidOptions(
'createSchemaQuery',
this.dialect.name,
this.dialect,
CREATE_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
CREATE_SCHEMA_QUERY_SUPPORTED_OPTIONS,
this.dialect.supports.createSchema,
options,
);
}
Expand All @@ -241,20 +216,11 @@ export class AbstractQueryGeneratorTypeScript {
}

if (options) {
const DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set<keyof DropSchemaQueryOptions>();
if (this.dialect.supports.dropSchema.cascade) {
DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('cascade');
}

if (this.dialect.supports.dropSchema.ifExists) {
DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS.add('ifExists');
}

rejectInvalidOptions(
'dropSchemaQuery',
this.dialect.name,
this.dialect,
DROP_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS,
this.dialect.supports.dropSchema,
options,
);
}
Expand All @@ -280,18 +246,12 @@ export class AbstractQueryGeneratorTypeScript {
}

dropTableQuery(tableName: TableNameOrModel, options?: DropTableQueryOptions): string {
const DROP_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof DropTableQueryOptions>();

if (this.dialect.supports.dropTable.cascade) {
DROP_TABLE_QUERY_SUPPORTED_OPTIONS.add('cascade');
}

if (options) {
rejectInvalidOptions(
'dropTableQuery',
this.dialect.name,
this.dialect,
DROP_TABLE_QUERY_SUPPORTABLE_OPTIONS,
DROP_TABLE_QUERY_SUPPORTED_OPTIONS,
this.dialect.supports.dropTable,
options,
);
}
Expand Down Expand Up @@ -328,21 +288,11 @@ export class AbstractQueryGeneratorTypeScript {

removeColumnQuery(tableName: TableNameOrModel, columnName: string, options?: RemoveColumnQueryOptions): string {
if (options) {
const REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveColumnQueryOptions>();

if (this.dialect.supports.removeColumn.cascade) {
REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS.add('cascade');
}

if (this.dialect.supports.removeColumn.ifExists) {
REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS.add('ifExists');
}

rejectInvalidOptions(
'removeColumnQuery',
this.dialect.name,
this.dialect,
REMOVE_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_COLUMN_QUERY_SUPPORTED_OPTIONS,
this.dialect.supports.removeColumn,
options,
);
}
Expand Down Expand Up @@ -376,21 +326,11 @@ export class AbstractQueryGeneratorTypeScript {
}

if (options) {
const REMOVE_CONSTRAINT_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveConstraintQueryOptions>();
const { removeOptions } = this.dialect.supports.constraints;
if (removeOptions.cascade) {
REMOVE_CONSTRAINT_QUERY_SUPPORTED_OPTIONS.add('cascade');
}

if (removeOptions.ifExists) {
REMOVE_CONSTRAINT_QUERY_SUPPORTED_OPTIONS.add('ifExists');
}

rejectInvalidOptions(
'removeConstraintQuery',
this.dialect.name,
this.dialect,
REMOVE_CONSTRAINT_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_CONSTRAINT_QUERY_SUPPORTED_OPTIONS,
this.dialect.supports.constraints.removeOptions,
options,
);
}
Expand Down Expand Up @@ -495,17 +435,19 @@ export class AbstractQueryGeneratorTypeScript {
* @param options options
*/
quoteTable(param: TableOrModel, options?: QuoteTableOptions): string {
const QUOTE_TABLE_SUPPORTED_OPTIONS = new Set<keyof QuoteTableOptions>();
if (this.dialect.supports.indexHints) {
QUOTE_TABLE_SUPPORTED_OPTIONS.add('indexHints');
}

if (this.dialect.supports.tableHints) {
QUOTE_TABLE_SUPPORTED_OPTIONS.add('tableHints');
if (options) {
rejectInvalidOptions(
'quoteTable',
this.dialect,
QUOTE_TABLE_SUPPORTABLE_OPTIONS,
{
indexHints: this.dialect.supports.indexHints,
tableHints: this.dialect.supports.tableHints,
},
options,
);
}

rejectInvalidOptions('quoteTable', this.dialect.name, QUOTE_TABLE_SUPPORTABLE_OPTIONS, QUOTE_TABLE_SUPPORTED_OPTIONS, { ...options });

if (isModelStatic(param)) {
param = param.getTableName();
}
Expand Down
22 changes: 9 additions & 13 deletions packages/core/src/dialects/db2/query-generator-typescript.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Op } from '../../operators.js';
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { EMPTY_SET } from '../../utils/object.js';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import {
Expand All @@ -22,11 +23,6 @@ import type { ConstraintType } from '../abstract/query-interface.types';
import { Db2QueryGeneratorInternal } from './query-generator-internal.js';
import type { Db2Dialect } from './index.js';

const DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS = new Set<keyof DropSchemaQueryOptions>();
const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>();
const RENAME_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof RenameTableQueryOptions>();
const TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof TruncateTableQueryOptions>();

/**
* Temporary class to ease the TypeScript migration
*/
Expand All @@ -46,9 +42,9 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'dropSchemaQuery',
this.dialect.name,
this.dialect,
DROP_SCHEMA_QUERY_SUPPORTABLE_OPTIONS,
DROP_SCHEMA_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down Expand Up @@ -110,9 +106,9 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'renameTableQuery',
this.dialect.name,
this.dialect,
RENAME_TABLE_QUERY_SUPPORTABLE_OPTIONS,
RENAME_TABLE_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand All @@ -131,9 +127,9 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'truncateTableQuery',
this.dialect.name,
this.dialect,
TRUNCATE_TABLE_QUERY_SUPPORTABLE_OPTIONS,
TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down Expand Up @@ -213,9 +209,9 @@ export class Db2QueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
this.dialect,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/dialects/db2/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { rejectInvalidOptions } from '../../utils/check';
import { removeNullishValuesFromHash } from '../../utils/format';
import { EMPTY_SET } from '../../utils/object.js';
import { removeTrailingSemicolon } from '../../utils/string';
import { defaultValueSchemable } from '../../utils/query-builder-utils';
import { attributeTypeToSql, normalizeDataType } from '../abstract/data-types-utils';
Expand All @@ -22,7 +23,6 @@ const DataTypes = require('../../data-types');
const randomBytes = require('node:crypto').randomBytes;
const { Op } = require('../../operators');

const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();
const CREATE_TABLE_QUERY_SUPPORTED_OPTIONS = new Set(['uniqueKeys']);

/* istanbul ignore next */
Expand All @@ -41,7 +41,7 @@ export class Db2QueryGenerator extends Db2QueryGeneratorTypeScript {
if (options) {
rejectInvalidOptions(
'createTableQuery',
this.dialect.name,
this.dialect,
CREATE_TABLE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_TABLE_QUERY_SUPPORTED_OPTIONS,
options,
Expand Down Expand Up @@ -144,9 +144,9 @@ export class Db2QueryGenerator extends Db2QueryGeneratorTypeScript {
if (options) {
rejectInvalidOptions(
'addColumnQuery',
this.dialect.name,
this.dialect,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down
13 changes: 6 additions & 7 deletions packages/core/src/dialects/ibmi/query-generator-typescript.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { EMPTY_SET } from '../../utils/object.js';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import {
Expand All @@ -19,8 +20,6 @@ import { IBMiQueryGeneratorInternal } from './query-generator-internal.js';
import type { IBMiDialect } from './index.js';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['ifExists']);
const RENAME_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof RenameTableQueryOptions>();
const TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof TruncateTableQueryOptions>();

/**
* Temporary class to ease the TypeScript migration
Expand Down Expand Up @@ -89,9 +88,9 @@ export class IBMiQueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'renameTableQuery',
this.dialect.name,
this.dialect,
RENAME_TABLE_QUERY_SUPPORTABLE_OPTIONS,
RENAME_TABLE_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand All @@ -110,9 +109,9 @@ export class IBMiQueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'truncateTableQuery',
this.dialect.name,
this.dialect,
TRUNCATE_TABLE_QUERY_SUPPORTABLE_OPTIONS,
TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down Expand Up @@ -180,7 +179,7 @@ export class IBMiQueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
this.dialect,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/dialects/ibmi/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { BaseSqlExpression } from '../../expression-builders/base-sql-expression.js';
import { conformIndex } from '../../model-internals';
import { rejectInvalidOptions } from '../../utils/check';
import { EMPTY_SET } from '../../utils/object.js';
import { nameIndex, removeTrailingSemicolon } from '../../utils/string';
import { defaultValueSchemable } from '../../utils/query-builder-utils';
import { attributeTypeToSql, normalizeDataType } from '../abstract/data-types-utils';
Expand All @@ -18,15 +19,14 @@ const DataTypes = require('../../data-types');
const typeWithoutDefault = new Set(['BLOB']);

const CREATE_TABLE_QUERY_SUPPORTED_OPTIONS = new Set(['uniqueKeys']);
const ADD_COLUMN_QUERY_SUPPORTED_OPTIONS = new Set();

export class IBMiQueryGenerator extends IBMiQueryGeneratorTypeScript {
// Table queries
createTableQuery(tableName, attributes, options) {
if (options) {
rejectInvalidOptions(
'createTableQuery',
this.dialect.name,
this.dialect,
CREATE_TABLE_QUERY_SUPPORTABLE_OPTIONS,
CREATE_TABLE_QUERY_SUPPORTED_OPTIONS,
options,
Expand Down Expand Up @@ -106,9 +106,9 @@ export class IBMiQueryGenerator extends IBMiQueryGeneratorTypeScript {
if (options) {
rejectInvalidOptions(
'addColumnQuery',
this.dialect.name,
this.dialect,
ADD_COLUMN_QUERY_SUPPORTABLE_OPTIONS,
ADD_COLUMN_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Expression } from '../../sequelize.js';
import { rejectInvalidOptions } from '../../utils/check';
import { joinSQLFragments } from '../../utils/join-sql-fragments';
import { buildJsonPath } from '../../utils/json.js';
import { EMPTY_SET } from '../../utils/object.js';
import { generateIndexName } from '../../utils/string';
import { AbstractQueryGenerator } from '../abstract/query-generator';
import {
Expand All @@ -20,7 +21,6 @@ import { MariaDbQueryGeneratorInternal } from './query-generator-internal.js';
import type { MariaDbDialect } from './index.js';

const REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS = new Set<keyof RemoveIndexQueryOptions>(['ifExists']);
const TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS = new Set<keyof TruncateTableQueryOptions>();

/**
* Temporary class to ease the TypeScript migration
Expand Down Expand Up @@ -74,9 +74,9 @@ export class MariaDbQueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'truncateTableQuery',
this.dialect.name,
this.dialect,
TRUNCATE_TABLE_QUERY_SUPPORTABLE_OPTIONS,
TRUNCATE_TABLE_QUERY_SUPPORTED_OPTIONS,
EMPTY_SET,
options,
);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ export class MariaDbQueryGeneratorTypeScript extends AbstractQueryGenerator {
if (options) {
rejectInvalidOptions(
'removeIndexQuery',
this.dialect.name,
this.dialect,
REMOVE_INDEX_QUERY_SUPPORTABLE_OPTIONS,
REMOVE_INDEX_QUERY_SUPPORTED_OPTIONS,
options,
Expand Down
Loading

0 comments on commit c091578

Please sign in to comment.