-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.ts
42 lines (39 loc) · 1.4 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
import { createConnection } from "typeorm";
import { Comment } from "./src/backend/entities/Comment";
import { Link } from "./src/backend/entities/Link";
import { User } from "./src/backend/entities/User";
import { Vote } from "./src/backend/entities/Vote";
export async function createDbConnection() {
// Read the database settings from the environment vairables
const DATABASE_HOST = process.env.DATABASE_HOST;
const DATABASE_PASSWORD = process.env.DATABASE_PASSWORD;
const DATABASE_USER = process.env.DATABASE_USER;
const DATABASE_DB = process.env.DATABASE_DB;//TODO: change
// Display the settings in the console so we can see if something is wrong
console.log(
`
host: ${DATABASE_HOST}
password: ${DATABASE_PASSWORD}
user: ${DATABASE_USER}
db: ${DATABASE_DB}
`
);
// Open database connection
await createConnection({
type: "postgres",
host: DATABASE_HOST,
port: 5432,
username: DATABASE_USER,
password: DATABASE_PASSWORD,
database: DATABASE_DB,
// If you forget to add your entities here you will get a "repository not found" error
entities: [
User,
Link,
Comment,
Vote
],
// This setting will automatically create database tables in the database server
synchronize: true
});
}