exodus-framework 2.0.66 → 2.0.68
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/contracts/tenant.d.ts +1 -0
- package/lib/contracts/tenant.d.ts.map +1 -0
- package/lib/contracts/tenant.js +1 -0
- package/lib/middlewares/access.d.ts +1 -1
- package/lib/middlewares/access.d.ts.map +1 -1
- package/lib/middlewares/access.js +7 -10
- package/lib/services/sequelize.d.ts +7 -3
- package/lib/services/sequelize.d.ts.map +1 -1
- package/lib/services/sequelize.js +39 -13
- package/package.json +1 -1
@@ -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
|
-
|
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,
|
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,SAAS,EAAE,cAAc,CAiBvB;CACH;AAED,eAAe,gBAAgB,CAAC"}
|
@@ -6,8 +6,8 @@ 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");
|
10
|
+
var _app = require("../app");
|
11
11
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
12
12
|
class AccessMiddleware extends _controller.default {
|
13
13
|
accessLevel = level => {
|
@@ -18,20 +18,17 @@ class AccessMiddleware extends _controller.default {
|
|
18
18
|
next();
|
19
19
|
};
|
20
20
|
};
|
21
|
-
|
21
|
+
getTenant = async (req, res, next) => {
|
22
22
|
const account = req.auth?.account;
|
23
|
-
const
|
24
|
-
const connUuid = req.header('X-Exodus-Connection');
|
23
|
+
const tenantId = req.header('X-Exodus-Tenant-ID');
|
25
24
|
if (account && account.type != 'ADMINISTRATOR') {
|
26
|
-
if (!
|
25
|
+
if (!tenantId) {
|
27
26
|
throw new _error.ApplicationHttpException('Informações do ambiente incompletas', _http.EHttpResponseCode.incompleteRequest);
|
28
27
|
}
|
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;
|
34
28
|
}
|
29
|
+
req.tenant = {
|
30
|
+
id: tenantId || `root@${_app.Core.settings.getDatabase().service}`
|
31
|
+
};
|
35
32
|
next();
|
36
33
|
};
|
37
34
|
}
|
@@ -14,12 +14,16 @@ declare class SequelizeService extends Service implements IService {
|
|
14
14
|
getMasterConnection(): Sequelize;
|
15
15
|
createDB(connection_uuid: string, name: string): Promise<boolean>;
|
16
16
|
deleteDB(connection_uuid: string, name: string): Promise<boolean>;
|
17
|
-
getDB(
|
18
|
-
initDB(
|
17
|
+
getDB(tenantId: string): Promise<Sequelize>;
|
18
|
+
initDB(tenantId: string): Promise<unknown>;
|
19
19
|
registerModels(models: ModelStatic<any>[]): void;
|
20
20
|
getModel<M extends typeof Model<any, any>>(model: typeof Model<any, any> & {
|
21
21
|
initialize: (connection: Sequelize) => M;
|
22
|
-
},
|
22
|
+
}, tenantId: string): Promise<M>;
|
23
|
+
parseDataInfoByTenantId(tenant: string): {
|
24
|
+
envToken: string;
|
25
|
+
con_uuid: string;
|
26
|
+
};
|
23
27
|
}
|
24
28
|
export default SequelizeService;
|
25
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,EAAE,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAE1D,OAAO,OAAO,MAAM,gBAAgB,CAAC;AACrC,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAahD,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;IACpC,iBAAiB,EAAE,GAAG,CAAC,MAAM,EAAE,OAAO,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;;IAMjD,IAAI;IAMJ,mBAAmB;IA4BzB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K,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"}
|
@@ -146,20 +146,33 @@ class SequelizeService extends _service.default {
|
|
146
146
|
});
|
147
147
|
});
|
148
148
|
}
|
149
|
-
async getDB(
|
150
|
-
const
|
151
|
-
|
149
|
+
async getDB(tenantId) {
|
150
|
+
const {
|
151
|
+
con_uuid,
|
152
|
+
envToken
|
153
|
+
} = this.parseDataInfoByTenantId(tenantId);
|
154
|
+
if (con_uuid == _app.Core.settings.getDatabase().service.database) {
|
155
|
+
return _database.serviceDB;
|
156
|
+
} else if (con_uuid == _app.Core.settings.getDatabase().master.database) {
|
157
|
+
return _database.masterDB;
|
158
|
+
}
|
159
|
+
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
|
160
|
+
const queryId = `${con_uuid}@${dbQueryName}`;
|
152
161
|
return this.connections.get(queryId);
|
153
162
|
}
|
154
|
-
async initDB(
|
155
|
-
const
|
156
|
-
|
163
|
+
async initDB(tenantId) {
|
164
|
+
const {
|
165
|
+
con_uuid,
|
166
|
+
envToken
|
167
|
+
} = this.parseDataInfoByTenantId(tenantId);
|
168
|
+
const dbQueryName = `${_app.Core.settings.getDatabase().service.database}_${envToken}`;
|
169
|
+
const queryId = `${con_uuid}@${dbQueryName}`;
|
157
170
|
if (this.connections.get(queryId)) {
|
158
171
|
return false;
|
159
172
|
}
|
160
|
-
const connData = await _Connection.Connection.findByPk(
|
173
|
+
const connData = await _Connection.Connection.findByPk(con_uuid);
|
161
174
|
if (!connData) {
|
162
|
-
this.log('Connection data not found: ' +
|
175
|
+
this.log('Connection data not found: ' + con_uuid);
|
163
176
|
return false;
|
164
177
|
}
|
165
178
|
const conn = new _sequelize.Sequelize({
|
@@ -183,22 +196,26 @@ class SequelizeService extends _service.default {
|
|
183
196
|
this.connections.set(queryId, conn);
|
184
197
|
resolve(true);
|
185
198
|
}).catch(reason => {
|
186
|
-
this.log(`error trying to connect: ${dbQueryName}, UUID: ${
|
199
|
+
this.log(`error trying to connect: ${dbQueryName}, UUID: ${con_uuid}`);
|
187
200
|
reject(reason);
|
188
|
-
new _error.ApplicationException(`error trying to connect: ${dbQueryName}, UUID: ${
|
201
|
+
new _error.ApplicationException(`error trying to connect: ${dbQueryName}, UUID: ${con_uuid}`, reason);
|
189
202
|
});
|
190
203
|
});
|
191
204
|
}
|
192
205
|
registerModels(models) {
|
193
206
|
this.models = models;
|
194
207
|
}
|
195
|
-
async getModel(model,
|
196
|
-
const
|
197
|
-
|
208
|
+
async getModel(model, tenantId) {
|
209
|
+
const {
|
210
|
+
con_uuid,
|
211
|
+
envToken
|
212
|
+
} = this.parseDataInfoByTenantId(tenantId);
|
213
|
+
const query = `${envToken}@${con_uuid}`;
|
198
214
|
const cache = this.initializedModels.get(query);
|
199
215
|
if (cache) {
|
200
216
|
return cache;
|
201
217
|
}
|
218
|
+
const connection = await this.getDB(tenantId);
|
202
219
|
if (connection) {
|
203
220
|
const m = model.initialize(connection);
|
204
221
|
this.initializedModels.set(query, m);
|
@@ -206,5 +223,14 @@ class SequelizeService extends _service.default {
|
|
206
223
|
}
|
207
224
|
return null;
|
208
225
|
}
|
226
|
+
parseDataInfoByTenantId(tenant) {
|
227
|
+
const e = tenant.split('@');
|
228
|
+
const envToken = e[0];
|
229
|
+
const con_uuid = e[1];
|
230
|
+
return {
|
231
|
+
envToken,
|
232
|
+
con_uuid
|
233
|
+
};
|
234
|
+
}
|
209
235
|
}
|
210
236
|
var _default = exports.default = SequelizeService;
|