move table filtering logic
This commit is contained in:
parent
06653f96ed
commit
a3edec7119
@ -17,7 +17,7 @@ export default interface IConnectionOptions {
|
||||
schemaName: string;
|
||||
ssl: boolean;
|
||||
skipTables: string[];
|
||||
tables: string[];
|
||||
onlyTables: string[];
|
||||
}
|
||||
|
||||
export function getDefaultConnectionOptions(): IConnectionOptions {
|
||||
@ -31,7 +31,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions {
|
||||
schemaName: "",
|
||||
ssl: false,
|
||||
skipTables: [],
|
||||
tables: [],
|
||||
onlyTables: [],
|
||||
};
|
||||
return connectionOptions;
|
||||
}
|
||||
|
@ -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;
|
||||
@ -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,
|
||||
@ -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({
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
|
@ -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;
|
||||
@ -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;
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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")) {
|
||||
|
10
src/index.ts
10
src/index.ts
@ -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",
|
||||
},
|
||||
@ -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"];
|
||||
@ -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({
|
||||
@ -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(",");
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -351,7 +351,7 @@ async function prepareTestRuns(
|
||||
schemaName: "ignored",
|
||||
ssl: yn(process.env.MYSQL_SSL, { default: false }),
|
||||
skipTables: [],
|
||||
tables: [],
|
||||
onlyTables: [],
|
||||
};
|
||||
break;
|
||||
case "mariadb":
|
||||
@ -365,7 +365,7 @@ async function prepareTestRuns(
|
||||
schemaName: "ignored",
|
||||
ssl: yn(process.env.MARIADB_SSL, { default: false }),
|
||||
skipTables: [],
|
||||
tables: []
|
||||
onlyTables: []
|
||||
};
|
||||
break;
|
||||
|
||||
|
@ -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);
|
||||
@ -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);
|
||||
@ -137,7 +137,7 @@ export async function createSQLiteModels(
|
||||
schemaName: "",
|
||||
ssl: false,
|
||||
skipTables: [],
|
||||
tables: []
|
||||
onlyTables: []
|
||||
};
|
||||
|
||||
const connOpt: ConnectionOptions = {
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
Loading…
Reference in New Issue
Block a user