mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed commands issues; made logger to accept query runner
This commit is contained in:
parent
580d70dcf2
commit
db15dd6af9
@ -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.`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -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.`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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.`);
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -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<any[]>(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);
|
||||
|
||||
@ -133,12 +133,12 @@ export class SqliteQueryRunner implements QueryRunner {
|
||||
throw new QueryRunnerAlreadyReleasedError();
|
||||
|
||||
return new Promise<any[]>(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<any[]>(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)
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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<any[]>(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);
|
||||
});
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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":
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -34,7 +34,6 @@ export class MigrationExecutor {
|
||||
* thus not saved in the database.
|
||||
*/
|
||||
async executePendingMigrations(): Promise<void> {
|
||||
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<void> {
|
||||
const entityManager = this.connection.createIsolatedManager(this.queryRunner);
|
||||
|
||||
// create migrations table if its not created yet
|
||||
await this.createMigrationsTableIfNotExist();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user