delore-crm-core 1.0.13 → 1.0.14

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/db/sequelize_db.js +46 -8
  2. package/package.json +1 -1
@@ -9,6 +9,11 @@ const uri = process.env.MONGODB_URI;
9
9
  const client = new MongoClient(uri);
10
10
  var db = null;
11
11
 
12
+ function envInt(name, defaultValue) {
13
+ const value = Number.parseInt(process.env[name], 10);
14
+ return Number.isNaN(value) ? defaultValue : value;
15
+ }
16
+
12
17
  async function connectMongoDB() {
13
18
  try {
14
19
  await client.connect();
@@ -31,9 +36,10 @@ module.exports = {
31
36
  },
32
37
  init(onConnect) {
33
38
  let isolationLevel =
34
- process.env.DB_ISOLATION === "READ_COMMITTED"
35
- ? Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
36
- : Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ;
39
+ process.env.DB_ISOLATION === "REPEATABLE_READ"
40
+ ? Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ
41
+ : Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED;
42
+ const lockWaitTimeout = envInt("DB_LOCK_WAIT_TIMEOUT", 15);
37
43
 
38
44
  logger.info("init - - > Iniciando conexão com o MySQL..." + isolationLevel);
39
45
 
@@ -45,16 +51,48 @@ module.exports = {
45
51
  dialect: "mysql",
46
52
  host: process.env.DB_HOST,
47
53
  pool: {
48
- max: 400, // Aumentar o limite máximo de conexões se o tráfego for alto
49
- min: 20, // Manter um número mínimo de conexões ativas
50
- idle: 10000, // Tempo que uma conexão pode ficar ociosa (30 segundos)
51
- acquire: 60000, // Tempo máximo para aguardar uma conexão do pool (60 segundos)
52
- evict: 30000, // Remover conexões ociosas após 30 segundos
54
+ max: envInt("DB_POOL_MAX", 30),
55
+ min: envInt("DB_POOL_MIN", 0),
56
+ idle: envInt("DB_POOL_IDLE", 10000),
57
+ acquire: envInt("DB_POOL_ACQUIRE", 30000),
58
+ evict: envInt("DB_POOL_EVICT", 10000),
53
59
  },
54
60
  timezone: "-03:00",
55
61
  dialectOptions: {
56
62
  multipleStatements: true,
57
63
  },
64
+ retry: {
65
+ max: envInt("DB_RETRY_MAX", 3),
66
+ match: [
67
+ /Deadlock/i,
68
+ /ER_LOCK_DEADLOCK/,
69
+ /ER_LOCK_WAIT_TIMEOUT/,
70
+ /Lock wait timeout exceeded/i,
71
+ ],
72
+ backoffBase: envInt("DB_RETRY_BACKOFF_BASE", 100),
73
+ backoffExponent: 1.5,
74
+ },
75
+ hooks: {
76
+ afterConnect: async (connection) => {
77
+ const query = (sql) =>
78
+ new Promise((resolve, reject) => {
79
+ connection.query(sql, (error) => {
80
+ if (error) reject(error);
81
+ else resolve();
82
+ });
83
+ });
84
+
85
+ await query(
86
+ `SET SESSION TRANSACTION ISOLATION LEVEL ${isolationLevel.replace(
87
+ "_",
88
+ " ",
89
+ )}`,
90
+ );
91
+ await query(
92
+ `SET SESSION innodb_lock_wait_timeout = ${lockWaitTimeout}`,
93
+ );
94
+ },
95
+ },
58
96
  define: {
59
97
  underscored: false,
60
98
  freezeTableName: true,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "delore-crm-core",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Core CRM utilities for Delore projects",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",