Skip to content

Commit

Permalink
feat: define types for events emitted by query builder
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 14, 2020
1 parent 2bf5a10 commit 22cfe68
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 6 deletions.
15 changes: 14 additions & 1 deletion adonis-typings/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,19 @@ declare module '@ioc:Adonis/Lucid/Database' {
profiler: ProfilerRowContract | ProfilerContract
}>

/**
* Shape of the data emitted by the `db:query event`
*/
export type DbQueryEventNode = {
connection: string
model?: string
ddl?: boolean
duration?: [number, number]
method: string
sql: string
bindings?: any[]
}

/**
* Database contract serves as the main API to interact with multiple
* database connections
Expand All @@ -657,7 +670,7 @@ declare module '@ioc:Adonis/Lucid/Database' {
/**
* Pretty print query logs
*/
prettyPrint: (queryLog: any) => void
prettyPrint: (queryLog: DbQueryEventNode) => void

/**
* Name of the primary connection defined inside `config/database.ts`
Expand Down
15 changes: 15 additions & 0 deletions adonis-typings/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* @adonisjs/lucid
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare module '@ioc:Adonis/Core/Event' {
import { DbQueryEventNode } from '@ioc:Adonis/Lucid/Database'
interface EventsList {
'db:query': DbQueryEventNode
}
}
1 change: 1 addition & 0 deletions adonis-typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

/// <reference path="./database.ts" />
/// <reference path="./events.ts" />
/// <reference path="./querybuilder.ts" />
/// <reference path="./model.ts" />
/// <reference path="./orm.ts" />
Expand Down
2 changes: 1 addition & 1 deletion npm-audit.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">
<div class="card">
<div class="card-body">
<h5 class="card-title">
October 14th 2020, 8:27:13 am
October 14th 2020, 8:27:38 am
</h5>
<p class="card-text">Last updated</p>
</div>
Expand Down
5 changes: 3 additions & 2 deletions src/Helpers/prettyPrint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
* file that was distributed with this source code.
*/

import kleur from 'kleur'
import type kleur from 'kleur'
import { inspect } from 'util'
import { DbQueryEventNode } from '@ioc:Adonis/Lucid/Database'

/**
* Colorizes the sql query based upon the method
Expand All @@ -34,7 +35,7 @@ function colorizeQuery(color: typeof kleur, method: string, sql: string) {
/**
* Pretty print queries
*/
export function prettyPrint(queryLog: any) {
export function prettyPrint(queryLog: DbQueryEventNode) {
/**
* Lazy loading pretty printed dependencies
*/
Expand Down
4 changes: 2 additions & 2 deletions src/QueryRunner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export class QueryRunner {
* toSQL too many times and also get the actual time it took to
* execute the query
*/
query['once']('query', (sql) => this.reporter.begin({ ...this.logData, ...sql }))
query['once']('query', (sql: any) => this.reporter.begin({ ...this.logData, ...sql }))

const [error, result] = await this.executeQuery(query)
this.reporter.end(error)
Expand Down Expand Up @@ -110,7 +110,7 @@ export class QueryRunner {
* toSQL too many times and also get the actual time it took to
* execute the query
*/
query['once']('query', (sql) => this.reporter.begin({ ...this.logData, ...sql }))
query['once']('query', (sql: any) => this.reporter.begin({ ...this.logData, ...sql }))

/**
* Execute query and report event and profiler data
Expand Down

0 comments on commit 22cfe68

Please sign in to comment.