delore-crm-core 1.0.12 → 1.0.13

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.
@@ -1,93 +1,120 @@
1
- const Sequelize = require('sequelize');
2
- const logger = require('../utils/logger.js');
3
- const authControl = require('../auth/auth.control.js');
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('mongodb');
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
12
  async function connectMongoDB() {
13
- try {
14
- await client.connect();
15
- db = client.db('sessions');
16
- logger.info('mongoDB - - > Conectado ao MongoDB!');
17
- if (db) {
18
- await authControl.loadAllSecretToMemory(db);
19
- }
20
- } catch (error) {
21
- logger.error('mongoDB - - > Erro ao conectar:', error.message);
22
- }
13
+ try {
14
+ await client.connect();
15
+ db = client.db("sessions");
16
+ logger.info("mongoDB - - > Conectado ao MongoDB!");
17
+ if (db) {
18
+ await authControl.loadAllSecretToMemory(db);
19
+ }
20
+ } catch (error) {
21
+ logger.error("mongoDB - - > Erro ao conectar:", error.message);
22
+ }
23
23
  }
24
24
 
25
25
  module.exports = {
26
- getMongoDB: function () {
27
- return db;
28
- },
29
- getSequelize: function () {
30
- return sequelize;
31
- },
32
- init(onConnect) {
33
- sequelize = new Sequelize(process.env.DB_DATABASE, process.env.DB_USER, process.env.DB_PASS, {
34
- dialect: 'mysql',
35
- host: process.env.DB_HOST,
36
- pool: {
37
- max: 400, // Aumentar o limite máximo de conexões se o tráfego for alto
38
- min: 20, // Manter um número mínimo de conexões ativas
39
- idle: 10000, // Tempo que uma conexão pode ficar ociosa (30 segundos)
40
- acquire: 60000, // Tempo máximo para aguardar uma conexão do pool (60 segundos)
41
- evict: 30000, // Remover conexões ociosas após 30 segundos
42
- },
43
- timezone: '-03:00',
44
- dialectOptions: {
45
- multipleStatements: true,
46
- },
47
- define: {
48
- underscored: false,
49
- freezeTableName: true,
50
- charset: 'utf8',
51
- timestamps: false,
52
- createdAt: false,
53
- updatedAt: false,
54
- hooks: {
55
- beforeValidate: async (model, options) => {
56
- var models = sequelize.models;
57
- var modelOriginal = models[model.constructor.name];
58
- for (const key in options.fields) {
59
- const field = options.fields[key];
60
- if (modelOriginal && modelOriginal.fieldRawAttributesMap[field] && modelOriginal.fieldRawAttributesMap[field].type.constructor.key == 'DATEONLY') {
61
- if (model[field] === '') {
62
- model[field] = null;
63
- }
64
- }
65
- if (modelOriginal && modelOriginal.fieldRawAttributesMap[field] && modelOriginal.fieldRawAttributesMap[field].type.constructor.key == 'DATE') {
66
- if (model[field] === '') {
67
- model[field] = null;
68
- }
69
- }
26
+ getMongoDB: function () {
27
+ return db;
28
+ },
29
+ getSequelize: function () {
30
+ return sequelize;
31
+ },
32
+ init(onConnect) {
33
+ let isolationLevel =
34
+ process.env.DB_ISOLATION === "READ_COMMITTED"
35
+ ? Sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
36
+ : Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ;
37
+
38
+ logger.info("init - - > Iniciando conexão com o MySQL..." + isolationLevel);
39
+
40
+ sequelize = new Sequelize(
41
+ process.env.DB_DATABASE,
42
+ process.env.DB_USER,
43
+ process.env.DB_PASS,
44
+ {
45
+ dialect: "mysql",
46
+ host: process.env.DB_HOST,
47
+ 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
53
+ },
54
+ timezone: "-03:00",
55
+ dialectOptions: {
56
+ multipleStatements: true,
57
+ },
58
+ define: {
59
+ underscored: false,
60
+ freezeTableName: true,
61
+ charset: "utf8",
62
+ timestamps: false,
63
+ createdAt: false,
64
+ updatedAt: false,
65
+ hooks: {
66
+ beforeValidate: async (model, options) => {
67
+ var models = sequelize.models;
68
+ var modelOriginal = models[model.constructor.name];
69
+ for (const key in options.fields) {
70
+ const field = options.fields[key];
71
+ if (
72
+ modelOriginal &&
73
+ modelOriginal.fieldRawAttributesMap[field] &&
74
+ modelOriginal.fieldRawAttributesMap[field].type.constructor
75
+ .key == "DATEONLY"
76
+ ) {
77
+ if (model[field] === "") {
78
+ model[field] = null;
79
+ }
80
+ }
81
+ if (
82
+ modelOriginal &&
83
+ modelOriginal.fieldRawAttributesMap[field] &&
84
+ modelOriginal.fieldRawAttributesMap[field].type.constructor
85
+ .key == "DATE"
86
+ ) {
87
+ if (model[field] === "") {
88
+ model[field] = null;
70
89
  }
71
- },
90
+ }
91
+ }
72
92
  },
73
- },
74
- isolationLevel: Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ,
75
- logging: (sql) => logger.info(sql),
76
- });
93
+ },
94
+ },
95
+ isolationLevel: isolationLevel,
96
+ logging: (sql) => logger.trace(sql),
97
+ },
98
+ );
77
99
 
78
- sequelize
79
- .authenticate()
80
- .then(async () => {
81
- //
82
- // Após conectar ao MySQL, conecta ao MongoDB
83
- //
84
- await connectMongoDB();
100
+ sequelize
101
+ .authenticate()
102
+ .then(async () => {
103
+ //
104
+ // Após conectar ao MySQL, conecta ao MongoDB
105
+ //
106
+ await connectMongoDB();
85
107
 
86
- logger.info('MySQL and MongoDB - - > Connection has been established successfully.');
87
- onConnect();
88
- })
89
- .catch((err) => {
90
- logger.error('MySQL and MongoDB - - > Unable to connect to the database:' + err.message);
91
- });
92
- },
108
+ logger.info(
109
+ "MySQL and MongoDB - - > Connection has been established successfully.",
110
+ );
111
+ onConnect();
112
+ })
113
+ .catch((err) => {
114
+ logger.error(
115
+ "MySQL and MongoDB - - > Unable to connect to the database:" +
116
+ err.message,
117
+ );
118
+ });
119
+ },
93
120
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "delore-crm-core",
3
- "version": "1.0.12",
3
+ "version": "1.0.13",
4
4
  "description": "Core CRM utilities for Delore projects",
5
5
  "main": "index.js",
6
6
  "type": "commonjs",
package/utils/logger.js CHANGED
@@ -1,63 +1,74 @@
1
- const pretty = require('pino-pretty');
1
+ const pretty = require("pino-pretty");
2
2
 
3
3
  const stream = pretty({
4
- levelFirst: true,
5
- colorize: true,
6
- ignore: 'pid,hostname',
7
- translateTime: 'yyyy-mm-dd HH:MM:ss.l',
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('pino')(stream);
10
+ const pino = require("pino")(stream);
11
11
 
12
- pino.level = process.env.PINO_LOG_LEVEL || 'fatal';
12
+ pino.level = process.env.PINO_LOG_LEVEL || "info";
13
13
 
14
14
  exports = module.exports = {
15
-
16
- setLevel(level) {
17
- pino.level = level || 'fatal';
18
- },
19
-
20
- getPino() {
21
- return pino;
22
- },
23
-
24
- info(message) {
25
- pino.info(message);
26
- },
27
-
28
- info(mensagem, fileName, methodName) {
29
- pino.info({ fileName, methodName }, mensagem);
30
- },
31
-
32
- debug(message) {
33
- pino.debug(message);
34
- },
35
-
36
- debug(mensagem, fileName, methodName) {
37
- pino.debug({ fileName, methodName }, mensagem);
38
- },
39
-
40
- warn(message) {
41
- pino.warn(message);
42
- },
43
-
44
- warn(mensagem, fileName, methodName) {
45
- pino.warn({ fileName, methodName }, mensagem);
46
- },
47
-
48
- error(message) {
49
- pino.error(message);
50
- },
51
-
52
- error(error, fileName, methodName) {
53
- pino.error({ error: error.stack || error.message || error, fileName, methodName });
54
- },
55
-
56
- fatal(message) {
57
- pino.fatal(message);
58
- },
59
-
60
- fatal(mensagem, fileName, methodName) {
61
- pino.fatal({ fileName, methodName }, mensagem);
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
  };