@flusys/nestjs-shared 7.0.2 → 8.0.0-beta.1
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/classes/api-controller.class.d.ts +4 -4
- package/classes/api-service.class.d.ts +7 -7
- package/classes/disabled-event-bus.class.d.ts +1 -1
- package/classes/domain-event.publisher.d.ts +1 -1
- package/classes/event-bus.registry.d.ts +1 -1
- package/classes/event-transports/event-transport.registry.d.ts +1 -1
- package/classes/event-transports/index.d.ts +2 -2
- package/classes/event-transports/kafka-transport.optional.d.ts +1 -1
- package/classes/event-transports/memory-transport.d.ts +1 -1
- package/classes/event-transports/rabbitmq-transport.optional.d.ts +1 -1
- package/classes/hybrid-event-bus.class.d.ts +1 -1
- package/classes/index.d.ts +11 -11
- package/constants/index.d.ts +2 -2
- package/constants/permissions.d.ts +12 -0
- package/decorators/api-response.decorator.d.ts +1 -1
- package/decorators/current-user.decorator.d.ts +1 -1
- package/decorators/index.d.ts +6 -6
- package/decorators/require-permission.decorator.d.ts +1 -1
- package/dtos/filter-and-pagination.dto.d.ts +1 -1
- package/dtos/index.d.ts +6 -6
- package/entities/index.d.ts +3 -3
- package/enums/index.d.ts +3 -3
- package/exceptions/index.d.ts +2 -2
- package/fesm/470.js +2713 -0
- package/fesm/509.js +793 -0
- package/fesm/667.js +606 -0
- package/fesm/67.js +808 -0
- package/fesm/720.js +914 -0
- package/{cjs/classes/event-transports/rabbitmq-transport.optional.js → fesm/chunks/123.js} +78 -55
- package/fesm/{classes/event-transports/kafka-transport.optional.js → chunks/37.js} +74 -41
- package/fesm/classes/index.js +62 -12
- package/fesm/constants/index.js +445 -17
- package/fesm/decorators/index.js +341 -6
- package/fesm/dtos/index.js +23 -6
- package/fesm/entities/index.js +336 -3
- package/fesm/enums/index.js +57 -3
- package/fesm/exceptions/index.js +280 -2
- package/fesm/guards/index.js +1003 -3
- package/fesm/index.js +1524 -14
- package/fesm/interceptors/index.js +924 -5
- package/fesm/interfaces/index.js +57 -11
- package/fesm/middlewares/index.js +33 -1
- package/fesm/modules/index.js +432 -9
- package/fesm/runtime.js +134 -0
- package/fesm/utils/index.js +1045 -8
- package/filters/global-exception.filter.d.ts +1 -1
- package/filters/index.d.ts +1 -1
- package/guards/index.d.ts +3 -3
- package/guards/jwt-auth.guard.d.ts +1 -1
- package/guards/optional-jwt-auth.guard.d.ts +1 -1
- package/guards/permission.guard.d.ts +2 -2
- package/index.d.ts +14 -14
- package/interceptors/delete-empty-id-from-body.interceptor.d.ts +1 -1
- package/interceptors/idempotency.interceptor.d.ts +2 -2
- package/interceptors/index.d.ts +5 -5
- package/interceptors/response-meta.interceptor.d.ts +1 -1
- package/interceptors/set-user-field-on-body.interceptor.d.ts +1 -1
- package/interceptors/slug.interceptor.d.ts +2 -2
- package/interfaces/ai-assistant-adapter.interface.d.ts +1 -1
- package/interfaces/api.interface.d.ts +2 -2
- package/interfaces/datasource.interface.d.ts +1 -1
- package/interfaces/event-manager-adapter.interface.d.ts +2 -2
- package/interfaces/index.d.ts +11 -11
- package/interfaces/module-config.interface.d.ts +1 -1
- package/interfaces/notification-adapter.interface.d.ts +1 -1
- package/middlewares/index.d.ts +1 -1
- package/middlewares/logger.middleware.d.ts +2 -2
- package/modules/cache/cache.module.d.ts +1 -1
- package/modules/datasource/index.d.ts +1 -1
- package/modules/datasource/multi-tenant-datasource.service.d.ts +4 -4
- package/modules/event-bus/event-bus.explorer.d.ts +2 -2
- package/modules/event-bus/event-bus.module.d.ts +2 -2
- package/modules/event-bus/module-events.provider.d.ts +1 -1
- package/modules/index.d.ts +9 -9
- package/modules/shared-permission-cache/shared-permission-cache.service.d.ts +3 -3
- package/modules/utils/utils.service.d.ts +1 -1
- package/package.json +30 -44
- package/utils/index.d.ts +8 -8
- package/utils/permission-cache-key.util.d.ts +1 -1
- package/utils/query-helpers.util.d.ts +2 -2
- package/utils/request.util.d.ts +1 -1
- package/cjs/classes/api-controller.class.js +0 -728
- package/cjs/classes/api-controller.class.spec.js +0 -684
- package/cjs/classes/api-service.class.js +0 -631
- package/cjs/classes/api-service.class.spec.js +0 -1137
- package/cjs/classes/disabled-event-bus.class.js +0 -15
- package/cjs/classes/disabled-event-bus.class.spec.js +0 -40
- package/cjs/classes/domain-event.publisher.js +0 -76
- package/cjs/classes/domain-event.publisher.spec.js +0 -234
- package/cjs/classes/event-bus.registry.js +0 -68
- package/cjs/classes/event-bus.registry.spec.js +0 -141
- package/cjs/classes/event-transports/event-transport.registry.js +0 -41
- package/cjs/classes/event-transports/event-transport.registry.spec.js +0 -31
- package/cjs/classes/event-transports/index.js +0 -23
- package/cjs/classes/event-transports/kafka-transport.optional.js +0 -151
- package/cjs/classes/event-transports/kafka-transport.optional.spec.js +0 -198
- package/cjs/classes/event-transports/memory-transport.js +0 -53
- package/cjs/classes/event-transports/memory-transport.spec.js +0 -57
- package/cjs/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -255
- package/cjs/classes/handle-custome-db.js +0 -37
- package/cjs/classes/handle-custome-db.spec.js +0 -82
- package/cjs/classes/hybrid-cache.class.js +0 -96
- package/cjs/classes/hybrid-cache.class.spec.js +0 -168
- package/cjs/classes/hybrid-event-bus.class.js +0 -281
- package/cjs/classes/hybrid-event-bus.class.spec.js +0 -218
- package/cjs/classes/index.js +0 -28
- package/cjs/classes/winston.logger.class.js +0 -274
- package/cjs/classes/winston.logger.class.spec.js +0 -330
- package/cjs/constants/index.js +0 -88
- package/cjs/constants/message-keys.js +0 -68
- package/cjs/constants/permissions.js +0 -297
- package/cjs/decorators/api-response.decorator.js +0 -52
- package/cjs/decorators/api-response.decorator.spec.js +0 -126
- package/cjs/decorators/current-user.decorator.js +0 -16
- package/cjs/decorators/current-user.decorator.spec.js +0 -74
- package/cjs/decorators/index.js +0 -23
- package/cjs/decorators/log-action.decorator.js +0 -149
- package/cjs/decorators/log-action.decorator.spec.js +0 -353
- package/cjs/decorators/on-domain-event.decorator.js +0 -13
- package/cjs/decorators/on-domain-event.decorator.spec.js +0 -50
- package/cjs/decorators/public.decorator.js +0 -13
- package/cjs/decorators/public.decorator.spec.js +0 -50
- package/cjs/decorators/require-permission.decorator.js +0 -32
- package/cjs/decorators/require-permission.decorator.spec.js +0 -147
- package/cjs/dtos/delete.dto.js +0 -92
- package/cjs/dtos/filter-and-pagination.dto.js +0 -164
- package/cjs/dtos/get-by-ids.dto.js +0 -65
- package/cjs/dtos/identity-response.dto.js +0 -108
- package/cjs/dtos/index.js +0 -23
- package/cjs/dtos/pagination.dto.js +0 -67
- package/cjs/dtos/response-payload.dto.js +0 -369
- package/cjs/entities/identity.js +0 -100
- package/cjs/entities/index.js +0 -20
- package/cjs/entities/raw-type.js +0 -4
- package/cjs/entities/user-root.js +0 -189
- package/cjs/enums/index.js +0 -20
- package/cjs/enums/notification-type.enum.js +0 -17
- package/cjs/enums/participant-status.enum.js +0 -17
- package/cjs/enums/recurrence-type.enum.js +0 -18
- package/cjs/exceptions/base-app.exception.js +0 -151
- package/cjs/exceptions/base-app.exception.spec.js +0 -157
- package/cjs/exceptions/index.js +0 -19
- package/cjs/exceptions/permission.exception.js +0 -56
- package/cjs/exceptions/permission.exception.spec.js +0 -82
- package/cjs/filters/global-exception.filter.js +0 -279
- package/cjs/filters/global-exception.filter.spec.js +0 -440
- package/cjs/filters/index.js +0 -18
- package/cjs/guards/index.js +0 -20
- package/cjs/guards/jwt-auth.guard.js +0 -75
- package/cjs/guards/jwt-auth.guard.spec.js +0 -75
- package/cjs/guards/optional-jwt-auth.guard.js +0 -67
- package/cjs/guards/optional-jwt-auth.guard.spec.js +0 -55
- package/cjs/guards/permission.guard.js +0 -179
- package/cjs/guards/permission.guard.spec.js +0 -373
- package/cjs/index.js +0 -31
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.js +0 -40
- package/cjs/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -107
- package/cjs/interceptors/idempotency.interceptor.js +0 -96
- package/cjs/interceptors/idempotency.interceptor.spec.js +0 -116
- package/cjs/interceptors/index.js +0 -22
- package/cjs/interceptors/response-meta.interceptor.js +0 -47
- package/cjs/interceptors/response-meta.interceptor.spec.js +0 -89
- package/cjs/interceptors/set-user-field-on-body.interceptor.js +0 -60
- package/cjs/interceptors/set-user-field-on-body.interceptor.spec.js +0 -128
- package/cjs/interceptors/slug.interceptor.js +0 -75
- package/cjs/interceptors/slug.interceptor.spec.js +0 -110
- package/cjs/interfaces/ai-assistant-adapter.interface.js +0 -24
- package/cjs/interfaces/api.interface.js +0 -4
- package/cjs/interfaces/datasource.interface.js +0 -4
- package/cjs/interfaces/event-bus.interface.js +0 -4
- package/cjs/interfaces/event-manager-adapter.interface.js +0 -11
- package/cjs/interfaces/identity.interface.js +0 -4
- package/cjs/interfaces/index.js +0 -28
- package/cjs/interfaces/logged-user-info.interface.js +0 -6
- package/cjs/interfaces/module-config.interface.js +0 -4
- package/cjs/interfaces/notification-adapter.interface.js +0 -11
- package/cjs/interfaces/permission-sync-adapter.interface.js +0 -11
- package/cjs/interfaces/permission.interface.js +0 -4
- package/cjs/middlewares/index.js +0 -18
- package/cjs/middlewares/logger.middleware.js +0 -264
- package/cjs/middlewares/logger.middleware.spec.js +0 -462
- package/cjs/modules/cache/cache.module.js +0 -39
- package/cjs/modules/cache/cache.module.spec.js +0 -48
- package/cjs/modules/datasource/index.js +0 -18
- package/cjs/modules/datasource/multi-tenant-datasource.service.js +0 -222
- package/cjs/modules/datasource/multi-tenant-datasource.service.spec.js +0 -381
- package/cjs/modules/event-bus/event-bus.explorer.js +0 -124
- package/cjs/modules/event-bus/event-bus.explorer.spec.js +0 -189
- package/cjs/modules/event-bus/event-bus.integration.spec.js +0 -253
- package/cjs/modules/event-bus/event-bus.module.js +0 -94
- package/cjs/modules/event-bus/event-bus.module.spec.js +0 -149
- package/cjs/modules/event-bus/module-events.provider.js +0 -39
- package/cjs/modules/event-bus/module-events.provider.spec.js +0 -56
- package/cjs/modules/index.js +0 -26
- package/cjs/modules/shared-permission-cache/shared-permission-cache.module.js +0 -32
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.js +0 -101
- package/cjs/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -165
- package/cjs/modules/utils/utils.module.js +0 -32
- package/cjs/modules/utils/utils.service.js +0 -76
- package/cjs/modules/utils/utils.service.spec.js +0 -169
- package/cjs/utils/date-time.util.js +0 -94
- package/cjs/utils/date-time.util.spec.js +0 -84
- package/cjs/utils/event-pattern.util.js +0 -103
- package/cjs/utils/event-pattern.util.spec.js +0 -251
- package/cjs/utils/html-sanitizer.util.js +0 -64
- package/cjs/utils/html-sanitizer.util.spec.js +0 -76
- package/cjs/utils/index.js +0 -25
- package/cjs/utils/optional-module.util.js +0 -35
- package/cjs/utils/optional-module.util.spec.js +0 -14
- package/cjs/utils/permission-cache-key.util.js +0 -31
- package/cjs/utils/permission-cache-key.util.spec.js +0 -88
- package/cjs/utils/query-helpers.util.js +0 -60
- package/cjs/utils/query-helpers.util.spec.js +0 -209
- package/cjs/utils/request.util.js +0 -96
- package/cjs/utils/request.util.spec.js +0 -249
- package/cjs/utils/string.util.js +0 -63
- package/cjs/utils/string.util.spec.js +0 -63
- package/fesm/classes/api-controller.class.js +0 -718
- package/fesm/classes/api-controller.class.spec.js +0 -680
- package/fesm/classes/api-service.class.js +0 -621
- package/fesm/classes/api-service.class.spec.js +0 -1133
- package/fesm/classes/disabled-event-bus.class.js +0 -11
- package/fesm/classes/disabled-event-bus.class.spec.js +0 -36
- package/fesm/classes/domain-event.publisher.js +0 -63
- package/fesm/classes/domain-event.publisher.spec.js +0 -230
- package/fesm/classes/event-bus.registry.js +0 -64
- package/fesm/classes/event-bus.registry.spec.js +0 -137
- package/fesm/classes/event-transports/event-transport.registry.js +0 -35
- package/fesm/classes/event-transports/event-transport.registry.spec.js +0 -27
- package/fesm/classes/event-transports/index.js +0 -6
- package/fesm/classes/event-transports/kafka-transport.optional.spec.js +0 -194
- package/fesm/classes/event-transports/memory-transport.js +0 -49
- package/fesm/classes/event-transports/memory-transport.spec.js +0 -53
- package/fesm/classes/event-transports/rabbitmq-transport.optional.js +0 -174
- package/fesm/classes/event-transports/rabbitmq-transport.optional.spec.js +0 -251
- package/fesm/classes/handle-custome-db.js +0 -19
- package/fesm/classes/handle-custome-db.spec.js +0 -78
- package/fesm/classes/hybrid-cache.class.js +0 -81
- package/fesm/classes/hybrid-cache.class.spec.js +0 -164
- package/fesm/classes/hybrid-event-bus.class.js +0 -237
- package/fesm/classes/hybrid-event-bus.class.spec.js +0 -214
- package/fesm/classes/winston.logger.class.js +0 -228
- package/fesm/classes/winston.logger.class.spec.js +0 -327
- package/fesm/constants/message-keys.js +0 -49
- package/fesm/constants/permissions.js +0 -208
- package/fesm/decorators/api-response.decorator.js +0 -42
- package/fesm/decorators/api-response.decorator.spec.js +0 -122
- package/fesm/decorators/current-user.decorator.js +0 -18
- package/fesm/decorators/current-user.decorator.spec.js +0 -73
- package/fesm/decorators/log-action.decorator.js +0 -139
- package/fesm/decorators/log-action.decorator.spec.js +0 -349
- package/fesm/decorators/on-domain-event.decorator.js +0 -10
- package/fesm/decorators/on-domain-event.decorator.spec.js +0 -46
- package/fesm/decorators/public.decorator.js +0 -20
- package/fesm/decorators/public.decorator.spec.js +0 -46
- package/fesm/decorators/require-permission.decorator.js +0 -27
- package/fesm/decorators/require-permission.decorator.spec.js +0 -143
- package/fesm/dtos/delete.dto.js +0 -82
- package/fesm/dtos/filter-and-pagination.dto.js +0 -148
- package/fesm/dtos/get-by-ids.dto.js +0 -55
- package/fesm/dtos/identity-response.dto.js +0 -98
- package/fesm/dtos/pagination.dto.js +0 -57
- package/fesm/dtos/response-payload.dto.js +0 -336
- package/fesm/entities/identity.js +0 -90
- package/fesm/entities/raw-type.js +0 -1
- package/fesm/entities/user-root.js +0 -179
- package/fesm/enums/notification-type.enum.js +0 -7
- package/fesm/enums/participant-status.enum.js +0 -7
- package/fesm/enums/recurrence-type.enum.js +0 -8
- package/fesm/exceptions/base-app.exception.js +0 -115
- package/fesm/exceptions/base-app.exception.spec.js +0 -153
- package/fesm/exceptions/permission.exception.js +0 -35
- package/fesm/exceptions/permission.exception.spec.js +0 -78
- package/fesm/filters/global-exception.filter.js +0 -269
- package/fesm/filters/global-exception.filter.spec.js +0 -436
- package/fesm/filters/index.js +0 -1
- package/fesm/guards/jwt-auth.guard.js +0 -65
- package/fesm/guards/jwt-auth.guard.spec.js +0 -71
- package/fesm/guards/optional-jwt-auth.guard.js +0 -57
- package/fesm/guards/optional-jwt-auth.guard.spec.js +0 -51
- package/fesm/guards/permission.guard.js +0 -169
- package/fesm/guards/permission.guard.spec.js +0 -369
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.js +0 -30
- package/fesm/interceptors/delete-empty-id-from-body.interceptor.spec.js +0 -103
- package/fesm/interceptors/idempotency.interceptor.js +0 -86
- package/fesm/interceptors/idempotency.interceptor.spec.js +0 -112
- package/fesm/interceptors/response-meta.interceptor.js +0 -37
- package/fesm/interceptors/response-meta.interceptor.spec.js +0 -85
- package/fesm/interceptors/set-user-field-on-body.interceptor.js +0 -39
- package/fesm/interceptors/set-user-field-on-body.interceptor.spec.js +0 -124
- package/fesm/interceptors/slug.interceptor.js +0 -65
- package/fesm/interceptors/slug.interceptor.spec.js +0 -106
- package/fesm/interfaces/ai-assistant-adapter.interface.js +0 -3
- package/fesm/interfaces/api.interface.js +0 -1
- package/fesm/interfaces/datasource.interface.js +0 -1
- package/fesm/interfaces/event-bus.interface.js +0 -1
- package/fesm/interfaces/event-manager-adapter.interface.js +0 -1
- package/fesm/interfaces/identity.interface.js +0 -1
- package/fesm/interfaces/logged-user-info.interface.js +0 -3
- package/fesm/interfaces/module-config.interface.js +0 -5
- package/fesm/interfaces/notification-adapter.interface.js +0 -1
- package/fesm/interfaces/permission-sync-adapter.interface.js +0 -1
- package/fesm/interfaces/permission.interface.js +0 -1
- package/fesm/middlewares/logger.middleware.js +0 -235
- package/fesm/middlewares/logger.middleware.spec.js +0 -459
- package/fesm/modules/cache/cache.module.js +0 -29
- package/fesm/modules/cache/cache.module.spec.js +0 -44
- package/fesm/modules/datasource/index.js +0 -1
- package/fesm/modules/datasource/multi-tenant-datasource.service.js +0 -212
- package/fesm/modules/datasource/multi-tenant-datasource.service.spec.js +0 -377
- package/fesm/modules/event-bus/event-bus.explorer.js +0 -114
- package/fesm/modules/event-bus/event-bus.explorer.spec.js +0 -185
- package/fesm/modules/event-bus/event-bus.integration.spec.js +0 -249
- package/fesm/modules/event-bus/event-bus.module.js +0 -84
- package/fesm/modules/event-bus/event-bus.module.spec.js +0 -145
- package/fesm/modules/event-bus/module-events.provider.js +0 -26
- package/fesm/modules/event-bus/module-events.provider.spec.js +0 -52
- package/fesm/modules/shared-permission-cache/shared-permission-cache.module.js +0 -22
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.js +0 -91
- package/fesm/modules/shared-permission-cache/shared-permission-cache.service.spec.js +0 -161
- package/fesm/modules/utils/utils.module.js +0 -22
- package/fesm/modules/utils/utils.service.js +0 -66
- package/fesm/modules/utils/utils.service.spec.js +0 -165
- package/fesm/utils/date-time.util.js +0 -70
- package/fesm/utils/date-time.util.spec.js +0 -80
- package/fesm/utils/event-pattern.util.js +0 -91
- package/fesm/utils/event-pattern.util.spec.js +0 -247
- package/fesm/utils/html-sanitizer.util.js +0 -69
- package/fesm/utils/html-sanitizer.util.spec.js +0 -72
- package/fesm/utils/optional-module.util.js +0 -25
- package/fesm/utils/optional-module.util.spec.js +0 -10
- package/fesm/utils/permission-cache-key.util.js +0 -10
- package/fesm/utils/permission-cache-key.util.spec.js +0 -84
- package/fesm/utils/query-helpers.util.js +0 -85
- package/fesm/utils/query-helpers.util.spec.js +0 -205
- package/fesm/utils/request.util.js +0 -81
- package/fesm/utils/request.util.spec.js +0 -245
- package/fesm/utils/string.util.js +0 -71
- package/fesm/utils/string.util.spec.js +0 -59
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
Object.defineProperty(exports, "MultiTenantDataSourceService", {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function() {
|
|
8
|
-
return MultiTenantDataSourceService;
|
|
9
|
-
}
|
|
10
|
-
});
|
|
11
|
-
const _nestjscore = require("@flusys/nestjs-core");
|
|
12
|
-
const _common = require("@nestjs/common");
|
|
13
|
-
const _constants = require("../../constants");
|
|
14
|
-
const _core = require("@nestjs/core");
|
|
15
|
-
const _express = require("express");
|
|
16
|
-
const _typeorm = require("typeorm");
|
|
17
|
-
const _typeormnamingstrategies = require("typeorm-naming-strategies");
|
|
18
|
-
function _define_property(obj, key, value) {
|
|
19
|
-
if (key in obj) {
|
|
20
|
-
Object.defineProperty(obj, key, {
|
|
21
|
-
value: value,
|
|
22
|
-
enumerable: true,
|
|
23
|
-
configurable: true,
|
|
24
|
-
writable: true
|
|
25
|
-
});
|
|
26
|
-
} else {
|
|
27
|
-
obj[key] = value;
|
|
28
|
-
}
|
|
29
|
-
return obj;
|
|
30
|
-
}
|
|
31
|
-
function _ts_decorate(decorators, target, key, desc) {
|
|
32
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
33
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
34
|
-
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
35
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
36
|
-
}
|
|
37
|
-
function _ts_metadata(k, v) {
|
|
38
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
39
|
-
}
|
|
40
|
-
function _ts_param(paramIndex, decorator) {
|
|
41
|
-
return function(target, key) {
|
|
42
|
-
decorator(target, key, paramIndex);
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
let MultiTenantDataSourceService = class MultiTenantDataSourceService {
|
|
46
|
-
get self() {
|
|
47
|
-
return this.constructor;
|
|
48
|
-
}
|
|
49
|
-
initializeFromOptions() {
|
|
50
|
-
if (!this.options) return;
|
|
51
|
-
if (!this.self.initialized) {
|
|
52
|
-
this.registerTenants();
|
|
53
|
-
this.self.initialized = true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
registerTenants() {
|
|
57
|
-
this.options?.tenants?.forEach((tenant)=>{
|
|
58
|
-
this.self.tenantsRegistry.set(tenant.id, tenant);
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
getDatabaseMode() {
|
|
62
|
-
return this.options?.bootstrapAppConfig?.databaseMode ?? 'single';
|
|
63
|
-
}
|
|
64
|
-
isMultiTenant() {
|
|
65
|
-
return this.getDatabaseMode() === 'multi-tenant';
|
|
66
|
-
}
|
|
67
|
-
getCurrentTenantId() {
|
|
68
|
-
if (!this.request) return null;
|
|
69
|
-
const tenantId = this.request.headers[this.tenantHeader];
|
|
70
|
-
if (!tenantId) return null;
|
|
71
|
-
if (!/^[a-zA-Z0-9_-]+$/.test(tenantId)) {
|
|
72
|
-
throw new _common.BadRequestException({
|
|
73
|
-
message: 'Invalid tenant ID format',
|
|
74
|
-
messageKey: _constants.SYSTEM_MESSAGES.INVALID_TENANT_ID
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
return tenantId;
|
|
78
|
-
}
|
|
79
|
-
getCurrentTenant() {
|
|
80
|
-
const tenantId = this.getCurrentTenantId();
|
|
81
|
-
return tenantId ? this.getTenant(tenantId) : null;
|
|
82
|
-
}
|
|
83
|
-
getTenant(tenantId) {
|
|
84
|
-
return this.self.tenantsRegistry.get(tenantId) ?? null;
|
|
85
|
-
}
|
|
86
|
-
async getDataSource() {
|
|
87
|
-
return this.isMultiTenant() ? this.getTenantDataSource() : this.getSingleDataSource();
|
|
88
|
-
}
|
|
89
|
-
async getRepository(entity) {
|
|
90
|
-
const dataSource = await this.getDataSource();
|
|
91
|
-
return dataSource.getRepository(entity);
|
|
92
|
-
}
|
|
93
|
-
async closeTenantConnection(tenantId) {
|
|
94
|
-
const connection = this.self.tenantConnections.get(tenantId);
|
|
95
|
-
if (connection?.isInitialized) {
|
|
96
|
-
await connection.destroy();
|
|
97
|
-
this.self.tenantConnections.delete(tenantId);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
async onModuleDestroy() {
|
|
101
|
-
for (const [tenantId] of this.self.tenantConnections){
|
|
102
|
-
await this.closeTenantConnection(tenantId);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
async getSingleDataSource() {
|
|
106
|
-
if (this.self.singleDataSource?.isInitialized) {
|
|
107
|
-
return this.self.singleDataSource;
|
|
108
|
-
}
|
|
109
|
-
if (this.self.singleConnectionLock) {
|
|
110
|
-
return this.self.singleConnectionLock;
|
|
111
|
-
}
|
|
112
|
-
const config = this.getDefaultDatabaseConfig();
|
|
113
|
-
if (!config) {
|
|
114
|
-
throw new _common.InternalServerErrorException({
|
|
115
|
-
message: 'No database config available. Provide defaultDatabaseConfig or tenantDefaultDatabaseConfig.',
|
|
116
|
-
messageKey: _constants.SYSTEM_MESSAGES.DATABASE_CONFIG_NOT_AVAILABLE
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
const connectionPromise = this.createDataSourceFromConfig(config);
|
|
120
|
-
this.self.singleConnectionLock = connectionPromise;
|
|
121
|
-
try {
|
|
122
|
-
const dataSource = await connectionPromise;
|
|
123
|
-
this.self.singleDataSource = dataSource;
|
|
124
|
-
return dataSource;
|
|
125
|
-
} finally{
|
|
126
|
-
this.self.singleConnectionLock = null;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
async getTenantDataSource() {
|
|
130
|
-
const tenant = this.getCurrentTenant();
|
|
131
|
-
if (!tenant) {
|
|
132
|
-
throw new _common.BadRequestException({
|
|
133
|
-
message: `Tenant not found. Ensure '${this.tenantHeader}' header is set.`,
|
|
134
|
-
messageKey: _constants.SYSTEM_MESSAGES.TENANT_HEADER_REQUIRED,
|
|
135
|
-
messageVariables: {
|
|
136
|
-
header: this.tenantHeader
|
|
137
|
-
}
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
return this.getOrCreateTenantConnection(tenant);
|
|
141
|
-
}
|
|
142
|
-
async getOrCreateTenantConnection(tenant) {
|
|
143
|
-
const existing = this.self.tenantConnections.get(tenant.id);
|
|
144
|
-
if (existing?.isInitialized) return existing;
|
|
145
|
-
const pendingConnection = this.self.connectionLocks.get(tenant.id);
|
|
146
|
-
if (pendingConnection) return pendingConnection;
|
|
147
|
-
const config = this.buildTenantDatabaseConfig(tenant);
|
|
148
|
-
const connectionPromise = this.createDataSourceFromConfig(config);
|
|
149
|
-
this.self.connectionLocks.set(tenant.id, connectionPromise);
|
|
150
|
-
try {
|
|
151
|
-
const dataSource = await connectionPromise;
|
|
152
|
-
this.self.tenantConnections.set(tenant.id, dataSource);
|
|
153
|
-
return dataSource;
|
|
154
|
-
} finally{
|
|
155
|
-
this.self.connectionLocks.delete(tenant.id);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
getDefaultDatabaseConfig() {
|
|
159
|
-
return this.options?.defaultDatabaseConfig ?? this.options?.tenantDefaultDatabaseConfig;
|
|
160
|
-
}
|
|
161
|
-
buildTenantDatabaseConfig(tenant) {
|
|
162
|
-
const defaultConfig = this.getDefaultDatabaseConfig();
|
|
163
|
-
if (!defaultConfig) {
|
|
164
|
-
throw new _common.InternalServerErrorException({
|
|
165
|
-
message: 'No default database config for multi-tenant mode.',
|
|
166
|
-
messageKey: _constants.SYSTEM_MESSAGES.DATABASE_CONFIG_NOT_AVAILABLE
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
return {
|
|
170
|
-
type: defaultConfig.type,
|
|
171
|
-
host: tenant.host ?? defaultConfig.host,
|
|
172
|
-
port: tenant.port ?? defaultConfig.port,
|
|
173
|
-
username: tenant.username ?? defaultConfig.username,
|
|
174
|
-
password: tenant.password ?? defaultConfig.password,
|
|
175
|
-
database: tenant.database
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
async createDataSourceFromConfig(config, entities = []) {
|
|
179
|
-
const dataSource = new _typeorm.DataSource({
|
|
180
|
-
type: config.type,
|
|
181
|
-
host: config.host,
|
|
182
|
-
port: config.port,
|
|
183
|
-
username: config.username,
|
|
184
|
-
password: config.password,
|
|
185
|
-
database: config.database,
|
|
186
|
-
entities,
|
|
187
|
-
synchronize: false,
|
|
188
|
-
namingStrategy: new _typeormnamingstrategies.SnakeNamingStrategy()
|
|
189
|
-
});
|
|
190
|
-
if (!dataSource.isInitialized) await dataSource.initialize();
|
|
191
|
-
return dataSource;
|
|
192
|
-
}
|
|
193
|
-
constructor(options, request){
|
|
194
|
-
_define_property(this, "options", void 0);
|
|
195
|
-
_define_property(this, "request", void 0);
|
|
196
|
-
_define_property(this, "tenantHeader", void 0);
|
|
197
|
-
this.options = options;
|
|
198
|
-
this.request = request;
|
|
199
|
-
this.tenantHeader = _nestjscore.DEFAULT_TENANT_HEADER;
|
|
200
|
-
this.initializeFromOptions();
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
_define_property(MultiTenantDataSourceService, "tenantConnections", new Map());
|
|
204
|
-
_define_property(MultiTenantDataSourceService, "singleDataSource", null);
|
|
205
|
-
_define_property(MultiTenantDataSourceService, "tenantsRegistry", new Map());
|
|
206
|
-
_define_property(MultiTenantDataSourceService, "initialized", false);
|
|
207
|
-
_define_property(MultiTenantDataSourceService, "connectionLocks", new Map());
|
|
208
|
-
_define_property(MultiTenantDataSourceService, "singleConnectionLock", null);
|
|
209
|
-
MultiTenantDataSourceService = _ts_decorate([
|
|
210
|
-
(0, _common.Injectable)({
|
|
211
|
-
scope: _common.Scope.REQUEST
|
|
212
|
-
}),
|
|
213
|
-
_ts_param(0, (0, _common.Optional)()),
|
|
214
|
-
_ts_param(0, (0, _common.Inject)(_nestjscore.MODULE_OPTIONS)),
|
|
215
|
-
_ts_param(1, (0, _common.Optional)()),
|
|
216
|
-
_ts_param(1, (0, _common.Inject)(_core.REQUEST)),
|
|
217
|
-
_ts_metadata("design:type", Function),
|
|
218
|
-
_ts_metadata("design:paramtypes", [
|
|
219
|
-
typeof _nestjscore.IDataSourceServiceOptions === "undefined" ? Object : _nestjscore.IDataSourceServiceOptions,
|
|
220
|
-
typeof _express.Request === "undefined" ? Object : _express.Request
|
|
221
|
-
])
|
|
222
|
-
], MultiTenantDataSourceService);
|
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
const _common = require("@nestjs/common");
|
|
6
|
-
const _multitenantdatasourceservice = require("./multi-tenant-datasource.service");
|
|
7
|
-
const _constants = require("../../constants");
|
|
8
|
-
function _define_property(obj, key, value) {
|
|
9
|
-
if (key in obj) {
|
|
10
|
-
Object.defineProperty(obj, key, {
|
|
11
|
-
value: value,
|
|
12
|
-
enumerable: true,
|
|
13
|
-
configurable: true,
|
|
14
|
-
writable: true
|
|
15
|
-
});
|
|
16
|
-
} else {
|
|
17
|
-
obj[key] = value;
|
|
18
|
-
}
|
|
19
|
-
return obj;
|
|
20
|
-
}
|
|
21
|
-
const mockInitialize = jest.fn();
|
|
22
|
-
const mockDestroy = jest.fn();
|
|
23
|
-
const mockGetRepository = jest.fn();
|
|
24
|
-
let FakeDataSource = class FakeDataSource {
|
|
25
|
-
constructor(options){
|
|
26
|
-
_define_property(this, "isInitialized", false);
|
|
27
|
-
_define_property(this, "options", void 0);
|
|
28
|
-
_define_property(this, "initialize", jest.fn(async ()=>{
|
|
29
|
-
this.isInitialized = true;
|
|
30
|
-
await mockInitialize(this.options);
|
|
31
|
-
return this;
|
|
32
|
-
}));
|
|
33
|
-
_define_property(this, "destroy", jest.fn(async ()=>{
|
|
34
|
-
this.isInitialized = false;
|
|
35
|
-
await mockDestroy();
|
|
36
|
-
}));
|
|
37
|
-
_define_property(this, "getRepository", (entity)=>mockGetRepository(entity));
|
|
38
|
-
this.options = options;
|
|
39
|
-
}
|
|
40
|
-
};
|
|
41
|
-
jest.mock('typeorm', ()=>({
|
|
42
|
-
...jest.requireActual('typeorm'),
|
|
43
|
-
DataSource: FakeDataSource
|
|
44
|
-
}));
|
|
45
|
-
function resetStatics() {
|
|
46
|
-
const cls = _multitenantdatasourceservice.MultiTenantDataSourceService;
|
|
47
|
-
cls.tenantConnections = new Map();
|
|
48
|
-
cls.singleDataSource = null;
|
|
49
|
-
cls.tenantsRegistry = new Map();
|
|
50
|
-
cls.initialized = false;
|
|
51
|
-
cls.connectionLocks = new Map();
|
|
52
|
-
cls.singleConnectionLock = null;
|
|
53
|
-
}
|
|
54
|
-
describe('MultiTenantDataSourceService', ()=>{
|
|
55
|
-
const defaultDatabaseConfig = {
|
|
56
|
-
type: 'postgres',
|
|
57
|
-
host: 'localhost',
|
|
58
|
-
port: 5432,
|
|
59
|
-
username: 'root',
|
|
60
|
-
password: 'pw',
|
|
61
|
-
database: 'flusys_default'
|
|
62
|
-
};
|
|
63
|
-
beforeEach(()=>{
|
|
64
|
-
jest.clearAllMocks();
|
|
65
|
-
resetStatics();
|
|
66
|
-
});
|
|
67
|
-
function makeOptions(overrides = {}) {
|
|
68
|
-
return {
|
|
69
|
-
defaultDatabaseConfig,
|
|
70
|
-
bootstrapAppConfig: {
|
|
71
|
-
databaseMode: 'single'
|
|
72
|
-
},
|
|
73
|
-
...overrides
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
function makeRequest(headers = {}) {
|
|
77
|
-
return {
|
|
78
|
-
headers
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
describe('database mode', ()=>{
|
|
82
|
-
it('should default to single mode when no options are provided', ()=>{
|
|
83
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService();
|
|
84
|
-
expect(service.getDatabaseMode()).toBe('single');
|
|
85
|
-
expect(service.isMultiTenant()).toBe(false);
|
|
86
|
-
});
|
|
87
|
-
it('should report multi-tenant mode from bootstrap config', ()=>{
|
|
88
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
89
|
-
bootstrapAppConfig: {
|
|
90
|
-
databaseMode: 'multi-tenant'
|
|
91
|
-
}
|
|
92
|
-
}));
|
|
93
|
-
expect(service.isMultiTenant()).toBe(true);
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
describe('tenant registration', ()=>{
|
|
97
|
-
it('should register tenants from options exactly once across instances', ()=>{
|
|
98
|
-
const tenants = [
|
|
99
|
-
{
|
|
100
|
-
id: 'tenant-a',
|
|
101
|
-
database: 'db_a'
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
id: 'tenant-b',
|
|
105
|
-
database: 'db_b'
|
|
106
|
-
}
|
|
107
|
-
];
|
|
108
|
-
const options = makeOptions({
|
|
109
|
-
tenants
|
|
110
|
-
});
|
|
111
|
-
const service1 = new _multitenantdatasourceservice.MultiTenantDataSourceService(options);
|
|
112
|
-
const service2 = new _multitenantdatasourceservice.MultiTenantDataSourceService(options);
|
|
113
|
-
expect(service1.getTenant('tenant-a')?.database).toBe('db_a');
|
|
114
|
-
expect(service2.getTenant('tenant-b')?.database).toBe('db_b');
|
|
115
|
-
});
|
|
116
|
-
it('should return null for an unregistered tenant', ()=>{
|
|
117
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
118
|
-
expect(service.getTenant('unknown')).toBeNull();
|
|
119
|
-
});
|
|
120
|
-
it('should be a no-op when constructed without options', ()=>{
|
|
121
|
-
expect(()=>new _multitenantdatasourceservice.MultiTenantDataSourceService()).not.toThrow();
|
|
122
|
-
});
|
|
123
|
-
});
|
|
124
|
-
describe('getCurrentTenantId', ()=>{
|
|
125
|
-
it('should return null when there is no request (e.g. background job)', ()=>{
|
|
126
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
127
|
-
expect(service.getCurrentTenantId()).toBeNull();
|
|
128
|
-
});
|
|
129
|
-
it('should return null when the tenant header is missing', ()=>{
|
|
130
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest());
|
|
131
|
-
expect(service.getCurrentTenantId()).toBeNull();
|
|
132
|
-
});
|
|
133
|
-
it('should return the tenant id from the header when valid', ()=>{
|
|
134
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
135
|
-
'x-tenant-id': 'tenant-a'
|
|
136
|
-
}));
|
|
137
|
-
expect(service.getCurrentTenantId()).toBe('tenant-a');
|
|
138
|
-
});
|
|
139
|
-
it('should reject tenant ids with invalid characters', ()=>{
|
|
140
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
141
|
-
'x-tenant-id': 'tenant a; DROP TABLE users;'
|
|
142
|
-
}));
|
|
143
|
-
expect(()=>service.getCurrentTenantId()).toThrow(_common.BadRequestException);
|
|
144
|
-
try {
|
|
145
|
-
service.getCurrentTenantId();
|
|
146
|
-
} catch (e) {
|
|
147
|
-
expect(e.getResponse().messageKey).toBe(_constants.SYSTEM_MESSAGES.INVALID_TENANT_ID);
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
it('should accept tenant ids with letters, numbers, underscores and dashes', ()=>{
|
|
151
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest({
|
|
152
|
-
'x-tenant-id': 'Tenant_123-ABC'
|
|
153
|
-
}));
|
|
154
|
-
expect(service.getCurrentTenantId()).toBe('Tenant_123-ABC');
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
describe('getCurrentTenant', ()=>{
|
|
158
|
-
it('should resolve the registered tenant config for the current request', ()=>{
|
|
159
|
-
const tenants = [
|
|
160
|
-
{
|
|
161
|
-
id: 'tenant-a',
|
|
162
|
-
database: 'db_a'
|
|
163
|
-
}
|
|
164
|
-
];
|
|
165
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
166
|
-
tenants
|
|
167
|
-
}), makeRequest({
|
|
168
|
-
'x-tenant-id': 'tenant-a'
|
|
169
|
-
}));
|
|
170
|
-
expect(service.getCurrentTenant()?.database).toBe('db_a');
|
|
171
|
-
});
|
|
172
|
-
it('should return null when there is no current tenant id', ()=>{
|
|
173
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions(), makeRequest());
|
|
174
|
-
expect(service.getCurrentTenant()).toBeNull();
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe('getSingleDataSource / getDataSource (single mode)', ()=>{
|
|
178
|
-
it('should create and initialize a datasource from the default config', async ()=>{
|
|
179
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
180
|
-
const ds = await service.getDataSource();
|
|
181
|
-
expect(ds.isInitialized).toBe(true);
|
|
182
|
-
expect(mockInitialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
183
|
-
database: 'flusys_default'
|
|
184
|
-
}));
|
|
185
|
-
});
|
|
186
|
-
it('should reuse the cached datasource on subsequent calls', async ()=>{
|
|
187
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
188
|
-
const ds1 = await service.getDataSource();
|
|
189
|
-
const ds2 = await service.getDataSource();
|
|
190
|
-
expect(ds1).toBe(ds2);
|
|
191
|
-
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
192
|
-
});
|
|
193
|
-
it('should dedupe concurrent creation attempts into a single connection', async ()=>{
|
|
194
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
195
|
-
const [ds1, ds2] = await Promise.all([
|
|
196
|
-
service.getDataSource(),
|
|
197
|
-
service.getDataSource()
|
|
198
|
-
]);
|
|
199
|
-
expect(ds1).toBe(ds2);
|
|
200
|
-
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
201
|
-
});
|
|
202
|
-
it('should throw InternalServerErrorException when no database config is available', async ()=>{
|
|
203
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
204
|
-
defaultDatabaseConfig: undefined
|
|
205
|
-
}));
|
|
206
|
-
await expect(service.getDataSource()).rejects.toThrow(_common.InternalServerErrorException);
|
|
207
|
-
});
|
|
208
|
-
it('should fall back to tenantDefaultDatabaseConfig when defaultDatabaseConfig is absent', async ()=>{
|
|
209
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
210
|
-
defaultDatabaseConfig: undefined,
|
|
211
|
-
tenantDefaultDatabaseConfig: defaultDatabaseConfig
|
|
212
|
-
}));
|
|
213
|
-
const ds = await service.getDataSource();
|
|
214
|
-
expect(ds.isInitialized).toBe(true);
|
|
215
|
-
});
|
|
216
|
-
it('getRepository should resolve the datasource then delegate to it', async ()=>{
|
|
217
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
218
|
-
let Product = class Product {
|
|
219
|
-
};
|
|
220
|
-
mockGetRepository.mockReturnValue('the-repo');
|
|
221
|
-
const repo = await service.getRepository(Product);
|
|
222
|
-
expect(mockGetRepository).toHaveBeenCalledWith(Product);
|
|
223
|
-
expect(repo).toBe('the-repo');
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
describe('getTenantDataSource / getDataSource (multi-tenant mode)', ()=>{
|
|
227
|
-
function multiTenantOptions(tenants) {
|
|
228
|
-
return makeOptions({
|
|
229
|
-
bootstrapAppConfig: {
|
|
230
|
-
databaseMode: 'multi-tenant'
|
|
231
|
-
},
|
|
232
|
-
tenants
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
it('should throw BadRequestException when no tenant header is present', async ()=>{
|
|
236
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([
|
|
237
|
-
{
|
|
238
|
-
id: 'tenant-a',
|
|
239
|
-
database: 'db_a'
|
|
240
|
-
}
|
|
241
|
-
]), makeRequest());
|
|
242
|
-
await expect(service.getDataSource()).rejects.toThrow(_common.BadRequestException);
|
|
243
|
-
});
|
|
244
|
-
it('should throw BadRequestException when the tenant is not registered', async ()=>{
|
|
245
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([]), makeRequest({
|
|
246
|
-
'x-tenant-id': 'ghost-tenant'
|
|
247
|
-
}));
|
|
248
|
-
await expect(service.getDataSource()).rejects.toThrow(_common.BadRequestException);
|
|
249
|
-
});
|
|
250
|
-
it('should create a tenant-specific connection merging tenant overrides onto the default config', async ()=>{
|
|
251
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions([
|
|
252
|
-
{
|
|
253
|
-
id: 'tenant-a',
|
|
254
|
-
database: 'db_a',
|
|
255
|
-
host: 'tenant-a-host'
|
|
256
|
-
}
|
|
257
|
-
]), makeRequest({
|
|
258
|
-
'x-tenant-id': 'tenant-a'
|
|
259
|
-
}));
|
|
260
|
-
const ds = await service.getDataSource();
|
|
261
|
-
expect(ds.isInitialized).toBe(true);
|
|
262
|
-
expect(mockInitialize).toHaveBeenCalledWith(expect.objectContaining({
|
|
263
|
-
database: 'db_a',
|
|
264
|
-
host: 'tenant-a-host',
|
|
265
|
-
username: defaultDatabaseConfig.username
|
|
266
|
-
}));
|
|
267
|
-
});
|
|
268
|
-
it('should cache the tenant connection and reuse it for subsequent requests to the same tenant', async ()=>{
|
|
269
|
-
const tenants = [
|
|
270
|
-
{
|
|
271
|
-
id: 'tenant-a',
|
|
272
|
-
database: 'db_a'
|
|
273
|
-
}
|
|
274
|
-
];
|
|
275
|
-
const service1 = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
276
|
-
'x-tenant-id': 'tenant-a'
|
|
277
|
-
}));
|
|
278
|
-
const service2 = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
279
|
-
'x-tenant-id': 'tenant-a'
|
|
280
|
-
}));
|
|
281
|
-
const ds1 = await service1.getDataSource();
|
|
282
|
-
const ds2 = await service2.getDataSource();
|
|
283
|
-
expect(ds1).toBe(ds2);
|
|
284
|
-
expect(mockInitialize).toHaveBeenCalledTimes(1);
|
|
285
|
-
});
|
|
286
|
-
it('should maintain separate connections per tenant', async ()=>{
|
|
287
|
-
const tenants = [
|
|
288
|
-
{
|
|
289
|
-
id: 'tenant-a',
|
|
290
|
-
database: 'db_a'
|
|
291
|
-
},
|
|
292
|
-
{
|
|
293
|
-
id: 'tenant-b',
|
|
294
|
-
database: 'db_b'
|
|
295
|
-
}
|
|
296
|
-
];
|
|
297
|
-
const serviceA = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
298
|
-
'x-tenant-id': 'tenant-a'
|
|
299
|
-
}));
|
|
300
|
-
const serviceB = new _multitenantdatasourceservice.MultiTenantDataSourceService(multiTenantOptions(tenants), makeRequest({
|
|
301
|
-
'x-tenant-id': 'tenant-b'
|
|
302
|
-
}));
|
|
303
|
-
const dsA = await serviceA.getDataSource();
|
|
304
|
-
const dsB = await serviceB.getDataSource();
|
|
305
|
-
expect(dsA).not.toBe(dsB);
|
|
306
|
-
expect(mockInitialize).toHaveBeenCalledTimes(2);
|
|
307
|
-
});
|
|
308
|
-
it('should throw InternalServerErrorException when no default config exists to base tenant configs on', async ()=>{
|
|
309
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService({
|
|
310
|
-
...multiTenantOptions([
|
|
311
|
-
{
|
|
312
|
-
id: 'tenant-a',
|
|
313
|
-
database: 'db_a'
|
|
314
|
-
}
|
|
315
|
-
]),
|
|
316
|
-
defaultDatabaseConfig: undefined,
|
|
317
|
-
tenantDefaultDatabaseConfig: undefined
|
|
318
|
-
}, makeRequest({
|
|
319
|
-
'x-tenant-id': 'tenant-a'
|
|
320
|
-
}));
|
|
321
|
-
await expect(service.getDataSource()).rejects.toThrow(_common.InternalServerErrorException);
|
|
322
|
-
});
|
|
323
|
-
});
|
|
324
|
-
describe('closeTenantConnection', ()=>{
|
|
325
|
-
it('should destroy and remove an initialized tenant connection', async ()=>{
|
|
326
|
-
const tenants = [
|
|
327
|
-
{
|
|
328
|
-
id: 'tenant-a',
|
|
329
|
-
database: 'db_a'
|
|
330
|
-
}
|
|
331
|
-
];
|
|
332
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions({
|
|
333
|
-
bootstrapAppConfig: {
|
|
334
|
-
databaseMode: 'multi-tenant'
|
|
335
|
-
},
|
|
336
|
-
tenants
|
|
337
|
-
}), makeRequest({
|
|
338
|
-
'x-tenant-id': 'tenant-a'
|
|
339
|
-
}));
|
|
340
|
-
await service.getDataSource();
|
|
341
|
-
await service.closeTenantConnection('tenant-a');
|
|
342
|
-
expect(mockDestroy).toHaveBeenCalledTimes(1);
|
|
343
|
-
expect(_multitenantdatasourceservice.MultiTenantDataSourceService.tenantConnections.has('tenant-a')).toBe(false);
|
|
344
|
-
});
|
|
345
|
-
it('should be a no-op for a tenant with no active connection', async ()=>{
|
|
346
|
-
const service = new _multitenantdatasourceservice.MultiTenantDataSourceService(makeOptions());
|
|
347
|
-
await expect(service.closeTenantConnection('never-connected')).resolves.toBeUndefined();
|
|
348
|
-
expect(mockDestroy).not.toHaveBeenCalled();
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
describe('onModuleDestroy', ()=>{
|
|
352
|
-
it('should close every tracked tenant connection', async ()=>{
|
|
353
|
-
const tenants = [
|
|
354
|
-
{
|
|
355
|
-
id: 'tenant-a',
|
|
356
|
-
database: 'db_a'
|
|
357
|
-
},
|
|
358
|
-
{
|
|
359
|
-
id: 'tenant-b',
|
|
360
|
-
database: 'db_b'
|
|
361
|
-
}
|
|
362
|
-
];
|
|
363
|
-
const options = makeOptions({
|
|
364
|
-
bootstrapAppConfig: {
|
|
365
|
-
databaseMode: 'multi-tenant'
|
|
366
|
-
},
|
|
367
|
-
tenants
|
|
368
|
-
});
|
|
369
|
-
const serviceA = new _multitenantdatasourceservice.MultiTenantDataSourceService(options, makeRequest({
|
|
370
|
-
'x-tenant-id': 'tenant-a'
|
|
371
|
-
}));
|
|
372
|
-
const serviceB = new _multitenantdatasourceservice.MultiTenantDataSourceService(options, makeRequest({
|
|
373
|
-
'x-tenant-id': 'tenant-b'
|
|
374
|
-
}));
|
|
375
|
-
await serviceA.getDataSource();
|
|
376
|
-
await serviceB.getDataSource();
|
|
377
|
-
await serviceA.onModuleDestroy();
|
|
378
|
-
expect(mockDestroy).toHaveBeenCalledTimes(2);
|
|
379
|
-
});
|
|
380
|
-
});
|
|
381
|
-
});
|