Skip to content

Commit

Permalink
feat: add support for pretty print debug queries
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Mar 13, 2024
1 parent 4427c8d commit f3976b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion providers/database_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { QueryClient } from '../src/query_client/index.js'
import { BaseModel } from '../src/orm/base_model/index.js'
import { DatabaseTestUtils } from '../src/test_utils/database.js'
import type { DatabaseConfig, DbQueryEventNode } from '../src/types/database.js'
import { emit } from 'node:process'

Check failure on line 19 in providers/database_provider.ts

View workflow job for this annotation

GitHub Actions / typecheck

'emit' is declared but its value is never read.

/**
* Extending AdonisJS types
Expand Down Expand Up @@ -104,6 +105,16 @@ export default class DatabaseServiceProvider {
})
}

/**
* Registeres a listener to pretty print debug queries
*/
protected async prettyPrintDebugQueries(db: Database) {
if (db.config.prettyPrintDebugQueries) {
const emitter = await this.app.container.make('emitter')
emitter.on('db:query', db.prettyPrint)
}
}

/**
* Invoked by AdonisJS to register container bindings
*/
Expand All @@ -112,7 +123,8 @@ export default class DatabaseServiceProvider {
const config = this.app.config.get<DatabaseConfig>('database')
const emitter = await resolver.make('emitter')
const logger = await resolver.make('logger')
return new Database(config, logger, emitter)
const db = new Database(config, logger, emitter)
return db
})

this.app.container.singleton(QueryClient, async (resolver) => {
Expand All @@ -131,6 +143,7 @@ export default class DatabaseServiceProvider {
const db = await this.app.container.make('lucid.db')
BaseModel.$adapter = new Adapter(db)

await this.prettyPrintDebugQueries(db)
await this.registerTestUtils()
await this.registerReplBindings()
await this.registerVineJSRules(db)
Expand Down
2 changes: 1 addition & 1 deletion src/database/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class Database extends Macroable {
prettyPrint = prettyPrint

constructor(
private config: DatabaseConfig,
public config: DatabaseConfig,
private logger: Logger,
private emitter: Emitter<any>
) {
Expand Down
1 change: 1 addition & 0 deletions src/types/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,7 @@ export type ConnectionConfig =
*/
export type DatabaseConfig = {
connection: string
prettyPrintDebugQueries?: boolean
connections: { [key: string]: ConnectionConfig }
}

Expand Down

0 comments on commit f3976b9

Please sign in to comment.