-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.ts
More file actions
44 lines (38 loc) · 1.29 KB
/
db.ts
File metadata and controls
44 lines (38 loc) · 1.29 KB
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
import { Connection, createConnection, EntityManager } from 'typeorm';
import { SnakeNamingStrategy } from 'typeorm-naming-strategies';
import { entities } from './entities';
export class Db {
public Ready: Promise<void>;
private conn!: Connection;
constructor() {
this.Ready = new Promise((resolve, reject) => {
createConnection({
type: 'postgres',
host: 'localhost',
port: 5432,
username: process.env.DB_USERNAME as string,
password: process.env.DB_PASSWORD as string,
database: 'test',
schema: 'public',
logging: true,
entities,
namingStrategy: new SnakeNamingStrategy(),
synchronize: true,
})
.then(conn => {
console.log('Connected to postgres database');
this.conn = conn;
resolve();
})
.catch(err => {
console.error(`While connecting to the postgres database: ${err}`);
reject(err);
});
});
}
public getManager(): EntityManager {
return this.conn.manager;
}
}
const db = new Db();
export default db;