Move getSchemaName to util (#13357)
## Context The method was very simple and deterministic, I'm moving its logic to a util so we don't have to import a service to use it.
This commit is contained in:
@ -0,0 +1,5 @@
|
||||
import { uuidToBase36 } from 'twenty-shared/utils';
|
||||
|
||||
export const getWorkspaceSchemaName = (workspaceId: string): string => {
|
||||
return `workspace_${uuidToBase36(workspaceId)}`;
|
||||
};
|
||||
@ -8,6 +8,7 @@ import {
|
||||
PermissionsException,
|
||||
PermissionsExceptionCode,
|
||||
} from 'src/engine/metadata-modules/permissions/permissions.exception';
|
||||
import { getWorkspaceSchemaName } from 'src/engine/workspace-datasource/utils/get-workspace-schema-name.util';
|
||||
|
||||
@Injectable()
|
||||
export class WorkspaceDataSourceService {
|
||||
@ -50,7 +51,7 @@ export class WorkspaceDataSourceService {
|
||||
* @returns
|
||||
*/
|
||||
public async createWorkspaceDBSchema(workspaceId: string): Promise<string> {
|
||||
const schemaName = this.getSchemaName(workspaceId);
|
||||
const schemaName = getWorkspaceSchemaName(workspaceId);
|
||||
|
||||
return await this.typeormService.createSchema(schemaName);
|
||||
}
|
||||
@ -63,45 +64,11 @@ export class WorkspaceDataSourceService {
|
||||
* @returns
|
||||
*/
|
||||
public async deleteWorkspaceDBSchema(workspaceId: string): Promise<void> {
|
||||
const schemaName = this.getSchemaName(workspaceId);
|
||||
const schemaName = getWorkspaceSchemaName(workspaceId);
|
||||
|
||||
return await this.typeormService.deleteSchema(schemaName);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Get the schema name for a workspace
|
||||
* Note: This is assuming that the workspace only has one schema but we should prefer querying the metadata table instead.
|
||||
*
|
||||
* @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}`;
|
||||
}
|
||||
|
||||
public async executeRawQuery(
|
||||
_query: string,
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
|
||||
Reference in New Issue
Block a user