blob: d6adcdf7798436400f4b0fe14900db057f899038 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
import pg from 'pg';
import { database } from '$lib/config';
const { Pool } = pg;
const pool = new Pool(database);
export async function query(text, params) {
const client = await pool.connect();
try {
const result = await client.query(text, params);
return result;
} finally {
client.release();
}
}
export async function queryWithAbort(text, params, signal) {
if (signal?.aborted) {
throw new Error('Query aborted');
}
const client = await pool.connect();
const queryPromise = client.query(text, params);
if (signal) {
const abortPromise = new Promise((_, reject) => {
signal.addEventListener('abort', () => {
client.release();
reject(new Error('Query aborted'));
}, { once: true });
});
try {
const result = await Promise.race([queryPromise, abortPromise]);
return result;
} finally {
client.release();
}
}
try {
const result = await queryPromise;
return result;
} finally {
client.release();
}
}
export default pool;
|