Skip to content

Commit

Permalink
fix(multi-tenancy-module): Load the config in the service
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Dec 6, 2022
1 parent e44ffcf commit 4d59aa3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 74 deletions.
7 changes: 1 addition & 6 deletions src/Medusa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
subscribersLoader,
validatorsLoader,
} from './loaders';
import { loadMonitoringModule, MonitoringOptions } from './modules/monitoring';

// Use to fix MiddlewareService typings
declare global {
Expand Down Expand Up @@ -46,15 +45,11 @@ export class Medusa {
*/
public async load(modules: Type[]): Promise<MedusaContainer> {
const { configModule } = getConfigFile(this.#rootDir, 'medusa-config') as {
configModule: {
monitoring: MonitoringOptions;
};
configModule: Record<string, unknown>;
};

const moduleComponentsOptions = await modulesLoader(modules, configModule);

await loadMonitoringModule(configModule, this.#express, configModule.monitoring);

await validatorsLoader(moduleComponentsOptions.get('validator') ?? []);
await overrideEntitiesLoader(moduleComponentsOptions.get('entity') ?? []);
await overrideRepositoriesLoader(moduleComponentsOptions.get('repository') ?? []);
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ export {
FulfillmentProvider,
} from './decorators';

export { MonitoringOptions } from './modules/monitoring';
export * from './modules/multi-tenancy';
60 changes: 0 additions & 60 deletions src/modules/monitoring.ts

This file was deleted.

18 changes: 11 additions & 7 deletions src/modules/multi-tenancy/tenant.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@ import { Service } from '../../decorators';
import { Connection, createConnection, EntityManager, getConnectionManager } from 'typeorm';
import { ShortenedNamingStrategy } from '@medusajs/medusa/dist/utils/naming-strategy';
import { ConfigModule } from './types';
import { MedusaContainer, MedusaRequest } from '../../core';
import { MedusaRequest } from '../../core';
import { getConfigFile } from 'medusa-core-utils';

@Service()
export class TenantService {
static readonly resolutionKey = 'tenantService';

constructor(_: MedusaContainer, private readonly config: ConfigModule) {}

/**
* Provide a way to switch between database connections depending on the request property holding the tenant code.
* @param defaultManager
* @param req
*/
public async getOrCreateConnection(defaultManager: EntityManager, req: MedusaRequest): Promise<EntityManager> {
if (!this.config.multi_tenancy?.tenant_code_resolver) {
// TODO: Remove once medusa allow to store custom configuration
const { configModule } = getConfigFile(process.cwd(), 'medusa-config') as {
configModule: ConfigModule;
};

if (!configModule.multi_tenancy?.tenant_code_resolver) {
throw new Error('Missing tenant_code_resolver from multi_tenancy config in medusa-config.');
}
const tenantCode = this.config.multi_tenancy.tenant_code_resolver(req);
const tenantCode = configModule.multi_tenancy.tenant_code_resolver(req);
if (!tenantCode) {
return defaultManager;
}

const tenant = this.config.multi_tenancy.tenants.find((tenantConfig) => {
const tenant = configModule.multi_tenancy.tenants.find((tenantConfig) => {
return tenantConfig.code === tenantCode;
});
if (!tenant) {
Expand All @@ -48,7 +52,7 @@ export class TenantService {
extra: tenant.database_config.database_extra || {},
entities: db_entities,
namingStrategy: new ShortenedNamingStrategy(),
logging: this.config.projectConfig.database_logging ?? false,
logging: configModule.projectConfig.database_logging ?? false,
});

connection = await connectionManager.get(tenant.code);
Expand Down

0 comments on commit 4d59aa3

Please sign in to comment.