delore-crm-core 1.0.12 → 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 +142 -77
- package/package.json +1 -1
- package/utils/logger.js +66 -55
package/db/sequelize_db.js
CHANGED
|
@@ -1,93 +1,158 @@
|
|
|
1
|
-
const Sequelize = require(
|
|
2
|
-
const logger = require(
|
|
3
|
-
const authControl = require(
|
|
1
|
+
const Sequelize = require("sequelize");
|
|
2
|
+
const logger = require("../utils/logger.js");
|
|
3
|
+
const authControl = require("../auth/auth.control.js");
|
|
4
4
|
let sequelize = null;
|
|
5
5
|
|
|
6
|
-
const { MongoClient } = require(
|
|
6
|
+
const { MongoClient } = require("mongodb");
|
|
7
7
|
|
|
8
8
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
try {
|
|
19
|
+
await client.connect();
|
|
20
|
+
db = client.db("sessions");
|
|
21
|
+
logger.info("mongoDB - - > Conectado ao MongoDB!");
|
|
22
|
+
if (db) {
|
|
23
|
+
await authControl.loadAllSecretToMemory(db);
|
|
24
|
+
}
|
|
25
|
+
} catch (error) {
|
|
26
|
+
logger.error("mongoDB - - > Erro ao conectar:", error.message);
|
|
27
|
+
}
|
|
23
28
|
}
|
|
24
29
|
|
|
25
30
|
module.exports = {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
31
|
+
getMongoDB: function () {
|
|
32
|
+
return db;
|
|
33
|
+
},
|
|
34
|
+
getSequelize: function () {
|
|
35
|
+
return sequelize;
|
|
36
|
+
},
|
|
37
|
+
init(onConnect) {
|
|
38
|
+
let isolationLevel =
|
|
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);
|
|
43
|
+
|
|
44
|
+
logger.info("init - - > Iniciando conexão com o MySQL..." + isolationLevel);
|
|
45
|
+
|
|
46
|
+
sequelize = new Sequelize(
|
|
47
|
+
process.env.DB_DATABASE,
|
|
48
|
+
process.env.DB_USER,
|
|
49
|
+
process.env.DB_PASS,
|
|
50
|
+
{
|
|
51
|
+
dialect: "mysql",
|
|
52
|
+
host: process.env.DB_HOST,
|
|
53
|
+
pool: {
|
|
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),
|
|
59
|
+
},
|
|
60
|
+
timezone: "-03:00",
|
|
61
|
+
dialectOptions: {
|
|
62
|
+
multipleStatements: true,
|
|
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
|
+
},
|
|
96
|
+
define: {
|
|
97
|
+
underscored: false,
|
|
98
|
+
freezeTableName: true,
|
|
99
|
+
charset: "utf8",
|
|
100
|
+
timestamps: false,
|
|
101
|
+
createdAt: false,
|
|
102
|
+
updatedAt: false,
|
|
103
|
+
hooks: {
|
|
104
|
+
beforeValidate: async (model, options) => {
|
|
105
|
+
var models = sequelize.models;
|
|
106
|
+
var modelOriginal = models[model.constructor.name];
|
|
107
|
+
for (const key in options.fields) {
|
|
108
|
+
const field = options.fields[key];
|
|
109
|
+
if (
|
|
110
|
+
modelOriginal &&
|
|
111
|
+
modelOriginal.fieldRawAttributesMap[field] &&
|
|
112
|
+
modelOriginal.fieldRawAttributesMap[field].type.constructor
|
|
113
|
+
.key == "DATEONLY"
|
|
114
|
+
) {
|
|
115
|
+
if (model[field] === "") {
|
|
116
|
+
model[field] = null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (
|
|
120
|
+
modelOriginal &&
|
|
121
|
+
modelOriginal.fieldRawAttributesMap[field] &&
|
|
122
|
+
modelOriginal.fieldRawAttributesMap[field].type.constructor
|
|
123
|
+
.key == "DATE"
|
|
124
|
+
) {
|
|
125
|
+
if (model[field] === "") {
|
|
126
|
+
model[field] = null;
|
|
70
127
|
}
|
|
71
|
-
|
|
128
|
+
}
|
|
129
|
+
}
|
|
72
130
|
},
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
isolationLevel: isolationLevel,
|
|
134
|
+
logging: (sql) => logger.trace(sql),
|
|
135
|
+
},
|
|
136
|
+
);
|
|
77
137
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
138
|
+
sequelize
|
|
139
|
+
.authenticate()
|
|
140
|
+
.then(async () => {
|
|
141
|
+
//
|
|
142
|
+
// Após conectar ao MySQL, conecta ao MongoDB
|
|
143
|
+
//
|
|
144
|
+
await connectMongoDB();
|
|
85
145
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
146
|
+
logger.info(
|
|
147
|
+
"MySQL and MongoDB - - > Connection has been established successfully.",
|
|
148
|
+
);
|
|
149
|
+
onConnect();
|
|
150
|
+
})
|
|
151
|
+
.catch((err) => {
|
|
152
|
+
logger.error(
|
|
153
|
+
"MySQL and MongoDB - - > Unable to connect to the database:" +
|
|
154
|
+
err.message,
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
},
|
|
93
158
|
};
|
package/package.json
CHANGED
package/utils/logger.js
CHANGED
|
@@ -1,63 +1,74 @@
|
|
|
1
|
-
const pretty = require(
|
|
1
|
+
const pretty = require("pino-pretty");
|
|
2
2
|
|
|
3
3
|
const stream = pretty({
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
levelFirst: true,
|
|
5
|
+
colorize: true,
|
|
6
|
+
ignore: "pid,hostname",
|
|
7
|
+
translateTime: "yyyy-mm-dd HH:MM:ss.l",
|
|
8
8
|
});
|
|
9
9
|
|
|
10
|
-
const pino = require(
|
|
10
|
+
const pino = require("pino")(stream);
|
|
11
11
|
|
|
12
|
-
pino.level = process.env.PINO_LOG_LEVEL ||
|
|
12
|
+
pino.level = process.env.PINO_LOG_LEVEL || "info";
|
|
13
13
|
|
|
14
14
|
exports = module.exports = {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
15
|
+
setLevel(level) {
|
|
16
|
+
pino.level = level || "info";
|
|
17
|
+
},
|
|
18
|
+
|
|
19
|
+
getPino() {
|
|
20
|
+
return pino;
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
info(message) {
|
|
24
|
+
pino.info(message);
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
info(mensagem, fileName, methodName) {
|
|
28
|
+
pino.info({ fileName, methodName }, mensagem);
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
debug(message) {
|
|
32
|
+
pino.debug(message);
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
debug(mensagem, fileName, methodName) {
|
|
36
|
+
pino.debug({ fileName, methodName }, mensagem);
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
warn(message) {
|
|
40
|
+
pino.warn(message);
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
warn(mensagem, fileName, methodName) {
|
|
44
|
+
pino.warn({ fileName, methodName }, mensagem);
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
error(message) {
|
|
48
|
+
pino.error(message);
|
|
49
|
+
},
|
|
50
|
+
|
|
51
|
+
error(error, fileName, methodName) {
|
|
52
|
+
pino.error({
|
|
53
|
+
error: error.stack || error.message || error,
|
|
54
|
+
fileName,
|
|
55
|
+
methodName,
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
fatal(message) {
|
|
60
|
+
pino.fatal(message);
|
|
61
|
+
},
|
|
62
|
+
|
|
63
|
+
fatal(mensagem, fileName, methodName) {
|
|
64
|
+
pino.fatal({ fileName, methodName }, mensagem);
|
|
65
|
+
},
|
|
66
|
+
|
|
67
|
+
trace(message) {
|
|
68
|
+
pino.trace(message);
|
|
69
|
+
},
|
|
70
|
+
|
|
71
|
+
trace(mensagem, fileName, methodName) {
|
|
72
|
+
pino.trace({ fileName, methodName }, mensagem);
|
|
73
|
+
},
|
|
63
74
|
};
|