-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-db-connection.ts
74 lines (69 loc) · 1.31 KB
/
create-db-connection.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
68
69
70
71
72
73
74
import { drizzle } from 'drizzle-orm/node-postgres';
import { Client, Pool } from 'pg';
import * as schema from '../db/schema';
import { logger } from '../utils/logger';
/**
* creates a postgres database pool connection
*/
export function createDbPool({
host,
port,
user,
password,
database,
}: {
host: string;
port?: number;
user: string;
password: string;
database: string;
}) {
const pool = new Pool({
host: host,
port: port,
user: user,
password: password,
database: database,
ssl: false,
});
// the pool will emit an error on behalf of any idle clients
// it contains if a backend error or network partition happens
pool.on('error', (err) => {
logger.error('Unexpected error on idle client', err);
process.exit(-1);
});
return {
pool,
db: drizzle(pool, { schema }),
};
}
/**
* creates a postgres database client connection
*/
export async function createDbClient({
host,
port,
user,
password,
database,
}: {
host: string;
port?: number;
user: string;
password: string;
database: string;
}) {
const client = new Client({
host: host,
port: port,
user: user,
password: password,
database: database,
ssl: false,
});
await client.connect();
return {
client,
db: drizzle(client, { schema }),
};
}