@flutry/sequelize 0.1.3 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/connect.d.ts +5 -4
- package/dist/utils/connect.js +49 -69
- package/package.json +7 -7
package/dist/utils/connect.d.ts
CHANGED
|
@@ -2,12 +2,13 @@ import { Sequelize } from 'sequelize';
|
|
|
2
2
|
export declare class Flutry_Connect {
|
|
3
3
|
static sequelize: Sequelize;
|
|
4
4
|
private readonly functions;
|
|
5
|
-
private
|
|
6
|
-
private
|
|
7
|
-
private retryDelay;
|
|
5
|
+
private static readonly MAX_RETRIES;
|
|
6
|
+
private static readonly RETRY_DELAY;
|
|
8
7
|
constructor();
|
|
9
8
|
static get isConnected(): boolean;
|
|
10
9
|
reconnect(): Promise<void>;
|
|
11
10
|
private init;
|
|
12
|
-
private
|
|
11
|
+
private disconnect;
|
|
12
|
+
private connectWithRetry;
|
|
13
|
+
private createConnection;
|
|
13
14
|
}
|
package/dist/utils/connect.js
CHANGED
|
@@ -8,83 +8,63 @@ const models_1 = require("../models/models");
|
|
|
8
8
|
class Flutry_Connect {
|
|
9
9
|
constructor() {
|
|
10
10
|
this.functions = new function_1.Functions();
|
|
11
|
-
this.retryCount = 0;
|
|
12
|
-
this.maxRetries = 5; // Maximum retry attempts
|
|
13
|
-
this.retryDelay = 5000; // 5 seconds
|
|
14
|
-
this.init = async () => {
|
|
15
|
-
if (await this.functions.DBConfigVerify()) {
|
|
16
|
-
this.connect();
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
this.connect = async () => {
|
|
20
|
-
Flutry_Connect.sequelize = new sequelize_1.Sequelize(`${process.env.DB_NAME}`, `${process.env.DB_USER}`, `${process.env.DB_PASS}`, {
|
|
21
|
-
host: `${process.env.DB_HOST}`,
|
|
22
|
-
dialect: `${process.env.DB_TYPE}`,
|
|
23
|
-
logging: false,
|
|
24
|
-
timezone: await this.functions.getTimeZone(),
|
|
25
|
-
pool: {
|
|
26
|
-
max: 5,
|
|
27
|
-
min: 0,
|
|
28
|
-
acquire: 30000, // 30 seconds timeout for acquiring connection
|
|
29
|
-
idle: 10000, // 10 seconds idle timeout
|
|
30
|
-
},
|
|
31
|
-
retry: {
|
|
32
|
-
match: [/ETIMEDOUT/, /EHOSTUNREACH/, /ECONNRESET/, /ECONNREFUSED/, /TIMEOUT/],
|
|
33
|
-
max: 3,
|
|
34
|
-
},
|
|
35
|
-
define: {
|
|
36
|
-
charset: 'utf8mb4',
|
|
37
|
-
collate: 'utf8mb4_bin',
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
try {
|
|
41
|
-
await Flutry_Connect.sequelize.authenticate();
|
|
42
|
-
main_1.Flutry_Sequelize.logger.info('Connection has been established successfully');
|
|
43
|
-
await new models_1.Flutry_Models();
|
|
44
|
-
this.retryCount = 0; // Reset retry count on successful connection
|
|
45
|
-
}
|
|
46
|
-
catch (error) {
|
|
47
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
48
|
-
const isTimeoutError = /timeout|ETIMEDOUT|ECONNRESET|EHOSTUNREACH|ECONNREFUSED/i.test(errorMessage);
|
|
49
|
-
if (isTimeoutError) {
|
|
50
|
-
main_1.Flutry_Sequelize.logger.error(`Database connection timeout. Attempt ${this.retryCount + 1}/${this.maxRetries}. Error: ${errorMessage}`);
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
main_1.Flutry_Sequelize.logger.error(`Database connection error. Attempt ${this.retryCount + 1}/${this.maxRetries}. Error: ${errorMessage}`);
|
|
54
|
-
}
|
|
55
|
-
if (this.retryCount < this.maxRetries) {
|
|
56
|
-
this.retryCount++;
|
|
57
|
-
const waitTime = isTimeoutError ? this.retryDelay * 2 : this.retryDelay; // Longer wait for timeout errors
|
|
58
|
-
main_1.Flutry_Sequelize.logger.info(`Retrying connection in ${waitTime / 1000} seconds...`);
|
|
59
|
-
setTimeout(() => {
|
|
60
|
-
this.connect();
|
|
61
|
-
}, waitTime);
|
|
62
|
-
}
|
|
63
|
-
else {
|
|
64
|
-
main_1.Flutry_Sequelize.logger.error('Maximum retry attempts reached. Unable to connect to database.');
|
|
65
|
-
main_1.Flutry_Sequelize.logger.error(`Final error: ${errorMessage}`);
|
|
66
|
-
this.retryCount = 0; // Reset for potential future attempts
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
11
|
this.init();
|
|
71
12
|
}
|
|
72
|
-
// Getter to check if connection is established
|
|
73
13
|
static get isConnected() {
|
|
74
|
-
return this.sequelize
|
|
14
|
+
return this.sequelize?.authenticate !== undefined;
|
|
75
15
|
}
|
|
76
|
-
// Manual reconnect method
|
|
77
16
|
async reconnect() {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
17
|
+
await this.disconnect();
|
|
18
|
+
await this.connectWithRetry();
|
|
19
|
+
}
|
|
20
|
+
async init() {
|
|
21
|
+
if (await this.functions.DBConfigVerify()) {
|
|
22
|
+
await this.connectWithRetry();
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async disconnect() {
|
|
26
|
+
try {
|
|
27
|
+
await Flutry_Connect.sequelize?.close();
|
|
28
|
+
}
|
|
29
|
+
catch (error) {
|
|
30
|
+
main_1.Flutry_Sequelize.logger.warn('Error closing connection:', error);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async connectWithRetry(attempt = 1) {
|
|
34
|
+
try {
|
|
35
|
+
await this.createConnection();
|
|
36
|
+
await Flutry_Connect.sequelize.authenticate();
|
|
37
|
+
main_1.Flutry_Sequelize.logger.info('Database connection established');
|
|
38
|
+
await new models_1.Flutry_Models();
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
42
|
+
main_1.Flutry_Sequelize.logger.error(`Connection failed (${attempt}/${Flutry_Connect.MAX_RETRIES}): ${errorMsg}`);
|
|
43
|
+
if (attempt < Flutry_Connect.MAX_RETRIES) {
|
|
44
|
+
main_1.Flutry_Sequelize.logger.info(`Retrying in ${Flutry_Connect.RETRY_DELAY / 1000}s...`);
|
|
45
|
+
setTimeout(() => this.connectWithRetry(attempt + 1), Flutry_Connect.RETRY_DELAY);
|
|
81
46
|
}
|
|
82
|
-
|
|
83
|
-
main_1.Flutry_Sequelize.logger.
|
|
47
|
+
else {
|
|
48
|
+
main_1.Flutry_Sequelize.logger.error('Max retries reached. Connection failed.');
|
|
84
49
|
}
|
|
85
50
|
}
|
|
86
|
-
|
|
87
|
-
|
|
51
|
+
}
|
|
52
|
+
async createConnection() {
|
|
53
|
+
Flutry_Connect.sequelize = new sequelize_1.Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASS, {
|
|
54
|
+
host: process.env.DB_HOST,
|
|
55
|
+
dialect: process.env.DB_TYPE,
|
|
56
|
+
logging: false,
|
|
57
|
+
timezone: await this.functions.getTimeZone(),
|
|
58
|
+
dialectOptions: { connectTimeout: 15000 },
|
|
59
|
+
pool: { max: 10, min: 2, acquire: 30000, idle: 10000 },
|
|
60
|
+
retry: {
|
|
61
|
+
match: [/ETIMEDOUT/, /EHOSTUNREACH/, /ECONNRESET/, /ECONNREFUSED/],
|
|
62
|
+
max: 3,
|
|
63
|
+
},
|
|
64
|
+
define: { charset: 'utf8mb4', collate: 'utf8mb4_bin' },
|
|
65
|
+
});
|
|
88
66
|
}
|
|
89
67
|
}
|
|
90
68
|
exports.Flutry_Connect = Flutry_Connect;
|
|
69
|
+
Flutry_Connect.MAX_RETRIES = 5;
|
|
70
|
+
Flutry_Connect.RETRY_DELAY = 5000;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flutry/sequelize",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
},
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"dotenv": "^17.
|
|
15
|
-
"mariadb": "^3.5.
|
|
16
|
-
"mysql2": "^3.
|
|
17
|
-
"sequelize": "^6.37.
|
|
14
|
+
"dotenv": "^17.4.0",
|
|
15
|
+
"mariadb": "^3.5.2",
|
|
16
|
+
"mysql2": "^3.20.0",
|
|
17
|
+
"sequelize": "^6.37.8"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@types/node": "^25.
|
|
21
|
-
"typescript": "^
|
|
20
|
+
"@types/node": "^25.5.2",
|
|
21
|
+
"typescript": "^6.0.2"
|
|
22
22
|
}
|
|
23
23
|
}
|