a804d65210
/closes #13580 <img width="1186" height="754" alt="image" src="https://github.com/user-attachments/assets/d5be9e3a-2748-4ad0-b29b-98038cbeeb6a" />
90 lines
2.5 KiB
TypeScript
90 lines
2.5 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
|
|
import { DataSource, EntityManager } from 'typeorm';
|
|
|
|
import { TypeORMService } from 'src/database/typeorm/typeorm.service';
|
|
import { DataSourceService } from 'src/engine/metadata-modules/data-source/data-source.service';
|
|
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 {
|
|
constructor(
|
|
private readonly dataSourceService: DataSourceService,
|
|
private readonly typeormService: TypeORMService,
|
|
) {}
|
|
|
|
/**
|
|
*
|
|
* Connect to the workspace data source
|
|
*
|
|
* @param workspaceId
|
|
* @returns
|
|
*/
|
|
public async connectToMainDataSource(): Promise<DataSource> {
|
|
const dataSource = this.typeormService.getMainDataSource();
|
|
|
|
if (!dataSource) {
|
|
throw new Error(`Could not connect to workspace data source`);
|
|
}
|
|
|
|
return dataSource;
|
|
}
|
|
|
|
public async checkSchemaExists(workspaceId: string) {
|
|
const dataSource =
|
|
await this.dataSourceService.getDataSourcesMetadataFromWorkspaceId(
|
|
workspaceId,
|
|
);
|
|
|
|
return dataSource.length > 0;
|
|
}
|
|
|
|
/**
|
|
*
|
|
* Create a new DB schema for a workspace
|
|
*
|
|
* @param workspaceId
|
|
* @returns
|
|
*/
|
|
public async createWorkspaceDBSchema(workspaceId: string): Promise<string> {
|
|
const schemaName = getWorkspaceSchemaName(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 = getWorkspaceSchemaName(workspaceId);
|
|
|
|
return await this.typeormService.deleteSchema(schemaName);
|
|
}
|
|
|
|
public async executeRawQuery(
|
|
_query: string,
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
_parameters: any[] = [],
|
|
_workspaceId: string,
|
|
_transactionManager?: EntityManager,
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
): Promise<any> {
|
|
throw new PermissionsException(
|
|
'Method not allowed as permissions are not handled at datasource level.',
|
|
PermissionsExceptionCode.METHOD_NOT_ALLOWED,
|
|
{
|
|
userFriendlyMessage:
|
|
'This operation is not allowed. Please try a different approach or contact support if you need assistance.',
|
|
},
|
|
);
|
|
}
|
|
}
|