@@ -37,20 +37,37 @@ type QueryResult = {
3737} ;
3838
3939export const bridge = ( postgres : typeof Postgres , poolConfiguration : PgPool ) => {
40- const events = new EventEmitter ( ) ;
40+ const poolEvents = new EventEmitter ( ) ;
4141
42- const pool = genericPool . createPool < AnySql > ( {
42+ const pool = genericPool . createPool < AnySql & { events : EventEmitter , } > ( {
4343 create : async ( ) => {
44- return postgres ( {
44+ const connectionEvents = new EventEmitter ( ) ;
45+
46+ const connection = postgres ( {
4547 database : poolConfiguration . database ,
4648 host : poolConfiguration . host ?? 'localhost' ,
4749 idle_timeout : poolConfiguration . idleTimeoutMillis ? poolConfiguration . idleTimeoutMillis / 1_000 : 0 ,
4850 max : 1 ,
51+ onnotice : ( notice ) => {
52+ connectionEvents . emit ( 'notice' , {
53+ code : notice . code ,
54+ file : notice . file ,
55+ line : notice . line ,
56+ message : notice . message ,
57+ routine : notice . routine ,
58+ severity : notice . severity ,
59+ where : notice . where ,
60+ } ) ;
61+ } ,
4962 password : poolConfiguration . password ,
5063 port : poolConfiguration . port ?? 5_432 ,
5164 ssl : poolConfiguration . ssl ,
5265 username : poolConfiguration . user ,
53- } ) ;
66+ } ) as AnySql & { events : EventEmitter , } ;
67+
68+ connection . events = connectionEvents ;
69+
70+ return connection ;
5471 } ,
5572 destroy : ( client : Sql < { } > ) => {
5673 return client . end ( {
@@ -67,29 +84,31 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
6784 const connection = await pool . acquire ( ) ;
6885
6986 const compatibleConnection = {
87+ off : connection . events . off . bind ( connection . events ) ,
88+ on : connection . events . on . bind ( connection . events ) ,
7089 query : async ( sql : string ) : Promise < QueryResult > => {
7190 // https://github.com/porsager/postgres#result-array
7291 const resultArray = await connection . unsafe ( sql ) ;
7392
7493 return {
7594 command : resultArray . command as Command ,
76- fields : resultArray . columns . map ( ( column ) => {
95+ fields : resultArray . columns ? .map ( ( column ) => {
7796 return {
7897 dataTypeID : column . type ,
7998 name : column . name ,
8099 } ;
81- } ) ,
100+ } ) ?? [ ] ,
82101 rowCount : resultArray . count ,
83102 rows : Array . from ( resultArray ) ,
84103 } ;
85104 } ,
86105 } ;
87106
88- events . emit ( 'connect' , compatibleConnection ) ;
107+ poolEvents . emit ( 'connect' , compatibleConnection ) ;
89108
90109 return compatibleConnection ;
91110 } ,
92- off : events . off . bind ( events ) ,
93- on : events . on . bind ( events ) ,
111+ off : poolEvents . off . bind ( poolEvents ) ,
112+ on : poolEvents . on . bind ( poolEvents ) ,
94113 } ;
95114} ;
0 commit comments