* Convert metadata tables to camelCase * datasourcemetadataid to datasourceid * refactor metadata folders * fix command * move commands out of metadata * fix seed * rename objectId and fieldId in objectMetadataId and fieldMetadataId in FE * fix field-metadata * Fix * Fix * remove logs --------- Co-authored-by: Charles Bochet <charles@twenty.com>
102 lines
2.4 KiB
TypeScript
102 lines
2.4 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
import { DataSourceMetadataService } from 'src/metadata/data-source-metadata/data-source-metadata.service';
|
|
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
|
|
|
@Injectable()
|
|
export class TenantDataSourceService {
|
|
constructor(
|
|
private readonly dataSourceMetadataService: DataSourceMetadataService,
|
|
private readonly typeormService: TypeORMService,
|
|
) {}
|
|
|
|
/**
|
|
*
|
|
* Connect to the workspace data source
|
|
*
|
|
* @param workspaceId
|
|
* @returns
|
|
*/
|
|
public async connectToWorkspaceDataSource(
|
|
workspaceId: string,
|
|
): Promise<DataSource> {
|
|
const dataSourceMetadata =
|
|
await this.dataSourceMetadataService.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}`;
|
|
}
|
|
}
|