@@ -3,6 +3,8 @@ import { AdminForthResource, IAdminForthSingleFilter, IAdminForthAndOrFilter, IA
33import { AdminForthDataTypes , AdminForthFilterOperators , AdminForthSortDirections , } from '../types/Common.js' ;
44import AdminForthBaseConnector from './baseConnector.js' ;
55import pkg from 'pg' ;
6+ import { dbLogger } from '../modules/logger.js' ;
7+
68const { Client } = pkg ;
79
810
@@ -351,9 +353,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
351353 const d = [ ...filterValues , limit , offset ] ;
352354 const orderBy = sort . length ? `ORDER BY ${ sort . map ( ( s ) => `"${ s . field } " ${ this . SortDirectionsMap [ s . direction ] } ` ) . join ( ', ' ) } ` : '' ;
353355 const selectQuery = `SELECT ${ columns } FROM "${ tableName } " ${ where } ${ orderBy } ${ limitOffset } ` ;
354- if ( process . env . HEAVY_DEBUG_QUERY ) {
355- console . log ( '🪲📜 PG Q:' , selectQuery , 'params:' , d ) ;
356- }
356+ dbLogger . trace ( `🪲📜 PG Q: ${ selectQuery } , params: ${ JSON . stringify ( d ) } ` ) ;
357357 const stmt = await this . client . query ( selectQuery , d ) ;
358358 const rows = stmt . rows ;
359359 return rows . map ( ( row ) => {
@@ -376,9 +376,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
376376 }
377377 const { sql : where , values : filterValues } = this . whereClauseAndValues ( resource , filters ) ;
378378 const q = `SELECT COUNT(*) FROM "${ tableName } " ${ where } ` ;
379- if ( process . env . HEAVY_DEBUG_QUERY ) {
380- console . log ( '🪲📜 PG Q:' , q , 'values:' , filterValues ) ;
381- }
379+ dbLogger . trace ( `🪲📜 PG Q: ${ q } , values: ${ JSON . stringify ( filterValues ) } ` ) ;
382380 const stmt = await this . client . query ( q , filterValues ) ;
383381 return + stmt . rows [ 0 ] . count ;
384382 }
@@ -388,9 +386,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
388386 const result = { } ;
389387 await Promise . all ( columns . map ( async ( col ) => {
390388 const q = `SELECT MIN(${ col . name } ) as min, MAX(${ col . name } ) as max FROM "${ tableName } "` ;
391- if ( process . env . HEAVY_DEBUG_QUERY ) {
392- console . log ( '🪲📜 PG Q:' , q ) ;
393- }
389+ dbLogger . trace ( `🪲📜 PG Q: ${ q } ` ) ;
394390 const stmt = await this . client . query ( q ) ;
395391 const { min, max } = stmt . rows [ 0 ] ;
396392 result [ col . name ] = {
@@ -412,9 +408,7 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
412408 const q = `INSERT INTO "${ tableName } " (${ columns . join ( ', ' ) } ) VALUES (${ placeholders } ) RETURNING "${ primaryKey } "` ;
413409 // console.log('\n🔵 [PG INSERT]:', q);
414410 // console.log('📦 [VALUES]:', JSON.stringify(values, null, 2));
415- if ( process . env . HEAVY_DEBUG_QUERY ) {
416- console . log ( '🪲📜 PG Q:' , q , 'values:' , values ) ;
417- }
411+ dbLogger . trace ( `🪲📜 PG Q: ${ q } , values: ${ JSON . stringify ( values ) } ` ) ;
418412 const ret = await this . client . query ( q , values ) ;
419413 return ret . rows [ 0 ] [ primaryKey ] ;
420414 }
@@ -423,17 +417,13 @@ class PostgresConnector extends AdminForthBaseConnector implements IAdminForthDa
423417 const values = [ ...Object . values ( newValues ) , recordId ] ;
424418 const columnsWithPlaceholders = Object . keys ( newValues ) . map ( ( col , i ) => `"${ col } " = $${ i + 1 } ` ) . join ( ', ' ) ;
425419 const q = `UPDATE "${ resource . table } " SET ${ columnsWithPlaceholders } WHERE "${ this . getPrimaryKey ( resource ) } " = $${ values . length } ` ;
426- if ( process . env . HEAVY_DEBUG_QUERY ) {
427- console . log ( '🪲📜 PG Q:' , q , 'values:' , values ) ;
428- }
420+ dbLogger . trace ( `🪲📜 PG Q: ${ q } , values: ${ JSON . stringify ( values ) } ` ) ;
429421 await this . client . query ( q , values ) ;
430422 }
431423
432424 async deleteRecord ( { resource, recordId } ) : Promise < boolean > {
433425 const q = `DELETE FROM "${ resource . table } " WHERE "${ this . getPrimaryKey ( resource ) } " = $1` ;
434- if ( process . env . HEAVY_DEBUG_QUERY ) {
435- console . log ( '🪲📜 PG Q:' , q , 'values:' , [ recordId ] ) ;
436- }
426+ dbLogger . trace ( `🪲📜 PG Q: ${ q } , values: ${ JSON . stringify ( [ recordId ] ) } ` ) ;
437427 const res = await this . client . query ( q , [ recordId ] ) ;
438428 return res . rowCount > 0 ;
439429 }
0 commit comments