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