@@ -5,9 +5,6 @@ import genericPool, {
55 type Pool as GenericPool ,
66} from 'generic-pool' ;
77import type Postgres from 'postgres' ;
8- import {
9- type Sql ,
10- } from 'postgres' ;
118
129type PgPool = {
1310 database ?: string ,
@@ -21,8 +18,6 @@ type PgPool = {
2118 user ?: string ,
2219} ;
2320
24- type AnySql = Sql < { } > ;
25-
2621type Command = 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE' ;
2722
2823// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -38,18 +33,22 @@ type QueryResult = {
3833 rows : Row [ ] ,
3934} ;
4035
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+ } ;
4241
4342export const createBridge = ( postgres : typeof Postgres ) => {
4443 return class PostgresBridge {
4544 private readonly poolEvents : EventEmitter ;
4645
47- private readonly pool : GenericPool < Client > ;
46+ private readonly pool : GenericPool < BridgetClient > ;
4847
4948 public constructor ( poolConfiguration : PgPool ) {
5049 this . poolEvents = new EventEmitter ( ) ;
5150
52- this . pool = genericPool . createPool < Client > ( {
51+ this . pool = genericPool . createPool < BridgetClient > ( {
5352 create : async ( ) => {
5453 const connectionEvents = new EventEmitter ( ) ;
5554
@@ -73,16 +72,39 @@ export const createBridge = (postgres: typeof Postgres) => {
7372 port : poolConfiguration . port ?? 5_432 ,
7473 ssl : poolConfiguration . ssl ,
7574 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 ) ;
7786
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+ } ;
79103
80- return connection ;
104+ return compatibleConnection ;
81105 } ,
82- destroy : ( client : Sql < { } > ) => {
83- return client . end ( {
84- timeout : 5 ,
85- } ) ;
106+ destroy : async ( client : BridgetClient ) => {
107+ client . end ( ) ;
86108 } ,
87109 } , {
88110 max : poolConfiguration . max ?? 10 ,
@@ -93,36 +115,9 @@ export const createBridge = (postgres: typeof Postgres) => {
93115 public async connect ( ) {
94116 const connection = await this . pool . acquire ( ) ;
95117
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 ) ;
124119
125- return compatibleConnection ;
120+ return connection ;
126121 }
127122
128123 public _pulseQueue ( ) {
@@ -133,9 +128,11 @@ export const createBridge = (postgres: typeof Postgres) => {
133128 await client . end ( ) ;
134129 }
135130
136- public _clients ( ) {
131+ public get _clients ( ) {
137132 // @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+ } ) ;
139136 }
140137
141138 public get idleCount ( ) {
0 commit comments