-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
67 lines (59 loc) · 1.37 KB
/
db.ts
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { createPool } from "@vercel/postgres";
import pg from "pg";
import { DB_CONFIG, dev } from "./serverConstants";
import {
readCommitted,
type Queryable,
type SQLFragment,
type TxnClientForReadCommitted,
} from "zapatos/db";
export function getPool() {
const pool = dev
? new pg.Pool({
connectionString: DB_CONFIG.connectionString,
})
: createPool(DB_CONFIG);
// pool.on('error', (err) => {
// pool = null;
// console.error('Unexpected error on pool client');
// });
return pool;
}
export async function runQuery<T>(query: SQLFragment<T>) {
const pool = getPool();
// const client = await pool.connect();
// console.log(query.compile());
try {
const res = await query.run(pool);
await pool.end();
return res;
} catch (e) {
// console.error(e);
throw e;
}
// client.release();
}
export async function runQueryTxn<T>(
callback: (client: Queryable | TxnClientForReadCommitted) => Promise<T>,
) {
const pool = getPool();
// const client = await pool.connect();
const res = await readCommitted<T>(pool, callback);
// client.release();
await pool.end();
return res;
}
// export function getNeon() {
// return neon(DB_CONFIG.connectionString);
// }
// export function getQueue() {
// return new QClient({
// token: process.env.QSTASH_TOKEN,
// retry: {
// retries: 3,
// },
// });
// }
// if (dev) {
// initDB();
// }