agrs-sequelize-sdk 1.1.0 → 1.1.1

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.
Files changed (2) hide show
  1. package/index.js +17 -191
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1,111 +1,10 @@
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: 100,
29
- // // min: 0,
30
- // // acquire: 30000,
31
- // // idle: 10000,
32
- // // },
33
- // pool: {
34
- // max: 20, // Reduced to a more reasonable number
35
- // min: 5, // Keep some connections alive
36
- // acquire: 60000, // Increased timeout for acquiring connections
37
- // idle: 20000, // Increased idle time
38
- // evict: 30000, // Added eviction running time
39
- // handleDisconnects: true, // Add automatic handling of disconnected connections
40
- // },
41
- // retry: {
42
- // max: 3,
43
- // timeout: 6000, // Increase timeout to 60 seconds
44
- // match: [
45
- // /Connection terminated/,
46
- // /Connection timed out/,
47
- // /Operation timeout/,
48
- // /unknown timed out/,
49
- // /TimeoutError/,
50
- // ],
51
- // backoffBase: 1000,
52
- // backoffExponent: 1.5,
53
- // },
54
- // }
55
- // );
56
-
57
- // // Load all models
58
- // fs.readdirSync(path.join(__dirname, "models"))
59
- // .filter((file) => {
60
- // return file.indexOf(".") !== 0 && file.slice(-3) === ".js";
61
- // })
62
- // .forEach((file) => {
63
- // const model = require(path.join(__dirname, "models", file))(
64
- // sequelize,
65
- // Sequelize.DataTypes
66
- // );
67
- // this.db[model.name] = model;
68
- // });
69
-
70
- // // Set up associations
71
- // Object.keys(this.db).forEach((modelName) => {
72
- // if (this.db[modelName].associate) {
73
- // this.db[modelName].associate(this.db);
74
- // }
75
- // });
76
-
77
- // // Export the db object with Sequelize and models
78
- // this.db.sequelize = sequelize;
79
- // this.db.Sequelize = Sequelize;
80
- // }
81
- // }
82
-
83
- // module.exports = DBModels;
84
-
85
1
  const Sequelize = require("sequelize");
86
2
  const path = require("path");
87
3
  const fs = require("fs");
88
4
 
5
+ // const env = process.env.NODE_ENV || "development";
89
6
  class DBModels {
90
7
  constructor(config) {
91
- // Validate config
92
- const requiredFields = [
93
- "database",
94
- "username",
95
- "password",
96
- "host",
97
- "dialect",
98
- "port",
99
- ];
100
- for (const field of requiredFields) {
101
- if (!config[field]) {
102
- throw new Error(`Missing required database config field: ${field}`);
103
- }
104
- }
105
-
106
- const sanitizedConfig = { ...config, password: "****" };
107
- console.log("Initializing DB with config:", sanitizedConfig);
108
-
109
8
  this.config = config;
110
9
  this.db = {};
111
10
 
@@ -118,37 +17,30 @@ class DBModels {
118
17
  host: this.config.host,
119
18
  dialect: this.config.dialect,
120
19
  port: this.config.port,
121
- logging: (msg) => {
122
- if (msg.includes("authentication") || msg.includes("connection")) {
123
- console.log("DB Operation:", msg);
124
- }
125
- },
20
+ logging: false, // Enable logging to console
126
21
  dialectOptions: {
127
22
  ssl: {
128
- require: true,
129
- rejectUnauthorized: false,
23
+ require: true, // Require SSL
24
+ rejectUnauthorized: false, // For self-signed or unverified certificates
130
25
  },
131
26
  },
27
+ // pool: {
28
+ // max: 100,
29
+ // min: 0,
30
+ // acquire: 30000,
31
+ // idle: 10000,
32
+ // },
132
33
  pool: {
133
- max: 20,
134
- min: 5,
135
- acquire: 60000,
136
- idle: 20000,
137
- evict: 30000,
138
- handleDisconnects: true,
139
- validate: async (connection) => {
140
- try {
141
- await connection.query("SELECT 1");
142
- return true;
143
- } catch (err) {
144
- console.error("Connection validation failed:", err);
145
- return false;
146
- }
147
- },
34
+ max: 20, // Reduced to a more reasonable number
35
+ min: 5, // Keep some connections alive
36
+ acquire: 60000, // Increased timeout for acquiring connections
37
+ idle: 20000, // Increased idle time
38
+ evict: 30000, // Added eviction running time
39
+ handleDisconnects: true, // Add automatic handling of disconnected connections
148
40
  },
149
41
  retry: {
150
42
  max: 3,
151
- timeout: 60000,
43
+ timeout: 6000, // Increase timeout to 60 seconds
152
44
  match: [
153
45
  /Connection terminated/,
154
46
  /Connection timed out/,
@@ -159,46 +51,9 @@ class DBModels {
159
51
  backoffBase: 1000,
160
52
  backoffExponent: 1.5,
161
53
  },
162
- hooks: {
163
- beforeConnect: (config) => {
164
- console.log("Attempting to connect with user:", config.username);
165
- },
166
- afterConnect: (connection) => {
167
- console.log("Connection established successfully");
168
- },
169
- },
170
54
  }
171
55
  );
172
56
 
173
- // Add periodic connection health check
174
- const healthCheckInterval = setInterval(async () => {
175
- try {
176
- const [results] = await sequelize.query(
177
- `
178
- SELECT count(*) as connection_count
179
- FROM pg_stat_activity
180
- WHERE datname = $1
181
- `,
182
- {
183
- bind: [this.config.database],
184
- type: Sequelize.QueryTypes.SELECT,
185
- }
186
- );
187
- console.log("Current database connections:", results.connection_count);
188
-
189
- // Check pool status
190
- await this.checkConnection();
191
- } catch (error) {
192
- console.error("Pool health check failed:", error);
193
- }
194
- }, 30000);
195
-
196
- // Cleanup on application shutdown
197
- process.on("SIGTERM", () => {
198
- clearInterval(healthCheckInterval);
199
- sequelize.close().catch(console.error);
200
- });
201
-
202
57
  // Load all models
203
58
  fs.readdirSync(path.join(__dirname, "models"))
204
59
  .filter((file) => {
@@ -222,35 +77,6 @@ class DBModels {
222
77
  // Export the db object with Sequelize and models
223
78
  this.db.sequelize = sequelize;
224
79
  this.db.Sequelize = Sequelize;
225
-
226
- // Initial connection test
227
- sequelize
228
- .authenticate()
229
- .then(() => {
230
- console.log("Database connection established successfully");
231
- })
232
- .catch((err) => {
233
- console.error("Unable to connect to the database:", err);
234
- });
235
- }
236
-
237
- // Helper method to check connection health
238
- async checkConnection() {
239
- try {
240
- await this.db.sequelize.authenticate();
241
- const [results] = await this.db.sequelize.query(
242
- "SELECT COUNT(*) as pool_size FROM pg_stat_activity WHERE state = $1",
243
- {
244
- bind: ["active"],
245
- type: this.db.Sequelize.QueryTypes.SELECT,
246
- }
247
- );
248
- console.log("Active connections:", results.pool_size);
249
- return true;
250
- } catch (error) {
251
- console.error("Database connection check failed:", error);
252
- return false;
253
- }
254
80
  }
255
81
  }
256
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",