From f3976b9e5f6ae327844b7a896ff1d414a84b53aa Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Wed, 13 Mar 2024 15:59:01 +0530 Subject: [PATCH] feat: add support for pretty print debug queries --- providers/database_provider.ts | 15 ++++++++++++++- src/database/main.ts | 2 +- src/types/database.ts | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/providers/database_provider.ts b/providers/database_provider.ts index 3c8164c2..d54ec607 100644 --- a/providers/database_provider.ts +++ b/providers/database_provider.ts @@ -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' /** * Extending AdonisJS types @@ -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 */ @@ -112,7 +123,8 @@ export default class DatabaseServiceProvider { const config = this.app.config.get('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) => { @@ -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) diff --git a/src/database/main.ts b/src/database/main.ts index a20318d5..bfc40548 100644 --- a/src/database/main.ts +++ b/src/database/main.ts @@ -57,7 +57,7 @@ export class Database extends Macroable { prettyPrint = prettyPrint constructor( - private config: DatabaseConfig, + public config: DatabaseConfig, private logger: Logger, private emitter: Emitter ) { diff --git a/src/types/database.ts b/src/types/database.ts index 4138d48d..47ae0f25 100644 --- a/src/types/database.ts +++ b/src/types/database.ts @@ -564,6 +564,7 @@ export type ConnectionConfig = */ export type DatabaseConfig = { connection: string + prettyPrintDebugQueries?: boolean connections: { [key: string]: ConnectionConfig } }