diff --git a/src/commands/EntityCreateCommand.ts b/src/commands/EntityCreateCommand.ts index 8e75c3685..bbeb1b8aa 100644 --- a/src/commands/EntityCreateCommand.ts +++ b/src/commands/EntityCreateCommand.ts @@ -45,7 +45,9 @@ export class EntityCreateCommand { } catch (err) { } } - await CommandUtils.createFile(process.cwd() + "/" + (directory ? (directory + "/") : "") + filename, fileContent); + const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename; + await CommandUtils.createFile(path, fileContent); + console.log(`Entity "${path}" has been created successfully.`); } // ------------------------------------------------------------------------- diff --git a/src/commands/MigrationCreateCommand.ts b/src/commands/MigrationCreateCommand.ts index da606ab16..f2d987104 100644 --- a/src/commands/MigrationCreateCommand.ts +++ b/src/commands/MigrationCreateCommand.ts @@ -49,7 +49,9 @@ export class MigrationCreateCommand { } catch (err) { } } - await CommandUtils.createFile(process.cwd() + "/" + (directory ? (directory + "/") : "") + filename, fileContent); + const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename; + await CommandUtils.createFile(path, fileContent); + console.log(`Migration "${path}" has been generated successfully.`); } // ------------------------------------------------------------------------- diff --git a/src/commands/MigrationGenerateCommand.ts b/src/commands/MigrationGenerateCommand.ts index 5955ad202..b9059bbd0 100644 --- a/src/commands/MigrationGenerateCommand.ts +++ b/src/commands/MigrationGenerateCommand.ts @@ -86,13 +86,14 @@ export class MigrationGenerateCommand { await CommandUtils.createFile(path, fileContent); if (upSqls.length) { - console.log(`Migration ${path} has been generated successfully.`); + console.log(`Migration "${path}" has been generated successfully.`); } else { console.error(`No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migrations:create" command`); } } catch (err) { console.error(err); + // throw err; } finally { if (connection) diff --git a/src/commands/MigrationRevertCommand.ts b/src/commands/MigrationRevertCommand.ts index 919644650..7ccdefede 100644 --- a/src/commands/MigrationRevertCommand.ts +++ b/src/commands/MigrationRevertCommand.ts @@ -36,7 +36,7 @@ export class MigrationRevertCommand { await connection.undoLastMigration(); } catch (err) { - connection.logger.log("error", err); + console.error(err); } finally { await connection.close(); @@ -44,7 +44,7 @@ export class MigrationRevertCommand { } catch (err) { console.error(err); - throw err; + // throw err; } } diff --git a/src/commands/MigrationRunCommand.ts b/src/commands/MigrationRunCommand.ts index 4a7df48fb..9a65679c3 100644 --- a/src/commands/MigrationRunCommand.ts +++ b/src/commands/MigrationRunCommand.ts @@ -36,7 +36,7 @@ export class MigrationRunCommand { await connection.runMigrations(); } catch (err) { - connection.logger.log("error", err); + console.error(err); } finally { await connection.close(); @@ -44,7 +44,7 @@ export class MigrationRunCommand { } catch (err) { console.error(err); - throw err; + // throw err; } } diff --git a/src/commands/QueryCommand.ts b/src/commands/QueryCommand.ts index 83162c52c..d1e815e6d 100644 --- a/src/commands/QueryCommand.ts +++ b/src/commands/QueryCommand.ts @@ -33,11 +33,11 @@ export class QueryCommand { try { queryRunner = await connection.createQueryRunner(); const queryResult = await queryRunner.query(argv._[1]); - connection.logger.log("info", "Query executed. Result: " + JSON.stringify(queryResult)); + console.log("Query executed. Result: " + JSON.stringify(queryResult)); } catch (err) { - connection.logger.log("error", err); - throw err; + console.error(err); + // throw err; } finally { if (queryRunner) diff --git a/src/commands/SchemaDropCommand.ts b/src/commands/SchemaDropCommand.ts index 33a532e25..9feda586b 100644 --- a/src/commands/SchemaDropCommand.ts +++ b/src/commands/SchemaDropCommand.ts @@ -44,9 +44,8 @@ export class SchemaDropCommand { } } catch (err) { - if (connection) - (connection as Connection).logger.log("error", err); - throw err; + console.error(err); + // throw err; } finally { if (connection) diff --git a/src/commands/SchemaSyncCommand.ts b/src/commands/SchemaSyncCommand.ts index 5ea3f1684..24b4d5bed 100644 --- a/src/commands/SchemaSyncCommand.ts +++ b/src/commands/SchemaSyncCommand.ts @@ -44,9 +44,8 @@ export class SchemaSyncCommand { } } catch (err) { - if (connection) - (connection as Connection).logger.log("error", err); - throw err; + console.error(err); + // throw err; } finally { if (connection) diff --git a/src/commands/SchemaSyncLogCommand.ts b/src/commands/SchemaSyncLogCommand.ts index 4f610ee8c..f59fa08e6 100644 --- a/src/commands/SchemaSyncLogCommand.ts +++ b/src/commands/SchemaSyncLogCommand.ts @@ -44,9 +44,8 @@ export class SchemaSyncLogCommand { }); } catch (err) { - if (connection) - (connection as Connection).logger.log("error", err); - throw err; + console.error(err); + // throw err; } finally { if (connection) diff --git a/src/commands/SubscriberCreateCommand.ts b/src/commands/SubscriberCreateCommand.ts index bde925b76..cd556647b 100644 --- a/src/commands/SubscriberCreateCommand.ts +++ b/src/commands/SubscriberCreateCommand.ts @@ -47,7 +47,9 @@ export class SubscriberCreateCommand { } catch (err) { } } - await CommandUtils.createFile(process.cwd() + "/" + (directory ? (directory + "/") : "") + filename, fileContent); + const path = process.cwd() + "/" + (directory ? (directory + "/") : "") + filename; + await CommandUtils.createFile(path, fileContent); + console.log(`Subscriber "${path}" has been created successfully.`); } // ------------------------------------------------------------------------- diff --git a/src/driver/mysql/MysqlQueryRunner.ts b/src/driver/mysql/MysqlQueryRunner.ts index 850a3d9a6..1c6cc3d13 100644 --- a/src/driver/mysql/MysqlQueryRunner.ts +++ b/src/driver/mysql/MysqlQueryRunner.ts @@ -155,12 +155,12 @@ export class MysqlQueryRunner implements QueryRunner { throw new QueryRunnerAlreadyReleasedError(); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const databaseConnection = await this.connect(); databaseConnection.query(query, parameters, (err: any, result: any) => { if (err) { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError(err, this); return fail(err); } diff --git a/src/driver/oracle/OracleQueryRunner.ts b/src/driver/oracle/OracleQueryRunner.ts index 988813bcc..38e0166e7 100644 --- a/src/driver/oracle/OracleQueryRunner.ts +++ b/src/driver/oracle/OracleQueryRunner.ts @@ -168,11 +168,11 @@ export class OracleQueryRunner implements QueryRunner { throw new QueryRunnerAlreadyReleasedError(); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const handler = (err: any, result: any) => { if (err) { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError(err, this); return fail(err); } diff --git a/src/driver/postgres/PostgresQueryRunner.ts b/src/driver/postgres/PostgresQueryRunner.ts index dd1d5309b..81b8ca3cb 100644 --- a/src/driver/postgres/PostgresQueryRunner.ts +++ b/src/driver/postgres/PostgresQueryRunner.ts @@ -106,8 +106,8 @@ export class PostgresQueryRunner implements QueryRunner { connection.query(`SET search_path TO '${this.schemaName}', 'public';`, (err: any) => { if (err) { - this.driver.connection.logger.logFailedQuery(`SET search_path TO '${this.schemaName}', 'public';`); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(`SET search_path TO '${this.schemaName}', 'public';`, [], this); + this.driver.connection.logger.logQueryError(err, this); fail(err); } else { ok(connection); @@ -180,12 +180,12 @@ export class PostgresQueryRunner implements QueryRunner { // console.log("query: ", query); // console.log("parameters: ", parameters); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const databaseConnection = await this.connect(); databaseConnection.query(query, parameters, (err: any, result: any) => { if (err) { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError(err, this); fail(err); } else { ok(result.rows); diff --git a/src/driver/sqlite/SqliteQueryRunner.ts b/src/driver/sqlite/SqliteQueryRunner.ts index 1b15d2cc4..7140d3407 100644 --- a/src/driver/sqlite/SqliteQueryRunner.ts +++ b/src/driver/sqlite/SqliteQueryRunner.ts @@ -133,12 +133,12 @@ export class SqliteQueryRunner implements QueryRunner { throw new QueryRunnerAlreadyReleasedError(); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const databaseConnection = await this.connect(); databaseConnection.all(query, parameters, (err: any, result: any) => { if (err) { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError(err, this); fail(err); } else { ok(result); @@ -159,13 +159,13 @@ export class SqliteQueryRunner implements QueryRunner { const parameters = keys.map(key => keyValues[key]); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(sql, parameters); + this.driver.connection.logger.logQuery(sql, parameters, this); const __this = this; const databaseConnection = await this.connect(); databaseConnection.run(sql, parameters, function (err: any): void { if (err) { - __this.driver.connection.logger.logFailedQuery(sql, parameters); - __this.driver.connection.logger.logQueryError(err); + __this.driver.connection.logger.logFailedQuery(sql, parameters, this); + __this.driver.connection.logger.logQueryError(err, this); fail(err); } else { if (generatedColumn) diff --git a/src/driver/sqlserver/SqlServerQueryRunner.ts b/src/driver/sqlserver/SqlServerQueryRunner.ts index 07085a8b9..64c7af806 100644 --- a/src/driver/sqlserver/SqlServerQueryRunner.ts +++ b/src/driver/sqlserver/SqlServerQueryRunner.ts @@ -183,7 +183,7 @@ export class SqlServerQueryRunner implements QueryRunner { const promise = new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const request = new this.driver.mssql.Request(this.isTransactionActive ? this.databaseConnection : this.driver.connectionPool); if (parameters && parameters.length) { parameters.forEach((parameter, index) => { @@ -203,8 +203,8 @@ export class SqlServerQueryRunner implements QueryRunner { let promiseIndex = this.queryResponsibilityChain.indexOf(promise); let waitingPromiseIndex = this.queryResponsibilityChain.indexOf(waitingPromise); if (err) { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError((err.originalError && err.originalError.info) ? err.originalError.info.message : err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError((err.originalError && err.originalError.info) ? err.originalError.info.message : err, this); resolveChain(); return fail(err); } diff --git a/src/driver/websql/WebsqlQueryRunner.ts b/src/driver/websql/WebsqlQueryRunner.ts index 5b4702704..4d26a848b 100644 --- a/src/driver/websql/WebsqlQueryRunner.ts +++ b/src/driver/websql/WebsqlQueryRunner.ts @@ -175,7 +175,7 @@ export class WebsqlQueryRunner implements QueryRunner { return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(query, parameters); + this.driver.connection.logger.logQuery(query, parameters, this); const db = await this.connect(); // todo(dima): check if transaction is not active db.transaction((tx: any) => { @@ -187,8 +187,8 @@ export class WebsqlQueryRunner implements QueryRunner { ok(rows); }, (tx: any, err: any) => { - this.driver.connection.logger.logFailedQuery(query, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(query, parameters, this); + this.driver.connection.logger.logQueryError(err, this); return fail(err); }); }); @@ -207,7 +207,7 @@ export class WebsqlQueryRunner implements QueryRunner { const parameters = keys.map(key => keyValues[key]); return new Promise(async (ok, fail) => { - this.driver.connection.logger.logQuery(sql, parameters); + this.driver.connection.logger.logQuery(sql, parameters, this); const db = await this.connect(); // todo: check if transaction is not active @@ -218,8 +218,8 @@ export class WebsqlQueryRunner implements QueryRunner { ok(); }, (tx: any, err: any) => { - this.driver.connection.logger.logFailedQuery(sql, parameters); - this.driver.connection.logger.logQueryError(err); + this.driver.connection.logger.logFailedQuery(sql, parameters, this); + this.driver.connection.logger.logQueryError(err, this); return fail(err); }); }); diff --git a/src/entity-manager/EntityManager.ts b/src/entity-manager/EntityManager.ts index 1db7db32e..19c761642 100644 --- a/src/entity-manager/EntityManager.ts +++ b/src/entity-manager/EntityManager.ts @@ -738,7 +738,8 @@ export class EntityManager { const queryRunner = this.queryRunner || this.connection.createQueryRunner(); try { const transactionEntityManager = this.connection.createIsolatedManager(queryRunner); - // transactionEntityManager.data = + if (options && options.data) + transactionEntityManager.data = options.data; const databaseEntityLoader = new SubjectBuilder(this.connection, queryRunner); await databaseEntityLoader.persist(entity, metadata); @@ -760,6 +761,8 @@ export class EntityManager { const queryRunner = this.queryRunner || this.connection.createQueryRunner(); try { const transactionEntityManager = this.connection.createIsolatedManager(queryRunner); + if (options && options.data) + transactionEntityManager.data = options.data; const databaseEntityLoader = new SubjectBuilder(this.connection, queryRunner); await databaseEntityLoader.remove(entity, metadata); diff --git a/src/logger/Logger.ts b/src/logger/Logger.ts index 7d2e80fce..ff1b4cfce 100644 --- a/src/logger/Logger.ts +++ b/src/logger/Logger.ts @@ -1,5 +1,6 @@ import {LoggerOptions} from "./LoggerOptions"; import {PlatformTools} from "../platform/PlatformTools"; +import {QueryRunner} from "../query-runner/QueryRunner"; /** * Performs logging of the events in TypeORM. @@ -22,49 +23,50 @@ export class Logger { /** * Logs query and parameters used in it. */ - logQuery(query: string, parameters?: any[]) { + logQuery(query: string, parameters: any[]|undefined, queryRunner?: QueryRunner) { if (this.options.logQueries || PlatformTools.getEnvVariable("LOGGER_CLI_SCHEMA_SYNC")) - this.log("log", `executing query: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`); + this.log("log", `executing query: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`, queryRunner); } /** * Logs query that failed. */ - logFailedQuery(query: string, parameters?: any[]) { + logFailedQuery(query: string, parameters: any[]|undefined, queryRunner?: QueryRunner) { if (this.options.logQueries || this.options.logOnlyFailedQueries || PlatformTools.getEnvVariable("LOGGER_CLI_SCHEMA_SYNC")) - this.log("error", `query failed: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`); + this.log("error", `query failed: ${query}${parameters && parameters.length ? " -- PARAMETERS: " + this.stringifyParams(parameters) : ""}`, queryRunner); } /** * Logs failed query's error. */ - logQueryError(error: any) { + logQueryError(error: any, queryRunner?: QueryRunner) { if (this.options.logFailedQueryError || PlatformTools.getEnvVariable("LOGGER_CLI_SCHEMA_SYNC")) - this.log("error", "error during executing query:" + error); + this.log("error", "error during executing query:" + error, queryRunner); } /** * Logs events from the schema build process. */ - logSchemaBuild(message: string) { + logSchemaBuild(message: string, queryRunner?: QueryRunner) { if (this.options.logSchemaCreation || PlatformTools.getEnvVariable("LOGGER_CLI_SCHEMA_SYNC")) - this.log("info", message); + this.log("info", message, queryRunner); } /** * Perform logging using given logger, or by default to the console. * Log has its own level and message. */ - log(level: "log"|"info"|"warn"|"error", message: any) { + log(level: "log"|"info"|"warn"|"error", message: any, queryRunner?: QueryRunner) { if (!this.options) return; if (this.options.logger) { - this.options.logger(level, message); + this.options.logger(level, message, queryRunner); + } else { switch (level) { case "log": diff --git a/src/logger/LoggerOptions.ts b/src/logger/LoggerOptions.ts index e56cd0a89..98e4dc1fc 100644 --- a/src/logger/LoggerOptions.ts +++ b/src/logger/LoggerOptions.ts @@ -1,3 +1,4 @@ +import {QueryRunner} from "../query-runner/QueryRunner"; /** * Logging options. */ @@ -6,7 +7,7 @@ export interface LoggerOptions { /** * Some specific logger to be used. By default it is a console. */ - readonly logger?: (level: string, message: any) => void; + readonly logger?: (level: string, message: any, queryRunner?: QueryRunner) => void; /** * Set to true if you want to log every executed query. diff --git a/src/migration/MigrationExecutor.ts b/src/migration/MigrationExecutor.ts index 6e1cb5447..23eb13558 100644 --- a/src/migration/MigrationExecutor.ts +++ b/src/migration/MigrationExecutor.ts @@ -34,7 +34,6 @@ export class MigrationExecutor { * thus not saved in the database. */ async executePendingMigrations(): Promise { - const entityManager = this.connection.createIsolatedManager(this.queryRunner); // create migrations table if its not created yet await this.createMigrationsTableIfNotExist(); @@ -115,7 +114,6 @@ export class MigrationExecutor { * Reverts last migration that were run. */ async undoLastMigration(): Promise { - const entityManager = this.connection.createIsolatedManager(this.queryRunner); // create migrations table if its not created yet await this.createMigrationsTableIfNotExist();