Skip to content

Commit

Permalink
fix(typeorm#8180) Correctly scope the "handler" function in Nativescr…
Browse files Browse the repository at this point in the history
…iptQueryRunner. this.driver is not defined.

Making the promise async now correctly scopes the handler.
Additional corrections were made to the syntax, eg conditionals.
  • Loading branch information
pekevski committed Sep 14, 2021
1 parent a868078 commit f41fa8d
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/driver/nativescript/NativescriptQueryRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,55 @@ export class NativescriptQueryRunner extends AbstractSqliteQueryRunner {
* Executes a given SQL query.
*/
async query(query: string, parameters?: any[], useStructuredResult = false): Promise<any> {
if (this.isReleased)

if (this.isReleased) {
throw new QueryRunnerAlreadyReleasedError();
}

const connection = this.driver.connection;

return new Promise( (ok, fail) => {
return new Promise(async (ok, fail) => {

const databaseConnection = await this.connect();
const isInsertQuery = query.substr(0, 11) === "INSERT INTO";

const handler = function (err: any, raw: any) {
const handler = (err: any, raw: any) => {

// log slow queries if maxQueryExecution time is set
const maxQueryExecutionTime = this.driver.options.maxQueryExecutionTime;
const queryEndTime = +new Date();
const queryExecutionTime = queryEndTime - queryStartTime;
if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime)

if (maxQueryExecutionTime && queryExecutionTime > maxQueryExecutionTime) {
connection.logger.logQuerySlow(queryExecutionTime, query, parameters, this);

}

if (err) {
connection.logger.logQueryError(err, query, parameters, this);
fail(new QueryFailedError(query, parameters, err));
} else {
const result = new QueryResult();
}

result.raw = raw;
const result = new QueryResult();
result.raw = raw;

if (!isInsertQuery && Array.isArray(raw)) {
result.records = raw;
}
if (!isInsertQuery && Array.isArray(raw)) {
result.records = raw;
}

if (useStructuredResult) {
ok(result);
} else {
ok(result.raw);
}
if (useStructuredResult) {
ok(result);
} else {
ok(result.raw);
}

};
this.driver.connection.logger.logQuery(query, parameters, this);
const queryStartTime = +new Date();
this.connect().then(databaseConnection => {
if (isInsertQuery) {
databaseConnection.execSQL(query, parameters, handler);
} else {
databaseConnection.all(query, parameters, handler);
}
});

if (isInsertQuery) {
databaseConnection.execSQL(query, parameters, handler);
} else {
databaseConnection.all(query, parameters, handler);
}
});
}

Expand Down

0 comments on commit f41fa8d

Please sign in to comment.