This repository has been archived by the owner on Sep 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbconn.ts
87 lines (75 loc) · 2.41 KB
/
dbconn.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
75
76
77
78
79
80
81
82
83
84
85
86
87
import { createConnection, Connection } from "typeorm";
import * as conn from "./config";
/**
* Responsable for all database functions
*/
export let dbConnection: Connection;
/**
* Creates connection to database
*/
export function connect(): Promise<void> {
let log: boolean = true;
// Only enable log if is development environment
if (process.env.NODE_ENV !== "DEV") {
log = false;
}
/**
* synchronize — Indicates if database schema should be auto created on every application launch.
* Be careful with this option and don’t use this in production — otherwise you can lose production data.
* This option is useful during debug and development.
*/
console.log(conn.dbConfig.username);
return createConnection({
type: "postgres",
host: conn.dbConfig.host,
port: Number.parseInt(conn.dbConfig.port),
username: conn.dbConfig.username,
password: conn.dbConfig.password,
database: conn.dbConfig.database,
synchronize: false,
migrationsRun: true,
logging: log,
maxQueryExecutionTime: 20000,
logger: "advanced-console",
entities: ["./build/src/entity/**/*.js"],
migrations: ["./build/src/migration/**/*.js"],
subscribers: ["./build/src/subscriber/**/*.js"],
cli: {
migrationsDir: "./src/migration"
}
})
.then(connection => {
dbConnection = connection;
console.log("> Connected to " + connection.options.database);
console.log("Running migrations...");
return connection
.runMigrations()
.then(migrations => {
if (migrations) {
const failMigrations = migrations
.filter(migration => migration.id === undefined)
.map(migration => migration.name);
if (failMigrations && failMigrations.length > 0) {
console.error("\n Migrations errors: " + failMigrations);
return Promise.reject();
}
console.log("Finished migrations");
if (migrations.length > 0) {
console.log(migrations.map(migration => migration.name));
}
return Promise.resolve();
}
console.log("Finished migrations");
return Promise.resolve();
})
.catch(() => {
return Promise.reject();
});
})
.catch(error => {
if (error) {
console.log(error);
}
return Promise.reject();
});
}