Skip to content

Commit

Permalink
move table filtering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kononnable committed Sep 6, 2020
1 parent 06653f9 commit a3edec7
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 102 deletions.
4 changes: 2 additions & 2 deletions src/IConnectionOptions.ts
Expand Up @@ -17,7 +17,7 @@ export default interface IConnectionOptions {
schemaName: string;
ssl: boolean;
skipTables: string[];
tables: string[];
onlyTables: string[];
}

export function getDefaultConnectionOptions(): IConnectionOptions {
Expand All @@ -31,7 +31,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions {
schemaName: "",
ssl: false,
skipTables: [],
tables: [],
onlyTables: [],
};
return connectionOptions;
}
38 changes: 23 additions & 15 deletions src/drivers/AbstractDriver.ts
Expand Up @@ -69,9 +69,7 @@ export default abstract class AbstractDriver {

public abstract GetAllTablesQuery: (
schema: string,
dbNames: string,
notIntTables: string[],
inTables: string[]
dbNames: string
) => Promise<
{
TABLE_SCHEMA: string;
Expand Down Expand Up @@ -189,9 +187,7 @@ export default abstract class AbstractDriver {
);
dbModel = await this.GetAllTables(
sqlEscapedSchema,
connectionOptions.databaseName,
connectionOptions.skipTables,
connectionOptions.tables
connectionOptions.databaseName
);
await this.GetCoulmnsFromEntity(
dbModel,
Expand All @@ -212,23 +208,35 @@ export default abstract class AbstractDriver {
);
await this.DisconnectFromServer();
dbModel = AbstractDriver.FindManyToManyRelations(dbModel);
dbModel = AbstractDriver.FilterGeneratedTables(
dbModel,
connectionOptions.skipTables,
connectionOptions.onlyTables
);
return dbModel;
}

static FilterGeneratedTables(
dbModel: Entity[],
skipTables: string[],
onlyTables: string[]
): Entity[] {
return dbModel
.filter((table) => !skipTables.includes(table.sqlName))
.filter(
(table) =>
onlyTables.length === 0 ||
onlyTables.includes(table.sqlName)
);
}

public abstract async ConnectToServer(connectionOptons: IConnectionOptions);

public async GetAllTables(
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
dbNames: string
): Promise<Entity[]> {
const response = await this.GetAllTablesQuery(
schema,
dbNames,
notInTables,
inTables
);
const response = await this.GetAllTablesQuery(schema, dbNames);
const ret: Entity[] = [] as Entity[];
response.forEach((val) => {
ret.push({
Expand Down
17 changes: 2 additions & 15 deletions src/drivers/MssqlDriver.ts
Expand Up @@ -37,21 +37,8 @@ export default class MssqlDriver extends AbstractDriver {
}
}

public GetAllTablesQuery = async (
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
) => {
public GetAllTablesQuery = async (schema: string, dbNames: string) => {
const request = new this.MSSQL.Request(this.Connection);
const tableCondition =
notInTables.length > 0
? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')`
: "";
const inTableCondition =
inTables.length > 0
? ` AND TABLE_NAME IN ('${inTables.join("','")}')`
: "";
const response: {
TABLE_SCHEMA: string;
TABLE_NAME: string;
Expand All @@ -61,7 +48,7 @@ export default class MssqlDriver extends AbstractDriver {
`SELECT TABLE_SCHEMA,TABLE_NAME, table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList(
dbNames
)}) ${tableCondition} ${inTableCondition}`
)})`
)
).recordset;
return response;
Expand Down
17 changes: 2 additions & 15 deletions src/drivers/MysqlDriver.ts
Expand Up @@ -39,20 +39,7 @@ export default class MysqlDriver extends AbstractDriver {
}
}

public GetAllTablesQuery = async (
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
) => {
const tableCondition =
notInTables.length > 0
? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')`
: "";
const inTableCondition =
inTables.length > 0
? ` AND TABLE_NAME IN ('${inTables.join("','")}')`
: "";
public GetAllTablesQuery = async (schema: string, dbNames: string) => {
const response = this.ExecQuery<{
TABLE_SCHEMA: string;
TABLE_NAME: string;
Expand All @@ -62,7 +49,7 @@ export default class MysqlDriver extends AbstractDriver {
WHERE table_type='BASE TABLE'
AND table_schema IN (${MysqlDriver.escapeCommaSeparatedList(
dbNames
)}) ${tableCondition} ${inTableCondition}`);
)})`);
return response;
};

Expand Down
17 changes: 2 additions & 15 deletions src/drivers/OracleDriver.ts
Expand Up @@ -38,27 +38,14 @@ export default class OracleDriver extends AbstractDriver {
}
}

public GetAllTablesQuery = async (
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
) => {
const tableCondition =
notInTables.length > 0
? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')`
: "";
const inTableCondition =
inTables.length > 0
? ` AND TABLE_NAME IN ('${inTables.join("','")}')`
: "";
public GetAllTablesQuery = async (schema: string, dbNames: string) => {
const response = (
await this.Connection.execute<{
TABLE_SCHEMA: string;
TABLE_NAME: string;
DB_NAME: string;
}>(
`SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual) ${tableCondition} ${inTableCondition}`
`SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual)`
)
).rows!;
return response;
Expand Down
17 changes: 2 additions & 15 deletions src/drivers/PostgresDriver.ts
Expand Up @@ -37,27 +37,14 @@ export default class PostgresDriver extends AbstractDriver {
}
}

public GetAllTablesQuery = async (
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
) => {
const tableCondition =
notInTables.length > 0
? ` AND NOT table_name IN ('${notInTables.join("','")}')`
: "";
const inTableCondition =
inTables.length > 0
? ` AND table_name IN ('${inTables.join("','")}')`
: "";
public GetAllTablesQuery = async (schema: string, dbNames: string) => {
const response: {
TABLE_SCHEMA: string;
TABLE_NAME: string;
DB_NAME: string;
}[] = (
await this.Connection.query(
`SELECT table_schema as "TABLE_SCHEMA",table_name as "TABLE_NAME", table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND table_schema in (${schema}) ${tableCondition} ${inTableCondition}`
`SELECT table_schema as "TABLE_SCHEMA",table_name as "TABLE_NAME", table_catalog as "DB_NAME" FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND table_schema in (${schema})`
)
).rows;
return response;
Expand Down
14 changes: 2 additions & 12 deletions src/drivers/SqliteDriver.ts
Expand Up @@ -46,22 +46,12 @@ export default class SqliteDriver extends AbstractDriver {

public async GetAllTables(
schema: string,
dbNames: string,
notInTables: string[],
inTables: string[]
dbNames: string
): Promise<Entity[]> {
const ret: Entity[] = [] as Entity[];
const tableCondition =
notInTables.length > 0
? ` AND NOT tbl_name IN ('${notInTables.join("','")}')`
: "";
const inTableCondition =
inTables.length > 0
? ` AND tbl_name IN ('${inTables.join("','")}')`
: "";
// eslint-disable-next-line camelcase
const rows = await this.ExecQuery<{ tbl_name: string; sql: string }>(
`SELECT tbl_name, sql FROM "sqlite_master" WHERE "type" = 'table' AND name NOT LIKE 'sqlite_%' ${tableCondition} ${inTableCondition}`
`SELECT tbl_name, sql FROM "sqlite_master" WHERE "type" = 'table' AND name NOT LIKE 'sqlite_%'`
);
rows.forEach((val) => {
if (val.sql.includes("AUTOINCREMENT")) {
Expand Down
10 changes: 5 additions & 5 deletions src/index.ts
Expand Up @@ -272,7 +272,7 @@ function checkYargsParameters(options: options): options {
},
tables: {
string: true,
default: options.connectionOptions.tables.join(","),
default: options.connectionOptions.onlyTables.join(","),
describe:
"Generate specific tables. You can pass multiple values separated by comma",
},
Expand Down Expand Up @@ -316,7 +316,7 @@ function checkYargsParameters(options: options): options {
tables = [];
}
options.connectionOptions.skipTables = skipTables;
options.connectionOptions.tables = tables;
options.connectionOptions.onlyTables = tables;
options.generationOptions.activeRecord = argv.a;
options.generationOptions.generateConstructor = argv.generateConstructor;
options.generationOptions.convertCaseEntity = argv.ce as IGenerationOptions["convertCaseEntity"];
Expand Down Expand Up @@ -466,7 +466,7 @@ async function useInquirer(options: options): Promise<options> {
const optionsMapper = {
"All of them": () => {
options.connectionOptions.skipTables = [];
options.connectionOptions.tables = [];
options.connectionOptions.onlyTables = [];
},
"Ignore specific tables": async () => {
const { tableNames } = await inquirer.prompt({
Expand All @@ -479,12 +479,12 @@ async function useInquirer(options: options): Promise<options> {
},
"Select specific tables": async () => {
const { tableNames } = await inquirer.prompt({
default: options.connectionOptions.tables.join(","),
default: options.connectionOptions.onlyTables.join(","),
message: "Table names(separated by comma)",
name: "tableNames",
type: "input",
});
options.connectionOptions.tables = tableNames.split(",");
options.connectionOptions.onlyTables = tableNames.split(",");
},
};

Expand Down
4 changes: 2 additions & 2 deletions test/integration/runTestsFromPath.test.ts
Expand Up @@ -351,7 +351,7 @@ async function prepareTestRuns(
schemaName: "ignored",
ssl: yn(process.env.MYSQL_SSL, { default: false }),
skipTables: [],
tables: [],
onlyTables: [],
};
break;
case "mariadb":
Expand All @@ -365,7 +365,7 @@ async function prepareTestRuns(
schemaName: "ignored",
ssl: yn(process.env.MARIADB_SSL, { default: false }),
skipTables: [],
tables: []
onlyTables: []
};
break;

Expand Down
12 changes: 6 additions & 6 deletions test/utils/GeneralTestUtils.ts
Expand Up @@ -32,7 +32,7 @@ export async function createMSSQLModels(
schemaName: "dbo,sch1,sch2",
ssl: yn(process.env.MSSQL_SSL, { default: false }),
skipTables: [],
tables: []
onlyTables: []
};
await driver.ConnectToServer(connectionOptions);
connectionOptions.databaseName = String(process.env.MSSQL_Database);
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function createPostgresModels(
schemaName: "public,sch1,sch2",
ssl: yn(process.env.POSTGRES_SSL, { default: false }),
skipTables: ["spatial_ref_sys"],
tables: []
onlyTables: []
};
await driver.ConnectToServer(connectionOptions);
connectionOptions.databaseName = String(process.env.POSTGRES_Database);
Expand Down Expand Up @@ -137,7 +137,7 @@ export async function createSQLiteModels(
schemaName: "",
ssl: false,
skipTables: [],
tables: []
onlyTables: []
};

const connOpt: ConnectionOptions = {
Expand Down Expand Up @@ -173,7 +173,7 @@ export async function createMysqlModels(
schemaName: "ignored",
ssl: yn(process.env.MYSQL_SSL, { default: false }),
skipTables: [],
tables: []
onlyTables: []
};
await driver.ConnectToServer(connectionOptions);

Expand Down Expand Up @@ -217,7 +217,7 @@ export async function createMariaDBModels(
schemaName: "ignored",
ssl: yn(process.env.MARIADB_SSL, { default: false }),
skipTables: [],
tables: []
onlyTables: []
};
await driver.ConnectToServer(connectionOptions);

Expand Down Expand Up @@ -263,7 +263,7 @@ export async function createOracleDBModels(
schemaName: String(process.env.ORACLE_Username),
ssl: yn(process.env.ORACLE_SSL, { default: false }),
skipTables: [],
tables: []
onlyTables: []
};
await driver.ConnectToServer(connectionOptions);
connectionOptions.user = String(process.env.ORACLE_Username);
Expand Down

0 comments on commit a3edec7

Please sign in to comment.