feat: rename tenant into workspace (#2553)
* feat: rename tenant into workspace * fix: missing some files and reset not working * fix: wrong import * Use link in company seeds * Use link in company seeds --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -0,0 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
|
||||
import { DataSourceModule } from 'src/metadata/data-source/data-source.module';
|
||||
|
||||
import { WorkspaceDataSourceService } from './workspace-datasource.service';
|
||||
|
||||
@Module({
|
||||
imports: [DataSourceModule, TypeORMModule],
|
||||
exports: [WorkspaceDataSourceService],
|
||||
providers: [WorkspaceDataSourceService],
|
||||
})
|
||||
export class WorkspaceDataSourceModule {}
|
||||
@ -0,0 +1,101 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { DataSourceService } from 'src/metadata/data-source/data-source.service';
|
||||
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceDataSourceService {
|
||||
constructor(
|
||||
private readonly dataSourceService: DataSourceService,
|
||||
private readonly typeormService: TypeORMService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
*
|
||||
* Connect to the workspace data source
|
||||
*
|
||||
* @param workspaceId
|
||||
* @returns
|
||||
*/
|
||||
public async connectToWorkspaceDataSource(
|
||||
workspaceId: string,
|
||||
): Promise<DataSource> {
|
||||
const dataSourceMetadata =
|
||||
await this.dataSourceService.getLastDataSourceMetadataFromWorkspaceIdOrFail(
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
const dataSource = await this.typeormService.connectToDataSource(
|
||||
dataSourceMetadata,
|
||||
);
|
||||
|
||||
if (!dataSource) {
|
||||
throw new Error(
|
||||
`Could not connect to workspace data source for workspace ${workspaceId}`,
|
||||
);
|
||||
}
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Create a new DB schema for a workspace
|
||||
*
|
||||
* @param workspaceId
|
||||
* @returns
|
||||
*/
|
||||
public async createWorkspaceDBSchema(workspaceId: string): Promise<string> {
|
||||
const schemaName = this.getSchemaName(workspaceId);
|
||||
|
||||
return await this.typeormService.createSchema(schemaName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Delete a DB schema for a workspace
|
||||
*
|
||||
* @param workspaceId
|
||||
* @returns
|
||||
*/
|
||||
public async deleteWorkspaceDBSchema(workspaceId: string): Promise<void> {
|
||||
const schemaName = this.getSchemaName(workspaceId);
|
||||
|
||||
return await this.typeormService.deleteSchema(schemaName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get the schema name for a workspace
|
||||
*
|
||||
* @param workspaceId
|
||||
* @returns string
|
||||
*/
|
||||
public getSchemaName(workspaceId: string): string {
|
||||
return `workspace_${this.uuidToBase36(workspaceId)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Convert a uuid to base36
|
||||
*
|
||||
* @param uuid
|
||||
* @returns string
|
||||
*/
|
||||
private uuidToBase36(uuid: string): string {
|
||||
let devId = false;
|
||||
|
||||
if (uuid.startsWith('twenty-')) {
|
||||
devId = true;
|
||||
// Clean dev uuids (twenty-)
|
||||
uuid = uuid.replace('twenty-', '');
|
||||
}
|
||||
const hexString = uuid.replace(/-/g, '');
|
||||
const base10Number = BigInt('0x' + hexString);
|
||||
const base36String = base10Number.toString(36);
|
||||
|
||||
return `${devId ? 'twenty_' : ''}${base36String}`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user