exodus-framework 2.0.722 → 2.0.724
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/lib/services/sequelize.d.ts +10 -12
- package/lib/services/sequelize.d.ts.map +1 -1
- package/lib/services/sequelize.js +115 -130
- package/package.json +1 -1
@@ -11,22 +11,20 @@ declare class SequelizeService extends Service implements IService {
|
|
11
11
|
relations: Map<string, EnvDBHost>;
|
12
12
|
constructor();
|
13
13
|
init(): Promise<void>;
|
14
|
-
|
15
|
-
|
16
|
-
getMasterConnection(): Sequelize;
|
14
|
+
connectDatabases(): Promise<unknown>;
|
15
|
+
getDB(tenantId: string): Promise<false | Sequelize>;
|
17
16
|
createDB(hostUuid: string, name: string): Promise<boolean>;
|
18
17
|
deleteDB(hostUuid: string, name: string): Promise<boolean>;
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
initDB(tenantId: string): Promise<false | Sequelize>;
|
19
|
+
getDBName(envToken: string): string;
|
20
|
+
reconnect(db: Sequelize, attempts?: number): void;
|
21
|
+
createConnection(hostUuid: string, database?: string): Promise<false | Sequelize>;
|
22
|
+
testConnection(host: string, port: number, username: string, password: string): Promise<boolean>;
|
23
|
+
getRelation(tenantId: string): Promise<false | EnvDBHost>;
|
22
24
|
getModel<M extends typeof Model<any, any>>(model: typeof Model<any, any> & {
|
23
25
|
initialize: (connection: Sequelize) => M;
|
24
|
-
}, tenantId: string): Promise<M>;
|
25
|
-
|
26
|
-
envToken: string;
|
27
|
-
con_uuid: string;
|
28
|
-
};
|
29
|
-
getDatabaseNamePattern(envToken: string): string;
|
26
|
+
}, tenantId: string): Promise<false | M>;
|
27
|
+
registerModels(models: ModelStatic<any>[]): void;
|
30
28
|
}
|
31
29
|
export default SequelizeService;
|
32
30
|
//# sourceMappingURL=sequelize.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"sequelize.d.ts","sourceRoot":"","sources":["../../src/services/sequelize.ts"],"names":[],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Q,CAAC,EAAE,MAAM;IA6CpD,cAAc,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;IA4B7E,WAAW,CAAC,QAAQ,EAAE,MAAM;IAmB5B,QAAQ,CAAC,CAAC,SAAS,OAAO,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,EAC7C,KAAK,EAAE,OAAO,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG;QAAE,UAAU,EAAE,CAAC,UAAU,EAAE,SAAS,KAAK,CAAC,CAAA;KAAE,EAC5E,QAAQ,EAAE,MAAM;IAsBlB,cAAc,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,EAAE;CAG1C;AAED,eAAe,gBAAgB,CAAC"}
|
@@ -7,11 +7,11 @@ exports.default = void 0;
|
|
7
7
|
var _sequelize = require("sequelize");
|
8
8
|
var _app = require("../app");
|
9
9
|
var _service = _interopRequireDefault(require("../app/service"));
|
10
|
+
var _models = require("../models");
|
10
11
|
var _DatabaseHost = require("../models/DatabaseHost");
|
11
12
|
var _database = require("../utils/database");
|
12
13
|
var _logger = _interopRequireDefault(require("../utils/logger"));
|
13
14
|
var _error = require("./error");
|
14
|
-
var _models = require("../models");
|
15
15
|
var _security = _interopRequireDefault(require("./security"));
|
16
16
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
17
17
|
/**
|
@@ -38,9 +38,9 @@ class SequelizeService extends _service.default {
|
|
38
38
|
this.masterDB = _database.masterDB;
|
39
39
|
this.connections = new Map();
|
40
40
|
this.initializedModels = new Map();
|
41
|
-
await this.
|
41
|
+
await this.connectDatabases();
|
42
42
|
}
|
43
|
-
async
|
43
|
+
async connectDatabases() {
|
44
44
|
if (!this.models) throw new _error.ApplicationException('Models are requireds! Call registerModels(modelsArray)');
|
45
45
|
|
46
46
|
// #Include native models
|
@@ -64,7 +64,81 @@ class SequelizeService extends _service.default {
|
|
64
64
|
});
|
65
65
|
}
|
66
66
|
|
67
|
-
|
67
|
+
// #Database
|
68
|
+
async getDB(tenantId) {
|
69
|
+
/* master or service */
|
70
|
+
if (tenantId == _app.Core.settings.getDatabase().service.database) {
|
71
|
+
return _database.serviceDB;
|
72
|
+
} else if (tenantId == _app.Core.settings.getDatabase().master.database) {
|
73
|
+
return _database.masterDB;
|
74
|
+
}
|
75
|
+
const relation = await this.getRelation(tenantId);
|
76
|
+
if (!relation) return false;
|
77
|
+
const key = `${relation.hostUuid}@${this.getDBName(relation.envToken)}`;
|
78
|
+
return this.connections.get(key) || (await this.initDB(tenantId));
|
79
|
+
}
|
80
|
+
async createDB(hostUuid, name) {
|
81
|
+
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
82
|
+
if (!host) {
|
83
|
+
this.log('Database host information not found: ' + hostUuid);
|
84
|
+
return false;
|
85
|
+
}
|
86
|
+
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
|
87
|
+
const connection = await this.createConnection(hostUuid);
|
88
|
+
if (connection) {
|
89
|
+
try {
|
90
|
+
const [results] = await connection.query(`SHOW DATABASES LIKE '${dbQueryName}';`);
|
91
|
+
if (results.length === 0) {
|
92
|
+
// Se o banco de dados não existir, cria-o
|
93
|
+
await connection.query(`CREATE DATABASE ${dbQueryName};`);
|
94
|
+
this.log(`Database "${dbQueryName}" has been created.`);
|
95
|
+
return true;
|
96
|
+
} else {
|
97
|
+
this.log(`Database "${dbQueryName}" already exists.`);
|
98
|
+
return false;
|
99
|
+
}
|
100
|
+
} catch (error) {
|
101
|
+
new _error.ApplicationException('Erro on creating database', error);
|
102
|
+
} finally {
|
103
|
+
connection.close();
|
104
|
+
}
|
105
|
+
}
|
106
|
+
return false;
|
107
|
+
}
|
108
|
+
async deleteDB(hostUuid, name) {
|
109
|
+
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
|
110
|
+
const connection = await this.createConnection(hostUuid);
|
111
|
+
if (connection) {
|
112
|
+
try {
|
113
|
+
await connection.query(`DROP DATABASE IF EXISTS ${dbQueryName};`);
|
114
|
+
this.log(`Database "${dbQueryName}" has been deleted.`);
|
115
|
+
return true;
|
116
|
+
} catch (error) {
|
117
|
+
return false;
|
118
|
+
} finally {
|
119
|
+
connection.close();
|
120
|
+
}
|
121
|
+
}
|
122
|
+
return false;
|
123
|
+
}
|
124
|
+
async initDB(tenantId) {
|
125
|
+
const relation = await this.getRelation(tenantId);
|
126
|
+
if (!relation) return false;
|
127
|
+
const dbName = this.getDBName(relation.envToken);
|
128
|
+
const key = `${relation.hostUuid}@${this.getDBName(relation.envToken)}`;
|
129
|
+
if (this.connections.has(key)) return this.connections.get(key);
|
130
|
+
const connection = await this.createConnection(relation.hostUuid, dbName);
|
131
|
+
if (connection) {
|
132
|
+
this.log(`✅ Connected ${dbName} database sucessfully`, 'success');
|
133
|
+
this.connections.set(key, connection);
|
134
|
+
return connection;
|
135
|
+
}
|
136
|
+
}
|
137
|
+
getDBName(envToken) {
|
138
|
+
return `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
|
139
|
+
}
|
140
|
+
|
141
|
+
//#Connection
|
68
142
|
reconnect(db, attempts = 2) {
|
69
143
|
db.sync().then(() => {
|
70
144
|
this.log('Database pronto', 'success');
|
@@ -78,20 +152,27 @@ class SequelizeService extends _service.default {
|
|
78
152
|
}
|
79
153
|
});
|
80
154
|
}
|
81
|
-
|
82
|
-
return this.masterDB;
|
83
|
-
}
|
84
|
-
async createDB(hostUuid, name) {
|
155
|
+
async createConnection(hostUuid, database) {
|
85
156
|
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
86
157
|
if (!host) {
|
87
|
-
this.log('Database host information not found: ' + hostUuid);
|
158
|
+
this.log('createConnection:: Database host information not found: ' + hostUuid);
|
88
159
|
return false;
|
89
160
|
}
|
90
|
-
const
|
161
|
+
const key = await _security.default.singleton().loadKeyByStr(host.credential);
|
162
|
+
const data = await _security.default.singleton().verifySignature(host.password, key);
|
163
|
+
if (!data) {
|
164
|
+
this.log('createConnection::Error on decript password by credential: ' + hostUuid, 'danger');
|
165
|
+
return false;
|
166
|
+
}
|
167
|
+
const password = data.payload;
|
168
|
+
const queryHost = host.host.split(':');
|
91
169
|
const sequelize = new _sequelize.Sequelize({
|
170
|
+
host: queryHost[0],
|
171
|
+
port: Number(queryHost[1]),
|
92
172
|
dialect: host.dialect,
|
173
|
+
database,
|
93
174
|
username: host.username,
|
94
|
-
password:
|
175
|
+
password: password,
|
95
176
|
define: {
|
96
177
|
timestamps: true
|
97
178
|
},
|
@@ -102,68 +183,38 @@ class SequelizeService extends _service.default {
|
|
102
183
|
});
|
103
184
|
return new Promise((resolve, reject) => {
|
104
185
|
sequelize.authenticate().then(async () => {
|
105
|
-
|
106
|
-
if (results.length === 0) {
|
107
|
-
// Se o banco de dados não existir, cria-o
|
108
|
-
await sequelize.query(`CREATE DATABASE ${dbQueryName};`);
|
109
|
-
this.log(`Database "${dbQueryName}" has been created.`);
|
110
|
-
resolve(true);
|
111
|
-
} else {
|
112
|
-
this.log(`Database "${dbQueryName}" already exists.`);
|
113
|
-
resolve(false);
|
114
|
-
}
|
186
|
+
resolve(sequelize);
|
115
187
|
}).catch(reason => {
|
116
|
-
new _error.ApplicationException('Error while checking/creating database:', reason);
|
117
188
|
reject(reason);
|
118
|
-
}).finally(() => {
|
119
|
-
sequelize.close();
|
120
189
|
});
|
121
190
|
});
|
122
191
|
}
|
123
|
-
async
|
124
|
-
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
|
125
|
-
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
126
|
-
if (!host) {
|
127
|
-
this.log('Database host information not found: ' + hostUuid);
|
128
|
-
return false;
|
129
|
-
}
|
192
|
+
async testConnection(host, port, username, password) {
|
130
193
|
const sequelize = new _sequelize.Sequelize({
|
131
|
-
host
|
132
|
-
|
133
|
-
username
|
134
|
-
password
|
135
|
-
define: {
|
136
|
-
timestamps: true
|
137
|
-
},
|
194
|
+
host,
|
195
|
+
port,
|
196
|
+
username,
|
197
|
+
password,
|
138
198
|
timezone: '-03:00',
|
139
199
|
logging(sql, timing) {
|
140
200
|
(0, _logger.default)().trace('DATABASE', sql, timing);
|
141
201
|
}
|
142
202
|
});
|
143
|
-
return new Promise(
|
203
|
+
return new Promise(resolve => {
|
144
204
|
sequelize.authenticate().then(async () => {
|
145
|
-
// Se o banco de dados não existir, cria-o
|
146
|
-
await sequelize.query(`DROP DATABASE IF EXISTS ${dbQueryName};`);
|
147
|
-
this.log(`Database "${dbQueryName}" has been deleted.`);
|
148
205
|
resolve(true);
|
149
206
|
}).catch(reason => {
|
150
|
-
new _error.ApplicationException('
|
151
|
-
|
207
|
+
new _error.ApplicationException('teste de conexão falhou', reason);
|
208
|
+
resolve(false);
|
152
209
|
}).finally(() => {
|
153
210
|
sequelize.close();
|
154
211
|
});
|
155
212
|
});
|
156
213
|
}
|
157
|
-
async getDatabaseConnection(tenantId) {
|
158
|
-
let relation;
|
159
|
-
|
160
|
-
/* master or service */
|
161
|
-
if (tenantId == _app.Core.settings.getDatabase().service.database) {
|
162
|
-
return _database.serviceDB;
|
163
|
-
} else if (tenantId == _app.Core.settings.getDatabase().master.database) {
|
164
|
-
return _database.masterDB;
|
165
|
-
}
|
166
214
|
|
215
|
+
//# Common
|
216
|
+
async getRelation(tenantId) {
|
217
|
+
let relation;
|
167
218
|
/* fetch relation */
|
168
219
|
if (this.relations.get(tenantId)) {
|
169
220
|
relation = this.relations.get(tenantId);
|
@@ -179,92 +230,26 @@ class SequelizeService extends _service.default {
|
|
179
230
|
}
|
180
231
|
this.relations.set(tenantId, relation);
|
181
232
|
}
|
182
|
-
|
183
|
-
return this.connections.get(queryId) || (await this.initDatabase(tenantId));
|
184
|
-
}
|
185
|
-
async initDatabase(tenantId) {
|
186
|
-
const {
|
187
|
-
con_uuid: hostUuid,
|
188
|
-
envToken
|
189
|
-
} = this.parseDataInfoByTenantId(tenantId);
|
190
|
-
const dbName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`; //nomeclatura padrão do banco de dados do serviço
|
191
|
-
const queryId = `${dbName}@${hostUuid}`;
|
192
|
-
if (this.connections.get(queryId)) {
|
193
|
-
return this.connections.get(queryId);
|
194
|
-
}
|
195
|
-
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
196
|
-
if (!host) {
|
197
|
-
this.log('Error on init connection, data not found! uuid: ' + hostUuid, 'danger');
|
198
|
-
return false;
|
199
|
-
}
|
200
|
-
const key = await _security.default.singleton().loadKeyByStr(host.credential);
|
201
|
-
const data = await _security.default.singleton().verifySignature(host.password, key);
|
202
|
-
if (!data) {
|
203
|
-
this.log('Error on decript password by credential: ' + hostUuid, 'danger');
|
204
|
-
return false;
|
205
|
-
}
|
206
|
-
const password = data.payload;
|
207
|
-
const conn = new _sequelize.Sequelize({
|
208
|
-
host: host.host,
|
209
|
-
database: dbName,
|
210
|
-
dialect: host.dialect,
|
211
|
-
username: host.username,
|
212
|
-
password: password,
|
213
|
-
define: {
|
214
|
-
timestamps: true
|
215
|
-
},
|
216
|
-
timezone: '-03:00',
|
217
|
-
logging(sql, timing) {
|
218
|
-
(0, _logger.default)().trace('DATABASE', sql, timing);
|
219
|
-
}
|
220
|
-
});
|
221
|
-
return new Promise((resolve, reject) => {
|
222
|
-
conn.sync({
|
223
|
-
force: false
|
224
|
-
}).then(() => {
|
225
|
-
this.log(`✅ Connected ${dbName} database sucessfully`, 'success');
|
226
|
-
this.connections.set(queryId, conn);
|
227
|
-
resolve(conn);
|
228
|
-
}).catch(reason => {
|
229
|
-
this.log(`error trying to connect: ${dbName}, UUID: ${hostUuid}`);
|
230
|
-
reject(reason);
|
231
|
-
new _error.ApplicationException(`error trying to connect: ${dbName}, UUID: ${hostUuid}`, reason);
|
232
|
-
});
|
233
|
-
});
|
234
|
-
}
|
235
|
-
registerModels(models) {
|
236
|
-
this.models = models;
|
233
|
+
return relation;
|
237
234
|
}
|
235
|
+
|
236
|
+
// #Entity
|
238
237
|
async getModel(model, tenantId) {
|
239
|
-
const
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
const
|
244
|
-
const cache = this.initializedModels.get(modelInitID);
|
245
|
-
if (cache) {
|
246
|
-
return cache;
|
247
|
-
}
|
248
|
-
const connection = await this.getDatabaseConnection(tenantId);
|
238
|
+
const relation = await this.getRelation(tenantId);
|
239
|
+
if (!relation) return false;
|
240
|
+
const modelKey = `${relation.envToken}@${relation.hostUuid}#${model.name}`;
|
241
|
+
if (this.initializedModels.has(modelKey)) return this.initializedModels.get(modelKey);
|
242
|
+
const connection = await this.getDB(tenantId);
|
249
243
|
if (!connection) {
|
250
244
|
throw new _error.ApplicationException('Não foi possível obter o model espeficificado: Conexão não encontrada');
|
251
245
|
}
|
252
246
|
const m = model.initialize(connection);
|
253
247
|
await m.sync();
|
254
|
-
this.initializedModels.set(
|
248
|
+
this.initializedModels.set(modelKey, m);
|
255
249
|
return m;
|
256
250
|
}
|
257
|
-
|
258
|
-
|
259
|
-
const envToken = e[0];
|
260
|
-
const con_uuid = e[1];
|
261
|
-
return {
|
262
|
-
envToken,
|
263
|
-
con_uuid
|
264
|
-
};
|
265
|
-
}
|
266
|
-
getDatabaseNamePattern(envToken) {
|
267
|
-
return `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
|
251
|
+
registerModels(models) {
|
252
|
+
this.models = models;
|
268
253
|
}
|
269
254
|
}
|
270
255
|
var _default = exports.default = SequelizeService;
|