Skip to content

Commit 6382e4c

Browse files
committed
feat: add idleCount, totalCount, waitingCount
1 parent abb7f0f commit 6382e4c

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/bridge.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
7979
min: poolConfiguration.min,
8080
});
8181

82-
return {
82+
const compatiblePool = {
8383
connect: async () => {
8484
const connection = await pool.acquire();
8585

8686
const compatibleConnection = {
87+
end: async () => {
88+
await pool.destroy(connection);
89+
},
8790
off: connection.events.off.bind(connection.events),
8891
on: connection.events.on.bind(connection.events),
8992
query: async (sql: string): Promise<QueryResult> => {
@@ -102,13 +105,27 @@ export const bridge = (postgres: typeof Postgres, poolConfiguration: PgPool) =>
102105
rows: Array.from(resultArray),
103106
};
104107
},
108+
release: async () => {
109+
await pool.release(connection);
110+
},
105111
};
106112

107113
poolEvents.emit('connect', compatibleConnection);
108114

109115
return compatibleConnection;
110116
},
117+
get idleCount () {
118+
return pool.available;
119+
},
111120
off: poolEvents.off.bind(poolEvents),
112121
on: poolEvents.on.bind(poolEvents),
122+
get totalCount () {
123+
return pool.size;
124+
},
125+
get waitingCount () {
126+
return pool.pending;
127+
},
113128
};
129+
130+
return compatiblePool;
114131
};

test/postgres-bridge/bridge.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,36 @@ for (const client of clients) {
7979
});
8080
});
8181

82+
test(client + ': pool.connect() 2x creates two connections', async (t) => {
83+
const pool = createPool(client, {
84+
user: 'postgres',
85+
});
86+
87+
t.is(pool.totalCount, 0);
88+
89+
const connection1 = await pool.connect();
90+
const connection2 = await pool.connect();
91+
92+
t.is(pool.totalCount, 2);
93+
94+
await connection1.end();
95+
await connection2.end();
96+
});
97+
98+
test(client + ': connection.release() releases connection back to the pool', async (t) => {
99+
const pool = createPool(client, {
100+
user: 'postgres',
101+
});
102+
103+
const connection = await pool.connect();
104+
105+
t.is(pool.idleCount, 0);
106+
107+
await connection.release();
108+
109+
t.is(pool.idleCount, 1);
110+
});
111+
82112
test(client + ': query method', async (t) => {
83113
const pool = createPool(client, {
84114
user: 'postgres',

0 commit comments

Comments
 (0)