-
Notifications
You must be signed in to change notification settings - Fork 0
/
db copy.ts
73 lines (63 loc) · 2.22 KB
/
db copy.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
import { drizzle } from "drizzle-orm/mysql2";
import { migrate } from "drizzle-orm/mysql2/migrator";
import mysql from "mysql2";
import * as dotenv from "dotenv";
dotenv.config({ path: '.env.development' });
console.log("\n\n\n\n")
console.log(Bun.env)
console.log("\n\n\n\n")
const dbInfo = {
host: 'db',
user: Bun.env.MYSQL_USER,
password: Bun.env.MYSQL_PASSWORD,
database: Bun.env.MYSQL_DATABASE,
};
let isConnected: boolean = false;
let connection;
let connectionURL = process.env.MYSQL_DATABASE_URL!;
// console.log("***********************")
// console.log("process.env")
// console.log(process.env)
// console.log("connectionURL")
// console.log(connectionURL)
// console.log("***********************")
const tryDBConnection = () => {
if (isConnected) {
console.log('にデースに接続されています。');
return drizzle(connection); // 存の接をす
}
for (let attempt = 1; attempt <= 100; attempt++) {
console.log(`デースに接に行します。 ${attempt}目`);
console.log("connectionURL: ", connectionURL)
try {
connection = mysql.createPool(connectionURL);
const dbConnection = drizzle(connection);
console.log('デースの接に成功しました。');
isConnected = true; //成功時にフラグをtrueに定
return dbConnection;
} catch (error) {
console.log(`接行 ${attempt}目に失しました。`, error);
if (attempt === 30) {
console.log('デースの接に失しました。');
throw new Error('デースの接に失しました。');
}
new Promise(resolve => setTimeout(resolve, 1000));
}
}
};
const init = () => {
try {
const dbConnection = tryDBConnection();
migrate(dbConnection, { migrationsFolder: "drizzle/migrations" });
return dbConnection;
} catch (error) {
console.error('デースの初期化中にエラーが生しました。', error);
} finally {
// if (connection) {
// connection.end();
// console.log('デース接をじました。');
// }
}
};
export const db = init();
export default db;