exodus-framework 2.0.65 → 2.0.67

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 @@
1
+ //# sourceMappingURL=tenant.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tenant.d.ts","sourceRoot":"","sources":["../../src/contracts/tenant.ts"],"names":[],"mappings":""}
@@ -0,0 +1 @@
1
+ "use strict";
@@ -3,7 +3,7 @@ import HttpController from '../app/controller';
3
3
  import { TAccountType } from '../contracts';
4
4
  declare class AccessMiddleware extends HttpController {
5
5
  accessLevel: (level: TAccountType[]) => RequestHandler;
6
- getConnection: RequestHandler;
6
+ getTenant: RequestHandler;
7
7
  }
8
8
  export default AccessMiddleware;
9
9
  //# sourceMappingURL=access.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../../src/middlewares/access.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAK5C,cAAM,gBAAiB,SAAQ,cAAc;IAC3C,WAAW,UAAW,YAAY,EAAE,KAAG,cAAc,CAWnD;IAEF,aAAa,EAAE,cAAc,CAqB3B;CACH;AAED,eAAe,gBAAgB,CAAC"}
1
+ {"version":3,"file":"access.d.ts","sourceRoot":"","sources":["../../src/middlewares/access.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AACzC,OAAO,cAAc,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAI5C,cAAM,gBAAiB,SAAQ,cAAc;IAC3C,WAAW,UAAW,YAAY,EAAE,KAAG,cAAc,CAWnD;IAEF,SAAS,EAAE,cAAc,CAevB;CACH;AAED,eAAe,gBAAgB,CAAC"}
@@ -6,7 +6,6 @@ Object.defineProperty(exports, "__esModule", {
6
6
  exports.default = void 0;
7
7
  var _controller = _interopRequireDefault(require("../app/controller"));
8
8
  var _http = require("../contracts/http");
9
- var _services = require("../services");
10
9
  var _error = require("../services/error");
11
10
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
12
11
  class AccessMiddleware extends _controller.default {
@@ -18,19 +17,14 @@ class AccessMiddleware extends _controller.default {
18
17
  next();
19
18
  };
20
19
  };
21
- getConnection = async (req, res, next) => {
20
+ getTenant = async (req, res, next) => {
22
21
  const account = req.auth?.account;
23
- const envToken = req.header('X-Exodus-Environment-Token');
24
- const connUuid = req.header('X-Exodus-Connection');
22
+ const tenantId = req.header('X-Exodus-Tenant-ID');
25
23
  if (account && account.type != 'ADMINISTRATOR') {
26
- if (!envToken || !connUuid) {
24
+ if (!tenantId) {
27
25
  throw new _error.ApplicationHttpException('Informações do ambiente incompletas', _http.EHttpResponseCode.incompleteRequest);
28
26
  }
29
- const connection = await _services.SequelizeService.singleton().getDB(connUuid, envToken);
30
- if (!connection) {
31
- throw new _error.ApplicationHttpException('Não foi possível obter a conexão deste ambiente');
32
- }
33
- req.auth.connection = connection;
27
+ req.tenant.id = tenantId;
34
28
  }
35
29
  next();
36
30
  };
@@ -1 +1 @@
1
- {"version":3,"file":"Connection.d.ts","sourceRoot":"","sources":["../../src/models/Connection.ts"],"names":[],"mappings":"AACA,OAAO,EACL,gBAAgB,EAEhB,OAAO,EACP,eAAe,EACf,uBAAuB,EACvB,KAAK,EACN,MAAM,WAAW,CAAC;AAEnB,qBAAa,UAAW,SAAQ,KAAK,CACnC,eAAe,CAAC,UAAU,CAAC,EAC3B,uBAAuB,CAAC,UAAU,CAAC,CACpC;IACS,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;CACvD"}
1
+ {"version":3,"file":"Connection.d.ts","sourceRoot":"","sources":["../../src/models/Connection.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAEhB,OAAO,EACP,eAAe,EACf,uBAAuB,EACvB,KAAK,EACN,MAAM,WAAW,CAAC;AAGnB,qBAAa,UAAW,SAAQ,KAAK,CACnC,eAAe,CAAC,UAAU,CAAC,EAC3B,uBAAuB,CAAC,UAAU,CAAC,CACpC;IACS,IAAI,EAAE,gBAAgB,CAAC,MAAM,CAAC,CAAC;IAC/B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,gBAAgB,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;CACvD"}
@@ -4,8 +4,8 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Connection = void 0;
7
- var _database = require("../utils/database");
8
7
  var _sequelize = require("sequelize");
8
+ var _database = require("../utils/database");
9
9
  class Connection extends _sequelize.Model {}
10
10
  exports.Connection = Connection;
11
11
  Connection.init({
@@ -1,11 +1,12 @@
1
+ import { Model, ModelStatic, Sequelize } from 'sequelize';
1
2
  import Service from '../app/service';
2
- import { ModelStatic, Sequelize } from 'sequelize';
3
3
  import { IService } from '../contracts/service';
4
4
  declare class SequelizeService extends Service implements IService {
5
5
  serviceDB: Sequelize;
6
6
  masterDB: Sequelize;
7
7
  models: ModelStatic<any>[];
8
8
  connections: Map<string, Sequelize>;
9
+ initializedModels: Map<string, typeof Model<any, any>>;
9
10
  constructor();
10
11
  init(): Promise<void>;
11
12
  connectMainDatabase(): Promise<unknown>;
@@ -13,9 +14,16 @@ declare class SequelizeService extends Service implements IService {
13
14
  getMasterConnection(): Sequelize;
14
15
  createDB(connection_uuid: string, name: string): Promise<boolean>;
15
16
  deleteDB(connection_uuid: string, name: string): Promise<boolean>;
16
- getDB(uuid: string, name: string): Promise<Sequelize>;
17
- initDB(uuid: string, name: string): Promise<unknown>;
17
+ getDB(tenantId: string): Promise<Sequelize>;
18
+ initDB(tenantId: string): Promise<unknown>;
18
19
  registerModels(models: ModelStatic<any>[]): void;
20
+ getModel<M extends typeof Model<any, any>>(model: typeof Model<any, any> & {
21
+ initialize: (connection: Sequelize) => M;
22
+ }, tenantId: string): Promise<M>;
23
+ parseDataInfoByTenantId(tenant: string): {
24
+ envToken: string;
25
+ con_uuid: string;
26
+ };
19
27
  }
20
28
  export default SequelizeService;
21
29
  //# sourceMappingURL=sequelize.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"sequelize.d.ts","sourceRoot":"","sources":["../../src/services/sequelize.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,gBAAgB,CAAC;AAIrC,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAWhD,cAAM,gBAAiB,SAAQ,OAAQ,YAAW,QAAQ;IACxD,SAAS,EAAE,SAAS,CAAC;IACrB,QAAQ,EAAE,SAAS,CAAC;IACpB,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;IAC3B,WAAW,EAAE,GAAG,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;;IAM9B,IAAI;IAMJ,mBAAmB;IA4BzB,SAAS,CAAC,EAAE,EAAE,SAAS,EAAE,QAAQ,SAAI;IAerC,mBAAmB;IAGb,QAAQ,CAAC,eAAe,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM;IA6C9C,QAAQ,CAAC,eAAe,EAAE,MAAM,EAA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E,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;IAmBlB,uBAAuB,CAAC,MAAM,EAAE,MAAM;;;;CAMvC;AAED,eAAe,gBAAgB,CAAC"}
@@ -4,13 +4,13 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _sequelize = require("sequelize");
8
+ var _app = require("../app");
7
9
  var _service = _interopRequireDefault(require("../app/service"));
8
10
  var _Connection = require("../models/Connection");
9
11
  var _database = require("../utils/database");
10
12
  var _logger = _interopRequireDefault(require("../utils/logger"));
11
- var _sequelize = require("sequelize");
12
13
  var _error = require("./error");
13
- var _app = require("../app");
14
14
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
15
  /**
16
16
  * Serviço de gerênciamento do banco de dados
@@ -24,6 +24,7 @@ class SequelizeService extends _service.default {
24
24
  masterDB;
25
25
  models;
26
26
  connections;
27
+ initializedModels;
27
28
 
28
29
  //# Initialization
29
30
  constructor() {
@@ -145,20 +146,28 @@ class SequelizeService extends _service.default {
145
146
  });
146
147
  });
147
148
  }
148
- async getDB(uuid, name) {
149
- const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
150
- const queryId = `${uuid}@${dbQueryName}`;
149
+ async getDB(tenantId) {
150
+ const {
151
+ con_uuid,
152
+ envToken
153
+ } = this.parseDataInfoByTenantId(tenantId);
154
+ const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
155
+ const queryId = `${con_uuid}@${dbQueryName}`;
151
156
  return this.connections.get(queryId);
152
157
  }
153
- async initDB(uuid, name) {
154
- const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${name}`;
155
- const queryId = `${uuid}@${dbQueryName}`;
158
+ async initDB(tenantId) {
159
+ const {
160
+ con_uuid,
161
+ envToken
162
+ } = this.parseDataInfoByTenantId(tenantId);
163
+ const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
164
+ const queryId = `${con_uuid}@${dbQueryName}`;
156
165
  if (this.connections.get(queryId)) {
157
166
  return false;
158
167
  }
159
- const connData = await _Connection.Connection.findByPk(uuid);
168
+ const connData = await _Connection.Connection.findByPk(con_uuid);
160
169
  if (!connData) {
161
- this.log('Connection data not found: ' + uuid);
170
+ this.log('Connection data not found: ' + con_uuid);
162
171
  return false;
163
172
  }
164
173
  const conn = new _sequelize.Sequelize({
@@ -182,14 +191,41 @@ class SequelizeService extends _service.default {
182
191
  this.connections.set(queryId, conn);
183
192
  resolve(true);
184
193
  }).catch(reason => {
185
- this.log(`error trying to connect: ${dbQueryName}, UUID: ${uuid}`);
194
+ this.log(`error trying to connect: ${dbQueryName}, UUID: ${con_uuid}`);
186
195
  reject(reason);
187
- new _error.ApplicationException(`error trying to connect: ${dbQueryName}, UUID: ${uuid}`, reason);
196
+ new _error.ApplicationException(`error trying to connect: ${dbQueryName}, UUID: ${con_uuid}`, reason);
188
197
  });
189
198
  });
190
199
  }
191
200
  registerModels(models) {
192
201
  this.models = models;
193
202
  }
203
+ async getModel(model, tenantId) {
204
+ const {
205
+ con_uuid,
206
+ envToken
207
+ } = this.parseDataInfoByTenantId(tenantId);
208
+ const connection = await this.getDB(tenantId);
209
+ const query = `${envToken}@${con_uuid}`;
210
+ const cache = this.initializedModels.get(query);
211
+ if (cache) {
212
+ return cache;
213
+ }
214
+ if (connection) {
215
+ const m = model.initialize(connection);
216
+ this.initializedModels.set(query, m);
217
+ return m;
218
+ }
219
+ return null;
220
+ }
221
+ parseDataInfoByTenantId(tenant) {
222
+ const e = tenant.split('@');
223
+ const envToken = e[0];
224
+ const con_uuid = e[1];
225
+ return {
226
+ envToken,
227
+ con_uuid
228
+ };
229
+ }
194
230
  }
195
231
  var _default = exports.default = SequelizeService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "exodus-framework",
3
- "version": "2.0.65",
3
+ "version": "2.0.67",
4
4
  "description": "Exodus Framework",
5
5
  "author": "jhownpaixao",
6
6
  "license": "ISC",