@flutry/sequelize 0.1.2 → 0.1.4

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.
@@ -2,10 +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 retryCount;
6
- private maxRetries;
7
- private retryDelay;
5
+ private static readonly MAX_RETRIES;
6
+ private static readonly RETRY_DELAY;
8
7
  constructor();
8
+ static get isConnected(): boolean;
9
+ reconnect(): Promise<void>;
9
10
  private init;
10
- private connect;
11
+ private disconnect;
12
+ private connectWithRetry;
13
+ private createConnection;
11
14
  }
@@ -8,47 +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 await this.functions.getTimeZone(),
25
- define: {
26
- charset: 'utf8mb4',
27
- collate: 'utf8mb4_bin',
28
- },
29
- });
30
- try {
31
- await Flutry_Connect.sequelize.authenticate();
32
- main_1.Flutry_Sequelize.logger.info('Connection has been established successfully');
33
- await new models_1.Flutry_Models();
34
- this.retryCount = 0; // Reset retry count on successful connection
11
+ this.init();
12
+ }
13
+ static get isConnected() {
14
+ return this.sequelize?.authenticate !== undefined;
15
+ }
16
+ async reconnect() {
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);
35
46
  }
36
- catch (error) {
37
- main_1.Flutry_Sequelize.logger.error(`Unable to connect to the database. Attempt ${this.retryCount + 1}/${this.maxRetries}`);
38
- if (this.retryCount < this.maxRetries) {
39
- this.retryCount++;
40
- main_1.Flutry_Sequelize.logger.info(`Retrying connection in ${this.retryDelay / 1000} seconds...`);
41
- setTimeout(() => {
42
- this.connect();
43
- }, this.retryDelay);
44
- }
45
- else {
46
- main_1.Flutry_Sequelize.logger.error('Maximum retry attempts reached. Unable to connect to database.');
47
- this.retryCount = 0; // Reset for potential future attempts
48
- }
47
+ else {
48
+ main_1.Flutry_Sequelize.logger.error('Max retries reached. Connection failed.');
49
49
  }
50
- };
51
- this.init();
50
+ }
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
+ });
52
66
  }
53
67
  }
54
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.2",
3
+ "version": "0.1.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -17,6 +17,7 @@
17
17
  "sequelize": "^6.37.7"
18
18
  },
19
19
  "devDependencies": {
20
+ "@types/node": "^25.3.1",
20
21
  "typescript": "^5.9.3"
21
22
  }
22
23
  }