exodus-framework 2.0.723 → 2.0.725
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 +9 -12
- package/lib/services/sequelize.d.ts.map +1 -1
- package/lib/services/sequelize.js +122 -159
- package/package.json +1 -1
@@ -11,23 +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
|
-
|
22
|
-
|
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<EnvDBHost>;
|
23
24
|
getModel<M extends typeof Model<any, any>>(model: typeof Model<any, any> & {
|
24
25
|
initialize: (connection: Sequelize) => M;
|
25
26
|
}, tenantId: string): Promise<M>;
|
26
|
-
|
27
|
-
envToken: string;
|
28
|
-
con_uuid: string;
|
29
|
-
};
|
30
|
-
getDatabaseNamePattern(envToken: string): string;
|
27
|
+
registerModels(models: ModelStatic<any>[]): void;
|
31
28
|
}
|
32
29
|
export default SequelizeService;
|
33
30
|
//# sourceMappingURL=sequelize.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"sequelize.d.ts","sourceRoot":"","sources":["../../src/services/sequelize.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,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,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;;IAM5B,IAAI;IAOJ,gBAAgB;IAgChB,KAAK,CAAC,QAAQ,EAAE,MAAM;IActB,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IA6BvC,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IAiBvC,MAAM,CAAC,QAAQ,EAAE,MAAM;IAiB7B,SAAS,CAAC,QAAQ,EAAE,MAAM;IAK1B,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,QAAQ,SAAI;IAe/B,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,QAAQ,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;IAe5B,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"}
|
@@ -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,22 +64,18 @@ class SequelizeService extends _service.default {
|
|
64
64
|
});
|
65
65
|
}
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
});
|
80
|
-
}
|
81
|
-
getMasterConnection() {
|
82
|
-
return this.masterDB;
|
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));
|
83
79
|
}
|
84
80
|
async createDB(hostUuid, name) {
|
85
81
|
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
@@ -88,52 +84,95 @@ class SequelizeService extends _service.default {
|
|
88
84
|
return false;
|
89
85
|
}
|
90
86
|
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
|
91
|
-
const
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
define: {
|
96
|
-
timestamps: true
|
97
|
-
},
|
98
|
-
timezone: '-03:00',
|
99
|
-
logging(sql, timing) {
|
100
|
-
(0, _logger.default)().trace('DATABASE', sql, timing);
|
101
|
-
}
|
102
|
-
});
|
103
|
-
return new Promise((resolve, reject) => {
|
104
|
-
sequelize.authenticate().then(async () => {
|
105
|
-
const [results] = await sequelize.query(`SHOW DATABASES LIKE '${dbQueryName}';`);
|
87
|
+
const connection = await this.createConnection(hostUuid);
|
88
|
+
if (connection) {
|
89
|
+
try {
|
90
|
+
const [results] = await connection.query(`SHOW DATABASES LIKE '${dbQueryName}';`);
|
106
91
|
if (results.length === 0) {
|
107
92
|
// Se o banco de dados não existir, cria-o
|
108
|
-
await
|
93
|
+
await connection.query(`CREATE DATABASE ${dbQueryName};`);
|
109
94
|
this.log(`Database "${dbQueryName}" has been created.`);
|
110
|
-
|
95
|
+
return true;
|
111
96
|
} else {
|
112
97
|
this.log(`Database "${dbQueryName}" already exists.`);
|
113
|
-
|
98
|
+
return false;
|
114
99
|
}
|
115
|
-
}
|
116
|
-
new _error.ApplicationException('
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
100
|
+
} catch (error) {
|
101
|
+
new _error.ApplicationException('Erro on creating database', error);
|
102
|
+
} finally {
|
103
|
+
connection.close();
|
104
|
+
}
|
105
|
+
}
|
106
|
+
return false;
|
122
107
|
}
|
123
108
|
async deleteDB(hostUuid, name) {
|
124
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
|
142
|
+
reconnect(db, attempts = 2) {
|
143
|
+
db.sync().then(() => {
|
144
|
+
this.log('Database pronto', 'success');
|
145
|
+
}).catch(reason => {
|
146
|
+
this.log('Attemping to connect master database', 'warning');
|
147
|
+
(0, _logger.default)().warn('Erro ao inicializar o banco de dados, tentando novamente...', reason);
|
148
|
+
if (attempts > 0) {
|
149
|
+
setTimeout(() => this.reconnect(db, attempts - 1), 5000);
|
150
|
+
} else {
|
151
|
+
new _error.ApplicationException('Error on connecting master database', reason);
|
152
|
+
}
|
153
|
+
});
|
154
|
+
}
|
155
|
+
async createConnection(hostUuid, database) {
|
125
156
|
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
126
157
|
if (!host) {
|
127
|
-
this.log('Database host information not found: ' + hostUuid);
|
158
|
+
this.log('createConnection:: Database host information not found: ' + hostUuid);
|
128
159
|
return false;
|
129
160
|
}
|
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;
|
130
168
|
const queryHost = host.host.split(':');
|
131
169
|
const sequelize = new _sequelize.Sequelize({
|
132
170
|
host: queryHost[0],
|
133
171
|
port: Number(queryHost[1]),
|
134
172
|
dialect: host.dialect,
|
173
|
+
database,
|
135
174
|
username: host.username,
|
136
|
-
password:
|
175
|
+
password: password,
|
137
176
|
define: {
|
138
177
|
timestamps: true
|
139
178
|
},
|
@@ -144,47 +183,13 @@ class SequelizeService extends _service.default {
|
|
144
183
|
});
|
145
184
|
return new Promise((resolve, reject) => {
|
146
185
|
sequelize.authenticate().then(async () => {
|
147
|
-
|
148
|
-
await sequelize.query(`DROP DATABASE IF EXISTS ${dbQueryName};`);
|
149
|
-
this.log(`Database "${dbQueryName}" has been deleted.`);
|
150
|
-
resolve(true);
|
186
|
+
resolve(sequelize);
|
151
187
|
}).catch(reason => {
|
152
|
-
new _error.ApplicationException('Error while checking/deleting database:', reason);
|
153
188
|
reject(reason);
|
154
|
-
}).finally(() => {
|
155
|
-
sequelize.close();
|
156
189
|
});
|
157
190
|
});
|
158
191
|
}
|
159
|
-
async
|
160
|
-
let relation;
|
161
|
-
|
162
|
-
/* master or service */
|
163
|
-
if (tenantId == _app.Core.settings.getDatabase().service.database) {
|
164
|
-
return _database.serviceDB;
|
165
|
-
} else if (tenantId == _app.Core.settings.getDatabase().master.database) {
|
166
|
-
return _database.masterDB;
|
167
|
-
}
|
168
|
-
|
169
|
-
/* fetch relation */
|
170
|
-
if (this.relations.get(tenantId)) {
|
171
|
-
relation = this.relations.get(tenantId);
|
172
|
-
} else {
|
173
|
-
relation = await _models.EnvDBHost.findOne({
|
174
|
-
where: {
|
175
|
-
envUuid: tenantId
|
176
|
-
}
|
177
|
-
});
|
178
|
-
if (!relation) {
|
179
|
-
new _error.ApplicationException('Não foi possível encontrar a relação env-db: tenantId=' + tenantId);
|
180
|
-
return false;
|
181
|
-
}
|
182
|
-
this.relations.set(tenantId, relation);
|
183
|
-
}
|
184
|
-
const queryId = `${relation.hostUuid}@${this.getDatabaseNamePattern(relation.envToken)}`;
|
185
|
-
return this.connections.get(queryId) || (await this.initDatabase(tenantId));
|
186
|
-
}
|
187
|
-
async testHostConnections(host, port, username, password) {
|
192
|
+
async testConnection(host, port, username, password) {
|
188
193
|
const sequelize = new _sequelize.Sequelize({
|
189
194
|
host,
|
190
195
|
port,
|
@@ -199,95 +204,53 @@ class SequelizeService extends _service.default {
|
|
199
204
|
sequelize.authenticate().then(async () => {
|
200
205
|
resolve(true);
|
201
206
|
}).catch(reason => {
|
207
|
+
new _error.ApplicationException('teste de conexão falhou', reason);
|
202
208
|
resolve(false);
|
203
209
|
}).finally(() => {
|
204
210
|
sequelize.close();
|
205
211
|
});
|
206
212
|
});
|
207
213
|
}
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
this.log('Error on init connection, data not found! uuid: ' + hostUuid, 'danger');
|
221
|
-
return false;
|
222
|
-
}
|
223
|
-
const key = await _security.default.singleton().loadKeyByStr(host.credential);
|
224
|
-
const data = await _security.default.singleton().verifySignature(host.password, key);
|
225
|
-
if (!data) {
|
226
|
-
this.log('Error on decript password by credential: ' + hostUuid, 'danger');
|
227
|
-
return false;
|
228
|
-
}
|
229
|
-
const password = data.payload;
|
230
|
-
const conn = new _sequelize.Sequelize({
|
231
|
-
host: host.host,
|
232
|
-
database: dbName,
|
233
|
-
dialect: host.dialect,
|
234
|
-
username: host.username,
|
235
|
-
password: password,
|
236
|
-
define: {
|
237
|
-
timestamps: true
|
238
|
-
},
|
239
|
-
timezone: '-03:00',
|
240
|
-
logging(sql, timing) {
|
241
|
-
(0, _logger.default)().trace('DATABASE', sql, timing);
|
242
|
-
}
|
243
|
-
});
|
244
|
-
return new Promise((resolve, reject) => {
|
245
|
-
conn.sync({
|
246
|
-
force: false
|
247
|
-
}).then(() => {
|
248
|
-
this.log(`✅ Connected ${dbName} database sucessfully`, 'success');
|
249
|
-
this.connections.set(queryId, conn);
|
250
|
-
resolve(conn);
|
251
|
-
}).catch(reason => {
|
252
|
-
this.log(`error trying to connect: ${dbName}, UUID: ${hostUuid}`);
|
253
|
-
reject(reason);
|
254
|
-
new _error.ApplicationException(`error trying to connect: ${dbName}, UUID: ${hostUuid}`, reason);
|
214
|
+
|
215
|
+
//# Common
|
216
|
+
async getRelation(tenantId) {
|
217
|
+
let relation;
|
218
|
+
/* fetch relation */
|
219
|
+
if (this.relations.get(tenantId)) {
|
220
|
+
relation = this.relations.get(tenantId);
|
221
|
+
} else {
|
222
|
+
relation = await _models.EnvDBHost.findOne({
|
223
|
+
where: {
|
224
|
+
envUuid: tenantId
|
225
|
+
}
|
255
226
|
});
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
227
|
+
if (relation) {
|
228
|
+
this.relations.set(tenantId, relation);
|
229
|
+
}
|
230
|
+
}
|
231
|
+
return relation;
|
260
232
|
}
|
233
|
+
|
234
|
+
// #Entity
|
261
235
|
async getModel(model, tenantId) {
|
262
|
-
|
263
|
-
|
264
|
-
envToken
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
236
|
+
try {
|
237
|
+
const relation = await this.getRelation(tenantId);
|
238
|
+
const modelKey = `${relation.envToken}@${relation.hostUuid}#${model.name}`;
|
239
|
+
if (this.initializedModels.has(modelKey)) return this.initializedModels.get(modelKey);
|
240
|
+
const connection = await this.getDB(tenantId);
|
241
|
+
if (!connection) {
|
242
|
+
throw new _error.ApplicationException('Conexão não encontrada');
|
243
|
+
}
|
244
|
+
const m = model.initialize(connection);
|
245
|
+
await m.sync();
|
246
|
+
this.initializedModels.set(modelKey, m);
|
247
|
+
return m;
|
248
|
+
} catch (error) {
|
249
|
+
throw new _error.ApplicationException('Erro ao obter o model', error);
|
274
250
|
}
|
275
|
-
const m = model.initialize(connection);
|
276
|
-
await m.sync();
|
277
|
-
this.initializedModels.set(modelInitID, m);
|
278
|
-
return m;
|
279
251
|
}
|
280
|
-
|
281
|
-
|
282
|
-
const envToken = e[0];
|
283
|
-
const con_uuid = e[1];
|
284
|
-
return {
|
285
|
-
envToken,
|
286
|
-
con_uuid
|
287
|
-
};
|
288
|
-
}
|
289
|
-
getDatabaseNamePattern(envToken) {
|
290
|
-
return `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
|
252
|
+
registerModels(models) {
|
253
|
+
this.models = models;
|
291
254
|
}
|
292
255
|
}
|
293
256
|
var _default = exports.default = SequelizeService;
|