@@ -5,9 +5,6 @@ import genericPool, {
5
5
type Pool as GenericPool ,
6
6
} from 'generic-pool' ;
7
7
import type Postgres from 'postgres' ;
8
- import {
9
- type Sql ,
10
- } from 'postgres' ;
11
8
12
9
type PgPool = {
13
10
database ?: string ,
@@ -21,8 +18,6 @@ type PgPool = {
21
18
user ?: string ,
22
19
} ;
23
20
24
- type AnySql = Sql < { } > ;
25
-
26
21
type Command = 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE' ;
27
22
28
23
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -38,18 +33,22 @@ type QueryResult = {
38
33
rows : Row [ ] ,
39
34
} ;
40
35
41
- type Client = AnySql & { events : EventEmitter , } ;
36
+ export type BridgetClient = {
37
+ end : ( ) => void ,
38
+ off : ( eventName : string , listener : ( ...args : any [ ] ) => void ) => void ,
39
+ on : ( eventName : string , listener : ( ...args : any [ ] ) => void ) => void ,
40
+ } ;
42
41
43
42
export const createBridge = ( postgres : typeof Postgres ) => {
44
43
return class PostgresBridge {
45
44
private readonly poolEvents : EventEmitter ;
46
45
47
- private readonly pool : GenericPool < Client > ;
46
+ private readonly pool : GenericPool < BridgetClient > ;
48
47
49
48
public constructor ( poolConfiguration : PgPool ) {
50
49
this . poolEvents = new EventEmitter ( ) ;
51
50
52
- this . pool = genericPool . createPool < Client > ( {
51
+ this . pool = genericPool . createPool < BridgetClient > ( {
53
52
create : async ( ) => {
54
53
const connectionEvents = new EventEmitter ( ) ;
55
54
@@ -73,16 +72,39 @@ export const createBridge = (postgres: typeof Postgres) => {
73
72
port : poolConfiguration . port ?? 5_432 ,
74
73
ssl : poolConfiguration . ssl ,
75
74
username : poolConfiguration . user ,
76
- } ) as Client ;
75
+ } ) ;
76
+
77
+ const compatibleConnection = {
78
+ end : async ( ) => {
79
+ await connection . end ( ) ;
80
+ } ,
81
+ off : connectionEvents . off . bind ( connectionEvents ) ,
82
+ on : connectionEvents . on . bind ( connectionEvents ) ,
83
+ query : async ( sql : string , parameters ) : Promise < QueryResult > => {
84
+ // https://github.com/porsager/postgres#result-array
85
+ const resultArray = await connection . unsafe ( sql , parameters ) ;
77
86
78
- connection . events = connectionEvents ;
87
+ return {
88
+ command : resultArray . command as Command ,
89
+ fields : resultArray . columns ?. map ( ( column ) => {
90
+ return {
91
+ dataTypeID : column . type ,
92
+ name : column . name ,
93
+ } ;
94
+ } ) ?? [ ] ,
95
+ rowCount : resultArray . count ,
96
+ rows : Array . from ( resultArray ) ,
97
+ } ;
98
+ } ,
99
+ release : async ( ) => {
100
+ await this . pool . release ( compatibleConnection ) ;
101
+ } ,
102
+ } ;
79
103
80
- return connection ;
104
+ return compatibleConnection ;
81
105
} ,
82
- destroy : ( client : Sql < { } > ) => {
83
- return client . end ( {
84
- timeout : 5 ,
85
- } ) ;
106
+ destroy : async ( client : BridgetClient ) => {
107
+ client . end ( ) ;
86
108
} ,
87
109
} , {
88
110
max : poolConfiguration . max ?? 10 ,
@@ -93,36 +115,9 @@ export const createBridge = (postgres: typeof Postgres) => {
93
115
public async connect ( ) {
94
116
const connection = await this . pool . acquire ( ) ;
95
117
96
- const compatibleConnection = {
97
- end : async ( ) => {
98
- await this . pool . destroy ( connection ) ;
99
- } ,
100
- off : connection . events . off . bind ( connection . events ) ,
101
- on : connection . events . on . bind ( connection . events ) ,
102
- query : async ( sql : string , parameters ) : Promise < QueryResult > => {
103
- // https://github.com/porsager/postgres#result-array
104
- const resultArray = await connection . unsafe ( sql , parameters ) ;
105
-
106
- return {
107
- command : resultArray . command as Command ,
108
- fields : resultArray . columns ?. map ( ( column ) => {
109
- return {
110
- dataTypeID : column . type ,
111
- name : column . name ,
112
- } ;
113
- } ) ?? [ ] ,
114
- rowCount : resultArray . count ,
115
- rows : Array . from ( resultArray ) ,
116
- } ;
117
- } ,
118
- release : async ( ) => {
119
- await this . pool . release ( connection ) ;
120
- } ,
121
- } ;
122
-
123
- this . poolEvents . emit ( 'connect' , compatibleConnection ) ;
118
+ this . poolEvents . emit ( 'connect' , connection ) ;
124
119
125
- return compatibleConnection ;
120
+ return connection ;
126
121
}
127
122
128
123
public _pulseQueue ( ) {
@@ -133,9 +128,11 @@ export const createBridge = (postgres: typeof Postgres) => {
133
128
await client . end ( ) ;
134
129
}
135
130
136
- public _clients ( ) {
131
+ public get _clients ( ) {
137
132
// @ts -expect-error accessing private method
138
- return Array . from ( this . pool . _allObjects ) ;
133
+ return Array . from < { obj : BridgetClient , } > ( this . pool . _allObjects , ( member ) => {
134
+ return member . obj ;
135
+ } ) ;
139
136
}
140
137
141
138
public get idleCount ( ) {
0 commit comments