agrs-sequelize-sdk 1.1.3 → 1.1.5

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/index.js CHANGED
@@ -1,14 +1,87 @@
1
+ // const Sequelize = require("sequelize");
2
+ // const path = require("path");
3
+ // const fs = require("fs");
4
+
5
+ // // const env = process.env.NODE_ENV || "development";
6
+ // class DBModels {
7
+ // constructor(config) {
8
+ // this.config = config;
9
+ // this.db = {};
10
+
11
+ // // Initialize Sequelize
12
+ // const sequelize = new Sequelize(
13
+ // this.config.database,
14
+ // this.config.username,
15
+ // this.config.password,
16
+ // {
17
+ // host: this.config.host,
18
+ // dialect: this.config.dialect,
19
+ // port: this.config.port,
20
+ // logging: false, // Enable logging to console
21
+ // dialectOptions: {
22
+ // ssl: {
23
+ // require: true, // Require SSL
24
+ // rejectUnauthorized: false, // For self-signed or unverified certificates
25
+ // },
26
+ // },
27
+ // pool: {
28
+ // max: 150, // Increase from 50, but stay under max_connections (200)
29
+ // min: 5, // Increase minimum connections
30
+ // acquire: 60000, // Double acquire timeout
31
+ // idle: 30000, // Align with idle_in_transaction_session_timeout
32
+ // },
33
+
34
+ // retry: {
35
+ // max: 5,
36
+ // timeout: 60000,
37
+ // match: [
38
+ // /Connection terminated/,
39
+ // /Connection timed out/,
40
+ // /Operation timeout/,
41
+ // /password authentication failed/,
42
+ // /canceling authentication/,
43
+ // ],
44
+ // },
45
+ // }
46
+ // );
47
+
48
+ // // Load all models
49
+ // fs.readdirSync(path.join(__dirname, "models"))
50
+ // .filter((file) => {
51
+ // return file.indexOf(".") !== 0 && file.slice(-3) === ".js";
52
+ // })
53
+ // .forEach((file) => {
54
+ // const model = require(path.join(__dirname, "models", file))(
55
+ // sequelize,
56
+ // Sequelize.DataTypes
57
+ // );
58
+ // this.db[model.name] = model;
59
+ // });
60
+
61
+ // // Set up associations
62
+ // Object.keys(this.db).forEach((modelName) => {
63
+ // if (this.db[modelName].associate) {
64
+ // this.db[modelName].associate(this.db);
65
+ // }
66
+ // });
67
+
68
+ // // Export the db object with Sequelize and models
69
+ // this.db.sequelize = sequelize;
70
+ // this.db.Sequelize = Sequelize;
71
+ // }
72
+ // }
73
+
74
+ // module.exports = DBModels;
75
+
1
76
  const Sequelize = require("sequelize");
2
77
  const path = require("path");
3
78
  const fs = require("fs");
4
79
 
5
- // const env = process.env.NODE_ENV || "development";
6
80
  class DBModels {
7
81
  constructor(config) {
8
82
  this.config = config;
9
83
  this.db = {};
10
84
 
11
- // Initialize Sequelize
12
85
  const sequelize = new Sequelize(
13
86
  this.config.database,
14
87
  this.config.username,
@@ -17,39 +90,45 @@ class DBModels {
17
90
  host: this.config.host,
18
91
  dialect: this.config.dialect,
19
92
  port: this.config.port,
20
- logging: false, // Enable logging to console
93
+ logging: false,
21
94
  dialectOptions: {
22
95
  ssl: {
23
- require: true, // Require SSL
24
- rejectUnauthorized: false, // For self-signed or unverified certificates
96
+ require: true,
97
+ rejectUnauthorized: false,
25
98
  },
99
+ statement_timeout: 30000,
100
+ idle_in_transaction_session_timeout: 30000,
101
+ connectTimeout: 30000,
26
102
  },
27
- // pool: {
28
- // max: 100,
29
- // min: 0,
30
- // acquire: 30000,
31
- // idle: 10000,
32
- // },
33
103
  pool: {
34
- max: 5, // Fewer connections for debugging
35
- min: 1,
36
- acquire: 30000,
37
- idle: 10000,
104
+ max: 50, // Quarter of max connections (200)
105
+ min: 0, // Start from 0 and scale up as needed
106
+ acquire: 30000, // Maximum time to wait for a connection
107
+ idle: 10000, // How long a connection can remain idle
108
+ },
109
+ retry: {
110
+ max: 3,
111
+ timeout: 20000,
112
+ match: [
113
+ /Connection terminated/,
114
+ /Connection timed out/,
115
+ /Operation timeout/,
116
+ /ECONNRESET/,
117
+ /PROTOCOL_CONNECTION_LOST/,
118
+ ],
38
119
  },
39
-
40
- // retry: {
41
- // // Add retry logic for failed connections
42
- // max: 3,
43
- // timeout: 30000,
44
- // match: [
45
- // /Connection terminated/,
46
- // /Connection timed out/,
47
- // /Operation timeout/,
48
- // ],
49
- // },
50
120
  }
51
121
  );
52
122
 
123
+ // Add connection monitoring listeners
124
+ sequelize.connectionManager.on("acquire", function (connection) {
125
+ console.log("Connection %d acquired", connection.threadId);
126
+ });
127
+
128
+ sequelize.connectionManager.on("release", function (connection) {
129
+ console.log("Connection %d released", connection.threadId);
130
+ });
131
+
53
132
  // Load all models
54
133
  fs.readdirSync(path.join(__dirname, "models"))
55
134
  .filter((file) => {
@@ -74,6 +153,17 @@ class DBModels {
74
153
  this.db.sequelize = sequelize;
75
154
  this.db.Sequelize = Sequelize;
76
155
  }
156
+
157
+ // Add method for pool monitoring
158
+ async getConnectionPoolStats() {
159
+ const pool = this.db.sequelize.connectionManager.pool;
160
+ return {
161
+ all: pool.size,
162
+ available: pool.available,
163
+ borrowed: pool.borrowed,
164
+ pending: pool.pending,
165
+ };
166
+ }
77
167
  }
78
168
 
79
169
  module.exports = DBModels;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",
@@ -1,24 +0,0 @@
1
- {
2
- "development": {
3
- "username": "postgres",
4
- "password": "1234",
5
- "database": "dev-db",
6
- "host": "localhost",
7
- "port": 5432,
8
- "dialect": "postgres"
9
- },
10
- "test": {
11
- "username": "root",
12
- "password": "1234",
13
- "database": "test-db",
14
- "host": "127:0.0.1",
15
- "dialect": "postgres"
16
- },
17
- "production": {
18
- "username": "root",
19
- "password": "1234",
20
- "database": "prod-db",
21
- "host": "127:0.0.1",
22
- "dialect": "postgres"
23
- }
24
- }