exodus-framework 2.0.950 → 2.0.951

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.
@@ -0,0 +1,19 @@
1
+ import { Dialect, Model, ModelStatic, Sequelize } from 'sequelize';
2
+ import { Service, ServiceModel } from '../app';
3
+ import { StaticServiceModel } from '../contracts';
4
+ import { EnvDBHost } from '../models';
5
+ declare class DatabaseService extends Service {
6
+ private entities;
7
+ private connections;
8
+ onServiceInit(): Promise<void>;
9
+ private initConnection;
10
+ createConnection(host: string, port: number, database: string, username: string, password: string, dialect: Dialect): Promise<Sequelize>;
11
+ getConnection(tenantId: string): Promise<Sequelize>;
12
+ getTenantIds(): Promise<string[]>;
13
+ getTenantInformation(tenantId: string): Promise<EnvDBHost>;
14
+ private loadEntity;
15
+ getEntity<M extends ServiceModel<Model>>(model: StaticServiceModel<M>, tenantId: string): Promise<ModelStatic<M>>;
16
+ private normalizeDatabaseName;
17
+ }
18
+ export default DatabaseService;
19
+ //# sourceMappingURL=database.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"database.d.ts","sourceRoot":"","sources":["../../src/services/database.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACnE,OAAO,EAAsB,OAAO,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAC;AACnE,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EAAgB,SAAS,EAAE,MAAM,WAAW,CAAC;AAIpD,cAAM,eAAgB,SAAQ,OAAO;IACnC,OAAO,CAAC,QAAQ,CAAgC;IAChD,OAAO,CAAC,WAAW,CAAyB;IAE/B,aAAa;YAUZ,cAAc;IA0Cf,gBAAgB,CAC3B,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE,OAAO;IAyBL,aAAa,CAAC,QAAQ,EAAE,MAAM;IAQ9B,YAAY;IAKZ,oBAAoB,CAAC,QAAQ,EAAE,MAAM;YAMpC,UAAU;IASX,SAAS,CAAC,CAAC,SAAS,YAAY,CAAC,KAAK,CAAC,EAClD,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,EAC5B,QAAQ,EAAE,MAAM,GACf,OAAO,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;IAU1B,OAAO,CAAC,qBAAqB;CAG9B;AAED,eAAe,eAAe,CAAC"}
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _sequelize = require("sequelize");
8
+ var _app = require("../app");
9
+ var _models = require("../models");
10
+ var _utils = require("../utils");
11
+ var _security = _interopRequireDefault(require("./security"));
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ class DatabaseService extends _app.Service {
14
+ entities;
15
+ connections;
16
+ async onServiceInit() {
17
+ this.entities = new Map();
18
+ this.connections = new Map();
19
+
20
+ // para busca rápidas
21
+ this.connections.set(_app.Core.settings.getDatabase().service.database, _utils.serviceDB);
22
+ this.connections.set(_app.Core.settings.getDatabase().master.database, _utils.masterDB);
23
+ }
24
+
25
+ //* Connection
26
+ async initConnection(tenantId) {
27
+ const tenantInfo = await this.getTenantInformation(tenantId);
28
+ if (!tenantInfo) {
29
+ throw new Error(`Não é possível inicia a conexão para o tenant ${tenantId}, a relação EnvDB não foi localizada`);
30
+ }
31
+ const dbHost = await _models.DatabaseHost.findByPk(tenantInfo.hostUuid);
32
+ if (!tenantInfo) {
33
+ throw new Error(`Não é possível inicia a conexão para o tenant ${tenantId}, não foi possível localizar a informação de host`);
34
+ }
35
+ const splitedInfo = dbHost.host.split(':');
36
+ const address = splitedInfo[0];
37
+ const port = Number(splitedInfo[1]);
38
+ const database = this.normalizeDatabaseName(tenantInfo.envToken);
39
+ const key = await _security.default.getService().loadKeyByStr(dbHost.credential);
40
+ const data = await _security.default.getService().verifySignature(dbHost.password, key);
41
+ if (!data) {
42
+ throw new Error(`Não foi possível descriptografar as credenciais de conexão para hostUuid: ${tenantInfo.hostUuid}`);
43
+ }
44
+ const password = data.payload;
45
+ const connection = await this.createConnection(address, port, database, dbHost.username, password, dbHost.dialect);
46
+ this.connections.set(tenantId, connection);
47
+ }
48
+ async createConnection(host, port, database, username, password, dialect) {
49
+ const sequelize = new _sequelize.Sequelize({
50
+ host,
51
+ port,
52
+ dialect,
53
+ database,
54
+ username,
55
+ password,
56
+ define: {
57
+ timestamps: true
58
+ },
59
+ timezone: '-03:00',
60
+ logging: _app.Core.settings.getDatabase().service.log
61
+ });
62
+ try {
63
+ await sequelize.authenticate();
64
+ return sequelize;
65
+ } catch (error) {
66
+ new _app.ErrorHandler('Não foi possível realizar uma conexão', error);
67
+ return null;
68
+ }
69
+ }
70
+ async getConnection(tenantId) {
71
+ if (!this.connections.has(tenantId)) {
72
+ await this.initConnection(tenantId);
73
+ }
74
+ return this.connections.get(tenantId);
75
+ }
76
+
77
+ //* Environment
78
+ async getTenantIds() {
79
+ const tenants = await _models.EnvDBHost.findAll({
80
+ attributes: ['envUuid']
81
+ });
82
+ return tenants.map(t => t.envUuid);
83
+ }
84
+ async getTenantInformation(tenantId) {
85
+ const relation = await _models.EnvDBHost.findOne({
86
+ where: {
87
+ envUuid: tenantId
88
+ }
89
+ });
90
+ return relation;
91
+ }
92
+
93
+ //* Entity
94
+ async loadEntity(entity, tenantId) {
95
+ const k = `${tenantId}#${entity.name}`;
96
+ const connection = await this.getConnection(tenantId);
97
+ const loadedEntity = entity.initialize(connection);
98
+ await loadedEntity.sync();
99
+ this.entities.set(k, loadedEntity);
100
+ }
101
+ async getEntity(model, tenantId) {
102
+ const k = `${tenantId}#${model.name}`;
103
+ if (!this.entities.has(k)) {
104
+ await this.loadEntity(model, tenantId);
105
+ }
106
+ return this.entities.get(k);
107
+ }
108
+
109
+ //* Utils
110
+ normalizeDatabaseName(envToken) {
111
+ return `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
112
+ }
113
+ }
114
+ var _default = exports.default = DatabaseService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exodus-framework",
3
- "version": "2.0.950",
3
+ "version": "2.0.951",
4
4
  "description": "Exodus Framework",
5
5
  "author": "jhownpaixao",
6
6
  "license": "ISC",