From 15638307715cc332e2c86dcc2d7c2afe9c452c1f Mon Sep 17 00:00:00 2001 From: pc3b3r Date: Thu, 20 Aug 2020 15:52:49 +0200 Subject: [PATCH 1/8] add connectionOptions for tables selection --- src/IConnectionOptions.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index b3299e3..37b0b8a 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -17,6 +17,7 @@ export default interface IConnectionOptions { schemaName: string; ssl: boolean; skipTables: string[]; + tables: string[]; } export function getDefaultConnectionOptions(): IConnectionOptions { @@ -30,6 +31,7 @@ export function getDefaultConnectionOptions(): IConnectionOptions { schemaName: "", ssl: false, skipTables: [], + tables: [], }; return connectionOptions; } From ef5371c19664336a6b94ab424840615fd7e13fda Mon Sep 17 00:00:00 2001 From: pc3b3r Date: Thu, 20 Aug 2020 16:01:18 +0200 Subject: [PATCH 2/8] inquirer options for selecting specific tables --- src/index.ts | 57 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/src/index.ts b/src/index.ts index 573b635..165d8c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,8 +7,8 @@ import IConnectionOptions, { import IGenerationOptions, { getDefaultGenerationOptions, } from "./IGenerationOptions"; - import fs = require("fs-extra"); + import inquirer = require("inquirer"); import path = require("path"); @@ -270,6 +270,12 @@ function checkYargsParameters(options: options): options { describe: "Skip schema generation for specific tables. You can pass multiple values separated by comma", }, + tables: { + string: true, + default: options.connectionOptions.tables.join(","), + describe: + "Generate specific tables. You can pass multiple values separated by comma", + }, strictMode: { choices: ["none", "?", "!"], default: options.generationOptions.strictMode, @@ -305,7 +311,12 @@ function checkYargsParameters(options: options): options { if (skipTables.length === 1 && skipTables[0] === "") { skipTables = []; // #252 } + let tables = argv.tables.split(","); + if (tables.length === 1 && tables[0] === "") { + tables = []; + } options.connectionOptions.skipTables = skipTables; + options.connectionOptions.tables = tables; options.generationOptions.activeRecord = argv.a; options.generationOptions.generateConstructor = argv.generateConstructor; options.generationOptions.convertCaseEntity = argv.ce as IGenerationOptions["convertCaseEntity"]; @@ -441,23 +452,43 @@ async function useInquirer(options: options): Promise { ? "All of them" : "Ignore specific tables", message: "Generate schema for tables:", - choices: ["All of them", "Ignore specific tables"], + choices: [ + "All of them", + "Ignore specific tables", + "Select specific tables", + ], name: "specyficTables", type: "list", }, ]) ).specyficTables; - if (ignoreSpecyficTables === "Ignore specific tables") { - const { tableNames } = await inquirer.prompt({ - default: options.connectionOptions.skipTables.join(","), - message: "Table names(separated by comma)", - name: "tableNames", - type: "input", - }); - options.connectionOptions.skipTables = tableNames.split(","); - } else { - options.connectionOptions.skipTables = []; - } + + const optionsMapper = { + "All of them": () => { + options.connectionOptions.skipTables = []; + options.connectionOptions.tables = []; + }, + "Ignore specific tables": async () => { + const { tableNames } = await inquirer.prompt({ + default: options.connectionOptions.skipTables.join(","), + message: "Table names(separated by comma)", + name: "tableNames", + type: "input", + }); + options.connectionOptions.skipTables = tableNames.split(","); + }, + "Select specific tables": async () => { + const { tableNames } = await inquirer.prompt({ + default: options.connectionOptions.tables.join(","), + message: "Table names(separated by comma)", + name: "tableNames", + type: "input", + }); + options.connectionOptions.tables = tableNames.split(","); + }, + }; + + await optionsMapper[ignoreSpecyficTables](); options.generationOptions.resultsPath = ( await inquirer.prompt([ From defe6fd63ffab3d6165c22c27f4f719428fd45cd Mon Sep 17 00:00:00 2001 From: pc3b3r Date: Thu, 20 Aug 2020 16:02:12 +0200 Subject: [PATCH 3/8] driver params for IN tables --- src/drivers/AbstractDriver.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index 7db7feb..c2eec21 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -70,7 +70,8 @@ export default abstract class AbstractDriver { public abstract GetAllTablesQuery: ( schema: string, dbNames: string, - tableNames: string[] + notIntTables: string[], + inTables: string[] ) => Promise< { TABLE_SCHEMA: string; @@ -189,7 +190,8 @@ export default abstract class AbstractDriver { dbModel = await this.GetAllTables( sqlEscapedSchema, connectionOptions.databaseName, - connectionOptions.skipTables + connectionOptions.skipTables, + connectionOptions.tables ); await this.GetCoulmnsFromEntity( dbModel, @@ -218,12 +220,14 @@ export default abstract class AbstractDriver { public async GetAllTables( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ): Promise { const response = await this.GetAllTablesQuery( schema, dbNames, - tableNames + notInTables, + inTables ); const ret: Entity[] = [] as Entity[]; response.forEach((val) => { From b1335eed7f1166f913d6cb12f718bbab7a420ef8 Mon Sep 17 00:00:00 2001 From: pc3b3r Date: Thu, 20 Aug 2020 16:03:00 +0200 Subject: [PATCH 4/8] drivers query IN --- src/drivers/MssqlDriver.ts | 13 +++++++++---- src/drivers/MysqlDriver.ts | 13 +++++++++---- src/drivers/OracleDriver.ts | 13 +++++++++---- src/drivers/PostgresDriver.ts | 13 +++++++++---- src/drivers/SqliteDriver.ts | 13 +++++++++---- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index ccc846f..054eead 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -40,12 +40,17 @@ export default class MssqlDriver extends AbstractDriver { public GetAllTablesQuery = async ( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ) => { const request = new this.MSSQL.Request(this.Connection); const tableCondition = - tableNames.length > 0 - ? ` AND NOT TABLE_NAME IN ('${tableNames.join("','")}')` + 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; @@ -56,7 +61,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}` + )}) ${tableCondition} ${inTableCondition}` ) ).recordset; return response; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index 95d7f17..fbb259d 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -42,11 +42,16 @@ export default class MysqlDriver extends AbstractDriver { public GetAllTablesQuery = async ( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ) => { const tableCondition = - tableNames.length > 0 - ? ` AND NOT TABLE_NAME IN ('${tableNames.join("','")}')` + notInTables.length > 0 + ? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')` + : ""; + const inTableCondition = + inTables.length > 0 + ? ` AND TABLE_NAME IN ('${inTables.join("','")}')` : ""; const response = this.ExecQuery<{ TABLE_SCHEMA: string; @@ -57,7 +62,7 @@ export default class MysqlDriver extends AbstractDriver { WHERE table_type='BASE TABLE' AND table_schema IN (${MysqlDriver.escapeCommaSeparatedList( dbNames - )}) ${tableCondition}`); + )}) ${tableCondition} ${inTableCondition}`); return response; }; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index 2282887..b283c13 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -41,11 +41,16 @@ export default class OracleDriver extends AbstractDriver { public GetAllTablesQuery = async ( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ) => { const tableCondition = - tableNames.length > 0 - ? ` AND NOT TABLE_NAME IN ('${tableNames.join("','")}')` + notInTables.length > 0 + ? ` AND NOT TABLE_NAME IN ('${notInTables.join("','")}')` + : ""; + const inTableCondition = + inTables.length > 0 + ? ` AND TABLE_NAME IN ('${inTables.join("','")}')` : ""; const response = ( await this.Connection.execute<{ @@ -53,7 +58,7 @@ export default class OracleDriver extends AbstractDriver { 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}` + `SELECT NULL AS TABLE_SCHEMA, TABLE_NAME, NULL AS DB_NAME FROM all_tables WHERE owner = (select user from dual) ${tableCondition} ${inTableCondition}` ) ).rows!; return response; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 6ac3d1b..a5884cc 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -40,11 +40,16 @@ export default class PostgresDriver extends AbstractDriver { public GetAllTablesQuery = async ( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ) => { const tableCondition = - tableNames.length > 0 - ? ` AND NOT table_name IN ('${tableNames.join("','")}')` + 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; @@ -52,7 +57,7 @@ export default class PostgresDriver extends AbstractDriver { 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}` + `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}` ) ).rows; return response; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index 1883a97..1c8ed19 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -47,16 +47,21 @@ export default class SqliteDriver extends AbstractDriver { public async GetAllTables( schema: string, dbNames: string, - tableNames: string[] + notInTables: string[], + inTables: string[] ): Promise { const ret: Entity[] = [] as Entity[]; const tableCondition = - tableNames.length > 0 - ? ` AND NOT tbl_name IN ('${tableNames.join("','")}')` + 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}` + `SELECT tbl_name, sql FROM "sqlite_master" WHERE "type" = 'table' AND name NOT LIKE 'sqlite_%' ${tableCondition} ${inTableCondition}` ); rows.forEach((val) => { if (val.sql.includes("AUTOINCREMENT")) { From 06653f96edda6bcda658c26e4fe4da0937aa8588 Mon Sep 17 00:00:00 2001 From: pc3b3r Date: Thu, 20 Aug 2020 16:03:23 +0200 Subject: [PATCH 5/8] IConnectionOptions fix for tests --- test/integration/runTestsFromPath.test.ts | 6 ++++-- test/utils/GeneralTestUtils.ts | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index f645fa0..66813b8 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -350,7 +350,8 @@ async function prepareTestRuns( databaseType: "mysql", schemaName: "ignored", ssl: yn(process.env.MYSQL_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [], }; break; case "mariadb": @@ -363,7 +364,8 @@ async function prepareTestRuns( databaseType: "mariadb", schemaName: "ignored", ssl: yn(process.env.MARIADB_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [] }; break; diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index 813ebd1..55163a9 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -31,7 +31,8 @@ export async function createMSSQLModels( databaseType: "mssql", schemaName: "dbo,sch1,sch2", ssl: yn(process.env.MSSQL_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.databaseName = String(process.env.MSSQL_Database); @@ -83,7 +84,8 @@ export async function createPostgresModels( databaseType: "postgres", schemaName: "public,sch1,sch2", ssl: yn(process.env.POSTGRES_SSL, { default: false }), - skipTables: ["spatial_ref_sys"] + skipTables: ["spatial_ref_sys"], + tables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.databaseName = String(process.env.POSTGRES_Database); @@ -134,7 +136,8 @@ export async function createSQLiteModels( databaseType: "sqlite", schemaName: "", ssl: false, - skipTables: [] + skipTables: [], + tables: [] }; const connOpt: ConnectionOptions = { @@ -169,7 +172,8 @@ export async function createMysqlModels( databaseType: "mysql", schemaName: "ignored", ssl: yn(process.env.MYSQL_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [] }; await driver.ConnectToServer(connectionOptions); @@ -212,7 +216,8 @@ export async function createMariaDBModels( databaseType: "mariadb", schemaName: "ignored", ssl: yn(process.env.MARIADB_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [] }; await driver.ConnectToServer(connectionOptions); @@ -257,7 +262,8 @@ export async function createOracleDBModels( databaseType: "oracle", schemaName: String(process.env.ORACLE_Username), ssl: yn(process.env.ORACLE_SSL, { default: false }), - skipTables: [] + skipTables: [], + tables: [] }; await driver.ConnectToServer(connectionOptions); connectionOptions.user = String(process.env.ORACLE_Username); From a3edec7119fb9584e7ee1b98d0b22beba023d8ee Mon Sep 17 00:00:00 2001 From: kononnable Date: Sun, 6 Sep 2020 23:19:38 +0200 Subject: [PATCH 6/8] move table filtering logic --- src/IConnectionOptions.ts | 4 +-- src/drivers/AbstractDriver.ts | 38 ++++++++++++++--------- src/drivers/MssqlDriver.ts | 17 ++-------- src/drivers/MysqlDriver.ts | 17 ++-------- src/drivers/OracleDriver.ts | 17 ++-------- src/drivers/PostgresDriver.ts | 17 ++-------- src/drivers/SqliteDriver.ts | 14 ++------- src/index.ts | 10 +++--- test/integration/runTestsFromPath.test.ts | 4 +-- test/utils/GeneralTestUtils.ts | 12 +++---- 10 files changed, 48 insertions(+), 102 deletions(-) diff --git a/src/IConnectionOptions.ts b/src/IConnectionOptions.ts index 37b0b8a..f7dec18 100644 --- a/src/IConnectionOptions.ts +++ b/src/IConnectionOptions.ts @@ -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; } diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index c2eec21..10b7d38 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -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 { - 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({ diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index 054eead..01dcb66 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -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; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index fbb259d..921c310 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -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; }; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index b283c13..9636349 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -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; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index a5884cc..1178e09 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -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; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index 1c8ed19..abe1fa2 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -46,22 +46,12 @@ export default class SqliteDriver extends AbstractDriver { public async GetAllTables( schema: string, - dbNames: string, - notInTables: string[], - inTables: string[] + dbNames: string ): Promise { 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")) { diff --git a/src/index.ts b/src/index.ts index 165d8c5..1cedf6b 100644 --- a/src/index.ts +++ b/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 { 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 { }, "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(","); }, }; diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index 66813b8..c7f048d 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -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; diff --git a/test/utils/GeneralTestUtils.ts b/test/utils/GeneralTestUtils.ts index 55163a9..763225d 100644 --- a/test/utils/GeneralTestUtils.ts +++ b/test/utils/GeneralTestUtils.ts @@ -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); From 79e9ae0fab1622f51a2cfa81036f85abde5c9a58 Mon Sep 17 00:00:00 2001 From: kononnable Date: Mon, 7 Sep 2020 00:12:48 +0200 Subject: [PATCH 7/8] add tests --- test/integration/runTestsFromPath.test.ts | 66 +++++++++++++++++++---- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index c7f048d..1357cca 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -38,22 +38,55 @@ describe("TypeOrm examples", async () => { const testPartialPath = "test/integration/examples"; await runTestsFromPath(testPartialPath, false); }); +describe("Filtering tables", async () => { + const testPartialPath = "test/integration/examples"; + it("skipTables",async ()=>{ + const dbDrivers: string[] = GTU.getEnabledDbDrivers(); + const modelGenerationPromises = dbDrivers.map(async dbDriver => { + const { + generationOptions, + driver, + connectionOptions + } = await prepareTestRuns(testPartialPath, "sample2-one-to-one", dbDriver); + const dbModel = await dataCollectionPhase( + driver, + {...connectionOptions, + skipTables:["Post"]}, + generationOptions + ); + expect(dbModel.length).to.equal(6); + expect(dbModel.find(x=>x.sqlName==="Post")).to.be.undefined; + }); + await Promise.all(modelGenerationPromises); + }); + it("onlyTables",async ()=>{ + const dbDrivers: string[] = GTU.getEnabledDbDrivers(); + const modelGenerationPromises = dbDrivers.map(async dbDriver => { + const { + generationOptions, + driver, + connectionOptions + } = await prepareTestRuns(testPartialPath, "sample2-one-to-one", dbDriver); + const dbModel = await dataCollectionPhase( + driver, + {...connectionOptions, + onlyTables:["Post"]}, + generationOptions + ); + expect(dbModel.length).to.equal(1); + expect(dbModel.find(x=>x.sqlName==="Post")).to.not.be.undefined; + }); + await Promise.all(modelGenerationPromises); + }) + +}); async function runTestsFromPath( testPartialPath: string, isDbSpecific: boolean ) { - const resultsPath = path.resolve(process.cwd(), `output`); - if (!fs.existsSync(resultsPath)) { - fs.mkdirSync(resultsPath); - } const dbDrivers: string[] = GTU.getEnabledDbDrivers(); - dbDrivers.forEach(dbDriver => { - const newDirPath = path.resolve(resultsPath, dbDriver); - if (!fs.existsSync(newDirPath)) { - fs.mkdirSync(newDirPath); - } - }); + createOutputDirs(dbDrivers); const files = fs.readdirSync(path.resolve(process.cwd(), testPartialPath)); if (isDbSpecific) { await runTest(dbDrivers, testPartialPath, files); @@ -64,6 +97,19 @@ async function runTestsFromPath( } } +function createOutputDirs(dbDrivers: string[]) { + const resultsPath = path.resolve(process.cwd(), `output`); + if (!fs.existsSync(resultsPath)) { + fs.mkdirSync(resultsPath); + } + dbDrivers.forEach(dbDriver => { + const newDirPath = path.resolve(resultsPath, dbDriver); + if (!fs.existsSync(newDirPath)) { + fs.mkdirSync(newDirPath); + } + }); +} + function runTestForMultipleDrivers( testName: string, dbDrivers: string[], From 5b290a5632dbb5b2b83e14de8df05c6b51f431df Mon Sep 17 00:00:00 2001 From: kononnable Date: Mon, 7 Sep 2020 00:25:00 +0200 Subject: [PATCH 8/8] fix lint issues --- test/integration/runTestsFromPath.test.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/integration/runTestsFromPath.test.ts b/test/integration/runTestsFromPath.test.ts index 1357cca..0eb7af6 100644 --- a/test/integration/runTestsFromPath.test.ts +++ b/test/integration/runTestsFromPath.test.ts @@ -55,6 +55,7 @@ describe("Filtering tables", async () => { generationOptions ); expect(dbModel.length).to.equal(6); + // eslint-disable-next-line no-unused-expressions expect(dbModel.find(x=>x.sqlName==="Post")).to.be.undefined; }); await Promise.all(modelGenerationPromises); @@ -74,6 +75,7 @@ describe("Filtering tables", async () => { generationOptions ); expect(dbModel.length).to.equal(1); + // eslint-disable-next-line no-unused-expressions expect(dbModel.find(x=>x.sqlName==="Post")).to.not.be.undefined; }); await Promise.all(modelGenerationPromises);