Skip to content

Commit

Permalink
refactor: ask query builder to return the profiler action
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 30, 2019
1 parent c7a62f8 commit 43fe9f7
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 19 deletions.
14 changes: 14 additions & 0 deletions src/Database/QueryBuilder/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,18 @@ export class DatabaseQueryBuilder extends Chainable implements DatabaseQueryBuil

return this.client!.getReadClient().client
}

/**
* Returns the profiler action
*/
public getProfilerAction () {
if (!this.client.profiler) {
return null
}

return this.client.profiler.profile('sql:query', Object.assign(this['toSQL'](), {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
}))
}
}
14 changes: 14 additions & 0 deletions src/Database/QueryBuilder/Insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ export class InsertQueryBuilder implements InsertQueryBuilderContract {
return this.client!.getWriteClient().client
}

/**
* Returns the profiler action
*/
public getProfilerAction () {
if (!this.client.profiler) {
return null
}

return this.client.profiler.profile('sql:query', Object.assign(this['toSQL'](), {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
}))
}

/**
* Define table for performing the insert query
*/
Expand Down
14 changes: 14 additions & 0 deletions src/Database/QueryBuilder/Raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export class RawQueryBuilder implements RawContract {
return undefined
}

/**
* Returns the profiler action
*/
public getProfilerAction () {
if (!this.client.profiler) {
return null
}

return this.client.profiler.profile('sql:query', Object.assign(this['toSQL'](), {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
}))
}

/**
* Wrap the query with before/after strings.
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Orm/QueryBuilder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,19 @@ export class ModelQueryBuilder extends Chainable implements ModelQueryBuilderCon
public getQueryClient () {
return this.client!.getReadClient().client
}

/**
* Returns the profiler action
*/
public getProfilerAction () {
if (!this.client.profiler) {
return null
}

return this.client.profiler.profile('sql:query', Object.assign(this['toSQL'](), {
connection: this.client.connectionName,
inTransaction: this.client.isTransaction,
model: this.model.name,
}))
}
}
28 changes: 9 additions & 19 deletions src/Traits/Executable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,25 @@ import {
* Enforcing constructor on the destination class
*/
export type ExecutableConstructor<T = {
$knexBuilder: knex.Raw | knex.QueryBuilder,
getQueryClient: () => undefined | knex,
client: QueryClientContract,
beforeExecute?: () => Promise<void>,
getQueryClient: () => undefined | knex,
$knexBuilder: knex.Raw | knex.QueryBuilder,
afterExecute?: (results: any[]) => Promise<any[]>,
getProfilerAction (): ProfilerActionContract | null,
}> = { new (...args: any[]): T }

/**
* To be used as a trait for executing a query that has a public
* `$knexBuilder`
*/
export class Executable implements ExcutableQueryBuilderContract<any> {
protected $knexBuilder: knex.QueryBuilder | knex.Raw
protected client: QueryClientContract
protected getQueryClient: () => undefined | knex
protected beforeExecute?: () => Promise<void>
protected getQueryClient: () => undefined | knex
protected $knexBuilder: knex.QueryBuilder | knex.Raw
protected afterExecute?: (results: any[]) => Promise<any[]>

/**
* Returns the profiler action
*/
private _getProfilerAction () {
if (!this.client.profiler) {
return null
}

return this.client.profiler.profile('sql:query', Object.assign(this.toSQL(), {
connection: this.client.connectionName,
}))
}
protected getProfilerAction: () => ProfilerActionContract | null

/**
* Ends the profile action
Expand All @@ -69,7 +58,8 @@ export class Executable implements ExcutableQueryBuilderContract<any> {
* Executes the knex query builder
*/
private async _executeQuery () {
const action = this._getProfilerAction()
const action = this.getProfilerAction()

try {
const result = await this.$knexBuilder
this._endProfilerAction(action)
Expand All @@ -85,7 +75,7 @@ export class Executable implements ExcutableQueryBuilderContract<any> {
* knex client
*/
private async _executeQueryWithCustomConnection (knexClient: knex) {
const action = this._getProfilerAction()
const action = this.getProfilerAction()

/**
* Acquire connection from the client and set it as the
Expand Down

0 comments on commit 43fe9f7

Please sign in to comment.