-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.ts
135 lines (111 loc) · 4.32 KB
/
app.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import express from 'express';
import { Router } from './startup/router';
import { execSync } from 'child_process';
// import { Logger } from './startup/logger';
import mysql from 'mysql2/promise';
import { Logger } from './common/logger';
// import ErrsoleMySQL from 'errsole-mysql';
// import errsole from 'errsole';
export default class Application {
public _app: express.Application = null;
private _router: Router = null;
private static _instance: Application = null;
private constructor() {
this._app = express();
this._router = new Router(this._app);
}
public static instance(): Application {
// if (this._instance === null) {
// this._instance = new this();
// return this._instance;
// }
// else {
// return this._instance;
// }
return this._instance || (this._instance = new this())
}
start = async () => {
try {
this._app.use(express.json());
this._app.use(express.urlencoded());
// errsole.initialize({
// storage: new ErrsoleMySQL({
// host: 'localhost',
// user: 'root',
// password: process.env.PASSWORD,
// database: process.env.DATABASE
// })
// });
this.migrate();
this._router.init();
this.listen();
}
catch (error) {
}
}
private listen = async () => {
return new Promise((resolve, reject) => {
try {
this._app.listen(process.env.PORT, () => {
Logger.instance().log(`Form Service is running and listening on PORT ${process.env.PORT}`);
});
}
catch (error) {
Logger.instance().error("Error in Starting the server", 500, error);
}
})
}
// public migrate = async () => {
// try {
// const output = execSync('npx prisma migrate dev --name init');
// const str = output.toString();
// Logger.instance().log('Database migrated successfully!');
// Logger.instance().log(str);
// return true;
// } catch (error) {
// Logger.instance().log(error.message);
// }
// return false;
// };
public migrate = async () => {
const databaseUrl = process.env.DATABASE_URL;
if (!databaseUrl) {
throw new Error('DATABASE_URL is not defined in the .env file');
}
// Parse the database URL to extract connection parameters
const regex = /mysql:\/\/(.*?):(.*?)@(.*?)@(.*?):(.*?)\/(.*?)$/;
const matches = databaseUrl.match(regex);
if (!matches) {
throw new Error('DATABASE_URL format is incorrect');
}
const [_, user, pass, tail, host, port, database] = matches;
const password = pass + '@' + tail;
try {
const connection = await mysql.createConnection({
host,
port: parseInt(port),
user,
password
});
// Directly construct the query string without placeholders
const query = `SHOW DATABASES LIKE '${database}'`;
const [rows]: [mysql.RowDataPacket[], mysql.FieldPacket[]] = await connection.execute(query);
if (rows.length > 0) {
Logger.instance().log(`Database ${database} already exists. Connecting and syncing...`);
// Here you would add code to sync with the existing database if needed
} else {
Logger.instance().log(`Database ${database} does not exist. Migrating and syncing...`);
execSync('npx prisma migrate dev --name init');
Logger.instance().log('Database migrated and synced successfully!');
}
await connection.end();
return true;
} catch (error) {
Logger.instance().error('Migration failed:', 500, error.message);
Logger.instance().error('Migration failed:', 500, error.stack);
// Logger.instance().log(error.message);
// Logger.instance().log(error.stack); // Log stack trace for debugging purposes
return false;
}
};
}