@@ -37,20 +37,37 @@ type QueryResult = {
37
37
} ;
38
38
39
39
export const bridge = ( postgres : typeof Postgres , poolConfiguration : PgPool ) => {
40
- const events = new EventEmitter ( ) ;
40
+ const poolEvents = new EventEmitter ( ) ;
41
41
42
- const pool = genericPool . createPool < AnySql > ( {
42
+ const pool = genericPool . createPool < AnySql & { events : EventEmitter , } > ( {
43
43
create : async ( ) => {
44
- return postgres ( {
44
+ const connectionEvents = new EventEmitter ( ) ;
45
+
46
+ const connection = postgres ( {
45
47
database : poolConfiguration . database ,
46
48
host : poolConfiguration . host ?? 'localhost' ,
47
49
idle_timeout : poolConfiguration . idleTimeoutMillis ? poolConfiguration . idleTimeoutMillis / 1_000 : 0 ,
48
50
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
+ } ,
49
62
password : poolConfiguration . password ,
50
63
port : poolConfiguration . port ?? 5_432 ,
51
64
ssl : poolConfiguration . ssl ,
52
65
username : poolConfiguration . user ,
53
- } ) ;
66
+ } ) as AnySql & { events : EventEmitter , } ;
67
+
68
+ connection . events = connectionEvents ;
69
+
70
+ return connection ;
54
71
} ,
55
72
destroy : ( client : Sql < { } > ) => {
56
73
return client . end ( {
@@ -67,29 +84,31 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
67
84
const connection = await pool . acquire ( ) ;
68
85
69
86
const compatibleConnection = {
87
+ off : connection . events . off . bind ( connection . events ) ,
88
+ on : connection . events . on . bind ( connection . events ) ,
70
89
query : async ( sql : string ) : Promise < QueryResult > => {
71
90
// https://github.com/porsager/postgres#result-array
72
91
const resultArray = await connection . unsafe ( sql ) ;
73
92
74
93
return {
75
94
command : resultArray . command as Command ,
76
- fields : resultArray . columns . map ( ( column ) => {
95
+ fields : resultArray . columns ? .map ( ( column ) => {
77
96
return {
78
97
dataTypeID : column . type ,
79
98
name : column . name ,
80
99
} ;
81
- } ) ,
100
+ } ) ?? [ ] ,
82
101
rowCount : resultArray . count ,
83
102
rows : Array . from ( resultArray ) ,
84
103
} ;
85
104
} ,
86
105
} ;
87
106
88
- events . emit ( 'connect' , compatibleConnection ) ;
107
+ poolEvents . emit ( 'connect' , compatibleConnection ) ;
89
108
90
109
return compatibleConnection ;
91
110
} ,
92
- off : events . off . bind ( events ) ,
93
- on : events . on . bind ( events ) ,
111
+ off : poolEvents . off . bind ( poolEvents ) ,
112
+ on : poolEvents . on . bind ( poolEvents ) ,
94
113
} ;
95
114
} ;
0 commit comments