1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
2
+
1
3
import {
2
4
EventEmitter ,
3
5
} from 'node:events' ;
@@ -20,7 +22,6 @@ type PgPool = {
20
22
21
23
type Command = 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE' ;
22
24
23
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
24
25
type Row = any ;
25
26
26
27
type QueryResult = {
@@ -33,10 +34,40 @@ type QueryResult = {
33
34
rows : Row [ ] ,
34
35
} ;
35
36
37
+ declare class NotAPromise {
38
+ private then ( ) : never ;
39
+ private catch ( ) : never ;
40
+ private finally ( ) : never ;
41
+ }
42
+
43
+ type Parameter < T = SerializableParameter > = NotAPromise & {
44
+ /**
45
+ * Raw value to serialize
46
+ */
47
+ raw : T | null ,
48
+ /**
49
+ * PostgreSQL OID of the type
50
+ */
51
+ type : number ,
52
+ /**
53
+ * Serialized value
54
+ */
55
+ value : string | null ,
56
+ } ;
57
+
58
+ type Serializable = Date | Uint8Array | boolean | never | number | string | null ;
59
+
60
+ type ArrayParameter < T extends readonly any [ ] = readonly any [ ] > = Parameter < T | T [ ] > & {
61
+ array : true ,
62
+ } ;
63
+
64
+ type SerializableParameter < T = never > = ArrayParameter | Parameter < any > | ReadonlyArray < SerializableParameter < T > > | Serializable | T | never ;
65
+
36
66
export type BridgetClient = {
37
67
end : ( ) => void ,
38
68
off : ( eventName : string , listener : ( ...args : any [ ] ) => void ) => void ,
39
69
on : ( eventName : string , listener : ( ...args : any [ ] ) => void ) => void ,
70
+ query : ( sql : string , parameters : SerializableParameter [ ] ) => Promise < QueryResult > ,
40
71
} ;
41
72
42
73
export const createBridge = ( postgres : typeof Postgres ) => {
@@ -80,9 +111,9 @@ export const createBridge = (postgres: typeof Postgres) => {
80
111
} ,
81
112
off : connectionEvents . off . bind ( connectionEvents ) ,
82
113
on : connectionEvents . on . bind ( connectionEvents ) ,
83
- query : async ( sql : string , parameters : Parameters < typeof connection . unsafe > [ 1 ] ) : Promise < QueryResult > => {
114
+ query : async ( sql : string , parameters : SerializableParameter [ ] ) : Promise < QueryResult > => {
84
115
// https://github.com/porsager/postgres#result-array
85
- const resultArray = await connection . unsafe ( sql , parameters ) ;
116
+ const resultArray = await connection . unsafe ( sql , parameters as any ) ;
86
117
87
118
return {
88
119
command : resultArray . command as Command ,
@@ -128,7 +159,7 @@ export const createBridge = (postgres: typeof Postgres) => {
128
159
await client . end ( ) ;
129
160
}
130
161
131
- public get _clients ( ) {
162
+ public get _clients ( ) : BridgetClient [ ] {
132
163
// @ts -expect-error accessing private method
133
164
return Array . from < { obj : BridgetClient , } > ( this . pool . _allObjects , ( member ) => {
134
165
return member . obj ;
@@ -154,5 +185,11 @@ export const createBridge = (postgres: typeof Postgres) => {
154
185
public get waitingCount ( ) {
155
186
return this . pool . pending ;
156
187
}
188
+
189
+ public async end ( ) {
190
+ for ( const client of this . _clients ) {
191
+ client . end ( ) ;
192
+ }
193
+ }
157
194
} ;
158
195
} ;
0 commit comments