exodus-framework 2.0.723 → 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 -13
- package/lib/services/sequelize.d.ts.map +1 -1
- package/lib/services/sequelize.js +114 -152
- 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<false | EnvDBHost>;
|
23
24
|
getModel<M extends typeof Model<any, any>>(model: typeof Model<any, any> & {
|
24
25
|
initialize: (connection: Sequelize) => M;
|
25
|
-
}, tenantId: string): Promise<M>;
|
26
|
-
|
27
|
-
envToken: string;
|
28
|
-
con_uuid: string;
|
29
|
-
};
|
30
|
-
getDatabaseNamePattern(envToken: string): string;
|
26
|
+
}, tenantId: string): Promise<false | M>;
|
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.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GAAG,CAAC,CAAC,CAAC;IACvD,SAAS,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;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"}
|
@@ -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,70 +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
|
-
}
|
130
|
-
const queryHost = host.host.split(':');
|
192
|
+
async testConnection(host, port, username, password) {
|
131
193
|
const sequelize = new _sequelize.Sequelize({
|
132
|
-
host
|
133
|
-
port
|
134
|
-
|
135
|
-
|
136
|
-
password: host.password,
|
137
|
-
define: {
|
138
|
-
timestamps: true
|
139
|
-
},
|
194
|
+
host,
|
195
|
+
port,
|
196
|
+
username,
|
197
|
+
password,
|
140
198
|
timezone: '-03:00',
|
141
199
|
logging(sql, timing) {
|
142
200
|
(0, _logger.default)().trace('DATABASE', sql, timing);
|
143
201
|
}
|
144
202
|
});
|
145
|
-
return new Promise(
|
203
|
+
return new Promise(resolve => {
|
146
204
|
sequelize.authenticate().then(async () => {
|
147
|
-
// Se o banco de dados não existir, cria-o
|
148
|
-
await sequelize.query(`DROP DATABASE IF EXISTS ${dbQueryName};`);
|
149
|
-
this.log(`Database "${dbQueryName}" has been deleted.`);
|
150
205
|
resolve(true);
|
151
206
|
}).catch(reason => {
|
152
|
-
new _error.ApplicationException('
|
153
|
-
|
207
|
+
new _error.ApplicationException('teste de conexão falhou', reason);
|
208
|
+
resolve(false);
|
154
209
|
}).finally(() => {
|
155
210
|
sequelize.close();
|
156
211
|
});
|
157
212
|
});
|
158
213
|
}
|
159
|
-
async getDatabaseConnection(tenantId) {
|
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
214
|
|
215
|
+
//# Common
|
216
|
+
async getRelation(tenantId) {
|
217
|
+
let relation;
|
169
218
|
/* fetch relation */
|
170
219
|
if (this.relations.get(tenantId)) {
|
171
220
|
relation = this.relations.get(tenantId);
|
@@ -181,113 +230,26 @@ class SequelizeService extends _service.default {
|
|
181
230
|
}
|
182
231
|
this.relations.set(tenantId, relation);
|
183
232
|
}
|
184
|
-
|
185
|
-
return this.connections.get(queryId) || (await this.initDatabase(tenantId));
|
186
|
-
}
|
187
|
-
async testHostConnections(host, port, username, password) {
|
188
|
-
const sequelize = new _sequelize.Sequelize({
|
189
|
-
host,
|
190
|
-
port,
|
191
|
-
username,
|
192
|
-
password,
|
193
|
-
timezone: '-03:00',
|
194
|
-
logging(sql, timing) {
|
195
|
-
(0, _logger.default)().trace('DATABASE', sql, timing);
|
196
|
-
}
|
197
|
-
});
|
198
|
-
return new Promise(resolve => {
|
199
|
-
sequelize.authenticate().then(async () => {
|
200
|
-
resolve(true);
|
201
|
-
}).catch(reason => {
|
202
|
-
resolve(false);
|
203
|
-
}).finally(() => {
|
204
|
-
sequelize.close();
|
205
|
-
});
|
206
|
-
});
|
207
|
-
}
|
208
|
-
async initDatabase(tenantId) {
|
209
|
-
const {
|
210
|
-
con_uuid: hostUuid,
|
211
|
-
envToken
|
212
|
-
} = this.parseDataInfoByTenantId(tenantId);
|
213
|
-
const dbName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`; //nomeclatura padrão do banco de dados do serviço
|
214
|
-
const queryId = `${dbName}@${hostUuid}`;
|
215
|
-
if (this.connections.get(queryId)) {
|
216
|
-
return this.connections.get(queryId);
|
217
|
-
}
|
218
|
-
const host = await _DatabaseHost.DatabaseHost.findByPk(hostUuid);
|
219
|
-
if (!host) {
|
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);
|
255
|
-
});
|
256
|
-
});
|
257
|
-
}
|
258
|
-
registerModels(models) {
|
259
|
-
this.models = models;
|
233
|
+
return relation;
|
260
234
|
}
|
235
|
+
|
236
|
+
// #Entity
|
261
237
|
async getModel(model, tenantId) {
|
262
|
-
const
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
const
|
267
|
-
const cache = this.initializedModels.get(modelInitID);
|
268
|
-
if (cache) {
|
269
|
-
return cache;
|
270
|
-
}
|
271
|
-
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);
|
272
243
|
if (!connection) {
|
273
244
|
throw new _error.ApplicationException('Não foi possível obter o model espeficificado: Conexão não encontrada');
|
274
245
|
}
|
275
246
|
const m = model.initialize(connection);
|
276
247
|
await m.sync();
|
277
|
-
this.initializedModels.set(
|
248
|
+
this.initializedModels.set(modelKey, m);
|
278
249
|
return m;
|
279
250
|
}
|
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}`;
|
251
|
+
registerModels(models) {
|
252
|
+
this.models = models;
|
291
253
|
}
|
292
254
|
}
|
293
255
|
var _default = exports.default = SequelizeService;
|