Skip to content

Commit 1e0d051

Browse files
committed
feat: add end method
1 parent 2a27caf commit 1e0d051

File tree

1 file changed

+41
-4
lines changed

1 file changed

+41
-4
lines changed

src/bridge.ts

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
13
import {
24
EventEmitter,
35
} from 'node:events';
@@ -20,7 +22,6 @@ type PgPool = {
2022

2123
type Command = 'DELETE' | 'INSERT' | 'SELECT' | 'UPDATE';
2224

23-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2425
type Row = any;
2526

2627
type QueryResult = {
@@ -33,10 +34,40 @@ type QueryResult = {
3334
rows: Row[],
3435
};
3536

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+
3666
export type BridgetClient = {
3767
end: () => void,
3868
off: (eventName: string, listener: (...args: any[]) => void) => void,
3969
on: (eventName: string, listener: (...args: any[]) => void) => void,
70+
query: (sql: string, parameters: SerializableParameter[]) => Promise<QueryResult>,
4071
};
4172

4273
export const createBridge = (postgres: typeof Postgres) => {
@@ -80,9 +111,9 @@ export const createBridge = (postgres: typeof Postgres) => {
80111
},
81112
off: connectionEvents.off.bind(connectionEvents),
82113
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> => {
84115
// 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);
86117

87118
return {
88119
command: resultArray.command as Command,
@@ -128,7 +159,7 @@ export const createBridge = (postgres: typeof Postgres) => {
128159
await client.end();
129160
}
130161

131-
public get _clients () {
162+
public get _clients (): BridgetClient[] {
132163
// @ts-expect-error accessing private method
133164
return Array.from<{obj: BridgetClient, }>(this.pool._allObjects, (member) => {
134165
return member.obj;
@@ -154,5 +185,11 @@ export const createBridge = (postgres: typeof Postgres) => {
154185
public get waitingCount () {
155186
return this.pool.pending;
156187
}
188+
189+
public async end () {
190+
for (const client of this._clients) {
191+
client.end();
192+
}
193+
}
157194
};
158195
};

0 commit comments

Comments
 (0)