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.
- package/db/sequelize_db.js +46 -8
- package/package.json +1 -1
package/db/sequelize_db.js
CHANGED
|
@@ -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 === "
|
|
35
|
-
? Sequelize.Transaction.ISOLATION_LEVELS.
|
|
36
|
-
: Sequelize.Transaction.ISOLATION_LEVELS.
|
|
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:
|
|
49
|
-
min:
|
|
50
|
-
idle:
|
|
51
|
-
acquire:
|
|
52
|
-
evict:
|
|
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,
|