Skip to content

Commit

Permalink
refactor: use unscoped profiler by default
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Aug 19, 2019
1 parent 4b0272a commit 536e83e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 14 deletions.
4 changes: 2 additions & 2 deletions adonis-typings/querybuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
declare module '@ioc:Adonis/Addons/DatabaseQueryBuilder' {
import * as knex from 'knex'
import { Dictionary } from 'ts-essentials'
import { ProfilerRowContract } from '@poppinss/profiler'
import { ProfilerRowContract, ProfilerContract } from '@poppinss/profiler'

/**
* The types for values for the aggregates. We need this coz of
Expand Down Expand Up @@ -927,7 +927,7 @@ declare module '@ioc:Adonis/Addons/DatabaseQueryBuilder' {
/**
* Custom profiler to time queries
*/
profiler?: ProfilerRowContract
profiler?: ProfilerRowContract | ProfilerContract

/**
* Tells if client is a transaction client or not
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"homepage": "https://github.com/adonisjs/adonis-lucid#readme",
"dependencies": {
"@poppinss/logger": "^1.1.2",
"@poppinss/profiler": "^1.0.0",
"@poppinss/profiler": "^1.1.0",
"@poppinss/utils": "^1.0.4",
"knex": "^0.19.2",
"knex-dynamic-connection": "^1.0.0",
Expand Down
4 changes: 2 additions & 2 deletions src/Database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ export class Database implements DatabaseContract {
*/
public connection (
connection: string = this.primaryConnectionName,
options?: Partial<{ mode: 'read' | 'write', profiler: ProfilerRowContract }>,
options?: Partial<{ mode: 'read' | 'write', profiler: ProfilerRowContract | ProfilerContract }>,
) {
options = options || {}

if (!options.profiler) {
options.profiler = this._profiler.create('')
options.profiler = this._profiler
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/QueryClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import * as knex from 'knex'
import { Exception } from '@poppinss/utils'
import { ProfilerRowContract } from '@poppinss/profiler'
import { ProfilerRowContract, ProfilerContract } from '@poppinss/profiler'
import { resolveClientNameWithAliases } from 'knex/lib/helpers'

import {
Expand Down Expand Up @@ -47,7 +47,7 @@ export class QueryClient implements QueryClientContract {
/**
* The profiler to be used for profiling queries
*/
public profiler?: ProfilerRowContract
public profiler?: ProfilerRowContract | ProfilerContract

/**
* Name of the connection in use
Expand Down Expand Up @@ -121,7 +121,10 @@ export class QueryClient implements QueryClientContract {
*/
public async transaction (): Promise<TransactionClientContract> {
const trx = await this.getWriteClient().transaction()
return new TransactionClient(trx, this.dialect, this.connectionName)
const transaction = new TransactionClient(trx, this.dialect, this.connectionName)
transaction.profiler = this.profiler

return transaction
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/TransactionClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference path="../../adonis-typings/database.ts" />

import * as knex from 'knex'
import { ProfilerRowContract } from '@poppinss/profiler'
import { ProfilerRowContract, ProfilerContract } from '@poppinss/profiler'
import { TransactionClientContract } from '@ioc:Adonis/Addons/DatabaseQueryBuilder'

import {
Expand Down Expand Up @@ -42,7 +42,7 @@ export class TransactionClient implements TransactionClientContract {
/**
* The profiler to be used for profiling queries
*/
public profiler?: ProfilerRowContract
public profiler?: ProfilerRowContract | ProfilerContract

constructor (
public knexClient: knex.Transaction,
Expand Down Expand Up @@ -110,7 +110,9 @@ export class TransactionClient implements TransactionClientContract {
*/
public async transaction (): Promise<TransactionClientContract> {
const trx = await this.knexClient.transaction()
return new TransactionClient(trx, this.dialect, this.connectionName)
const transaction = new TransactionClient(trx, this.dialect, this.connectionName)
transaction.profiler = this.profiler
return transaction
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// <reference path="../../adonis-typings/database.ts" />

import * as knex from 'knex'
import { ProfilerRowContract } from '@poppinss/profiler'
import { ProfilerRowContract, ProfilerContract } from '@poppinss/profiler'
import { ProfilerActionContract } from '@poppinss/profiler/build/src/Contracts'
import { QueryClientContract } from '@ioc:Adonis/Addons/DatabaseQueryBuilder'

Expand All @@ -20,7 +20,7 @@ import { QueryClientContract } from '@ioc:Adonis/Addons/DatabaseQueryBuilder'
*/
function getProfilerAction (
builder: knex.QueryBuilder | knex.Raw,
profiler?: ProfilerRowContract,
profiler?: ProfilerRowContract | ProfilerContract,
profilerData?: any,
) {
if (!profiler) {
Expand Down Expand Up @@ -63,7 +63,7 @@ export function isInTransaction (
export async function executeQuery (
builder: knex.QueryBuilder | knex.Raw,
knexClient?: knex,
profiler?: ProfilerRowContract,
profiler?: ProfilerRowContract | ProfilerContract,
profilerData?: any,
) {
let action = getProfilerAction(builder, profiler, profilerData)
Expand Down
68 changes: 68 additions & 0 deletions test/database.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,72 @@ test.group('Database', (group) => {
assert.isDefined(result)
await db.manager.closeAll()
})

test('pass profiler to query client', async (assert) => {
const config = {
connection: 'primary',
connections: { primary: getConfig() },
}

const profiler = getProfiler()
const db = new Database(config, getLogger(), profiler)
const client = db.connection('primary')
assert.deepEqual(client.profiler, profiler)

await db.manager.closeAll()
})

test('pass custom profiler to query client', async (assert) => {
const config = {
connection: 'primary',
connections: { primary: getConfig() },
}

const profiler = getProfiler()
const row = profiler.create('scoped')

const db = new Database(config, getLogger(), profiler)
const client = db.connection('primary', { profiler: row })
assert.deepEqual(client.profiler, row)

await db.manager.closeAll()
})

test('forward profiler to transaction client', async (assert) => {
const config = {
connection: 'primary',
connections: { primary: getConfig() },
}

const profiler = getProfiler()

const db = new Database(config, getLogger(), profiler)
const client = db.connection('primary')
const trx = await client.transaction()
assert.equal(trx.profiler, profiler)

await trx.rollback()
await db.manager.closeAll()
})

test('forward profiler to nested transaction client', async (assert) => {
const config = {
connection: 'primary',
connections: { primary: getConfig() },
}

const profiler = getProfiler()

const db = new Database(config, getLogger(), profiler)
const client = db.connection('primary')
const trx = await client.transaction()
const trx1 = await trx.transaction()

assert.equal(trx.profiler, profiler)
assert.equal(trx1.profiler, profiler)

await trx1.rollback()
await trx.rollback()
await db.manager.closeAll()
})
})

0 comments on commit 536e83e

Please sign in to comment.